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