1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: scons-utils.eclass
7 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
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 # As of dev-util/scons-3.0.1-r100, SCons supports Python 3. Since
14 # SCons* files in build systems are written as Python, all packages
15 # need to explicitly verify which versions of Python are supported
16 # and use appropriate Python suite eclass to select the implementation.
17 # The eclass needs to be inherited before scons-utils, and scons-utils
18 # will automatically take advantage of it. For more details, please see:
19 # https://wiki.gentoo.org/wiki/Project:Python/scons-utils_integration
21 # Please note that SCons is more like a 'build system creation kit',
22 # and requires a lot of upstream customization to be used sanely.
23 # You will often need to request fixes upstream and/or patch the build
24 # system. In particular:
26 # 1. There are no 'standard' variables. To respect CC, CXX, CFLAGS,
27 # CXXFLAGS, CPPFLAGS, LDFLAGS, upstream needs to define appropriate
28 # variables explicitly. In some cases, upstreams respect envvars,
29 # in others you need to pass them as options.
31 # 2. SCons scrubs out environment by default and replaces it with some
32 # pre-defined values. To respect environment variables such as PATH,
33 # Upstreams need to explicitly get them from os.environ and copy them
34 # to the build environment.
38 # PYTHON_COMPAT=( python2_7 )
39 # inherit python-any-r1 scons-utils toolchain-funcs
46 # ENABLE_NLS=$(usex nls)
51 # escons "${MYSCONS[@]}"
55 # # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
56 # escons "${MYSCONS[@]}" DESTDIR="${D}" install
60 # -- public variables --
62 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
65 # The minimal version of SCons required for the build to work.
67 # @VARIABLE: myesconsargs
70 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
72 # List of package-specific options to pass to all SCons calls. Supposed to be
73 # set in src_configure().
75 # @ECLASS-VARIABLE: SCONSOPTS
78 # The default set of options to pass to scons. Similar to MAKEOPTS,
79 # supposed to be set in make.conf. If unset, escons() will use cleaned
80 # up MAKEOPTS instead.
82 # @ECLASS-VARIABLE: EXTRA_ESCONS
85 # The additional parameters to pass to SCons whenever escons() is used.
86 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
89 # @ECLASS-VARIABLE: USE_SCONS_TRUE
91 # DEPRECATED: use usex instead
93 # The default value for truth in scons-use() (1 by default).
94 : ${USE_SCONS_TRUE:=1}
96 # @ECLASS-VARIABLE: USE_SCONS_FALSE
98 # DEPRECATED: use usex instead
100 # The default value for false in scons-use() (0 by default).
101 : ${USE_SCONS_FALSE:=0}
103 # -- EAPI support check --
107 *) die "EAPI ${EAPI} unsupported."
110 inherit multiprocessing
112 # -- ebuild variables setup --
114 if [[ -n ${SCONS_MIN_VERSION} ]]; then
115 SCONS_DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
117 SCONS_DEPEND="dev-util/scons"
120 if [[ ${_PYTHON_ANY_R1} ]]; then
121 # when using python-any-r1, use any-of dep API
122 BDEPEND="$(python_gen_any_dep "${SCONS_DEPEND}[\${PYTHON_USEDEP}]")"
124 scons-utils_python_check_deps() {
125 has_version "${SCONS_DEPEND}[${PYTHON_USEDEP}]"
127 python_check_deps() { scons-utils_python_check_deps; }
128 elif [[ ${_PYTHON_SINGLE_R1} ]]; then
129 # when using python-single-r1, use PYTHON_MULTI_USEDEP API
131 $(python_gen_cond_dep "${SCONS_DEPEND}[\${PYTHON_MULTI_USEDEP}]")
133 elif [[ ${EAPI:-0} == [0123456] ]]; then
134 # in older EAPIs, just force Python 2.7
135 BDEPEND="${SCONS_DEPEND}[python_targets_python2_7]"
136 elif [[ ${_PYTHON_R1} ]]; then
137 # when using python-r1, you need to depend on scons yourself
138 # (depending on whether you need any-r1 or full -r1 API)
139 # -- since this is a breaking API change, it applies to EAPI 7+ only
141 elif [[ ${EAPI:-0} != [0123456] ]]; then
142 # in EAPI 7+, require appropriate eclass use
143 eerror "Using scons-utils.eclass without any python-r1 suite eclass is not supported."
144 eerror "Please make sure to configure and inherit appropriate -r1 eclass."
145 eerror "For more information and examples, please see:"
146 eerror " https://wiki.gentoo.org/wiki/Project:Python/scons-utils_integration"
147 die "Invalid use of scons-utils.eclass"
150 if [[ ${EAPI:-0} == [0123456] ]]; then
155 # -- public functions --
158 # @USAGE: [<args>...]
160 # Call scons, passing the supplied arguments. Like emake, this function
161 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
165 debug-print-function ${FUNCNAME} "${@}"
167 if [[ ! ${EPYTHON} ]]; then
168 if [[ ${EAPI:-0} != [0123456] ]]; then
169 eerror "EPYTHON is unset while calling escons. This most likely means that"
170 eerror "the ebuild did not call the appropriate eclass function before calling scons."
171 if [[ ${_PYTHON_ANY_R1} ]]; then
172 eerror "Please ensure that python-any-r1_pkg_setup is called in pkg_setup()."
173 elif [[ ${_PYTHON_SINGLE_R1} ]]; then
174 eerror "Please ensure that python-single-r1_pkg_setup is called in pkg_setup()."
176 eerror "Please ensure that python_setup is called before escons, or that escons"
177 eerror "is used within python_foreach_impl as appropriate."
179 die "EPYTHON unset in escons"
181 local -x EPYTHON=python2.7
185 # Use myesconsargs in EAPI 5 and older
186 if [[ ${EAPI} == [012345] ]]; then
187 set -- "${myesconsargs[@]}" "${@}"
190 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
191 if [[ ! ${SCONSOPTS+set} ]]; then
193 _scons_clean_makeopts
196 # pass ebuild environment variables through!
197 local -x GENTOO_SCONS_ENV_PASSTHROUGH=1
199 set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
204 if [[ ${ret} -ne 0 ]]; then
206 0|1|2|3) # nonfatal in EAPIs 0 through 3
208 4|5) # 100% fatal in 4 & 5
211 *) # respect nonfatal in 6 onwards
212 die -n "escons failed."
219 # @FUNCTION: _scons_clean_makeopts
221 # @USAGE: [makeflags] [...]
223 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
224 # an argument) of options not supported by SCons and make sure --jobs
225 # gets an argument. Output the resulting flag list (suitable
226 # for an assignment to SCONSOPTS).
227 _scons_clean_makeopts() {
228 local new_makeopts=()
230 debug-print-function ${FUNCNAME} "${@}"
232 if [[ ${#} -eq 0 ]]; then
233 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
236 # unquote if necessary
240 # empty MAKEOPTS give out empty SCONSOPTS
241 # thus, we do need to worry about the initial setup
242 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
243 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
244 debug-print "Cache hit: [${SCONSOPTS}]"
247 _SCONS_CACHE_MAKEOPTS=${*}
249 while [[ ${#} -gt 0 ]]; do
251 # clean, simple to check -- we like that
252 --jobs=*|--keep-going)
253 new_makeopts+=( ${1} )
255 # need to take a look at the next arg and guess
257 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
258 new_makeopts+=( ${1} ${2} )
261 # no value means no limit, let's pass a default instead
262 new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
265 # strip other long options
274 while [[ -n ${str} ]]; do
279 # -j needs to come last
281 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
285 new_optstr+="j $(( $(get_nproc) + 1 ))"
288 # otherwise, everything after -j is treated as an arg
297 if [[ -n ${new_optstr} ]]; then
298 new_makeopts+=( -${new_optstr} )
305 SCONSOPTS=${new_makeopts[*]}
306 _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
307 debug-print "New SCONSOPTS: [${SCONSOPTS}]"
310 # @FUNCTION: use_scons
311 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
313 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
315 # Output a SCons parameter with value depending on the USE flag state.
316 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
317 # <var-name>=<var-opt-false>.
319 # If <var-name> is omitted, <use-flag> will be used instead. However,
320 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
321 # prepended to the name (e.g. noflag).
323 # If <var-opt-true> and/or <var-opt-false> are omitted,
324 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
326 [[ ${EAPI} == [012345] ]] \
327 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
330 local varname=${2:-${flag/\!/no}}
331 local vartrue=${3:-${USE_SCONS_TRUE}}
332 local varfalse=${4:-${USE_SCONS_FALSE}}
334 debug-print-function ${FUNCNAME} "${@}"
336 if [[ ${#} -eq 0 ]]; then
337 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
338 die 'scons-use(): not enough arguments'
341 if use "${flag}"; then
342 echo "${varname}=${vartrue}"
344 echo "${varname}=${varfalse}"