ocaml.scm 12 KB

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