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 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
63 # List of package-specific options to pass to all SCons calls. Supposed to be
64 # set in src_configure().
66 # @ECLASS-VARIABLE: SCONSOPTS
69 # The default set of options to pass to scons. Similar to MAKEOPTS,
70 # supposed to be set in make.conf. If unset, escons() will use cleaned
71 # up MAKEOPTS instead.
73 # @ECLASS-VARIABLE: EXTRA_ESCONS
76 # The additional parameters to pass to SCons whenever escons() is used.
77 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
80 # @ECLASS-VARIABLE: USE_SCONS_TRUE
82 # DEPRECATED: use usex instead
84 # The default value for truth in scons-use() (1 by default).
85 : ${USE_SCONS_TRUE:=1}
87 # @ECLASS-VARIABLE: USE_SCONS_FALSE
89 # DEPRECATED: use usex instead
91 # The default value for false in scons-use() (0 by default).
92 : ${USE_SCONS_FALSE:=0}
94 # -- EAPI support check --
98 *) die "EAPI ${EAPI} unsupported."
101 # -- ebuild variables setup --
103 if [[ -n ${SCONS_MIN_VERSION} ]]; then
104 DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
106 DEPEND="dev-util/scons"
109 # -- public functions --
112 # @USAGE: [<args>...]
114 # Call scons, passing the supplied arguments. Like emake, this function
115 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
119 debug-print-function ${FUNCNAME} "${@}"
121 # Use myesconsargs in EAPI 5 and older
122 if [[ ${EAPI} == [012345] ]]; then
123 set -- "${myesconsargs[@]}" "${@}"
126 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
127 if [[ ! ${SCONSOPTS+set} ]]; then
129 _scons_clean_makeopts
132 set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
137 if [[ ${ret} -ne 0 ]]; then
139 0|1|2|3) # nonfatal in EAPIs 0 through 3
141 4|5) # 100% fatal in 4 & 5
144 *) # respect nonfatal in 6 onwards
145 die -n "escons failed."
152 # @FUNCTION: _scons_clean_makeopts
154 # @USAGE: [makeflags] [...]
156 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
157 # an argument) of options not supported by SCons and make sure --jobs
158 # gets an argument. Output the resulting flag list (suitable
159 # for an assignment to SCONSOPTS).
160 _scons_clean_makeopts() {
163 debug-print-function ${FUNCNAME} "${@}"
165 if [[ ${#} -eq 0 ]]; then
166 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
169 # unquote if necessary
173 # empty MAKEOPTS give out empty SCONSOPTS
174 # thus, we do need to worry about the initial setup
175 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
176 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
177 debug-print "Cache hit: [${SCONSOPTS}]"
180 export _SCONS_CACHE_MAKEOPTS=${*}
182 while [[ ${#} -gt 0 ]]; do
184 # clean, simple to check -- we like that
185 --jobs=*|--keep-going)
186 new_makeopts=${new_makeopts+${new_makeopts} }${1}
188 # need to take a look at the next arg and guess
190 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
191 new_makeopts="${new_makeopts+${new_makeopts} }${1} ${2}"
194 # no value means no limit, let's pass a random int
195 new_makeopts=${new_makeopts+${new_makeopts} }${1}=5
198 # strip other long options
207 while [[ -n ${str} ]]; do
210 new_optstr=${new_optstr}k
212 # -j needs to come last
214 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
215 new_optstr="${new_optstr}j ${2}"
218 new_optstr="${new_optstr}j 5"
221 # otherwise, everything after -j is treated as an arg
223 new_optstr=${new_optstr}${str}
230 if [[ -n ${new_optstr} ]]; then
231 new_makeopts=${new_makeopts+${new_makeopts} }-${new_optstr}
238 set -- ${new_makeopts}
239 export _SCONS_CACHE_SCONSOPTS=${*}
240 debug-print "New 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}"