1 # Copyright 2017 Gentoo Foundation
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 # @BLURB: common ebuild functions for meson-based packages
10 # This eclass contains the default phase functions for packages which
11 # use the meson build system.
14 # Typical ebuild using meson.eclass:
25 # -Dqt4=$(usex qt4 true false)
26 # -Dthreads=$(usex threads true false)
27 # -Dtiff=$(usex tiff true false)
38 *) die "EAPI=${EAPI} is not supported" ;;
41 if [[ ${__MESON_AUTO_DEPEND+set} == "set" ]] ; then
42 # See if we were included already, but someone changed the value
43 # of MESON_AUTO_DEPEND on us. We could reload the entire
44 # eclass at that point, but that adds overhead, and it's trivial
45 # to re-order inherit in eclasses/ebuilds instead. #409611
46 if [[ ${__MESON_AUTO_DEPEND} != ${MESON_AUTO_DEPEND} ]] ; then
47 die "MESON_AUTO_DEPEND changed value between inherits; please inherit meson.eclass first! ${__MESON_AUTO_DEPEND} -> ${MESON_AUTO_DEPEND}"
51 if [[ -z ${_MESON_ECLASS} ]]; then
53 inherit ninja-utils python-utils-r1 toolchain-funcs
57 EXPORT_FUNCTIONS src_configure src_compile src_test src_install
59 if [[ -z ${_MESON_ECLASS} ]]; then
62 MESON_DEPEND=">=dev-util/meson-0.40.0
63 >=dev-util/ninja-1.7.2"
65 # @ECLASS-VARIABLE: MESON_AUTO_DEPEND
67 # Set to 'no' to disable automatically adding to DEPEND. This lets
68 # ebuilds form conditional depends by using ${MESON_DEPEND} in
69 # their own DEPEND string.
70 : ${MESON_AUTO_DEPEND:=yes}
71 if [[ ${MESON_AUTO_DEPEND} != "no" ]] ; then
72 DEPEND=${MESON_DEPEND}
74 __MESON_AUTO_DEPEND=${MESON_AUTO_DEPEND} # See top of eclass
76 # @ECLASS-VARIABLE: BUILD_DIR
79 # Build directory, location where all generated files should be placed.
80 # If this isn't set, it defaults to ${WORKDIR}/${P}-build.
82 # @ECLASS-VARIABLE: EMESON_SOURCE
85 # The location of the source files for the project; this is the source
86 # directory to pass to meson.
87 # If this isn't set, it defaults to ${S}
89 # @VARIABLE: emesonargs
92 # Optional meson arguments as Bash array; this should be defined before
93 # calling meson_src_configure.
96 read -d '' __MESON_ARRAY_PARSER <<"EOF"
100 # See http://mesonbuild.com/Syntax.html#strings
102 escaped = str.replace("\\\\", "\\\\\\\\").replace("'", "\\\\'")
103 return "'{}'".format(escaped)
106 ", ".join([quote(x) for x in shlex.split(" ".join(sys.argv[1:]))])))
109 # @FUNCTION: _meson_env_array
112 # Parses the command line flags and converts them into an array suitable for
113 # use in a cross file.
115 # Input: --single-quote=\' --double-quote=\" --dollar=\$ --backtick=\`
116 # --backslash=\\ --full-word-double="Hello World"
117 # --full-word-single='Hello World'
118 # --full-word-backslash=Hello\ World
119 # --simple --unicode-8=© --unicode-16=𐐷 --unicode-32=𐤅
121 # Output: ['--single-quote=\'', '--double-quote="', '--dollar=$',
122 # '--backtick=`', '--backslash=\\', '--full-word-double=Hello World',
123 # '--full-word-single=Hello World',
124 # '--full-word-backslash=Hello World', '--simple', '--unicode-8=©',
125 # '--unicode-16=𐐷', '--unicode-32=𐤅']
128 python -c "${__MESON_ARRAY_PARSER}" "$@"
131 # @FUNCTION: _meson_create_cross_file
134 # Creates a cross file. meson uses this to define settings for
135 # cross-compilers. This function is called from meson_src_configure.
136 _meson_create_cross_file() {
137 # Reference: http://mesonbuild.com/Cross-compilation.html
139 # system roughly corresponds to uname -s (lowercase)
142 *-aix*) system=aix ;;
143 *-cygwin*) system=cygwin ;;
144 *-darwin*) system=darwin ;;
145 *-freebsd*) system=freebsd ;;
146 *-linux*) system=linux ;;
147 mingw*|*-mingw*) system=windows ;;
148 *-solaris*) system=sunos ;;
151 local cpu_family=$(tc-arch)
152 case ${cpu_family} in
153 amd64) cpu_family=x86_64 ;;
154 arm64) cpu_family=aarch64 ;;
157 # This may require adjustment based on CFLAGS
158 local cpu=${CHOST%%-*}
160 cat > "${T}/meson.${CHOST}" <<-EOF
165 pkgconfig = '$(tc-getPKG_CONFIG)'
166 strip = '$(tc-getSTRIP)'
169 c_args = $(_meson_env_array "${CFLAGS}")
170 c_link_args = $(_meson_env_array "${LDFLAGS}")
171 cpp_args = $(_meson_env_array "${CXXFLAGS}")
172 cpp_link_args = $(_meson_env_array "${LDFLAGS}")
173 fortran_args = $(_meson_env_array "${FCFLAGS}")
174 objc_args = $(_meson_env_array "${OBJCFLAGS}")
175 objcpp_args = $(_meson_env_array "${OBJCXXFLAGS}")
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_src_configure
200 # This is the meson_src_configure function.
201 meson_src_configure() {
202 debug-print-function ${FUNCNAME} "$@"
207 --libdir "$(get_libdir)"
208 --localstatedir "${EPREFIX}/var/lib"
209 --prefix "${EPREFIX}/usr"
210 --sysconfdir "${EPREFIX}/etc"
211 --wrap-mode nodownload
214 if tc-is-cross-compiler; then
215 _meson_create_cross_file || die "unable to write meson cross file"
216 mesonargs+=( --cross-file "${T}/meson.${CHOST}" )
219 # https://bugs.gentoo.org/625396
220 python_export_utf8_locale
222 # Append additional arguments from ebuild
223 mesonargs+=("${emesonargs[@]}")
225 BUILD_DIR="${BUILD_DIR:-${WORKDIR}/${P}-build}"
226 set -- meson "${mesonargs[@]}" "$@" \
227 "${EMESON_SOURCE:-${S}}" "${BUILD_DIR}"
229 tc-env_build "$@" || die
232 # @FUNCTION: meson_src_compile
234 # This is the meson_src_compile function.
235 meson_src_compile() {
236 debug-print-function ${FUNCNAME} "$@"
238 eninja -C "${BUILD_DIR}"
241 # @FUNCTION: meson_src_test
243 # This is the meson_src_test function.
245 debug-print-function ${FUNCNAME} "$@"
247 eninja -C "${BUILD_DIR}" test
250 # @FUNCTION: meson_src_install
252 # This is the meson_src_install function.
253 meson_src_install() {
254 debug-print-function ${FUNCNAME} "$@"
256 DESTDIR="${D}" eninja -C "${BUILD_DIR}" install