1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: scons-utils.eclass
8 # @BLURB: helper functions to deal with SCons buildsystem
10 # This eclass provides a set of function to help developers sanely call
11 # dev-util/scons and pass parameters to it.
13 # Please note that SCons is more like a 'build system creation kit',
14 # and requires a lot of upstream customization to be used sanely.
15 # You will often need to request fixes upstream and/or patch the build
16 # system. In particular:
18 # 1. There are no 'standard' variables. To respect CC, CXX, CFLAGS,
19 # CXXFLAGS, CPPFLAGS, LDFLAGS, upstream needs to define appropriate
20 # variables explicitly. In some cases, upstreams respect envvars,
21 # in others you need to pass them as options.
23 # 2. SCons scrubs out environment by default and replaces it with some
24 # pre-defined values. To respect environment variables such as PATH,
25 # Upstreams need to explicitly get them from os.environ and copy them
26 # to the build environment.
30 # inherit scons-utils toolchain-funcs
37 # ENABLE_NLS=$(usex nls)
42 # escons "${MYSCONS[@]}"
46 # # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
47 # escons "${MYSCONS[@]}" DESTDIR="${D}" install
51 # -- public variables --
53 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
56 # The minimal version of SCons required for the build to work.
58 # @VARIABLE: myesconsargs
61 # List of package-specific options to pass to all SCons calls. Supposed to be
62 # set in src_configure().
64 # @ECLASS-VARIABLE: SCONSOPTS
67 # The default set of options to pass to scons. Similar to MAKEOPTS,
68 # supposed to be set in make.conf. If unset, escons() will use cleaned
69 # up MAKEOPTS instead.
71 # @ECLASS-VARIABLE: EXTRA_ESCONS
74 # The additional parameters to pass to SCons whenever escons() is used.
75 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
78 # @ECLASS-VARIABLE: USE_SCONS_TRUE
80 # The default value for truth in scons-use() (1 by default).
81 : ${USE_SCONS_TRUE:=1}
83 # @ECLASS-VARIABLE: USE_SCONS_FALSE
85 # The default value for false in scons-use() (0 by default).
86 : ${USE_SCONS_FALSE:=0}
88 # -- EAPI support check --
92 *) die "EAPI ${EAPI} unsupported."
95 # -- ebuild variables setup --
97 if [[ -n ${SCONS_MIN_VERSION} ]]; then
98 DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
100 DEPEND="dev-util/scons"
103 # -- public functions --
106 # @USAGE: [scons-arg] ...
108 # Call scons, passing the supplied arguments, ${myesconsargs[@]},
109 # filtered ${MAKEOPTS}, ${EXTRA_ESCONS}. Similar to emake. Like emake,
110 # this function does die on failure in EAPI 4.
114 debug-print-function ${FUNCNAME} "${@}"
116 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
117 set -- scons ${SCONSOPTS-$(scons_clean_makeopts)} ${EXTRA_ESCONS} \
118 "${myesconsargs[@]}" "${@}"
123 if [[ ${ret} -ne 0 ]]; then
124 [[ ${EAPI:-0} != [0123] ]] && die "escons failed."
129 # @FUNCTION: scons_clean_makeopts
130 # @USAGE: [makeflags] [...]
132 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
133 # an argument) of options not supported by SCons and make sure --jobs
134 # gets an argument. Output the resulting flag list (suitable
135 # for an assignment to SCONSOPTS).
136 scons_clean_makeopts() {
139 debug-print-function ${FUNCNAME} "${@}"
141 if [[ ${#} -eq 0 ]]; then
142 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
145 # unquote if necessary
149 # empty MAKEOPTS give out empty SCONSOPTS
150 # thus, we do need to worry about the initial setup
151 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
152 set -- ${_SCONS_CACHE_SCONSOPTS}
153 debug-print "Cache hit: [${*}]"
157 export _SCONS_CACHE_MAKEOPTS=${*}
159 while [[ ${#} -gt 0 ]]; do
161 # clean, simple to check -- we like that
162 --jobs=*|--keep-going)
163 new_makeopts=${new_makeopts+${new_makeopts} }${1}
165 # need to take a look at the next arg and guess
167 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
168 new_makeopts="${new_makeopts+${new_makeopts} }${1} ${2}"
171 # no value means no limit, let's pass a random int
172 new_makeopts=${new_makeopts+${new_makeopts} }${1}=5
175 # strip other long options
184 while [[ -n ${str} ]]; do
187 new_optstr=${new_optstr}k
189 # -j needs to come last
191 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
192 new_optstr="${new_optstr}j ${2}"
195 new_optstr="${new_optstr}j 5"
198 # otherwise, everything after -j is treated as an arg
200 new_optstr=${new_optstr}${str}
207 if [[ -n ${new_optstr} ]]; then
208 new_makeopts=${new_makeopts+${new_makeopts} }-${new_optstr}
215 set -- ${new_makeopts}
216 export _SCONS_CACHE_SCONSOPTS=${*}
217 debug-print "New SCONSOPTS: [${*}]"
221 # @FUNCTION: use_scons
222 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
224 # Output a SCons parameter with value depending on the USE flag state.
225 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
226 # <var-name>=<var-opt-false>.
228 # If <var-name> is omitted, <use-flag> will be used instead. However,
229 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
230 # prepended to the name (e.g. noflag).
232 # If <var-opt-true> and/or <var-opt-false> are omitted,
233 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
236 local varname=${2:-${flag/\!/no}}
237 local vartrue=${3:-${USE_SCONS_TRUE}}
238 local varfalse=${4:-${USE_SCONS_FALSE}}
240 debug-print-function ${FUNCNAME} "${@}"
242 if [[ ${#} -eq 0 ]]; then
243 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
244 die 'scons-use(): not enough arguments'
247 if use "${flag}"; then
248 echo "${varname}=${vartrue}"
250 echo "${varname}=${varfalse}"