1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 David Craven <david@craven.ch>
3 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
4 ;;; Copyright © 2019 Ivan Petkov <ivanppetkov@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix build cargo-utils)
22 #:use-module (guix build utils)
23 #:use-module (ice-9 popen)
24 #:use-module (ice-9 rdelim)
25 #:export (generate-checksums))
29 ;; Stand alone utilities for building Rust crates or the compiler itself,
30 ;; without depending on the entire cargo build-system itself.
34 (define (file-sha256 file-name)
35 "Calculate the hexdigest of the sha256 checksum of FILE-NAME and return it."
36 (let ((port (open-pipe* OPEN_READ
40 (let ((result (read-delimited " " port)))
44 (define (generate-checksums dir-name)
45 "Given DIR-NAME, a store directory, checksum all the files in it one
46 by one and put the result into the file \".cargo-checksum.json\" in
48 (let* ((file-names (find-files dir-name "."))
49 (dir-prefix-name (string-append dir-name "/"))
50 (dir-prefix-name-len (string-length dir-prefix-name))
51 (checksums-file-name (string-append dir-name "/.cargo-checksum.json")))
52 (call-with-output-file checksums-file-name
54 (display "{\"files\":{" port)
56 (for-each (lambda (file-name)
57 (let ((file-relative-name (string-drop file-name dir-prefix-name-len)))
60 (write file-relative-name port)
62 (write (file-sha256 file-name) port))) file-names))
63 ;; NB: cargo requires the "package" field in order to check if the Cargo.lock
64 ;; file needs to be regenerated when the value changes. However, it doesn't
65 ;; appear to care what the value is to begin with...
66 (display "},\"package\":" port)
67 (write (file-sha256 "/dev/null") port)
68 (display "}" port)))))