1 # Copyright 2017-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: meson.eclass
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
11 # This eclass contains the default phase functions for packages which
12 # use the meson build system.
15 # Typical ebuild using meson.eclass:
26 # -Dqt4=$(usex qt4 true false)
27 # -Dthreads=$(usex threads true false)
28 # -Dtiff=$(usex tiff true false)
39 *) die "EAPI=${EAPI} is not supported" ;;
42 if [[ -z ${_MESON_ECLASS} ]]; then
44 inherit multiprocessing ninja-utils python-utils-r1 toolchain-funcs
48 EXPORT_FUNCTIONS src_configure src_compile src_test src_install
50 if [[ -z ${_MESON_ECLASS} ]]; then
53 MESON_DEPEND=">=dev-util/meson-0.51.2
54 >=dev-util/ninja-1.8.2"
56 if [[ ${EAPI:-0} == [6] ]]; then
57 DEPEND=${MESON_DEPEND}
59 BDEPEND=${MESON_DEPEND}
62 # @ECLASS-VARIABLE: BUILD_DIR
65 # Build directory, location where all generated files should be placed.
66 # If this isn't set, it defaults to ${WORKDIR}/${P}-build.
68 # @ECLASS-VARIABLE: EMESON_SOURCE
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}
75 # @VARIABLE: emesonargs
78 # Optional meson arguments as Bash array; this should be defined before
79 # calling meson_src_configure.
81 # @VARIABLE: emesontestargs
84 # Optional meson test arguments as Bash array; this should be defined before
85 # calling meson_src_test.
87 # @VARIABLE: MYMESONARGS
90 # User-controlled environment variable containing arguments to be passed to
91 # meson in meson_src_configure.
93 read -d '' __MESON_ARRAY_PARSER <<"EOF"
97 # See http://mesonbuild.com/Syntax.html#strings
99 escaped = str.replace("\\\\", "\\\\\\\\").replace("'", "\\\\'")
100 return "'{}'".format(escaped)
103 ", ".join([quote(x) for x in shlex.split(" ".join(sys.argv[1:]))])))
106 # @FUNCTION: _meson_env_array
109 # Parses the command line flags and converts them into an array suitable for
110 # use in a cross file.
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=𐤅
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=𐤅']
125 python -c "${__MESON_ARRAY_PARSER}" "$@"
128 # @FUNCTION: _meson_create_cross_file
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
136 # system roughly corresponds to uname -s (lowercase)
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 ;;
148 local cpu_family=$(tc-arch)
149 case ${cpu_family} in
150 amd64) cpu_family=x86_64 ;;
151 arm64) cpu_family=aarch64 ;;
154 # This may require adjustment based on CFLAGS
155 local cpu=${CHOST%%-*}
157 local needs_exe_wrapper=false
158 tc-is-cross-compiler && needs_exe_wrapper=true
160 cat > "${T}/meson.${CHOST}.${ABI}" <<-EOF
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)")
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}
188 cpu_family = '${cpu_family}'
190 endian = '$(tc-endian)'
194 # @FUNCTION: meson_use
195 # @USAGE: <USE flag> [option name]
197 # Given a USE flag and meson project option, outputs a string like:
202 # If the project option is unspecified, it defaults to the USE flag.
204 usex "$1" "-D${2-$1}=true" "-D${2-$1}=false"
207 # @FUNCTION: meson_feature
208 # @USAGE: <USE flag> [option name]
210 # Given a USE flag and meson project option, outputs a string like:
215 # If the project option is unspecified, it defaults to the USE flag.
217 usex "$1" "-D${2-$1}=enabled" "-D${2-$1}=disabled"
220 # @FUNCTION: meson_src_configure
221 # @USAGE: [extra meson arguments]
223 # This is the meson_src_configure function.
224 meson_src_configure() {
225 debug-print-function ${FUNCNAME} "$@"
230 --libdir "$(get_libdir)"
231 --localstatedir "${EPREFIX}/var/lib"
232 --prefix "${EPREFIX}/usr"
233 --sysconfdir "${EPREFIX}/etc"
234 --wrap-mode nodownload
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}" )
242 BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"
244 # Handle quoted whitespace
245 eval "local -a MYMESONARGS=( ${MYMESONARGS} )"
248 # Arguments from ebuild
251 # Arguments passed to this function
254 # Arguments from user
258 "${EMESON_SOURCE:-${S}}"
264 # https://bugs.gentoo.org/625396
265 python_export_utf8_locale
267 echo "${mesonargs[@]}" >&2
268 tc-env_build "${mesonargs[@]}" || die
271 # @FUNCTION: meson_src_compile
272 # @USAGE: [extra ninja arguments]
274 # This is the meson_src_compile function.
275 meson_src_compile() {
276 debug-print-function ${FUNCNAME} "$@"
278 eninja -C "${BUILD_DIR}" "$@"
281 # @FUNCTION: meson_src_test
282 # @USAGE: [extra meson test arguments]
284 # This is the meson_src_test function.
286 debug-print-function ${FUNCNAME} "$@"
288 local mesontestargs=(
291 [[ -n ${NINJAOPTS} || -n ${MAKEOPTS} ]] &&
293 --num-processes "$(makeopts_jobs ${NINJAOPTS:-${MAKEOPTS}})"
296 # Append additional arguments from ebuild
297 mesontestargs+=("${emesontestargs[@]}")
299 set -- meson test "${mesontestargs[@]}" "$@"
301 "$@" || die "tests failed"
304 # @FUNCTION: meson_src_install
305 # @USAGE: [extra ninja install arguments]
307 # This is the meson_src_install function.
308 meson_src_install() {
309 debug-print-function ${FUNCNAME} "$@"
311 DESTDIR="${D}" eninja -C "${BUILD_DIR}" install "$@"