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