cargo.scm 11 KB

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