1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: scons-utils.eclass
7 # @BLURB: helper functions to deal with SCons buildsystem
9 # This eclass provides a set of function to help developers sanely call
10 # dev-util/scons and pass parameters to it.
12 # Please note that SCons is more like a 'build system creation kit',
13 # and requires a lot of upstream customization to be used sanely.
14 # You will often need to request fixes upstream and/or patch the build
15 # system. In particular:
17 # 1. There are no 'standard' variables. To respect CC, CXX, CFLAGS,
18 # CXXFLAGS, CPPFLAGS, LDFLAGS, upstream needs to define appropriate
19 # variables explicitly. In some cases, upstreams respect envvars,
20 # in others you need to pass them as options.
22 # 2. SCons scrubs out environment by default and replaces it with some
23 # pre-defined values. To respect environment variables such as PATH,
24 # Upstreams need to explicitly get them from os.environ and copy them
25 # to the build environment.
29 # inherit scons-utils toolchain-funcs
36 # ENABLE_NLS=$(usex nls)
41 # escons "${MYSCONS[@]}"
45 # # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
46 # escons "${MYSCONS[@]}" DESTDIR="${D}" install
50 # -- public variables --
52 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
55 # The minimal version of SCons required for the build to work.
57 # @VARIABLE: myesconsargs
60 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
62 # List of package-specific options to pass to all SCons calls. Supposed to be
63 # set in src_configure().
65 # @ECLASS-VARIABLE: SCONSOPTS
68 # The default set of options to pass to scons. Similar to MAKEOPTS,
69 # supposed to be set in make.conf. If unset, escons() will use cleaned
70 # up MAKEOPTS instead.
72 # @ECLASS-VARIABLE: EXTRA_ESCONS
75 # The additional parameters to pass to SCons whenever escons() is used.
76 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
79 # @ECLASS-VARIABLE: USE_SCONS_TRUE
81 # DEPRECATED: use usex instead
83 # The default value for truth in scons-use() (1 by default).
84 : ${USE_SCONS_TRUE:=1}
86 # @ECLASS-VARIABLE: USE_SCONS_FALSE
88 # DEPRECATED: use usex instead
90 # The default value for false in scons-use() (0 by default).
91 : ${USE_SCONS_FALSE:=0}
93 # -- EAPI support check --
97 *) die "EAPI ${EAPI} unsupported."
100 inherit multiprocessing
102 # -- ebuild variables setup --
104 if [[ -n ${SCONS_MIN_VERSION} ]]; then
105 BDEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
107 BDEPEND="dev-util/scons"
110 if [[ ${EAPI:-0} == [0123456] ]]; then
115 # -- public functions --
118 # @USAGE: [<args>...]
120 # Call scons, passing the supplied arguments. Like emake, this function
121 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
125 debug-print-function ${FUNCNAME} "${@}"
127 # Use myesconsargs in EAPI 5 and older
128 if [[ ${EAPI} == [012345] ]]; then
129 set -- "${myesconsargs[@]}" "${@}"
132 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
133 if [[ ! ${SCONSOPTS+set} ]]; then
135 _scons_clean_makeopts
138 # pass ebuild environment variables through!
139 local -x GENTOO_SCONS_ENV_PASSTHROUGH=1
141 set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
146 if [[ ${ret} -ne 0 ]]; then
148 0|1|2|3) # nonfatal in EAPIs 0 through 3
150 4|5) # 100% fatal in 4 & 5
153 *) # respect nonfatal in 6 onwards
154 die -n "escons failed."
161 # @FUNCTION: _scons_clean_makeopts
163 # @USAGE: [makeflags] [...]
165 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
166 # an argument) of options not supported by SCons and make sure --jobs
167 # gets an argument. Output the resulting flag list (suitable
168 # for an assignment to SCONSOPTS).
169 _scons_clean_makeopts() {
170 local new_makeopts=()
172 debug-print-function ${FUNCNAME} "${@}"
174 if [[ ${#} -eq 0 ]]; then
175 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
178 # unquote if necessary
182 # empty MAKEOPTS give out empty SCONSOPTS
183 # thus, we do need to worry about the initial setup
184 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
185 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
186 debug-print "Cache hit: [${SCONSOPTS}]"
189 _SCONS_CACHE_MAKEOPTS=${*}
191 while [[ ${#} -gt 0 ]]; do
193 # clean, simple to check -- we like that
194 --jobs=*|--keep-going)
195 new_makeopts+=( ${1} )
197 # need to take a look at the next arg and guess
199 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
200 new_makeopts+=( ${1} ${2} )
203 # no value means no limit, let's pass a default instead
204 new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
207 # strip other long options
216 while [[ -n ${str} ]]; do
221 # -j needs to come last
223 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
227 new_optstr+="j $(( $(get_nproc) + 1 ))"
230 # otherwise, everything after -j is treated as an arg
239 if [[ -n ${new_optstr} ]]; then
240 new_makeopts+=( -${new_optstr} )
247 SCONSOPTS=${new_makeopts[*]}
248 _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
249 debug-print "New SCONSOPTS: [${SCONSOPTS}]"
252 # @FUNCTION: use_scons
253 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
255 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
257 # Output a SCons parameter with value depending on the USE flag state.
258 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
259 # <var-name>=<var-opt-false>.
261 # If <var-name> is omitted, <use-flag> will be used instead. However,
262 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
263 # prepended to the name (e.g. noflag).
265 # If <var-opt-true> and/or <var-opt-false> are omitted,
266 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
268 [[ ${EAPI} == [012345] ]] \
269 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
272 local varname=${2:-${flag/\!/no}}
273 local vartrue=${3:-${USE_SCONS_TRUE}}
274 local varfalse=${4:-${USE_SCONS_FALSE}}
276 debug-print-function ${FUNCNAME} "${@}"
278 if [[ ${#} -eq 0 ]]; then
279 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
280 die 'scons-use(): not enough arguments'
283 if use "${flag}"; then
284 echo "${varname}=${vartrue}"
286 echo "${varname}=${varfalse}"