529a2b8b0fba87ec8d066f4ae9b29ba9dac08ce6
[jackhill/guix/guix.git] / guix / build-system / meson.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Peter Mikkelsen <petermikkelsen10@gmail.com>
3 ;;; Copyright © 2018 Marius Bakke <mbakke@fastmail.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
20 (define-module (guix build-system meson)
21 #:use-module (guix store)
22 #:use-module (guix utils)
23 #:use-module (guix derivations)
24 #:use-module (guix search-paths)
25 #:use-module (guix build-system)
26 #:use-module (guix build-system gnu)
27 #:use-module (guix build-system glib-or-gtk)
28 #:use-module (guix packages)
29 #:use-module (ice-9 match)
30 #:export (%meson-build-system-modules
31 meson-build-system))
32
33 ;; Commentary:
34 ;;
35 ;; Standard build procedure for packages using Meson. This is implemented as an
36 ;; extension of `gnu-build-system', with the option to turn on the glib/gtk
37 ;; phases from `glib-or-gtk-build-system'.
38 ;;
39 ;; Code:
40
41 (define %meson-build-system-modules
42 ;; Build-side modules imported by default.
43 `((guix build meson-build-system)
44 (guix build rpath)
45 ;; The modules from glib-or-gtk contains the modules from gnu-build-system,
46 ;; so there is no need to import that too.
47 ,@%glib-or-gtk-build-system-modules))
48
49 (define (default-ninja)
50 "Return the default ninja package."
51 ;; Lazily resolve the binding to avoid a circular dependency.
52 (let ((module (resolve-interface '(gnu packages ninja))))
53 (module-ref module 'ninja)))
54
55 (define (default-meson)
56 "Return the default meson package."
57 ;; Lazily resolve the binding to avoid a circular dependency.
58 (let ((module (resolve-interface '(gnu packages build-tools))))
59 (module-ref module 'meson-for-build)))
60
61 (define (default-patchelf)
62 "Return the default patchelf package."
63 ;; Lazily resolve the binding to avoid a circular dependency.
64 (let ((module (resolve-interface '(gnu packages elf))))
65 (module-ref module 'patchelf)))
66
67 (define* (lower name
68 #:key source inputs native-inputs outputs system target
69 (meson (default-meson))
70 (ninja (default-ninja))
71 (glib-or-gtk? #f)
72 #:allow-other-keys
73 #:rest arguments)
74 "Return a bag for NAME."
75 (define private-keywords
76 `(#:source #:meson #:ninja #:inputs #:native-inputs #:outputs #:target))
77
78 (and (not target) ;; TODO: add support for cross-compilation.
79 (bag
80 (name name)
81 (system system)
82 (build-inputs `(("meson" ,meson)
83 ("ninja" ,ninja)
84 ;; XXX PatchELF fails to build on armhf, so we skip
85 ;; the 'fix-runpath' phase there for now. It is used
86 ;; to avoid superfluous entries in RUNPATH as described
87 ;; in <https://bugs.gnu.org/28444#46>, so armhf may now
88 ;; have different runtime dependencies from other arches.
89 ,@(if (not (string-prefix? "arm" (or (%current-target-system)
90 (%current-system))))
91 `(("patchelf" ,(default-patchelf)))
92 '())
93 ,@native-inputs))
94 (host-inputs `(,@(if source
95 `(("source" ,source))
96 '())
97 ,@inputs
98 ;; Keep the standard inputs of 'gnu-build-system'.
99 ,@(standard-packages)))
100 (outputs outputs)
101 (build meson-build)
102 (arguments (strip-keyword-arguments private-keywords arguments)))))
103
104 (define* (meson-build store name inputs
105 #:key (guile #f)
106 (outputs '("out"))
107 (configure-flags ''())
108 (search-paths '())
109 (build-type "plain")
110 (tests? #t)
111 (test-target "test")
112 (glib-or-gtk? #f)
113 (parallel-build? #t)
114 (parallel-tests? #f)
115 (validate-runpath? #t)
116 (patch-shebangs? #t)
117 (strip-binaries? #t)
118 (strip-flags ''("--strip-debug"))
119 (strip-directories ''("lib" "lib64" "libexec"
120 "bin" "sbin"))
121 (elf-directories ''("lib" "lib64" "libexec"
122 "bin" "sbin"))
123 (phases '(@ (guix build meson-build-system)
124 %standard-phases))
125 (system (%current-system))
126 (imported-modules %meson-build-system-modules)
127 (modules '((guix build meson-build-system)
128 (guix build utils))))
129 "Build SOURCE using MESON, and with INPUTS, assuming that SOURCE
130 has a 'meson.build' file."
131 (define builder
132 `(let ((build-phases (if ,glib-or-gtk?
133 ,phases
134 (modify-phases ,phases
135 (delete 'glib-or-gtk-compile-schemas)
136 (delete 'glib-or-gtk-wrap)))))
137 (use-modules ,@modules)
138 (meson-build #:source ,(match (assoc-ref inputs "source")
139 (((? derivation? source))
140 (derivation->output-path source))
141 ((source)
142 source)
143 (source
144 source))
145 #:system ,system
146 #:outputs %outputs
147 #:inputs %build-inputs
148 #:search-paths ',(map search-path-specification->sexp
149 search-paths)
150 #:phases
151 (if (string-prefix? "arm" ,(or (%current-target-system)
152 (%current-system)))
153 (modify-phases build-phases (delete 'fix-runpath))
154 build-phases)
155 #:configure-flags ,configure-flags
156 #:build-type ,build-type
157 #:tests? ,tests?
158 #:test-target ,test-target
159 #:parallel-build? ,parallel-build?
160 #:parallel-tests? ,parallel-tests?
161 #:validate-runpath? ,validate-runpath?
162 #:patch-shebangs? ,patch-shebangs?
163 #:strip-binaries? ,strip-binaries?
164 #:strip-flags ,strip-flags
165 #:strip-directories ,strip-directories
166 #:elf-directories ,elf-directories)))
167
168 (define guile-for-build
169 (match guile
170 ((? package?)
171 (package-derivation store guile system #:graft? #f))
172 (#f ; the default
173 (let* ((distro (resolve-interface '(gnu packages commencement)))
174 (guile (module-ref distro 'guile-final)))
175 (package-derivation store guile system #:graft? #f)))))
176
177 (build-expression->derivation store name builder
178 #:system system
179 #:inputs inputs
180 #:modules imported-modules
181 #:outputs outputs
182 #:guile-for-build guile-for-build))
183
184 (define meson-build-system
185 (build-system
186 (name 'meson)
187 (description "The standard Meson build system")
188 (lower lower)))
189
190 ;;; meson.scm ends here