dev-python/josepy: 1.1.0 cleanup
[gentoo.git] / eclass / meson.eclass
1 # Copyright 2017-2019 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
88 read -d '' __MESON_ARRAY_PARSER <<"EOF"
89 import shlex
90 import sys
91
92 # See http://mesonbuild.com/Syntax.html#strings
93 def quote(str):
94         escaped = str.replace("\\\\", "\\\\\\\\").replace("'", "\\\\'")
95         return "'{}'".format(escaped)
96
97 print("[{}]".format(
98         ", ".join([quote(x) for x in shlex.split(" ".join(sys.argv[1:]))])))
99 EOF
100
101 # @FUNCTION: _meson_env_array
102 # @INTERNAL
103 # @DESCRIPTION:
104 # Parses the command line flags and converts them into an array suitable for
105 # use in a cross file.
106 #
107 # Input: --single-quote=\' --double-quote=\" --dollar=\$ --backtick=\`
108 #        --backslash=\\ --full-word-double="Hello World"
109 #        --full-word-single='Hello World'
110 #        --full-word-backslash=Hello\ World
111 #        --simple --unicode-8=© --unicode-16=𐐷 --unicode-32=𐤅
112 #
113 # Output: ['--single-quote=\'', '--double-quote="', '--dollar=$',
114 #          '--backtick=`', '--backslash=\\', '--full-word-double=Hello World',
115 #          '--full-word-single=Hello World',
116 #          '--full-word-backslash=Hello World', '--simple', '--unicode-8=©',
117 #          '--unicode-16=𐐷', '--unicode-32=𐤅']
118 #
119 _meson_env_array() {
120         python -c "${__MESON_ARRAY_PARSER}" "$@"
121 }
122
123 # @FUNCTION: _meson_create_cross_file
124 # @INTERNAL
125 # @DESCRIPTION:
126 # Creates a cross file. meson uses this to define settings for
127 # cross-compilers. This function is called from meson_src_configure.
128 _meson_create_cross_file() {
129         # Reference: http://mesonbuild.com/Cross-compilation.html
130
131         # system roughly corresponds to uname -s (lowercase)
132         local system=unknown
133         case ${CHOST} in
134                 *-aix*)          system=aix ;;
135                 *-cygwin*)       system=cygwin ;;
136                 *-darwin*)       system=darwin ;;
137                 *-freebsd*)      system=freebsd ;;
138                 *-linux*)        system=linux ;;
139                 mingw*|*-mingw*) system=windows ;;
140                 *-solaris*)      system=sunos ;;
141         esac
142
143         local cpu_family=$(tc-arch)
144         case ${cpu_family} in
145                 amd64) cpu_family=x86_64 ;;
146                 arm64) cpu_family=aarch64 ;;
147         esac
148
149         # This may require adjustment based on CFLAGS
150         local cpu=${CHOST%%-*}
151
152         cat > "${T}/meson.${CHOST}.${ABI}" <<-EOF
153         [binaries]
154         ar = $(_meson_env_array "$(tc-getAR)")
155         c = $(_meson_env_array "$(tc-getCC)")
156         cpp = $(_meson_env_array "$(tc-getCXX)")
157         fortran = $(_meson_env_array "$(tc-getFC)")
158         llvm-config = '$(tc-getPROG LLVM_CONFIG llvm-config)'
159         objc = $(_meson_env_array "$(tc-getPROG OBJC cc)")
160         objcpp = $(_meson_env_array "$(tc-getPROG OBJCXX c++)")
161         pkgconfig = '$(tc-getPKG_CONFIG)'
162         strip = $(_meson_env_array "$(tc-getSTRIP)")
163         windres = $(_meson_env_array "$(tc-getRC)")
164
165         [properties]
166         c_args = $(_meson_env_array "${CFLAGS} ${CPPFLAGS}")
167         c_link_args = $(_meson_env_array "${CFLAGS} ${LDFLAGS}")
168         cpp_args = $(_meson_env_array "${CXXFLAGS} ${CPPFLAGS}")
169         cpp_link_args = $(_meson_env_array "${CXXFLAGS} ${LDFLAGS}")
170         fortran_args = $(_meson_env_array "${FCFLAGS}")
171         fortran_link_args = $(_meson_env_array "${FCFLAGS} ${LDFLAGS}")
172         objc_args = $(_meson_env_array "${OBJCFLAGS} ${CPPFLAGS}")
173         objc_link_args = $(_meson_env_array "${OBJCFLAGS} ${LDFLAGS}")
174         objcpp_args = $(_meson_env_array "${OBJCXXFLAGS} ${CPPFLAGS}")
175         objcpp_link_args = $(_meson_env_array "${OBJCXXFLAGS} ${LDFLAGS}")
176
177         [host_machine]
178         system = '${system}'
179         cpu_family = '${cpu_family}'
180         cpu = '${cpu}'
181         endian = '$(tc-endian)'
182         EOF
183 }
184
185 # @FUNCTION: meson_use
186 # @USAGE: <USE flag> [option name]
187 # @DESCRIPTION:
188 # Given a USE flag and meson project option, outputs a string like:
189 #
190 #   -Doption=true
191 #   -Doption=false
192 #
193 # If the project option is unspecified, it defaults to the USE flag.
194 meson_use() {
195         usex "$1" "-D${2-$1}=true" "-D${2-$1}=false"
196 }
197
198 # @FUNCTION: meson_feature
199 # @USAGE: <USE flag> [option name]
200 # @DESCRIPTION:
201 # Given a USE flag and meson project option, outputs a string like:
202 #
203 #   -Doption=enabled
204 #   -Doption=disabled
205 #
206 # If the project option is unspecified, it defaults to the USE flag.
207 meson_feature() {
208         usex "$1" "-D${2-$1}=enabled" "-D${2-$1}=disabled"
209 }
210
211 # @FUNCTION: meson_src_configure
212 # @USAGE: [extra meson arguments]
213 # @DESCRIPTION:
214 # This is the meson_src_configure function.
215 meson_src_configure() {
216         debug-print-function ${FUNCNAME} "$@"
217
218         # Common args
219         local mesonargs=(
220                 --buildtype plain
221                 --libdir "$(get_libdir)"
222                 --localstatedir "${EPREFIX}/var/lib"
223                 --prefix "${EPREFIX}/usr"
224                 --sysconfdir "${EPREFIX}/etc"
225                 --wrap-mode nodownload
226                 )
227
228         if tc-is-cross-compiler || [[ ${ABI} != ${DEFAULT_ABI-${ABI}} ]]; then
229                 _meson_create_cross_file || die "unable to write meson cross file"
230                 mesonargs+=( --cross-file "${T}/meson.${CHOST}.${ABI}" )
231         fi
232
233         # https://bugs.gentoo.org/625396
234         python_export_utf8_locale
235
236         # Append additional arguments from ebuild
237         mesonargs+=("${emesonargs[@]}")
238
239         BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"
240         set -- meson "${mesonargs[@]}" "$@" \
241                 "${EMESON_SOURCE:-${S}}" "${BUILD_DIR}"
242         echo "$@"
243         tc-env_build "$@" || die
244 }
245
246 # @FUNCTION: meson_src_compile
247 # @USAGE: [extra ninja arguments]
248 # @DESCRIPTION:
249 # This is the meson_src_compile function.
250 meson_src_compile() {
251         debug-print-function ${FUNCNAME} "$@"
252
253         eninja -C "${BUILD_DIR}" "$@"
254 }
255
256 # @FUNCTION: meson_src_test
257 # @USAGE: [extra meson test arguments]
258 # @DESCRIPTION:
259 # This is the meson_src_test function.
260 meson_src_test() {
261         debug-print-function ${FUNCNAME} "$@"
262
263         local mesontestargs=(
264                 -C "${BUILD_DIR}"
265         )
266         [[ -n ${NINJAOPTS} || -n ${MAKEOPTS} ]] &&
267                 mesontestargs+=(
268                         --num-processes "$(makeopts_jobs ${NINJAOPTS:-${MAKEOPTS}})"
269                 )
270
271         # Append additional arguments from ebuild
272         mesontestargs+=("${emesontestargs[@]}")
273
274         set -- meson test "${mesontestargs[@]}" "$@"
275         echo "$@" >&2
276         "$@" || die "tests failed"
277 }
278
279 # @FUNCTION: meson_src_install
280 # @USAGE: [extra ninja install arguments]
281 # @DESCRIPTION:
282 # This is the meson_src_install function.
283 meson_src_install() {
284         debug-print-function ${FUNCNAME} "$@"
285
286         DESTDIR="${D}" eninja -C "${BUILD_DIR}" install "$@"
287         einstalldocs
288 }
289
290 fi