cargo-build-system.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, 2021 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. (invoke "tar" "xvf" path "-C" crate-dir "--strip-components" "1")))))
  129. inputs)
  130. ;; Configure cargo to actually use this new directory.
  131. (setenv "CARGO_HOME" (string-append (getcwd) "/.cargo"))
  132. (mkdir-p ".cargo")
  133. (let ((port (open-file ".cargo/config" "w" #:encoding "utf-8")))
  134. (display "
  135. [source.crates-io]
  136. replace-with = 'vendored-sources'
  137. [source.vendored-sources]
  138. directory = '" port)
  139. (display (string-append (getcwd) "/" vendor-dir) port)
  140. (display "'
  141. " port)
  142. (close-port port))
  143. ;; Lift restriction on any lints: a crate author may have decided to opt
  144. ;; into stricter lints (e.g. #![deny(warnings)]) during their own builds
  145. ;; but we don't want any build failures that could be caused later by
  146. ;; upgrading the compiler for example.
  147. (setenv "RUSTFLAGS" "--cap-lints allow")
  148. (setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc"))
  149. (setenv "LIBGIT2_SYS_USE_PKG_CONFIG" "1")
  150. (setenv "LIBSSH2_SYS_USE_PKG_CONFIG" "1")
  151. (when (assoc-ref inputs "openssl")
  152. (setenv "OPENSSL_DIR" (assoc-ref inputs "openssl")))
  153. (when (assoc-ref inputs "gettext")
  154. (setenv "GETTEXT_SYSTEM" (assoc-ref inputs "gettext")))
  155. (when (assoc-ref inputs "clang")
  156. (setenv "LIBCLANG_PATH"
  157. (string-append (assoc-ref inputs "clang") "/lib")))
  158. ;; We don't use the Cargo.lock file to determine the package versions we use
  159. ;; during building, and in any case if one is not present it is created
  160. ;; during the 'build phase by cargo.
  161. (when (file-exists? "Cargo.lock")
  162. (delete-file "Cargo.lock"))
  163. #t)
  164. ;; After the 'patch-generated-file-shebangs phase any vendored crates who have
  165. ;; their shebangs patched will have a mismatch on their checksum.
  166. (define* (patch-cargo-checksums #:key
  167. (vendor-dir "guix-vendor")
  168. #:allow-other-keys)
  169. "Patch the checksums of the vendored crates after patching their shebangs."
  170. (generate-all-checksums vendor-dir)
  171. #t)
  172. (define* (build #:key
  173. skip-build?
  174. (features '())
  175. (cargo-build-flags '("--release"))
  176. #:allow-other-keys)
  177. "Build a given Cargo package."
  178. (or skip-build?
  179. (apply invoke
  180. `("cargo" "build"
  181. ,@(if (null? features)
  182. '()
  183. `("--features" ,(string-join features)))
  184. ,@cargo-build-flags))))
  185. (define* (check #:key
  186. tests?
  187. (cargo-test-flags '("--release"))
  188. #:allow-other-keys)
  189. "Run tests for a given Cargo package."
  190. (if tests?
  191. (apply invoke "cargo" "test" cargo-test-flags)
  192. #t))
  193. (define* (package #:key
  194. install-source?
  195. (cargo-package-flags '("--no-metadata" "--no-verify"))
  196. #:allow-other-keys)
  197. "Run 'cargo-package' for a given Cargo package."
  198. (if install-source?
  199. (apply invoke `("cargo" "package" ,@cargo-package-flags))
  200. (format #t "Not installing cargo sources, skipping `cargo package`.~%"))
  201. #t)
  202. (define* (install #:key
  203. inputs
  204. outputs
  205. skip-build?
  206. install-source?
  207. features
  208. #:allow-other-keys)
  209. "Install a given Cargo package."
  210. (let* ((out (assoc-ref outputs "out"))
  211. (registry (string-append out "/share/cargo/registry"))
  212. (sources (string-append out "/share/cargo/src")))
  213. (mkdir-p out)
  214. ;; Make cargo reuse all the artifacts we just built instead
  215. ;; of defaulting to making a new temp directory
  216. (setenv "CARGO_TARGET_DIR" "./target")
  217. ;; Only install crates which include binary targets,
  218. ;; otherwise cargo will raise an error.
  219. (or skip-build?
  220. (not (has-executable-target?))
  221. (invoke "cargo" "install" "--no-track" "--path" "." "--root" out
  222. "--features" (string-join features)))
  223. (when install-source?
  224. ;; Install crate tarballs and unpacked sources for later use.
  225. ;; TODO: Is there a better format/directory for these files?
  226. (mkdir-p sources)
  227. (for-each (lambda (crate)
  228. (install-file crate registry))
  229. (find-files "target/package" "\\.crate$"))
  230. (for-each (lambda (crate)
  231. (invoke "tar" "xzf" crate "-C" sources))
  232. (find-files registry "\\.crate$")))
  233. #t))
  234. (define %standard-phases
  235. (modify-phases gnu:%standard-phases
  236. (delete 'bootstrap)
  237. (replace 'configure configure)
  238. (replace 'build build)
  239. (replace 'check check)
  240. (replace 'install install)
  241. (add-after 'build 'package package)
  242. (add-after 'unpack 'unpack-rust-crates unpack-rust-crates)
  243. (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums patch-cargo-checksums)))
  244. (define* (cargo-build #:key inputs (phases %standard-phases)
  245. #:allow-other-keys #:rest args)
  246. "Build the given Cargo package, applying all of PHASES in order."
  247. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  248. ;;; cargo-build-system.scm ends here