Merge branch 'master' into staging
[jackhill/guix/guix.git] / guix / build / cargo-build-system.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-build-system)
23 #:use-module ((guix build gnu-build-system) #:prefix gnu:)
24 #:use-module (guix build json)
25 #:use-module (guix build utils)
26 #:use-module (guix build cargo-utils)
27 #:use-module (ice-9 popen)
28 #:use-module (ice-9 rdelim)
29 #:use-module (ice-9 ftw)
30 #:use-module (ice-9 format)
31 #:use-module (ice-9 match)
32 #:use-module (srfi srfi-1)
33 #:use-module (srfi srfi-26)
34 #:export (%standard-phases
35 cargo-build))
36
37 ;; Commentary:
38 ;;
39 ;; Builder-side code of the standard Rust package build procedure.
40 ;;
41 ;; Code:
42
43 (define (manifest-targets)
44 "Extract all targets from the Cargo.toml manifest"
45 (let* ((port (open-input-pipe "cargo read-manifest"))
46 (data (read-json port))
47 (targets (or (assoc-ref data "targets") '())))
48 (close-port port)
49 targets))
50
51 (define (has-executable-target?)
52 "Check if the current cargo project declares any binary targets."
53 (let* ((bin? (lambda (kind) (string=? kind "bin")))
54 (get-kinds (lambda (dep) (assoc-ref dep "kind")))
55 (bin-dep? (lambda (dep) (find bin? (get-kinds dep)))))
56 (find bin-dep? (manifest-targets))))
57
58 (define (crate-src? path)
59 "Check if PATH refers to a crate source, namely a gzipped tarball with a
60 Cargo.toml file present at its root."
61 (and (gzip-file? path)
62 ;; First we print out all file names within the tarball to see if it
63 ;; looks like the source of a crate. However, the tarball will include
64 ;; an extra path component which we would like to ignore (since we're
65 ;; interested in checking if a Cargo.toml exists at the root of the
66 ;; archive, but not nested anywhere else). We do this by cutting up
67 ;; each output line and only looking at the second component. We then
68 ;; check if it matches Cargo.toml exactly and short circuit if it does.
69 (apply invoke (list "sh" "-c"
70 (string-append "tar -tf " path
71 " | cut -d/ -f2"
72 " | grep -q '^Cargo.toml$'")))))
73
74 (define* (configure #:key inputs
75 (vendor-dir "guix-vendor")
76 #:allow-other-keys)
77 "Vendor Cargo.toml dependencies as guix inputs."
78 (chmod "." #o755)
79 ;; Prepare one new directory with all the required dependencies.
80 ;; It's necessary to do this (instead of just using /gnu/store as the
81 ;; directory) because we want to hide the libraries in subdirectories
82 ;; share/rust-source/... instead of polluting the user's profile root.
83 (mkdir-p vendor-dir)
84 (for-each
85 (match-lambda
86 ((name . path)
87 (let* ((basepath (strip-store-file-name path))
88 (crate-dir (string-append vendor-dir "/" basepath)))
89 (and (crate-src? path)
90 ;; Gracefully handle duplicate inputs
91 (not (file-exists? crate-dir))
92 (mkdir-p crate-dir)
93 ;; Cargo crates are simply gzipped tarballs but with a .crate
94 ;; extension. We expand the source to a directory name we control
95 ;; so that we can generate any cargo checksums.
96 ;; The --strip-components argument is needed to prevent creating
97 ;; an extra directory within `crate-dir`.
98 (invoke "tar" "xvf" path "-C" crate-dir "--strip-components" "1")))))
99 inputs)
100
101 ;; Configure cargo to actually use this new directory.
102 (setenv "CARGO_HOME" (string-append (getcwd) "/.cargo"))
103 (mkdir-p ".cargo")
104 (let ((port (open-file ".cargo/config" "w" #:encoding "utf-8")))
105 (display "
106 [source.crates-io]
107 replace-with = 'vendored-sources'
108
109 [source.vendored-sources]
110 directory = '" port)
111 (display (string-append (getcwd) "/" vendor-dir) port)
112 (display "'
113 " port)
114 (close-port port))
115
116 ;; Lift restriction on any lints: a crate author may have decided to opt
117 ;; into stricter lints (e.g. #![deny(warnings)]) during their own builds
118 ;; but we don't want any build failures that could be caused later by
119 ;; upgrading the compiler for example.
120 (setenv "RUSTFLAGS" "--cap-lints allow")
121 (setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc"))
122
123 ;; We don't use the Cargo.lock file to determine the package versions we use
124 ;; during building, and in any case if one is not present it is created
125 ;; during the 'build phase by cargo.
126 (when (file-exists? "Cargo.lock")
127 (delete-file "Cargo.lock"))
128 #t)
129
130 ;; After the 'patch-generated-file-shebangs phase any vendored crates who have
131 ;; their shebangs patched will have a mismatch on their checksum.
132 (define* (patch-cargo-checksums #:key
133 (vendor-dir "guix-vendor")
134 #:allow-other-keys)
135 "Patch the checksums of the vendored crates after patching their shebangs."
136 (generate-all-checksums vendor-dir)
137 #t)
138
139 (define* (build #:key
140 skip-build?
141 (cargo-build-flags '("--release"))
142 #:allow-other-keys)
143 "Build a given Cargo package."
144 (or skip-build?
145 (apply invoke `("cargo" "build" ,@cargo-build-flags))))
146
147 (define* (check #:key
148 tests?
149 (cargo-test-flags '("--release"))
150 #:allow-other-keys)
151 "Run tests for a given Cargo package."
152 (if tests?
153 (apply invoke `("cargo" "test" ,@cargo-test-flags))
154 #t))
155
156 (define* (install #:key inputs outputs skip-build? #:allow-other-keys)
157 "Install a given Cargo package."
158 (let* ((out (assoc-ref outputs "out")))
159 (mkdir-p out)
160
161 ;; Make cargo reuse all the artifacts we just built instead
162 ;; of defaulting to making a new temp directory
163 (setenv "CARGO_TARGET_DIR" "./target")
164
165 ;; Only install crates which include binary targets,
166 ;; otherwise cargo will raise an error.
167 (or skip-build?
168 (not (has-executable-target?))
169 (invoke "cargo" "install" "--path" "." "--root" out))))
170
171 (define %standard-phases
172 (modify-phases gnu:%standard-phases
173 (delete 'bootstrap)
174 (replace 'configure configure)
175 (replace 'build build)
176 (replace 'check check)
177 (replace 'install install)
178 (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums patch-cargo-checksums)))
179
180 (define* (cargo-build #:key inputs (phases %standard-phases)
181 #:allow-other-keys #:rest args)
182 "Build the given Cargo package, applying all of PHASES in order."
183 (apply gnu:gnu-build #:inputs inputs #:phases phases args))
184
185 ;;; cargo-build-system.scm ends here