gnu: varnish-modules: Update to 0.19.0.
[jackhill/guix/guix.git] / guix / build-system / dune.scm
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 pukkamustard <pukkamustard@posteo.net>
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
21 (define-module (guix build-system dune)
22 #:use-module (guix store)
23 #:use-module (guix utils)
24 #:use-module (guix derivations)
25 #:use-module (guix search-paths)
26 #:use-module (guix build-system)
27 #:use-module ((guix build-system gnu) #:prefix gnu:)
28 #:use-module ((guix build-system ocaml) #:prefix ocaml:)
29 #:use-module (guix packages)
30 #:use-module (ice-9 match)
31 #:use-module (srfi srfi-1)
32 #:export (%dune-build-system-modules
33 dune-build
34 dune-build-system))
35
36 ;; Commentary:
37 ;;
38 ;; Standard build procedure for packages using dune. This is implemented as an
39 ;; extension of `ocaml-build-system'.
40 ;;
41 ;; Code:
42
43 (define %dune-build-system-modules
44 ;; Build-side modules imported by default.
45 `((guix build dune-build-system)
46 ,@ocaml:%ocaml-build-system-modules))
47
48 (define (default-dune)
49 "Return the default OCaml package."
50
51 ;; Do not use `@' to avoid introducing circular dependencies.
52 (let ((module (resolve-interface '(gnu packages ocaml))))
53 (module-ref module 'dune)))
54
55 (define* (lower name
56 #:key source inputs native-inputs outputs system target
57 (dune (default-dune))
58 (ocaml (ocaml:default-ocaml))
59 (findlib (ocaml:default-findlib))
60 #:allow-other-keys
61 #:rest arguments)
62 "Return a bag for NAME."
63
64 ;; Flags that put dune into reproducible build mode.
65 (define dune-release-flags
66 (if (version>=? (package-version dune) "2.5.0")
67 ;; For dune >= 2.5.0 this is just --release.
68 ''("--release")
69 ;; --release does not exist before 2.5.0. Replace with flags compatible
70 ;; with our old ocaml4.07-dune (1.11.3)
71 ''("--root" "." "--ignore-promoted-rules" "--no-config"
72 "--profile" "release")))
73
74 (define private-keywords
75 '(#:source #:target #:dune #:findlib #:ocaml #:inputs #:native-inputs))
76
77 (and (not target) ;XXX: no cross-compilation
78 (let ((base (ocaml:lower name
79 #:source source
80 #:inputs inputs
81 #:native-inputs native-inputs
82 #:outputs outputs
83 #:system system
84 #:target target
85 #:ocaml ocaml
86 #:findlib findlib
87 arguments)))
88 (bag
89 (inherit base)
90 (build-inputs `(("dune" ,dune)
91 ,@(bag-build-inputs base)))
92 (build dune-build)
93 (arguments (append
94 `(#:dune-release-flags ,dune-release-flags)
95 (strip-keyword-arguments private-keywords arguments)))))))
96
97 (define* (dune-build store name inputs
98 #:key (guile #f)
99 (outputs '("out"))
100 (search-paths '())
101 (build-flags ''())
102 (out-of-source? #t)
103 (jbuild? #f)
104 (package #f)
105 (dune-release-flags ''())
106 (tests? #t)
107 (test-flags ''())
108 (test-target "test")
109 (install-target "install")
110 (validate-runpath? #t)
111 (patch-shebangs? #t)
112 (strip-binaries? #t)
113 (strip-flags ''("--strip-debug"))
114 (strip-directories ''("lib" "lib64" "libexec"
115 "bin" "sbin"))
116 (phases '(@ (guix build dune-build-system)
117 %standard-phases))
118 (system (%current-system))
119 (imported-modules %dune-build-system-modules)
120 (modules '((guix build dune-build-system)
121 (guix build utils))))
122 "Build SOURCE using OCAML, and with INPUTS. This assumes that SOURCE
123 provides a 'setup.ml' file as its build system."
124 (define builder
125 `(begin
126 (use-modules ,@modules)
127 (dune-build #:source ,(match (assoc-ref inputs "source")
128 (((? derivation? source))
129 (derivation->output-path source))
130 ((source)
131 source)
132 (source
133 source))
134 #:system ,system
135 #:outputs %outputs
136 #:inputs %build-inputs
137 #:search-paths ',(map search-path-specification->sexp
138 search-paths)
139 #:phases ,phases
140 #:test-flags ,test-flags
141 #:build-flags ,build-flags
142 #:out-of-source? ,out-of-source?
143 #:jbuild? ,jbuild?
144 #:package ,package
145 #:dune-release-flags ,dune-release-flags
146 #:tests? ,tests?
147 #:test-target ,test-target
148 #:install-target ,install-target
149 #:validate-runpath? ,validate-runpath?
150 #:patch-shebangs? ,patch-shebangs?
151 #:strip-binaries? ,strip-binaries?
152 #:strip-flags ,strip-flags
153 #:strip-directories ,strip-directories)))
154
155 (define guile-for-build
156 (match guile
157 ((? package?)
158 (package-derivation store guile system #:graft? #f))
159 (#f ; the default
160 (let* ((distro (resolve-interface '(gnu packages commencement)))
161 (guile (module-ref distro 'guile-final)))
162 (package-derivation store guile system #:graft? #f)))))
163
164 (build-expression->derivation store name builder
165 #:system system
166 #:inputs inputs
167 #:modules imported-modules
168 #:outputs outputs
169 #:guile-for-build guile-for-build))
170
171 (define dune-build-system
172 (build-system
173 (name 'dune)
174 (description "The standard Dune build system")
175 (lower lower)))
176
177 ;;; dune.scm ends here