ocaml.scm 11 KB

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