cargo.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #:guile-for-build guile))
  117. (define (package-cargo-inputs p)
  118. (apply
  119. (lambda* (#:key (cargo-inputs '()) #:allow-other-keys)
  120. cargo-inputs)
  121. (package-arguments p)))
  122. (define (package-cargo-development-inputs p)
  123. (apply
  124. (lambda* (#:key (cargo-development-inputs '()) #:allow-other-keys)
  125. cargo-development-inputs)
  126. (package-arguments p)))
  127. (define (crate-closure inputs)
  128. "Return the closure of INPUTS when considering the 'cargo-inputs' and
  129. 'cargod-dev-deps' edges. Omit duplicate inputs, except for those
  130. already present in INPUTS itself.
  131. This is implemented as a breadth-first traversal such that INPUTS is
  132. preserved, and only duplicate extracted inputs are removed.
  133. Forked from ((guix packages) transitive-inputs) since this extraction
  134. uses slightly different rules compared to the rest of Guix (i.e. we
  135. do not extract the conventional inputs)."
  136. (define (seen? seen item)
  137. ;; FIXME: We're using pointer identity here, which is extremely sensitive
  138. ;; to memoization in package-producing procedures; see
  139. ;; <https://bugs.gnu.org/30155>.
  140. (vhash-assq item seen))
  141. (let loop ((inputs inputs)
  142. (result '())
  143. (propagated '())
  144. (first? #t)
  145. (seen vlist-null))
  146. (match inputs
  147. (()
  148. (if (null? propagated)
  149. (reverse result)
  150. (loop (reverse (concatenate propagated)) result '() #f seen)))
  151. (((and input (label (? package? package))) rest ...)
  152. (if (and (not first?) (seen? seen package))
  153. (loop rest result propagated first? seen)
  154. (loop rest
  155. (cons input result)
  156. (cons (package-cargo-inputs package)
  157. propagated)
  158. first?
  159. (vhash-consq package package seen))))
  160. ((input rest ...)
  161. (loop rest (cons input result) propagated first? seen)))))
  162. (define (expand-crate-sources cargo-inputs cargo-development-inputs)
  163. "Extract all transitive sources for CARGO-INPUTS and CARGO-DEVELOPMENT-INPUTS
  164. along their 'cargo-inputs' edges.
  165. Cargo requires all transitive crate dependencies' sources to be available
  166. in its index, even if they are optional (this is so it can generate
  167. deterministic Cargo.lock files regardless of the target platform or enabled
  168. features). Thus we need all transitive crate dependencies for any cargo
  169. dev-dependencies, but this is only needed when building/testing a crate directly
  170. (i.e. we will never need transitive dev-dependencies for any dependency crates).
  171. Another complication arises due potential dependency cycles from Guix's
  172. perspective: Although cargo does not permit cyclic dependencies between crates,
  173. however, it permits cycles to occur via dev-dependencies. For example, if crate
  174. X depends on crate Y, crate Y's tests could pull in crate X to to verify
  175. everything builds properly (this is a rare scenario, but it it happens for
  176. example with the `proc-macro2` and `quote` crates). This is allowed by cargo
  177. because tests are built as a pseudo-crate which happens to depend on the
  178. X and Y crates, forming an acyclic graph.
  179. We can side step this problem by only considering regular cargo dependencies
  180. since they are guaranteed to not have cycles. We can further resolve any
  181. potential dev-dependency cycles by extracting package sources (which never have
  182. any dependencies and thus no cycles can exist).
  183. There are several implications of this decision:
  184. * Building a package definition does not require actually building/checking
  185. any dependent crates. This can be a benefits:
  186. - For example, sometimes a crate may have an optional dependency on some OS
  187. specific package which cannot be built or run on the current system. This
  188. approach means that the build will not fail if cargo ends up internally ignoring
  189. the dependency.
  190. - It avoids waiting for quadratic builds from source: cargo always builds
  191. dependencies within the current workspace. This is largely due to Rust not
  192. having a stable ABI and other resolutions that cargo applies. This means that
  193. if we have a dependency chain of X -> Y -> Z and we build each definition
  194. independently the following will happen:
  195. * Cargo will build and test crate Z
  196. * Cargo will build crate Z in Y's workspace, then build and test Y
  197. * Cargo will build crates Y and Z in X's workspace, then build and test X
  198. * But there are also some downsides with this approach:
  199. - If a dependent crate is subtly broken on the system (i.e. it builds but its
  200. tests fail) the consuming crates may build and test successfully but
  201. actually fail during normal usage (however, the CI will still build all
  202. packages which will give visibility in case packages suddenly break).
  203. - Because crates aren't declared as regular inputs, other Guix facilities
  204. such as tracking package graphs may not work by default (however, this is
  205. something that can always be extended or reworked in the future)."
  206. (filter-map
  207. (match-lambda
  208. ((label (? package? p))
  209. (list label (package-source p)))
  210. ((label input)
  211. (list label input)))
  212. (crate-closure (append cargo-inputs cargo-development-inputs))))
  213. (define* (lower name
  214. #:key source inputs native-inputs outputs system target
  215. (rust (default-rust))
  216. (cargo-inputs '())
  217. (cargo-development-inputs '())
  218. #:allow-other-keys
  219. #:rest arguments)
  220. "Return a bag for NAME."
  221. (define private-keywords
  222. '(#:target #:rust #:inputs #:native-inputs #:outputs
  223. #:cargo-inputs #:cargo-development-inputs))
  224. (and (not target) ;; TODO: support cross-compilation
  225. (bag
  226. (name name)
  227. (system system)
  228. (target target)
  229. (host-inputs `(,@(if source
  230. `(("source" ,source))
  231. '())
  232. ,@inputs
  233. ;; Keep the standard inputs of 'gnu-build-system'
  234. ,@(standard-packages)))
  235. (build-inputs `(("cargo" ,rust "cargo")
  236. ("rustc" ,rust)
  237. ,@(expand-crate-sources cargo-inputs cargo-development-inputs)
  238. ,@native-inputs))
  239. (outputs outputs)
  240. (build cargo-build)
  241. (arguments (strip-keyword-arguments private-keywords arguments)))))
  242. (define cargo-build-system
  243. (build-system
  244. (name 'cargo)
  245. (description
  246. "Cargo build system, to build Rust crates")
  247. (lower lower)))