cargo-build-system.scm 7.3 KB

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