cargo-build-system.scm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. ;;; 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* (configure #:key inputs
  71. (vendor-dir "guix-vendor")
  72. #:allow-other-keys)
  73. "Vendor Cargo.toml dependencies as guix inputs."
  74. (chmod "." #o755)
  75. ;; Prepare one new directory with all the required dependencies.
  76. ;; It's necessary to do this (instead of just using /gnu/store as the
  77. ;; directory) because we want to hide the libraries in subdirectories
  78. ;; share/rust-source/... instead of polluting the user's profile root.
  79. (mkdir-p vendor-dir)
  80. (for-each
  81. (match-lambda
  82. ((name . path)
  83. (let* ((basepath (strip-store-file-name path))
  84. (crate-dir (string-append vendor-dir "/" basepath)))
  85. (and (crate-src? path)
  86. ;; Gracefully handle duplicate inputs
  87. (not (file-exists? crate-dir))
  88. (mkdir-p crate-dir)
  89. ;; Cargo crates are simply gzipped tarballs but with a .crate
  90. ;; extension. We expand the source to a directory name we control
  91. ;; so that we can generate any cargo checksums.
  92. ;; The --strip-components argument is needed to prevent creating
  93. ;; an extra directory within `crate-dir`.
  94. (invoke "tar" "xvf" path "-C" crate-dir "--strip-components" "1")))))
  95. inputs)
  96. ;; Configure cargo to actually use this new directory.
  97. (setenv "CARGO_HOME" (string-append (getcwd) "/.cargo"))
  98. (mkdir-p ".cargo")
  99. (let ((port (open-file ".cargo/config" "w" #:encoding "utf-8")))
  100. (display "
  101. [source.crates-io]
  102. replace-with = 'vendored-sources'
  103. [source.vendored-sources]
  104. directory = '" port)
  105. (display (string-append (getcwd) "/" vendor-dir) port)
  106. (display "'
  107. " port)
  108. (close-port port))
  109. ;; Lift restriction on any lints: a crate author may have decided to opt
  110. ;; into stricter lints (e.g. #![deny(warnings)]) during their own builds
  111. ;; but we don't want any build failures that could be caused later by
  112. ;; upgrading the compiler for example.
  113. (setenv "RUSTFLAGS" "--cap-lints allow")
  114. (setenv "CC" (string-append (assoc-ref inputs "gcc") "/bin/gcc"))
  115. (setenv "LIBGIT2_SYS_USE_PKG_CONFIG" "1")
  116. (setenv "LIBSSH2_SYS_USE_PKG_CONFIG" "1")
  117. (when (assoc-ref inputs "openssl")
  118. (setenv "OPENSSL_DIR" (assoc-ref inputs "openssl")))
  119. (when (assoc-ref inputs "gettext")
  120. (setenv "GETTEXT_SYSTEM" (assoc-ref inputs "gettext")))
  121. (when (assoc-ref inputs "clang")
  122. (setenv "LIBCLANG_PATH"
  123. (string-append (assoc-ref inputs "clang") "/lib")))
  124. ;; We don't use the Cargo.lock file to determine the package versions we use
  125. ;; during building, and in any case if one is not present it is created
  126. ;; during the 'build phase by cargo.
  127. (when (file-exists? "Cargo.lock")
  128. (delete-file "Cargo.lock"))
  129. #t)
  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. (define* (build #:key
  139. skip-build?
  140. (features '())
  141. (cargo-build-flags '("--release"))
  142. #:allow-other-keys)
  143. "Build a given Cargo package."
  144. (or skip-build?
  145. (apply invoke
  146. `("cargo" "build"
  147. ,@(if (null? features)
  148. '()
  149. `("--features" ,(string-join features)))
  150. ,@cargo-build-flags))))
  151. (define* (check #:key
  152. tests?
  153. (cargo-test-flags '("--release"))
  154. #:allow-other-keys)
  155. "Run tests for a given Cargo package."
  156. (if tests?
  157. (apply invoke "cargo" "test" cargo-test-flags)
  158. #t))
  159. (define* (install #:key inputs outputs skip-build? features #:allow-other-keys)
  160. "Install a given Cargo package."
  161. (let* ((out (assoc-ref outputs "out")))
  162. (mkdir-p out)
  163. ;; Make cargo reuse all the artifacts we just built instead
  164. ;; of defaulting to making a new temp directory
  165. (setenv "CARGO_TARGET_DIR" "./target")
  166. ;; Only install crates which include binary targets,
  167. ;; otherwise cargo will raise an error.
  168. (or skip-build?
  169. (not (has-executable-target?))
  170. (invoke "cargo" "install" "--no-track" "--path" "." "--root" out
  171. "--features" (string-join features)))
  172. #t))
  173. (define %standard-phases
  174. (modify-phases gnu:%standard-phases
  175. (delete 'bootstrap)
  176. (replace 'configure configure)
  177. (replace 'build build)
  178. (replace 'check check)
  179. (replace 'install install)
  180. (add-after 'patch-generated-file-shebangs 'patch-cargo-checksums patch-cargo-checksums)))
  181. (define* (cargo-build #:key inputs (phases %standard-phases)
  182. #:allow-other-keys #:rest args)
  183. "Build the given Cargo package, applying all of PHASES in order."
  184. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  185. ;;; cargo-build-system.scm ends here