dev-python/hypothesis: Bump to 5.8.3
[gentoo.git] / eclass / meson.eclass
1 # Copyright 2017-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: meson.eclass
5 # @MAINTAINER:
6 # William Hubbs <williamh@gentoo.org>
7 # Mike Gilbert <floppym@gentoo.org>
8 # @SUPPORTED_EAPIS: 6 7
9 # @BLURB: common ebuild functions for meson-based packages
10 # @DESCRIPTION:
11 # This eclass contains the default phase functions for packages which
12 # use the meson build system.
13 #
14 # @EXAMPLE:
15 # Typical ebuild using meson.eclass:
16 #
17 # @CODE
18 # EAPI=6
19 #
20 # inherit meson
21 #
22 # ...
23 #
24 # src_configure() {
25 #       local emesonargs=(
26 #               -Dqt4=$(usex qt4 true false)
27 #               -Dthreads=$(usex threads true false)
28 #               -Dtiff=$(usex tiff true false)
29 #       )
30 #       meson_src_configure
31 # }
32 #
33 # ...
34 #
35 # @CODE
36
37 case ${EAPI:-0} in
38         6|7) ;;
39         *) die "EAPI=${EAPI} is not supported" ;;
40 esac
41
42 if [[ -z ${_MESON_ECLASS} ]]; then
43
44 inherit multiprocessing ninja-utils python-utils-r1 toolchain-funcs
45
46 fi
47
48 EXPORT_FUNCTIONS src_configure src_compile src_test src_install
49
50 if [[ -z ${_MESON_ECLASS} ]]; then
51 _MESON_ECLASS=1
52
53 MESON_DEPEND=">=dev-util/meson-0.51.2
54         >=dev-util/ninja-1.8.2"
55
56 if [[ ${EAPI:-0} == [6] ]]; then
57         DEPEND=${MESON_DEPEND}
58 else
59         BDEPEND=${MESON_DEPEND}
60 fi
61
62 # @ECLASS-VARIABLE: BUILD_DIR
63 # @DEFAULT_UNSET
64 # @DESCRIPTION:
65 # Build directory, location where all generated files should be placed.
66 # If this isn't set, it defaults to ${WORKDIR}/${P}-build.
67
68 # @ECLASS-VARIABLE: EMESON_SOURCE
69 # @DEFAULT_UNSET
70 # @DESCRIPTION:
71 # The location of the source files for the project; this is the source
72 # directory to pass to meson.
73 # If this isn't set, it defaults to ${S}
74
75 # @VARIABLE: emesonargs
76 # @DEFAULT_UNSET
77 # @DESCRIPTION:
78 # Optional meson arguments as Bash array; this should be defined before
79 # calling meson_src_configure.
80
81 # @VARIABLE: emesontestargs
82 # @DEFAULT_UNSET
83 # @DESCRIPTION:
84 # Optional meson test arguments as Bash array; this should be defined before
85 # calling meson_src_test.
86
87 # @VARIABLE: MYMESONARGS
88 # @DEFAULT_UNSET
89 # @DESCRIPTION:
90 # User-controlled environment variable containing arguments to be passed to
91 # meson in meson_src_configure.
92
93 read -d '' __MESON_ARRAY_PARSER <<"EOF"
94 import shlex
95 import sys
96
97 # See http://mesonbuild.com/Syntax.html#strings
98 def quote(str):
99         escaped = str.replace("\\\\", "\\\\\\\\").replace("'", "\\\\'")
100         return "'{}'".format(escaped)
101
102 print("[{}]".format(
103         ", ".join([quote(x) for x in shlex.split(" ".join(sys.argv[1:]))])))
104 EOF
105
106 # @FUNCTION: _meson_env_array
107 # @INTERNAL
108 # @DESCRIPTION:
109 # Parses the command line flags and converts them into an array suitable for
110 # use in a cross file.
111 #
112 # Input: --single-quote=\' --double-quote=\" --dollar=\$ --backtick=\`
113 #        --backslash=\\ --full-word-double="Hello World"
114 #        --full-word-single='Hello World'
115 #        --full-word-backslash=Hello\ World
116 #        --simple --unicode-8=© --unicode-16=𐐷 --unicode-32=𐤅
117 #
118 # Output: ['--single-quote=\'', '--double-quote="', '--dollar=$',
119 #          '--backtick=`', '--backslash=\\', '--full-word-double=Hello World',
120 #          '--full-word-single=Hello World',
121 #          '--full-word-backslash=Hello World', '--simple', '--unicode-8=©',
122 #          '--unicode-16=𐐷', '--unicode-32=𐤅']
123 #
124 _meson_env_array() {
125         python -c "${__MESON_ARRAY_PARSER}" "$@"
126 }
127
128 # @FUNCTION: _meson_create_cross_file
129 # @INTERNAL
130 # @DESCRIPTION:
131 # Creates a cross file. meson uses this to define settings for
132 # cross-compilers. This function is called from meson_src_configure.
133 _meson_create_cross_file() {
134         # Reference: http://mesonbuild.com/Cross-compilation.html
135
136         # system roughly corresponds to uname -s (lowercase)
137         local system=unknown
138         case ${CHOST} in
139                 *-aix*)          system=aix ;;
140                 *-cygwin*)       system=cygwin ;;
141                 *-darwin*)       system=darwin ;;
142                 *-freebsd*)      system=freebsd ;;
143                 *-linux*)        system=linux ;;
144                 mingw*|*-mingw*) system=windows ;;
145                 *-solaris*)      system=sunos ;;
146         esac
147
148         local cpu_family=$(tc-arch)
149         case ${cpu_family} in
150                 amd64) cpu_family=x86_64 ;;
151                 arm64) cpu_family=aarch64 ;;
152         esac
153
154         # This may require adjustment based on CFLAGS
155         local cpu=${CHOST%%-*}
156
157         local needs_exe_wrapper=false
158         tc-is-cross-compiler && needs_exe_wrapper=true
159
160         cat > "${T}/meson.${CHOST}.${ABI}" <<-EOF
161         [binaries]
162         ar = $(_meson_env_array "$(tc-getAR)")
163         c = $(_meson_env_array "$(tc-getCC)")
164         cpp = $(_meson_env_array "$(tc-getCXX)")
165         fortran = $(_meson_env_array "$(tc-getFC)")
166         llvm-config = '$(tc-getPROG LLVM_CONFIG llvm-config)'
167         objc = $(_meson_env_array "$(tc-getPROG OBJC cc)")
168         objcpp = $(_meson_env_array "$(tc-getPROG OBJCXX c++)")
169         pkgconfig = '$(tc-getPKG_CONFIG)'
170         strip = $(_meson_env_array "$(tc-getSTRIP)")
171         windres = $(_meson_env_array "$(tc-getRC)")
172
173         [properties]
174         c_args = $(_meson_env_array "${CFLAGS} ${CPPFLAGS}")
175         c_link_args = $(_meson_env_array "${CFLAGS} ${LDFLAGS}")
176         cpp_args = $(_meson_env_array "${CXXFLAGS} ${CPPFLAGS}")
177         cpp_link_args = $(_meson_env_array "${CXXFLAGS} ${LDFLAGS}")
178         fortran_args = $(_meson_env_array "${FCFLAGS}")
179         fortran_link_args = $(_meson_env_array "${FCFLAGS} ${LDFLAGS}")
180         objc_args = $(_meson_env_array "${OBJCFLAGS} ${CPPFLAGS}")
181         objc_link_args = $(_meson_env_array "${OBJCFLAGS} ${LDFLAGS}")
182         objcpp_args = $(_meson_env_array "${OBJCXXFLAGS} ${CPPFLAGS}")
183         objcpp_link_args = $(_meson_env_array "${OBJCXXFLAGS} ${LDFLAGS}")
184         needs_exe_wrapper = ${needs_exe_wrapper}
185
186         [host_machine]
187         system = '${system}'
188         cpu_family = '${cpu_family}'
189         cpu = '${cpu}'
190         endian = '$(tc-endian)'
191         EOF
192 }
193
194 # @FUNCTION: meson_use
195 # @USAGE: <USE flag> [option name]
196 # @DESCRIPTION:
197 # Given a USE flag and meson project option, outputs a string like:
198 #
199 #   -Doption=true
200 #   -Doption=false
201 #
202 # If the project option is unspecified, it defaults to the USE flag.
203 meson_use() {
204         usex "$1" "-D${2-$1}=true" "-D${2-$1}=false"
205 }
206
207 # @FUNCTION: meson_feature
208 # @USAGE: <USE flag> [option name]
209 # @DESCRIPTION:
210 # Given a USE flag and meson project option, outputs a string like:
211 #
212 #   -Doption=enabled
213 #   -Doption=disabled
214 #
215 # If the project option is unspecified, it defaults to the USE flag.
216 meson_feature() {
217         usex "$1" "-D${2-$1}=enabled" "-D${2-$1}=disabled"
218 }
219
220 # @FUNCTION: meson_src_configure
221 # @USAGE: [extra meson arguments]
222 # @DESCRIPTION:
223 # This is the meson_src_configure function.
224 meson_src_configure() {
225         debug-print-function ${FUNCNAME} "$@"
226
227         local mesonargs=(
228                 meson setup
229                 --buildtype plain
230                 --libdir "$(get_libdir)"
231                 --localstatedir "${EPREFIX}/var/lib"
232                 --prefix "${EPREFIX}/usr"
233                 --sysconfdir "${EPREFIX}/etc"
234                 --wrap-mode nodownload
235         )
236
237         if tc-is-cross-compiler || [[ ${ABI} != ${DEFAULT_ABI-${ABI}} ]]; then
238                 _meson_create_cross_file || die "unable to write meson cross file"
239                 mesonargs+=( --cross-file "${T}/meson.${CHOST}.${ABI}" )
240         fi
241
242         BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"
243
244         # Handle quoted whitespace
245         eval "local -a MYMESONARGS=( ${MYMESONARGS} )"
246
247         mesonargs+=(
248                 # Arguments from ebuild
249                 "${emesonargs[@]}"
250
251                 # Arguments passed to this function
252                 "$@"
253
254                 # Arguments from user
255                 "${MYMESONARGS[@]}"
256
257                 # Source directory
258                 "${EMESON_SOURCE:-${S}}"
259
260                 # Build directory
261                 "${BUILD_DIR}"
262         )
263
264         # https://bugs.gentoo.org/625396
265         python_export_utf8_locale
266
267         echo "${mesonargs[@]}" >&2
268         tc-env_build "${mesonargs[@]}" || die
269 }
270
271 # @FUNCTION: meson_src_compile
272 # @USAGE: [extra ninja arguments]
273 # @DESCRIPTION:
274 # This is the meson_src_compile function.
275 meson_src_compile() {
276         debug-print-function ${FUNCNAME} "$@"
277
278         eninja -C "${BUILD_DIR}" "$@"
279 }
280
281 # @FUNCTION: meson_src_test
282 # @USAGE: [extra meson test arguments]
283 # @DESCRIPTION:
284 # This is the meson_src_test function.
285 meson_src_test() {
286         debug-print-function ${FUNCNAME} "$@"
287
288         local mesontestargs=(
289                 -C "${BUILD_DIR}"
290         )
291         [[ -n ${NINJAOPTS} || -n ${MAKEOPTS} ]] &&
292                 mesontestargs+=(
293                         --num-processes "$(makeopts_jobs ${NINJAOPTS:-${MAKEOPTS}})"
294                 )
295
296         # Append additional arguments from ebuild
297         mesontestargs+=("${emesontestargs[@]}")
298
299         set -- meson test "${mesontestargs[@]}" "$@"
300         echo "$@" >&2
301         "$@" || die "tests failed"
302 }
303
304 # @FUNCTION: meson_src_install
305 # @USAGE: [extra ninja install arguments]
306 # @DESCRIPTION:
307 # This is the meson_src_install function.
308 meson_src_install() {
309         debug-print-function ${FUNCNAME} "$@"
310
311         DESTDIR="${D}" eninja -C "${BUILD_DIR}" install "$@"
312         einstalldocs
313 }
314
315 fi