cargo.scm 12 KB

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