cargo-build-system.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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-2023 Efraim Flashner <efraim@flashner.co.il>
  6. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  7. ;;; Copyright © 2020 Marius Bakke <marius@gnu.org>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  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. ;; Commentary:
  38. ;;
  39. ;; Builder-side code of the standard Rust package build procedure.
  40. ;;
  41. ;; Code:
  42. (define (manifest-targets)
  43. "Extract all targets from the Cargo.toml manifest"
  44. (let* ((port (open-input-pipe "cargo read-manifest"))
  45. (data (read-json port))
  46. (targets (or (assoc-ref data "targets") '())))
  47. (close-port port)
  48. targets))
  49. (define (has-executable-target?)
  50. "Check if the current cargo project declares any binary targets."
  51. (let* ((bin? (lambda (kind) (string=? kind "bin")))
  52. (get-kinds (lambda (dep) (assoc-ref dep "kind")))
  53. (bin-dep? (lambda (dep) (find bin? (get-kinds dep)))))
  54. (find bin-dep? (manifest-targets))))
  55. (define (crate-src? path)
  56. "Check if PATH refers to a crate source, namely a gzipped tarball with a
  57. Cargo.toml file present at its root."
  58. (and (not (directory-exists? path)) ; not a tarball
  59. ;; First we print out all file names within the tarball to see if it
  60. ;; looks like the source of a crate. However, the tarball will include
  61. ;; an extra path component which we would like to ignore (since we're
  62. ;; interested in checking if a Cargo.toml exists at the root of the
  63. ;; archive, but not nested anywhere else). We do this by cutting up
  64. ;; each output line and only looking at the second component. We then
  65. ;; check if it matches Cargo.toml exactly and short circuit if it does.
  66. (apply invoke (list "sh" "-c"
  67. (string-append "tar -tf " path
  68. " | cut -d/ -f2"
  69. " | grep -q '^Cargo.toml$'")))))
  70. (define* (unpack-rust-crates #:key inputs vendor-dir #:allow-other-keys)
  71. (define (inputs->rust-inputs inputs)
  72. "Filter using the label part from INPUTS."
  73. (filter (lambda (input)
  74. (match input
  75. ((name . _) (rust-package? name))))
  76. inputs))
  77. (define (inputs->directories inputs)
  78. "Extract the directory part from INPUTS."
  79. (match inputs
  80. (((names . directories) ...)
  81. directories)))
  82. (let ((rust-inputs (inputs->directories (inputs->rust-inputs inputs))))
  83. (unless (null? rust-inputs)
  84. (mkdir-p "target/package")
  85. (mkdir-p vendor-dir)
  86. ;; TODO: copy only regular inputs to target/package, not native-inputs.
  87. (for-each
  88. (lambda (input-crate)
  89. (for-each
  90. (lambda (packaged-crate)
  91. (unless
  92. (file-exists?
  93. (string-append "target/package/" (basename packaged-crate)))
  94. (install-file packaged-crate "target/package/")))
  95. (find-files
  96. (string-append input-crate "/share/cargo/registry") "\\.crate$")))
  97. (delete-duplicates rust-inputs))
  98. (for-each (lambda (crate)
  99. (invoke "tar" "xzf" crate "-C" vendor-dir))
  100. (find-files "target/package" "\\.crate$"))))
  101. #t)
  102. (define (rust-package? name)
  103. (string-prefix? "rust-" name))
  104. (define* (configure #:key inputs
  105. (vendor-dir "guix-vendor")
  106. #:allow-other-keys)
  107. "Vendor Cargo.toml dependencies as guix inputs."
  108. (chmod "." #o755)
  109. ;; Prepare one new directory with all the required dependencies.
  110. ;; It's necessary to do this (instead of just using /gnu/store as the
  111. ;; directory) because we want to hide the libraries in subdirectories
  112. ;; share/rust-source/... instead of polluting the user's profile root.
  113. (mkdir-p vendor-dir)
  114. (for-each
  115. (match-lambda
  116. ((name . path)
  117. (let* ((basepath (strip-store-file-name path))
  118. (crate-dir (string-append vendor-dir "/" basepath)))
  119. (and (crate-src? path)
  120. ;; Gracefully handle duplicate inputs
  121. (not (file-exists? crate-dir))
  122. (mkdir-p crate-dir)
  123. ;; Cargo crates are simply gzipped tarballs but with a .crate
  124. ;; extension. We expand the source to a directory name we control
  125. ;; so that we can generate any cargo checksums.
  126. ;; The --strip-components argument is needed to prevent creating
  127. ;; an extra directory within `crate-dir`.
  128. (format #t "Unpacking ~a~%" name)
  129. (invoke "tar" "xf" path "-C" crate-dir "--strip-components" "1")))))
  130. inputs)
  131. ;; Configure cargo to actually use this new directory.
  132. (setenv "CARGO_HOME" (string-append (getcwd) "/.cargo"))
  133. (mkdir-p ".cargo")
  134. (let ((port (open-file ".cargo/config" "w" #:encoding "utf-8")))
  135. (display "
  136. [source.crates-io]
  137. replace-with = 'vendored-sources'
  138. [source.vendored-sources]
  139. directory = '" port)
  140. (display (string-append (getcwd) "/" vendor-dir) port)
  141. (display "'
  142. " port)
  143. (close-port port))
  144. ;; Lift restriction on any lints: a crate author may have decided to opt
  145. ;; into stricter lints (e.g. #![deny(warnings)]) during their own builds
  146. ;; but we don't want any build failures that could be caused later by
  147. ;; upgrading the compiler for example.
  148. (setenv "RUSTFLAGS" "--cap-lints allow")
  149. (setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc"))
  150. (setenv "LIBGIT2_SYS_USE_PKG_CONFIG" "1")
  151. (setenv "LIBSSH2_SYS_USE_PKG_CONFIG" "1")
  152. (when (assoc-ref inputs "openssl")
  153. (setenv "OPENSSL_DIR" (assoc-ref inputs "openssl")))
  154. (when (assoc-ref inputs "gettext")
  155. (setenv "GETTEXT_SYSTEM" (assoc-ref inputs "gettext")))
  156. (when (assoc-ref inputs "clang")
  157. (setenv "LIBCLANG_PATH"
  158. (string-append (assoc-ref inputs "clang") "/lib")))
  159. ;; We don't use the Cargo.lock file to determine the package versions we use
  160. ;; during building, and in any case if one is not present it is created
  161. ;; during the 'build phase by cargo.
  162. (when (file-exists? "Cargo.lock")
  163. (delete-file "Cargo.lock"))
  164. #t)
  165. ;; After the 'patch-generated-file-shebangs phase any vendored crates who have
  166. ;; their shebangs patched will have a mismatch on their checksum.
  167. (define* (patch-cargo-checksums #:key
  168. (vendor-dir "guix-vendor")
  169. #:allow-other-keys)
  170. "Patch the checksums of the vendored crates after patching their shebangs."
  171. (generate-all-checksums vendor-dir)
  172. #t)
  173. (define* (build #:key
  174. skip-build?
  175. (features '())
  176. (cargo-build-flags '("--release"))
  177. #:allow-other-keys)
  178. "Build a given Cargo package."
  179. (or skip-build?
  180. (apply invoke
  181. `("cargo" "build"
  182. ,@(if (null? features)
  183. '()
  184. `("--features" ,(string-join features)))
  185. ,@cargo-build-flags))))
  186. (define* (check #:key
  187. tests?
  188. (cargo-test-flags '("--release"))
  189. #:allow-other-keys)
  190. "Run tests for a given Cargo package."
  191. (if tests?
  192. (apply invoke "cargo" "test" cargo-test-flags)
  193. #t))
  194. (define* (package #:key
  195. source
  196. skip-build?
  197. install-source?
  198. (cargo-package-flags '("--no-metadata" "--no-verify"))
  199. #:allow-other-keys)
  200. "Run 'cargo-package' for a given Cargo package."
  201. (if install-source?
  202. (if skip-build?
  203. (begin
  204. (install-file source "target/package")
  205. (with-directory-excursion "target/package"
  206. (for-each
  207. (lambda (file)
  208. (make-file-writable file)
  209. ;; Strip the hash and replace '.tar.gz' with '.crate'.
  210. (rename-file file
  211. (string-append (string-drop-right
  212. (string-drop file 35)
  213. (string-length ".tar.gz"))
  214. ".crate")))
  215. (find-files "." "\\.tar\\.gz$"))))
  216. (begin
  217. ;;error: invalid inclusion of reserved file name Cargo.toml.orig in package source
  218. (when (file-exists? "Cargo.toml.orig")
  219. (delete-file "Cargo.toml.orig"))
  220. (apply invoke `("cargo" "package" ,@cargo-package-flags))))
  221. (format #t "Not installing cargo sources, skipping `cargo package`.~%"))
  222. #t)
  223. (define* (install #:key
  224. inputs
  225. outputs
  226. skip-build?
  227. install-source?
  228. features
  229. #:allow-other-keys)
  230. "Install a given Cargo package."
  231. (let* ((out (assoc-ref outputs "out"))
  232. (registry (string-append out "/share/cargo/registry"))
  233. (sources (string-append out "/share/cargo/src")))
  234. (mkdir-p out)
  235. ;; Make cargo reuse all the artifacts we just built instead
  236. ;; of defaulting to making a new temp directory
  237. (setenv "CARGO_TARGET_DIR" "./target")
  238. ;; Only install crates which include binary targets,
  239. ;; otherwise cargo will raise an error.
  240. (or skip-build?
  241. (not (has-executable-target?))
  242. (invoke "cargo" "install" "--no-track" "--path" "." "--root" out
  243. "--features" (string-join features)))
  244. (when install-source?
  245. ;; Install crate tarballs and unpacked sources for later use.
  246. ;; TODO: Is there a better format/directory for these files?
  247. (mkdir-p sources)
  248. (for-each (lambda (crate)
  249. (install-file crate registry))
  250. (find-files "target/package" "\\.crate$"))
  251. (for-each (lambda (crate)
  252. (invoke "tar" "xzf" crate "-C" sources))
  253. (find-files registry "\\.crate$")))
  254. #t))
  255. (define %standard-phases
  256. (modify-phases gnu:%standard-phases
  257. (delete 'bootstrap)
  258. (replace 'configure configure)
  259. (replace 'build build)
  260. (replace 'check check)
  261. (replace 'install install)
  262. (add-after 'build 'package package)
  263. (add-after 'unpack 'unpack-rust-crates unpack-rust-crates)
  264. (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums patch-cargo-checksums)))
  265. (define* (cargo-build #:key inputs (phases %standard-phases)
  266. #:allow-other-keys #:rest args)
  267. "Build the given Cargo package, applying all of PHASES in order."
  268. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  269. ;;; cargo-build-system.scm ends here