cargo.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2019, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2013 Andreas Enge <andreas@enge.fr>
  4. ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
  5. ;;; Copyright © 2016 David Craven <david@craven.ch>
  6. ;;; Copyright © 2019 Ivan Petkov <ivanppetkov@gmail.com>
  7. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  8. ;;; Copyright © 2021 Efraim Flashner <efraim@flashner.co.il>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (guix build-system cargo)
  25. #:use-module (guix search-paths)
  26. #:use-module (guix store)
  27. #:use-module (guix utils)
  28. #:use-module (guix gexp)
  29. #:use-module (guix monads)
  30. #:use-module (guix packages)
  31. #:use-module (guix build-system)
  32. #:use-module (guix build-system gnu)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 vlist)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-26)
  37. #:export (%cargo-build-system-modules
  38. %cargo-utils-modules
  39. cargo-build-system
  40. %crate-base-url
  41. crate-url
  42. crate-url?
  43. crate-uri))
  44. (define %crate-base-url
  45. (make-parameter "https://crates.io"))
  46. (define crate-url
  47. (string-append (%crate-base-url) "/api/v1/crates/"))
  48. (define crate-url?
  49. (cut string-prefix? crate-url <>))
  50. (define (crate-uri name version)
  51. "Return a URI string for the crate package hosted at crates.io corresponding
  52. to NAME and VERSION."
  53. (string-append crate-url name "/" version "/download"))
  54. (define (default-rust)
  55. "Return the default Rust package."
  56. ;; Lazily resolve the binding to avoid a circular dependency.
  57. (let ((rust (resolve-interface '(gnu packages rust))))
  58. (module-ref rust 'rust)))
  59. (define %cargo-utils-modules
  60. ;; Build-side modules imported by default.
  61. `((guix build cargo-utils)
  62. ,@%gnu-build-system-modules))
  63. (define %cargo-build-system-modules
  64. ;; Build-side modules imported by default.
  65. `((guix build cargo-build-system)
  66. (guix build json)
  67. ,@%cargo-utils-modules))
  68. (define* (cargo-build name inputs
  69. #:key
  70. source
  71. (tests? #t)
  72. (test-target #f)
  73. (vendor-dir "guix-vendor")
  74. (cargo-build-flags ''("--release"))
  75. (cargo-test-flags ''("--release"))
  76. (cargo-package-flags ''("--no-metadata" "--no-verify"))
  77. (features ''())
  78. (skip-build? #f)
  79. (install-source? #t)
  80. (phases '%standard-phases)
  81. (outputs '("out"))
  82. (search-paths '())
  83. (system (%current-system))
  84. (guile #f)
  85. (imported-modules %cargo-build-system-modules)
  86. (modules '((guix build cargo-build-system)
  87. (guix build utils))))
  88. "Build SOURCE using CARGO, and with INPUTS."
  89. (define builder
  90. (with-imported-modules imported-modules
  91. #~(begin
  92. (use-modules #$@(sexp->gexp modules))
  93. (cargo-build #:name #$name
  94. #:source #+source
  95. #:system #$system
  96. #:test-target #$test-target
  97. #:vendor-dir #$vendor-dir
  98. #:cargo-build-flags #$(sexp->gexp cargo-build-flags)
  99. #:cargo-test-flags #$(sexp->gexp cargo-test-flags)
  100. #:cargo-package-flags #$(sexp->gexp cargo-package-flags)
  101. #:features #$(sexp->gexp features)
  102. #:skip-build? #$skip-build?
  103. #:install-source? #$install-source?
  104. #:tests? #$(and tests? (not skip-build?))
  105. #:phases #$(if (pair? phases)
  106. (sexp->gexp phases)
  107. phases)
  108. #:outputs #$(outputs->gexp outputs)
  109. #:inputs #$(input-tuples->gexp inputs)
  110. #:search-paths '#$(sexp->gexp
  111. (map search-path-specification->sexp
  112. search-paths))))))
  113. (gexp->derivation name builder
  114. #:system system
  115. #:target #f
  116. #:graft? #f
  117. #:guile-for-build guile))
  118. (define (package-cargo-inputs p)
  119. (apply
  120. (lambda* (#:key (cargo-inputs '()) #:allow-other-keys)
  121. cargo-inputs)
  122. (package-arguments p)))
  123. (define (package-cargo-development-inputs p)
  124. (apply
  125. (lambda* (#:key (cargo-development-inputs '()) #:allow-other-keys)
  126. cargo-development-inputs)
  127. (package-arguments p)))
  128. (define (crate-closure inputs)
  129. "Return the closure of INPUTS when considering the 'cargo-inputs' and
  130. 'cargod-dev-deps' edges. Omit duplicate inputs, except for those
  131. already present in INPUTS itself.
  132. This is implemented as a breadth-first traversal such that INPUTS is
  133. preserved, and only duplicate extracted inputs are removed.
  134. Forked from ((guix packages) transitive-inputs) since this extraction
  135. uses slightly different rules compared to the rest of Guix (i.e. we
  136. do not extract the conventional inputs)."
  137. (define (seen? seen item)
  138. ;; FIXME: We're using pointer identity here, which is extremely sensitive
  139. ;; to memoization in package-producing procedures; see
  140. ;; <https://bugs.gnu.org/30155>.
  141. (vhash-assq item seen))
  142. (let loop ((inputs inputs)
  143. (result '())
  144. (propagated '())
  145. (first? #t)
  146. (seen vlist-null))
  147. (match inputs
  148. (()
  149. (if (null? propagated)
  150. (reverse result)
  151. (loop (reverse (concatenate propagated)) result '() #f seen)))
  152. (((and input (label (? package? package))) rest ...)
  153. (if (and (not first?) (seen? seen package))
  154. (loop rest result propagated first? seen)
  155. (loop rest
  156. (cons input result)
  157. (cons (package-cargo-inputs package)
  158. propagated)
  159. first?
  160. (vhash-consq package package seen))))
  161. ((input rest ...)
  162. (loop rest (cons input result) propagated first? seen)))))
  163. (define (expand-crate-sources cargo-inputs cargo-development-inputs)
  164. "Extract all transitive sources for CARGO-INPUTS and CARGO-DEVELOPMENT-INPUTS
  165. along their 'cargo-inputs' edges.
  166. Cargo requires all transitive crate dependencies' sources to be available
  167. in its index, even if they are optional (this is so it can generate
  168. deterministic Cargo.lock files regardless of the target platform or enabled
  169. features). Thus we need all transitive crate dependencies for any cargo
  170. dev-dependencies, but this is only needed when building/testing a crate directly
  171. (i.e. we will never need transitive dev-dependencies for any dependency crates).
  172. Another complication arises due potential dependency cycles from Guix's
  173. perspective: Although cargo does not permit cyclic dependencies between crates,
  174. however, it permits cycles to occur via dev-dependencies. For example, if crate
  175. X depends on crate Y, crate Y's tests could pull in crate X to to verify
  176. everything builds properly (this is a rare scenario, but it it happens for
  177. example with the `proc-macro2` and `quote` crates). This is allowed by cargo
  178. because tests are built as a pseudo-crate which happens to depend on the
  179. X and Y crates, forming an acyclic graph.
  180. We can side step this problem by only considering regular cargo dependencies
  181. since they are guaranteed to not have cycles. We can further resolve any
  182. potential dev-dependency cycles by extracting package sources (which never have
  183. any dependencies and thus no cycles can exist).
  184. There are several implications of this decision:
  185. * Building a package definition does not require actually building/checking
  186. any dependent crates. This can be a benefits:
  187. - For example, sometimes a crate may have an optional dependency on some OS
  188. specific package which cannot be built or run on the current system. This
  189. approach means that the build will not fail if cargo ends up internally ignoring
  190. the dependency.
  191. - It avoids waiting for quadratic builds from source: cargo always builds
  192. dependencies within the current workspace. This is largely due to Rust not
  193. having a stable ABI and other resolutions that cargo applies. This means that
  194. if we have a dependency chain of X -> Y -> Z and we build each definition
  195. independently the following will happen:
  196. * Cargo will build and test crate Z
  197. * Cargo will build crate Z in Y's workspace, then build and test Y
  198. * Cargo will build crates Y and Z in X's workspace, then build and test X
  199. * But there are also some downsides with this approach:
  200. - If a dependent crate is subtly broken on the system (i.e. it builds but its
  201. tests fail) the consuming crates may build and test successfully but
  202. actually fail during normal usage (however, the CI will still build all
  203. packages which will give visibility in case packages suddenly break).
  204. - Because crates aren't declared as regular inputs, other Guix facilities
  205. such as tracking package graphs may not work by default (however, this is
  206. something that can always be extended or reworked in the future)."
  207. (filter-map
  208. (match-lambda
  209. ((label (? package? p))
  210. (list label (package-source p)))
  211. ((label input)
  212. (list label input)))
  213. (crate-closure (append cargo-inputs cargo-development-inputs))))
  214. (define* (lower name
  215. #:key source inputs native-inputs outputs system target
  216. (rust (default-rust))
  217. (cargo-inputs '())
  218. (cargo-development-inputs '())
  219. #:allow-other-keys
  220. #:rest arguments)
  221. "Return a bag for NAME."
  222. (define private-keywords
  223. '(#:target #:rust #:inputs #:native-inputs #:outputs
  224. #:cargo-inputs #:cargo-development-inputs))
  225. (and (not target) ;; TODO: support cross-compilation
  226. (bag
  227. (name name)
  228. (system system)
  229. (target target)
  230. (host-inputs `(,@(if source
  231. `(("source" ,source))
  232. '())
  233. ,@inputs
  234. ;; Keep the standard inputs of 'gnu-build-system'
  235. ,@(standard-packages)))
  236. (build-inputs `(("cargo" ,rust "cargo")
  237. ("rustc" ,rust)
  238. ,@(expand-crate-sources cargo-inputs cargo-development-inputs)
  239. ,@native-inputs))
  240. (outputs outputs)
  241. (build cargo-build)
  242. (arguments (strip-keyword-arguments private-keywords arguments)))))
  243. (define cargo-build-system
  244. (build-system
  245. (name 'cargo)
  246. (description
  247. "Cargo build system, to build Rust crates")
  248. (lower lower)))