ocaml.scm 12 KB

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