guile.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix build-system guile)
  19. #:use-module (guix store)
  20. #:use-module (guix utils)
  21. #:use-module (guix packages)
  22. #:use-module (guix derivations)
  23. #:use-module (guix search-paths)
  24. #:use-module (guix build-system)
  25. #:use-module (guix build-system gnu)
  26. #:use-module (ice-9 match)
  27. #:use-module (srfi srfi-26)
  28. #:export (%guile-build-system-modules
  29. guile-build-system))
  30. (define %scheme-file-regexp
  31. ;; Regexp to match Scheme files.
  32. "\\.(scm|sls)$")
  33. (define %guile-build-system-modules
  34. ;; Build-side modules imported by default.
  35. `((guix build guile-build-system)
  36. ,@%gnu-build-system-modules))
  37. (define* (lower name
  38. #:key source inputs native-inputs outputs system target
  39. (implicit-inputs? #t)
  40. #:allow-other-keys
  41. #:rest arguments)
  42. "Return a bag for NAME."
  43. ;; Note: There's no #:guile argument (unlike, for instance,
  44. ;; 'ocaml-build-system' which has #:ocaml.) This is so we can keep
  45. ;; procedures like 'package-for-guile-2.0' unchanged and simple.
  46. (define private-keywords
  47. '(#:target #:inputs #:native-inputs
  48. #:implicit-inputs?))
  49. (bag
  50. (name name)
  51. (system system) (target target)
  52. (host-inputs `(
  53. ,@inputs))
  54. (build-inputs `(,@(if source
  55. `(("source" ,source))
  56. '())
  57. ,@native-inputs
  58. ,@(if implicit-inputs?
  59. (map (cute assoc <> (standard-packages))
  60. '("tar" "gzip" "bzip2" "xz" "locales"))
  61. '())))
  62. (outputs outputs)
  63. (build (if target guile-cross-build guile-build))
  64. (arguments (strip-keyword-arguments private-keywords arguments))))
  65. (define %compile-flags
  66. ;; Flags passed to 'guild compile' by default. We choose a common
  67. ;; denominator between Guile 2.0 and 2.2.
  68. ''("-Wunbound-variable" "-Warity-mismatch" "-Wformat"))
  69. (define* (guile-build store name inputs
  70. #:key source
  71. (guile #f)
  72. (phases '%standard-phases)
  73. (outputs '("out"))
  74. (search-paths '())
  75. (system (%current-system))
  76. (source-directory ".")
  77. not-compiled-file-regexp
  78. (scheme-file-regexp %scheme-file-regexp)
  79. (compile-flags %compile-flags)
  80. (imported-modules %guile-build-system-modules)
  81. (modules '((guix build guile-build-system)
  82. (guix build utils))))
  83. "Build SOURCE using Guile taken from the native inputs, and with INPUTS."
  84. (define builder
  85. `(begin
  86. (use-modules ,@modules)
  87. (guile-build #:name ,name
  88. #:source ,(match (assoc-ref inputs "source")
  89. (((? derivation? source))
  90. (derivation->output-path source))
  91. ((source)
  92. source)
  93. (source
  94. source))
  95. #:source-directory ,source-directory
  96. #:scheme-file-regexp ,scheme-file-regexp
  97. #:not-compiled-file-regexp ,not-compiled-file-regexp
  98. #:compile-flags ,compile-flags
  99. #:phases ,phases
  100. #:system ,system
  101. #:outputs %outputs
  102. #:search-paths ',(map search-path-specification->sexp
  103. search-paths)
  104. #:inputs %build-inputs)))
  105. (define guile-for-build
  106. (match guile
  107. ((? package?)
  108. (package-derivation store guile system #:graft? #f))
  109. (#f ; the default
  110. (let* ((distro (resolve-interface '(gnu packages commencement)))
  111. (guile (module-ref distro 'guile-final)))
  112. (package-derivation store guile system #:graft? #f)))))
  113. (build-expression->derivation store name builder
  114. #:inputs inputs
  115. #:system system
  116. #:modules imported-modules
  117. #:outputs outputs
  118. #:guile-for-build guile-for-build))
  119. (define* (guile-cross-build store name
  120. #:key
  121. (system (%current-system)) target
  122. native-drvs target-drvs
  123. (guile #f)
  124. source
  125. (outputs '("out"))
  126. (search-paths '())
  127. (native-search-paths '())
  128. (phases '%standard-phases)
  129. (source-directory ".")
  130. not-compiled-file-regexp
  131. (compile-flags %compile-flags)
  132. (imported-modules %guile-build-system-modules)
  133. (modules '((guix build guile-build-system)
  134. (guix build utils))))
  135. (define builder
  136. `(begin
  137. (use-modules ,@modules)
  138. (let ()
  139. (define %build-host-inputs
  140. ',(map (match-lambda
  141. ((name (? derivation? drv) sub ...)
  142. `(,name . ,(apply derivation->output-path drv sub)))
  143. ((name path)
  144. `(,name . ,path)))
  145. native-drvs))
  146. (define %build-target-inputs
  147. ',(map (match-lambda
  148. ((name (? derivation? drv) sub ...)
  149. `(,name . ,(apply derivation->output-path drv sub)))
  150. ((name (? package? pkg) sub ...)
  151. (let ((drv (package-cross-derivation store pkg
  152. target system)))
  153. `(,name . ,(apply derivation->output-path drv sub))))
  154. ((name path)
  155. `(,name . ,path)))
  156. target-drvs))
  157. (guile-build #:source ,(match (assoc-ref native-drvs "source")
  158. (((? derivation? source))
  159. (derivation->output-path source))
  160. ((source)
  161. source)
  162. (source
  163. source))
  164. #:system ,system
  165. #:target ,target
  166. #:outputs %outputs
  167. #:source-directory ,source-directory
  168. #:not-compiled-file-regexp ,not-compiled-file-regexp
  169. #:compile-flags ,compile-flags
  170. #:inputs %build-target-inputs
  171. #:native-inputs %build-host-inputs
  172. #:search-paths ',(map search-path-specification->sexp
  173. search-paths)
  174. #:native-search-paths ',(map
  175. search-path-specification->sexp
  176. native-search-paths)
  177. #:phases ,phases))))
  178. (define guile-for-build
  179. (match guile
  180. ((? package?)
  181. (package-derivation store guile system #:graft? #f))
  182. (#f ; the default
  183. (let* ((distro (resolve-interface '(gnu packages commencement)))
  184. (guile (module-ref distro 'guile-final)))
  185. (package-derivation store guile system #:graft? #f)))))
  186. (build-expression->derivation store name builder
  187. #:system system
  188. #:inputs (append native-drvs target-drvs)
  189. #:outputs outputs
  190. #:modules imported-modules
  191. #:substitutable? substitutable?
  192. #:guile-for-build guile-for-build))
  193. (define guile-build-system
  194. (build-system
  195. (name 'guile)
  196. (description "The build system for simple Guile packages")
  197. (lower lower)))