gexp: Leave grafting as is when lowering allowed/disallowed references.
[jackhill/guix/guix.git] / guix / build-system / cmake.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2013 Cyril Roelandt <tipecaml@gmail.com>
4 ;;; Copyright © 2017 Ricardo Wurmus <rekado@elephly.net>
5 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22 (define-module (guix build-system cmake)
23 #:use-module (guix store)
24 #:use-module (guix gexp)
25 #:use-module (guix utils)
26 #:use-module (guix monads)
27 #:use-module (guix derivations)
28 #:use-module (guix search-paths)
29 #:use-module (guix build-system)
30 #:use-module (guix build-system gnu)
31 #:use-module (guix packages)
32 #:use-module (ice-9 match)
33 #:export (%cmake-build-system-modules
34 cmake-build
35 cmake-build-system))
36
37 ;; Commentary:
38 ;;
39 ;; Standard build procedure for packages using CMake. This is implemented as an
40 ;; extension of `gnu-build-system'.
41 ;;
42 ;; Code:
43
44 (define %cmake-build-system-modules
45 ;; Build-side modules imported by default.
46 `((guix build cmake-build-system)
47 ,@%gnu-build-system-modules))
48
49 (define (default-cmake target)
50 "Return the default CMake package."
51
52 ;; Do not use `@' to avoid introducing circular dependencies.
53 (let ((module (resolve-interface '(gnu packages cmake))))
54 (module-ref module
55 (if target
56 'cmake-minimal-cross
57 'cmake-minimal))))
58
59 (define* (lower name
60 #:key source inputs native-inputs outputs system target
61 (cmake (default-cmake target))
62 #:allow-other-keys
63 #:rest arguments)
64 "Return a bag for NAME."
65 (define private-keywords
66 `(#:cmake #:inputs #:native-inputs
67 ,@(if target '() '(#:target))))
68
69 (bag
70 (name name)
71 (system system)
72 (target target)
73 (build-inputs `(,@(if source
74 `(("source" ,source))
75 '())
76 ,@`(("cmake" ,cmake))
77 ,@native-inputs
78 ,@(if target '() inputs)
79 ,@(if target
80 ;; Use the standard cross inputs of
81 ;; 'gnu-build-system'.
82 (standard-cross-packages target 'host)
83 '())
84 ;; Keep the standard inputs of 'gnu-build-system'.
85 ,@(standard-packages)))
86 (host-inputs (if target inputs '()))
87
88 ;; The cross-libc is really a target package, but for bootstrapping
89 ;; reasons, we can't put it in 'host-inputs'. Namely, 'cross-gcc' is a
90 ;; native package, so it would end up using a "native" variant of
91 ;; 'cross-libc' (built with 'gnu-build'), whereas all the other packages
92 ;; would use a target variant (built with 'gnu-cross-build'.)
93 (target-inputs (if target
94 (standard-cross-packages target 'target)
95 '()))
96 (outputs outputs)
97 (build (if target cmake-cross-build cmake-build))
98 (arguments (strip-keyword-arguments private-keywords arguments))))
99
100 (define* (cmake-build name inputs
101 #:key guile source
102 (outputs '("out")) (configure-flags ''())
103 (search-paths '())
104 (make-flags ''())
105 (out-of-source? #t)
106 (build-type "RelWithDebInfo")
107 (tests? #t)
108 (test-target "test")
109 (parallel-build? #t) (parallel-tests? #t)
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 '%standard-phases)
117 (system (%current-system))
118 (substitutable? #t)
119 (imported-modules %cmake-build-system-modules)
120 (modules '((guix build cmake-build-system)
121 (guix build utils))))
122 "Build SOURCE using CMAKE, and with INPUTS. This assumes that SOURCE
123 provides a 'CMakeLists.txt' file as its build system."
124 (define build
125 (with-imported-modules imported-modules
126 #~(begin
127 (use-modules #$@(sexp->gexp modules))
128
129 #$(with-build-variables inputs outputs
130 #~(cmake-build #:source #+source
131 #:system #$system
132 #:outputs %outputs
133 #:inputs %build-inputs
134 #:search-paths '#$(sexp->gexp
135 (map search-path-specification->sexp
136 search-paths))
137 #:phases #$(if (pair? phases)
138 (sexp->gexp phases)
139 phases)
140 #:configure-flags #$(if (pair? configure-flags)
141 (sexp->gexp configure-flags)
142 configure-flags)
143 #:make-flags #$make-flags
144 #:out-of-source? #$out-of-source?
145 #:build-type #$build-type
146 #:tests? #$tests?
147 #:test-target #$test-target
148 #:parallel-build? #$parallel-build?
149 #:parallel-tests? #$parallel-tests?
150 #:validate-runpath? #$validate-runpath?
151 #:patch-shebangs? #$patch-shebangs?
152 #:strip-binaries? #$strip-binaries?
153 #:strip-flags #$(sexp->gexp strip-flags)
154 #:strip-directories #$(sexp->gexp strip-directories))))))
155
156 (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
157 system #:graft? #f)))
158 (gexp->derivation name build
159 #:system system
160 #:target #f
161 #:graft? #f
162 #:substitutable? substitutable?
163 #:guile-for-build guile)))
164
165 \f
166 ;;;
167 ;;; Cross-compilation.
168 ;;;
169
170 (define* (cmake-cross-build name
171 #:key
172 target
173 build-inputs target-inputs host-inputs
174 source guile
175 (outputs '("out"))
176 (configure-flags ''())
177 (search-paths '())
178 (native-search-paths '())
179 (make-flags ''())
180 (out-of-source? #t)
181 (build-type "RelWithDebInfo")
182 (tests? #f) ; nothing can be done
183 (test-target "test")
184 (parallel-build? #t) (parallel-tests? #t)
185 (validate-runpath? #t)
186 (patch-shebangs? #t)
187 (strip-binaries? #t)
188 (strip-flags ''("--strip-debug"
189 "--enable-deterministic-archives"))
190 (strip-directories ''("lib" "lib64" "libexec"
191 "bin" "sbin"))
192 (phases '%standard-phases)
193 (substitutable? #t)
194 (system (%current-system))
195 (build (nix-system->gnu-triplet system))
196 (imported-modules %cmake-build-system-modules)
197 (modules '((guix build cmake-build-system)
198 (guix build utils))))
199 "Cross-build NAME using CMAKE for TARGET, where TARGET is a GNU triplet and
200 with INPUTS. This assumes that SOURCE provides a 'CMakeLists.txt' file as its
201 build system."
202 (define builder
203 (with-imported-modules imported-modules
204 #~(begin
205 (use-modules #$@(sexp->gexp modules))
206
207 (define %build-host-inputs
208 #+(input-tuples->gexp build-inputs))
209
210 (define %build-target-inputs
211 (append #$(input-tuples->gexp host-inputs)
212 #+(input-tuples->gexp target-inputs)))
213
214 (define %build-inputs
215 (append %build-host-inputs %build-target-inputs))
216
217 (define %outputs
218 #$(outputs->gexp outputs))
219
220 (cmake-build #:source #+source
221 #:system #$system
222 #:build #$build
223 #:target #$target
224 #:outputs %outputs
225 #:inputs %build-target-inputs
226 #:native-inputs %build-host-inputs
227 #:search-paths '#$(map search-path-specification->sexp
228 search-paths)
229 #:native-search-paths '#$(map
230 search-path-specification->sexp
231 native-search-paths)
232 #:phases #$phases
233 #:configure-flags #$configure-flags
234 #:make-flags #$make-flags
235 #:out-of-source? #$out-of-source?
236 #:build-type #$build-type
237 #:tests? #$tests?
238 #:test-target #$test-target
239 #:parallel-build? #$parallel-build?
240 #:parallel-tests? #$parallel-tests?
241 #:validate-runpath? #$validate-runpath?
242 #:patch-shebangs? #$patch-shebangs?
243 #:strip-binaries? #$strip-binaries?
244 #:strip-flags #$strip-flags
245 #:strip-directories #$strip-directories))))
246
247 (mlet %store-monad ((guile (package->derivation (or guile (default-guile))
248 system #:graft? #f)))
249 (gexp->derivation name builder
250 #:system system
251 #:target target
252 #:graft? #f
253 #:substitutable? substitutable?
254 #:guile-for-build guile)))
255
256 (define cmake-build-system
257 (build-system
258 (name 'cmake)
259 (description "The standard CMake build system")
260 (lower lower)))
261
262 ;;; cmake.scm ends here