ocaml.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2017, 2018 Julien Lepiller <julien@lepiller.eu>
  3. ;;; Copyright © 2017 Ben Woodcroft <donttrustben@gmail.com>
  4. ;;; Copyright © 2021-2022 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 ocaml)
  21. #:use-module (guix store)
  22. #:use-module (guix utils)
  23. #:use-module (guix gexp)
  24. #:use-module (guix search-paths)
  25. #:use-module (guix build-system)
  26. #:use-module (guix build-system gnu)
  27. #:use-module (guix packages)
  28. #:use-module (srfi srfi-1)
  29. #:export (%ocaml-build-system-modules
  30. package-with-ocaml4.07
  31. strip-ocaml4.07-variant
  32. package-with-ocaml4.09
  33. strip-ocaml4.09-variant
  34. package-with-ocaml5.0
  35. strip-ocaml5.0-variant
  36. default-findlib
  37. default-ocaml
  38. lower
  39. ocaml-build
  40. ocaml-build-system))
  41. ;; Commentary:
  42. ;;
  43. ;; Standard build procedure for packages using ocaml. This is implemented as an
  44. ;; extension of `gnu-build-system'.
  45. ;;
  46. ;; OCaml packages don't use a single standard for their build system. Some use
  47. ;; autotools, other use custom configure scripts with Makefiles, others use
  48. ;; oasis to generate the configure script and Makefile and lastly, some use
  49. ;; custom ocaml scripts.
  50. ;;
  51. ;; Each phase in the build system will try to figure out what the build system
  52. ;; is for that package. Most packages come with a custom configure script and
  53. ;; a Makefile that in turn call custom build tools. Packages built with oasis
  54. ;; will have a `setup.ml' file in the top directory, that can be used for all
  55. ;; phases. In that case the Makefile is here only to call that script. In case
  56. ;; the setup.ml do not work as expected, the @var{use-make} argument can be
  57. ;; used to ignore the setup.ml file and run make instead.
  58. ;;
  59. ;; Some packages use their own custom scripts, `pkg/pkg.ml' or
  60. ;; `pkg/build.ml'. They can be used here too.
  61. ;;
  62. ;; Code:
  63. (define %ocaml-build-system-modules
  64. ;; Build-side modules imported by default.
  65. `((guix build ocaml-build-system)
  66. ,@%gnu-build-system-modules))
  67. (define (default-ocaml)
  68. "Return the default OCaml package."
  69. ;; Do not use `@' to avoid introducing circular dependencies.
  70. (let ((module (resolve-interface '(gnu packages ocaml))))
  71. (module-ref module 'ocaml)))
  72. (define (default-findlib)
  73. "Return the default OCaml-findlib package."
  74. ;; Do not use `@' to avoid introducing circular dependencies.
  75. (let ((module (resolve-interface '(gnu packages ocaml))))
  76. (module-ref module 'ocaml-findlib)))
  77. (define (default-dune-build-system)
  78. "Return the dune-build-system."
  79. ;; Do not use `@' to avoid introducing circular dependencies.
  80. (let ((module (resolve-interface '(guix build-system dune))))
  81. (module-ref module 'dune-build-system)))
  82. (define (default-ocaml4.07)
  83. (let ((ocaml (resolve-interface '(gnu packages ocaml))))
  84. (module-ref ocaml 'ocaml-4.07)))
  85. (define (default-ocaml4.07-findlib)
  86. (let ((module (resolve-interface '(gnu packages ocaml))))
  87. (module-ref module 'ocaml4.07-findlib)))
  88. (define (default-ocaml4.07-dune)
  89. (let ((module (resolve-interface '(gnu packages ocaml))))
  90. (module-ref module 'ocaml4.07-dune)))
  91. (define (default-ocaml4.09)
  92. (let ((ocaml (resolve-interface '(gnu packages ocaml))))
  93. (module-ref ocaml 'ocaml-4.09)))
  94. (define (default-ocaml4.09-findlib)
  95. (let ((module (resolve-interface '(gnu packages ocaml))))
  96. (module-ref module 'ocaml4.09-findlib)))
  97. (define (default-ocaml4.09-dune)
  98. (let ((module (resolve-interface '(gnu packages ocaml))))
  99. (module-ref module 'ocaml4.09-dune)))
  100. (define (default-ocaml5.0)
  101. (let ((ocaml (resolve-interface '(gnu packages ocaml))))
  102. (module-ref ocaml 'ocaml-5.0)))
  103. (define (default-ocaml5.0-findlib)
  104. (let ((module (resolve-interface '(gnu packages ocaml))))
  105. (module-ref module 'ocaml5.0-findlib)))
  106. (define (default-ocaml5.0-dune)
  107. (let ((module (resolve-interface '(gnu packages ocaml))))
  108. (module-ref module 'ocaml5.0-dune)))
  109. (define* (package-with-explicit-ocaml ocaml findlib dune old-prefix new-prefix
  110. #:key variant-property)
  111. "Return a procedure of one argument, P. The procedure creates a package
  112. with the same fields as P, which is assumed to use OCAML-BUILD-SYSTEM, such
  113. that it is compiled with OCAML and FINDLIB instead. The inputs are changed
  114. recursively accordingly. If the name of P starts with OLD-PREFIX, this is
  115. replaced by NEW-PREFIX; otherwise, NEW-PREFIX is prepended to the name.
  116. When the package uses the DUNE-BUILD-SYSTEM, the procedure creates a package
  117. with the same fields as P, such that it is compiled with OCAML, FINDLIB and DUNE
  118. instead.
  119. When VARIANT-PROPERTY is present, it is used as a key to search for
  120. pre-defined variants of this transformation recorded in the 'properties' field
  121. of packages. The property value must be the promise of a package. This is a
  122. convenient way for package writers to force the transformation to use
  123. pre-defined variants."
  124. (define package-variant
  125. (if variant-property
  126. (lambda (package)
  127. (assq-ref (package-properties package)
  128. variant-property))
  129. (const #f)))
  130. (define (transform p)
  131. (cond
  132. ;; If VARIANT-PROPERTY is present, use that.
  133. ((package-variant p)
  134. => force)
  135. ;; Otherwise build the new package object graph.
  136. ((or (eq? (package-build-system p) ocaml-build-system)
  137. (eq? (package-build-system p) (default-dune-build-system)))
  138. (package
  139. (inherit p)
  140. (location (package-location p))
  141. (name (let ((name (package-name p)))
  142. (string-append new-prefix
  143. (if (string-prefix? old-prefix name)
  144. (substring name
  145. (string-length old-prefix))
  146. name))))
  147. (arguments
  148. (let ((ocaml (if (promise? ocaml) (force ocaml) ocaml))
  149. (findlib (if (promise? findlib) (force findlib) findlib))
  150. (dune (if (promise? dune) (force dune) dune)))
  151. (ensure-keyword-arguments (package-arguments p)
  152. `(#:ocaml ,ocaml
  153. #:findlib ,findlib
  154. ,@(if (eq? (package-build-system p)
  155. (default-dune-build-system))
  156. `(#:dune ,dune)
  157. '())))))))
  158. (else p)))
  159. (define (cut? p)
  160. (or (not (or (eq? (package-build-system p) ocaml-build-system)
  161. (eq? (package-build-system p) (default-dune-build-system))))
  162. (package-variant p)))
  163. (package-mapping transform cut?))
  164. (define package-with-ocaml4.07
  165. (package-with-explicit-ocaml (delay (default-ocaml4.07))
  166. (delay (default-ocaml4.07-findlib))
  167. (delay (default-ocaml4.07-dune))
  168. "ocaml-" "ocaml4.07-"
  169. #:variant-property 'ocaml4.07-variant))
  170. (define (strip-ocaml4.07-variant p)
  171. "Remove the 'ocaml4.07-variant' property from P."
  172. (package
  173. (inherit p)
  174. (properties (alist-delete 'ocaml4.07-variant (package-properties p)))))
  175. (define package-with-ocaml4.09
  176. (package-with-explicit-ocaml (delay (default-ocaml4.09))
  177. (delay (default-ocaml4.09-findlib))
  178. (delay (default-ocaml4.09-dune))
  179. "ocaml-" "ocaml4.09-"
  180. #:variant-property 'ocaml4.09-variant))
  181. (define (strip-ocaml4.09-variant p)
  182. "Remove the 'ocaml4.09-variant' property from P."
  183. (package
  184. (inherit p)
  185. (properties (alist-delete 'ocaml4.09-variant (package-properties p)))))
  186. (define package-with-ocaml5.0
  187. (package-with-explicit-ocaml (delay (default-ocaml5.0))
  188. (delay (default-ocaml5.0-findlib))
  189. (delay (default-ocaml5.0-dune))
  190. "ocaml-" "ocaml5.0-"
  191. #:variant-property 'ocaml5.0-variant))
  192. (define (strip-ocaml5.0-variant p)
  193. "Remove the 'ocaml5.0-variant' property from P."
  194. (package
  195. (inherit p)
  196. (properties (alist-delete 'ocaml5.0-variant (package-properties p)))))
  197. (define* (lower name
  198. #:key source inputs native-inputs outputs system target
  199. (ocaml (default-ocaml))
  200. (findlib (default-findlib))
  201. #:allow-other-keys
  202. #:rest arguments)
  203. "Return a bag for NAME."
  204. (define private-keywords
  205. '(#:target #:ocaml #:findlib #:inputs #:native-inputs))
  206. (and (not target) ;XXX: no cross-compilation
  207. (bag
  208. (name name)
  209. (system system)
  210. (host-inputs `(,@(if source
  211. `(("source" ,source))
  212. '())
  213. ,@inputs))
  214. (build-inputs `(("ocaml" ,ocaml)
  215. ("findlib" ,findlib)
  216. ,@native-inputs
  217. ;; Keep the standard inputs of 'gnu-build-system'.
  218. ,@(standard-packages)))
  219. (outputs outputs)
  220. (build ocaml-build)
  221. (arguments (strip-keyword-arguments private-keywords arguments)))))
  222. (define* (ocaml-build name inputs
  223. #:key
  224. guile source
  225. (outputs '("out")) (configure-flags ''())
  226. (search-paths '())
  227. (make-flags ''())
  228. (build-flags ''())
  229. (out-of-source? #t)
  230. (use-make? #f)
  231. (tests? #t)
  232. (test-flags ''("--enable-tests"))
  233. (test-target "test")
  234. (install-target "install")
  235. (validate-runpath? #t)
  236. (patch-shebangs? #t)
  237. (strip-binaries? #t)
  238. (strip-flags %strip-flags)
  239. (strip-directories %strip-directories)
  240. (phases '(@ (guix build ocaml-build-system)
  241. %standard-phases))
  242. (system (%current-system))
  243. (imported-modules %ocaml-build-system-modules)
  244. (modules '((guix build ocaml-build-system)
  245. (guix build utils))))
  246. "Build SOURCE using OCAML, and with INPUTS. This assumes that SOURCE
  247. provides a 'setup.ml' file as its build system."
  248. (define builder
  249. (with-imported-modules imported-modules
  250. #~(begin
  251. (use-modules #$@modules)
  252. (ocaml-build #:source #$source
  253. #:system #$system
  254. #:outputs #$(outputs->gexp outputs)
  255. #:inputs #$(input-tuples->gexp inputs)
  256. #:search-paths '#$(map search-path-specification->sexp
  257. search-paths)
  258. #:phases #$phases
  259. #:configure-flags #$configure-flags
  260. #:test-flags #$test-flags
  261. #:make-flags #$make-flags
  262. #:build-flags #$build-flags
  263. #:out-of-source? #$out-of-source?
  264. #:use-make? #$use-make?
  265. #:tests? #$tests?
  266. #:test-target #$test-target
  267. #:install-target #$install-target
  268. #:validate-runpath? #$validate-runpath?
  269. #:patch-shebangs? #$patch-shebangs?
  270. #:strip-binaries? #$strip-binaries?
  271. #:strip-flags #$strip-flags
  272. #:strip-directories #$strip-directories))))
  273. (gexp->derivation name builder
  274. #:system system
  275. #:target #f
  276. #:graft? #f
  277. #:guile-for-build guile))
  278. (define ocaml-build-system
  279. (build-system
  280. (name 'ocaml)
  281. (description "The standard OCaml build system")
  282. (lower lower)))
  283. ;;; ocaml.scm ends here