1 # Copyright 1999-2015 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 DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
107 DEPEND="dev-util/scons"
110 # -- public functions --
113 # @USAGE: [<args>...]
115 # Call scons, passing the supplied arguments. Like emake, this function
116 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
120 debug-print-function ${FUNCNAME} "${@}"
122 # Use myesconsargs in EAPI 5 and older
123 if [[ ${EAPI} == [012345] ]]; then
124 set -- "${myesconsargs[@]}" "${@}"
127 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
128 if [[ ! ${SCONSOPTS+set} ]]; then
130 _scons_clean_makeopts
133 set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
138 if [[ ${ret} -ne 0 ]]; then
140 0|1|2|3) # nonfatal in EAPIs 0 through 3
142 4|5) # 100% fatal in 4 & 5
145 *) # respect nonfatal in 6 onwards
146 die -n "escons failed."
153 # @FUNCTION: _scons_clean_makeopts
155 # @USAGE: [makeflags] [...]
157 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
158 # an argument) of options not supported by SCons and make sure --jobs
159 # gets an argument. Output the resulting flag list (suitable
160 # for an assignment to SCONSOPTS).
161 _scons_clean_makeopts() {
162 local new_makeopts=()
164 debug-print-function ${FUNCNAME} "${@}"
166 if [[ ${#} -eq 0 ]]; then
167 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
170 # unquote if necessary
174 # empty MAKEOPTS give out empty SCONSOPTS
175 # thus, we do need to worry about the initial setup
176 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
177 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
178 debug-print "Cache hit: [${SCONSOPTS}]"
181 _SCONS_CACHE_MAKEOPTS=${*}
183 while [[ ${#} -gt 0 ]]; do
185 # clean, simple to check -- we like that
186 --jobs=*|--keep-going)
187 new_makeopts+=( ${1} )
189 # need to take a look at the next arg and guess
191 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
192 new_makeopts+=( ${1} ${2} )
195 # no value means no limit, let's pass a default instead
196 new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
199 # strip other long options
208 while [[ -n ${str} ]]; do
213 # -j needs to come last
215 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
219 new_optstr+="j $(( $(get_nproc) + 1 ))"
222 # otherwise, everything after -j is treated as an arg
231 if [[ -n ${new_optstr} ]]; then
232 new_makeopts+=( -${new_optstr} )
239 SCONSOPTS=${new_makeopts[*]}
240 _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
241 debug-print "New SCONSOPTS: [${SCONSOPTS}]"
244 # @FUNCTION: use_scons
245 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
247 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
249 # Output a SCons parameter with value depending on the USE flag state.
250 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
251 # <var-name>=<var-opt-false>.
253 # If <var-name> is omitted, <use-flag> will be used instead. However,
254 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
255 # prepended to the name (e.g. noflag).
257 # If <var-opt-true> and/or <var-opt-false> are omitted,
258 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
260 [[ ${EAPI} == [012345] ]] \
261 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
264 local varname=${2:-${flag/\!/no}}
265 local vartrue=${3:-${USE_SCONS_TRUE}}
266 local varfalse=${4:-${USE_SCONS_FALSE}}
268 debug-print-function ${FUNCNAME} "${@}"
270 if [[ ${#} -eq 0 ]]; then
271 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
272 die 'scons-use(): not enough arguments'
275 if use "${flag}"; then
276 echo "${varname}=${vartrue}"
278 echo "${varname}=${varfalse}"