gnu: Add cxxopts.
[jackhill/guix/guix.git] / guix / build / cargo-utils.scm
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>
5 ;;; Copyright © 2019 Efraim Flashner <efraim@flashner.co.il>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (guix build cargo-utils)
23 #:use-module (guix build utils)
24 #:use-module (ice-9 popen)
25 #:use-module (ice-9 rdelim)
26 #:export (generate-checksums
27 generate-all-checksums))
28
29 ;; Commentary:
30 ;;
31 ;; Stand alone utilities for building Rust crates or the compiler itself,
32 ;; without depending on the entire cargo build-system itself.
33 ;;
34 ;; Code:
35
36 (define (file-sha256 file-name)
37 "Calculate the hexdigest of the sha256 checksum of FILE-NAME and return it."
38 (let ((port (open-pipe* OPEN_READ
39 "sha256sum"
40 "--"
41 file-name)))
42 (let ((result (read-delimited " " port)))
43 (close-pipe port)
44 result)))
45
46 (define (generate-checksums dir-name)
47 "Given DIR-NAME, a store directory, checksum all the files in it one
48 by one and put the result into the file \".cargo-checksum.json\" in
49 the same directory."
50 (let* ((file-names (find-files dir-name "."))
51 (dir-prefix-name (string-append dir-name "/"))
52 (dir-prefix-name-len (string-length dir-prefix-name))
53 (checksums-file-name (string-append dir-name "/.cargo-checksum.json")))
54 (call-with-output-file checksums-file-name
55 (lambda (port)
56 (display "{\"files\":{" port)
57 (let ((sep ""))
58 (for-each (lambda (file-name)
59 (let ((file-relative-name (string-drop file-name dir-prefix-name-len)))
60 (display sep port)
61 (set! sep ",")
62 (write file-relative-name port)
63 (display ":" port)
64 (write (file-sha256 file-name) port))) file-names))
65 ;; NB: cargo requires the "package" field in order to check if the Cargo.lock
66 ;; file needs to be regenerated when the value changes. However, it doesn't
67 ;; appear to care what the value is to begin with...
68 (display "},\"package\":" port)
69 (write (file-sha256 "/dev/null") port)
70 (display "}" port)))))
71
72 (define (generate-all-checksums dir-name)
73 (for-each
74 (lambda (filename)
75 (let* ((dir (dirname filename))
76 (checksum-file (string-append dir "/.cargo-checksum.json")))
77 (when (file-exists? checksum-file) (delete-file checksum-file))
78 (display (string-append
79 "patch-cargo-checksums: generate-checksums for "
80 dir "\n"))
81 (generate-checksums dir)))
82 (find-files dir-name "Cargo.toml$")))