asdf.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017 Andy Patterson <ajpatter@uwaterloo.ca>
  3. ;;; Copyright © 2019, 2020, 2021 Guillaume Le Vaillant <glv@posteo.net>
  4. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix build-system asdf)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix memoization)
  24. #:use-module (guix packages)
  25. #:use-module (guix gexp)
  26. #:use-module (guix monads)
  27. #:use-module (guix search-paths)
  28. #:use-module ((guix build utils)
  29. #:select ((package-name->name+version
  30. . hyphen-separated-name->name+version)))
  31. #:use-module (guix build-system)
  32. #:use-module (guix build-system gnu)
  33. #:use-module (ice-9 match)
  34. #:use-module (ice-9 regex)
  35. #:use-module (srfi srfi-1)
  36. #:use-module (srfi srfi-26)
  37. #:use-module (gnu packages)
  38. #:export (%asdf-build-system-modules
  39. %asdf-build-modules
  40. asdf-build
  41. asdf-build-system/sbcl
  42. asdf-build-system/ecl
  43. asdf-build-system/source
  44. sbcl-package->cl-source-package
  45. sbcl-package->ecl-package))
  46. ;; Commentary:
  47. ;;
  48. ;; Standard build procedure for asdf packages. This is implemented as an
  49. ;; extension of 'gnu-build-system'.
  50. ;;
  51. ;; Code:
  52. (define %asdf-build-system-modules
  53. ;; Imported build-side modules
  54. `((guix build asdf-build-system)
  55. (guix build lisp-utils)
  56. (guix build union)
  57. ,@%gnu-build-system-modules))
  58. (define %asdf-build-modules
  59. ;; Used (visible) build-side modules
  60. '((guix build asdf-build-system)
  61. (guix build utils)
  62. (guix build union)
  63. (guix build lisp-utils)))
  64. (define (default-lisp implementation)
  65. "Return the default package for the lisp IMPLEMENTATION."
  66. ;; Lazily resolve the binding to avoid a circular dependency.
  67. (let ((lisp-module (resolve-interface '(gnu packages lisp))))
  68. (module-ref lisp-module implementation)))
  69. (define* (lower/source name
  70. #:key source inputs outputs native-inputs system target
  71. #:allow-other-keys
  72. #:rest arguments)
  73. "Return a bag for NAME"
  74. (define private-keywords
  75. '(#:target #:inputs #:native-inputs))
  76. (and (not target)
  77. (bag
  78. (name name)
  79. (system system)
  80. (host-inputs `(,@(if source
  81. `(("source" ,source))
  82. '())
  83. ,@inputs
  84. ,@(standard-packages)))
  85. (build-inputs native-inputs)
  86. (outputs outputs)
  87. (build asdf-build/source)
  88. (arguments (strip-keyword-arguments private-keywords arguments)))))
  89. (define* (asdf-build/source name inputs
  90. #:key source outputs
  91. (phases '%standard-phases/source)
  92. (search-paths '())
  93. (system (%current-system))
  94. (guile #f)
  95. (imported-modules %asdf-build-system-modules)
  96. (modules %asdf-build-modules))
  97. (define builder
  98. (with-imported-modules imported-modules
  99. #~(begin
  100. (use-modules #$@(sexp->gexp modules))
  101. (asdf-build/source #:name #$name
  102. #:source #+source
  103. #:system #$system
  104. #:phases #$phases
  105. #:outputs #$(outputs->gexp outputs)
  106. #:search-paths '#$(sexp->gexp
  107. (map search-path-specification->sexp
  108. search-paths))
  109. #:inputs #$(input-tuples->gexp inputs)))))
  110. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  111. system #:graft? #f)))
  112. (gexp->derivation name builder
  113. #:system system
  114. #:guile-for-build guile)))
  115. (define* (package-with-build-system from-build-system to-build-system
  116. from-prefix to-prefix
  117. #:key variant-property
  118. phases-transformer)
  119. "Return a precedure which takes a package PKG which uses FROM-BUILD-SYSTEM,
  120. and returns one using TO-BUILD-SYSTEM. If PKG was prefixed by FROM-PREFIX,
  121. the resulting package will be prefixed by TO-PREFIX. Inputs of PKG are
  122. recursively transformed using the same rule. The result's #:phases argument
  123. will be modified by PHASES-TRANSFORMER, an S-expression which evaluates on the
  124. build side to a procedure of one argument.
  125. VARIANT-PROPERTY can be added to a package's properties to indicate that the
  126. corresponding package promise should be used as the result of this
  127. transformation. This allows the result to differ from what the transformation
  128. would otherwise produce.
  129. If TO-BUILD-SYSTEM is asdf-build-system/source, the resulting package will be
  130. set up using CL source package conventions."
  131. (define target-is-source? (eq? asdf-build-system/source to-build-system))
  132. (define (transform-package-name name)
  133. (if (string-prefix? from-prefix name)
  134. (let ((new-name (string-drop name (string-length from-prefix))))
  135. (if (string-prefix? to-prefix new-name)
  136. new-name
  137. (string-append to-prefix new-name)))
  138. name))
  139. (define (has-from-build-system? pkg)
  140. (eq? from-build-system (package-build-system pkg)))
  141. (define (find-input-package pkg)
  142. (let* ((name (package-name pkg))
  143. (new-name (transform-package-name name))
  144. (pkgs (find-packages-by-name new-name)))
  145. (if (null? pkgs) #f (list-ref pkgs 0))))
  146. (define transform
  147. (mlambda (pkg)
  148. (define rewrite
  149. (match-lambda
  150. ((name content . rest)
  151. (let* ((is-package? (package? content))
  152. (new-content (if is-package?
  153. (or (find-input-package content)
  154. (transform content))
  155. content)))
  156. `(,name ,new-content ,@rest)))))
  157. ;; Special considerations for source packages: CL inputs become
  158. ;; propagated, and un-handled arguments are removed.
  159. (define (new-propagated-inputs)
  160. (if target-is-source?
  161. (map rewrite
  162. (append
  163. (filter (match-lambda
  164. ((_ input . _)
  165. (has-from-build-system? input)))
  166. (append (package-inputs pkg)
  167. ;; The native inputs might be needed just
  168. ;; to load the system.
  169. (package-native-inputs pkg)))
  170. (package-propagated-inputs pkg)))
  171. (map rewrite (package-propagated-inputs pkg))))
  172. (define (new-inputs inputs-getter)
  173. (if target-is-source?
  174. (map rewrite
  175. (filter (match-lambda
  176. ((_ input . _)
  177. (not (has-from-build-system? input))))
  178. (inputs-getter pkg)))
  179. (map rewrite (inputs-getter pkg))))
  180. (define base-arguments
  181. (if target-is-source?
  182. (strip-keyword-arguments
  183. '(#:tests? #:asd-files #:lisp #:asd-systems #:test-asd-file)
  184. (package-arguments pkg))
  185. (package-arguments pkg)))
  186. (cond
  187. ((and variant-property
  188. (assoc-ref (package-properties pkg) variant-property))
  189. => force)
  190. ((has-from-build-system? pkg)
  191. (package
  192. (inherit pkg)
  193. (location (package-location pkg))
  194. (name (transform-package-name (package-name pkg)))
  195. (build-system to-build-system)
  196. (arguments
  197. (substitute-keyword-arguments base-arguments
  198. ((#:phases phases) (list phases-transformer phases))))
  199. (inputs (new-inputs package-inputs))
  200. (propagated-inputs (new-propagated-inputs))
  201. (native-inputs (append (if target-is-source?
  202. (list (list (package-name pkg) pkg))
  203. '())
  204. (new-inputs package-native-inputs)))
  205. (outputs (if target-is-source?
  206. '("out")
  207. (package-outputs pkg)))))
  208. (else pkg))))
  209. transform)
  210. (define (strip-variant-as-necessary variant pkg)
  211. (define properties (package-properties pkg))
  212. (if (assoc variant properties)
  213. (package
  214. (inherit pkg)
  215. (properties (alist-delete variant properties)))
  216. pkg))
  217. (define (lower lisp-type)
  218. (lambda* (name
  219. #:key source inputs outputs native-inputs system target
  220. (lisp (default-lisp (string->symbol lisp-type)))
  221. #:allow-other-keys
  222. #:rest arguments)
  223. "Return a bag for NAME"
  224. (define private-keywords
  225. '(#:target #:inputs #:native-inputs #:lisp))
  226. (and (not target)
  227. (bag
  228. (name name)
  229. (system system)
  230. (host-inputs `(,@(if source
  231. `(("source" ,source))
  232. '())
  233. ,@inputs
  234. ,@(standard-packages)))
  235. (build-inputs `((,lisp-type ,lisp)
  236. ,@native-inputs))
  237. (outputs outputs)
  238. (build (asdf-build lisp-type))
  239. (arguments (strip-keyword-arguments private-keywords arguments))))))
  240. (define (asdf-build lisp-type)
  241. (lambda* (name inputs
  242. #:key source outputs
  243. (tests? #t)
  244. (asd-files ''())
  245. (asd-systems ''())
  246. (test-asd-file #f)
  247. (phases '%standard-phases)
  248. (search-paths '())
  249. (system (%current-system))
  250. (guile #f)
  251. (imported-modules %asdf-build-system-modules)
  252. (modules %asdf-build-modules))
  253. (define systems
  254. (if (null? (cadr asd-systems))
  255. ;; FIXME: Find a more reliable way to get the main system name.
  256. (let* ((lisp-prefix (string-append lisp-type "-"))
  257. (package-name (hyphen-separated-name->name+version
  258. (if (string-prefix? lisp-prefix name)
  259. (string-drop name
  260. (string-length lisp-prefix))
  261. name))))
  262. `(quote ,(list package-name)))
  263. asd-systems))
  264. (define builder
  265. (with-imported-modules imported-modules
  266. #~(begin
  267. (use-modules #$@(sexp->gexp modules))
  268. (parameterize ((%lisp (string-append
  269. (assoc-ref %build-inputs #$lisp-type)
  270. "/bin/" #$lisp-type))
  271. (%lisp-type #$lisp-type))
  272. (asdf-build #:name #$name
  273. #:source #+source
  274. #:asd-files #$asd-files
  275. #:asd-systems #$systems
  276. #:test-asd-file #$test-asd-file
  277. #:system #$system
  278. #:tests? #$tests?
  279. #:phases #$phases
  280. #:outputs #$(outputs->gexp outputs)
  281. #:search-paths '#$(sexp->gexp
  282. (map search-path-specification->sexp
  283. search-paths))
  284. #:inputs #$(input-tuples->gexp inputs))))))
  285. (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
  286. system #:graft? #f)))
  287. (gexp->derivation name builder
  288. #:system system
  289. #:guile-for-build guile))))
  290. (define asdf-build-system/sbcl
  291. (build-system
  292. (name 'asdf/sbcl)
  293. (description "The build system for ASDF binary packages using SBCL")
  294. (lower (lower "sbcl"))))
  295. (define asdf-build-system/ecl
  296. (build-system
  297. (name 'asdf/ecl)
  298. (description "The build system for ASDF binary packages using ECL")
  299. (lower (lower "ecl"))))
  300. (define asdf-build-system/source
  301. (build-system
  302. (name 'asdf/source)
  303. (description "The build system for ASDF source packages")
  304. (lower lower/source)))
  305. (define sbcl-package->cl-source-package
  306. (let* ((property 'cl-source-variant)
  307. (transformer
  308. (package-with-build-system asdf-build-system/sbcl
  309. asdf-build-system/source
  310. "sbcl-"
  311. "cl-"
  312. #:variant-property property
  313. #:phases-transformer
  314. '(const %standard-phases/source))))
  315. (lambda (pkg)
  316. (transformer
  317. (strip-variant-as-necessary property pkg)))))
  318. (define sbcl-package->ecl-package
  319. (let* ((property 'ecl-variant)
  320. (transformer
  321. (package-with-build-system asdf-build-system/sbcl
  322. asdf-build-system/ecl
  323. "sbcl-"
  324. "ecl-"
  325. #:variant-property property
  326. #:phases-transformer
  327. 'identity)))
  328. (lambda (pkg)
  329. (transformer
  330. (strip-variant-as-necessary property pkg)))))
  331. ;;; asdf.scm ends here