1 # Copyright 2017-2019 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.
88 read -d '' __MESON_ARRAY_PARSER <<"EOF"
92 # See http://mesonbuild.com/Syntax.html#strings
94 escaped = str.replace("\\\\", "\\\\\\\\").replace("'", "\\\\'")
95 return "'{}'".format(escaped)
98 ", ".join([quote(x) for x in shlex.split(" ".join(sys.argv[1:]))])))
101 # @FUNCTION: _meson_env_array
104 # Parses the command line flags and converts them into an array suitable for
105 # use in a cross file.
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=𐤅
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=𐤅']
120 python -c "${__MESON_ARRAY_PARSER}" "$@"
123 # @FUNCTION: _meson_create_cross_file
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
131 # system roughly corresponds to uname -s (lowercase)
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 ;;
143 local cpu_family=$(tc-arch)
144 case ${cpu_family} in
145 amd64) cpu_family=x86_64 ;;
146 arm64) cpu_family=aarch64 ;;
149 # This may require adjustment based on CFLAGS
150 local cpu=${CHOST%%-*}
152 cat > "${T}/meson.${CHOST}.${ABI}" <<-EOF
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)")
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}")
179 cpu_family = '${cpu_family}'
181 endian = '$(tc-endian)'
185 # @FUNCTION: meson_use
186 # @USAGE: <USE flag> [option name]
188 # Given a USE flag and meson project option, outputs a string like:
193 # If the project option is unspecified, it defaults to the USE flag.
195 usex "$1" "-D${2-$1}=true" "-D${2-$1}=false"
198 # @FUNCTION: meson_feature
199 # @USAGE: <USE flag> [option name]
201 # Given a USE flag and meson project option, outputs a string like:
206 # If the project option is unspecified, it defaults to the USE flag.
208 usex "$1" "-D${2-$1}=enabled" "-D${2-$1}=disabled"
211 # @FUNCTION: meson_src_configure
212 # @USAGE: [extra meson arguments]
214 # This is the meson_src_configure function.
215 meson_src_configure() {
216 debug-print-function ${FUNCNAME} "$@"
221 --libdir "$(get_libdir)"
222 --localstatedir "${EPREFIX}/var/lib"
223 --prefix "${EPREFIX}/usr"
224 --sysconfdir "${EPREFIX}/etc"
225 --wrap-mode nodownload
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}" )
233 # https://bugs.gentoo.org/625396
234 python_export_utf8_locale
236 # Append additional arguments from ebuild
237 mesonargs+=("${emesonargs[@]}")
239 BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"
240 set -- meson "${mesonargs[@]}" "$@" \
241 "${EMESON_SOURCE:-${S}}" "${BUILD_DIR}"
243 tc-env_build "$@" || die
246 # @FUNCTION: meson_src_compile
247 # @USAGE: [extra ninja arguments]
249 # This is the meson_src_compile function.
250 meson_src_compile() {
251 debug-print-function ${FUNCNAME} "$@"
253 eninja -C "${BUILD_DIR}" "$@"
256 # @FUNCTION: meson_src_test
257 # @USAGE: [extra meson test arguments]
259 # This is the meson_src_test function.
261 debug-print-function ${FUNCNAME} "$@"
263 local mesontestargs=(
266 [[ -n ${NINJAOPTS} || -n ${MAKEOPTS} ]] &&
268 --num-processes "$(makeopts_jobs ${NINJAOPTS:-${MAKEOPTS}})"
271 # Append additional arguments from ebuild
272 mesontestargs+=("${emesontestargs[@]}")
274 set -- meson test "${mesontestargs[@]}" "$@"
276 "$@" || die "tests failed"
279 # @FUNCTION: meson_src_install
280 # @USAGE: [extra ninja install arguments]
282 # This is the meson_src_install function.
283 meson_src_install() {
284 debug-print-function ${FUNCNAME} "$@"
286 DESTDIR="${D}" eninja -C "${BUILD_DIR}" install "$@"