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 # As of dev-util/scons-3.0.1-r100, SCons supports Python 3. Since
13 # SCons* files in build systems are written as Python, all packages
14 # need to explicitly verify which versions of Python are supported
15 # and use appropriate Python suite eclass to select the implementation.
16 # The eclass needs to be inherited before scons-utils, and scons-utils
17 # will automatically take advantage of it. For more details, please see:
18 # https://wiki.gentoo.org/wiki/Project:Python/scons-utils_integration
20 # Please note that SCons is more like a 'build system creation kit',
21 # and requires a lot of upstream customization to be used sanely.
22 # You will often need to request fixes upstream and/or patch the build
23 # system. In particular:
25 # 1. There are no 'standard' variables. To respect CC, CXX, CFLAGS,
26 # CXXFLAGS, CPPFLAGS, LDFLAGS, upstream needs to define appropriate
27 # variables explicitly. In some cases, upstreams respect envvars,
28 # in others you need to pass them as options.
30 # 2. SCons scrubs out environment by default and replaces it with some
31 # pre-defined values. To respect environment variables such as PATH,
32 # Upstreams need to explicitly get them from os.environ and copy them
33 # to the build environment.
37 # PYTHON_COMPAT=( python2_7 )
38 # inherit python-any-r1 scons-utils toolchain-funcs
45 # ENABLE_NLS=$(usex nls)
50 # escons "${MYSCONS[@]}"
54 # # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
55 # escons "${MYSCONS[@]}" DESTDIR="${D}" install
59 # -- public variables --
61 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
64 # The minimal version of SCons required for the build to work.
66 # @VARIABLE: myesconsargs
69 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
71 # List of package-specific options to pass to all SCons calls. Supposed to be
72 # set in src_configure().
74 # @ECLASS-VARIABLE: SCONSOPTS
77 # The default set of options to pass to scons. Similar to MAKEOPTS,
78 # supposed to be set in make.conf. If unset, escons() will use cleaned
79 # up MAKEOPTS instead.
81 # @ECLASS-VARIABLE: EXTRA_ESCONS
84 # The additional parameters to pass to SCons whenever escons() is used.
85 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
88 # @ECLASS-VARIABLE: USE_SCONS_TRUE
90 # DEPRECATED: use usex instead
92 # The default value for truth in scons-use() (1 by default).
93 : ${USE_SCONS_TRUE:=1}
95 # @ECLASS-VARIABLE: USE_SCONS_FALSE
97 # DEPRECATED: use usex instead
99 # The default value for false in scons-use() (0 by default).
100 : ${USE_SCONS_FALSE:=0}
102 # -- EAPI support check --
106 *) die "EAPI ${EAPI} unsupported."
109 inherit multiprocessing
111 # -- ebuild variables setup --
113 if [[ -n ${SCONS_MIN_VERSION} ]]; then
114 SCONS_DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
116 SCONS_DEPEND="dev-util/scons"
119 if [[ ${_PYTHON_ANY_R1} ]]; then
120 # when using python-any-r1, use any-of dep API
121 BDEPEND="$(python_gen_any_dep "${SCONS_DEPEND}[\${PYTHON_USEDEP}]")"
123 scons-utils_python_check_deps() {
124 has_version "${SCONS_DEPEND}[${PYTHON_USEDEP}]"
126 python_check_deps() { scons-utils_python_check_deps; }
127 elif [[ ${_PYTHON_SINGLE_R1} ]]; then
128 # when using python-single-r1, use plain PYTHON_USEDEP API
129 BDEPEND="${SCONS_DEPEND}[${PYTHON_USEDEP}]
131 elif [[ ${EAPI:-0} == [0123456] ]]; then
132 # in older EAPIs, just force Python 2.7
133 BDEPEND="${SCONS_DEPEND}[python_targets_python2_7]"
134 elif [[ ${_PYTHON_R1} ]]; then
135 # when using python-r1, you need to depend on scons yourself
136 # (depending on whether you need any-r1 or full -r1 API)
137 # -- since this is a breaking API change, it applies to EAPI 7+ only
139 elif [[ ${EAPI:-0} != [0123456] ]]; then
140 # in EAPI 7+, require appropriate eclass use
141 eerror "Using scons-utils.eclass without any python-r1 suite eclass is not supported."
142 eerror "Please make sure to configure and inherit appropriate -r1 eclass."
143 eerror "For more information and examples, please see:"
144 eerror " https://wiki.gentoo.org/wiki/Project:Python/scons-utils_integration"
145 die "Invalid use of scons-utils.eclass"
148 if [[ ${EAPI:-0} == [0123456] ]]; then
153 # -- public functions --
156 # @USAGE: [<args>...]
158 # Call scons, passing the supplied arguments. Like emake, this function
159 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
163 debug-print-function ${FUNCNAME} "${@}"
165 if [[ ! ${EPYTHON} ]]; then
166 if [[ ${EAPI:-0} != [0123456] ]]; then
167 eerror "EPYTHON is unset while calling escons. This most likely means that"
168 eerror "the ebuild did not call the appropriate eclass function before calling scons."
169 if [[ ${_PYTHON_ANY_R1} ]]; then
170 eerror "Please ensure that python-any-r1_pkg_setup is called in pkg_setup()."
171 elif [[ ${_PYTHON_SINGLE_R1} ]]; then
172 eerror "Please ensure that python-single-r1_pkg_setup is called in pkg_setup()."
174 eerror "Please ensure that python_setup is called before escons, or that escons"
175 eerror "is used within python_foreach_impl as appropriate."
177 die "EPYTHON unset in escons"
179 local -x EPYTHON=python2.7
183 # Use myesconsargs in EAPI 5 and older
184 if [[ ${EAPI} == [012345] ]]; then
185 set -- "${myesconsargs[@]}" "${@}"
188 # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
189 if [[ ! ${SCONSOPTS+set} ]]; then
191 _scons_clean_makeopts
194 # pass ebuild environment variables through!
195 local -x GENTOO_SCONS_ENV_PASSTHROUGH=1
197 set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
202 if [[ ${ret} -ne 0 ]]; then
204 0|1|2|3) # nonfatal in EAPIs 0 through 3
206 4|5) # 100% fatal in 4 & 5
209 *) # respect nonfatal in 6 onwards
210 die -n "escons failed."
217 # @FUNCTION: _scons_clean_makeopts
219 # @USAGE: [makeflags] [...]
221 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
222 # an argument) of options not supported by SCons and make sure --jobs
223 # gets an argument. Output the resulting flag list (suitable
224 # for an assignment to SCONSOPTS).
225 _scons_clean_makeopts() {
226 local new_makeopts=()
228 debug-print-function ${FUNCNAME} "${@}"
230 if [[ ${#} -eq 0 ]]; then
231 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
234 # unquote if necessary
238 # empty MAKEOPTS give out empty SCONSOPTS
239 # thus, we do need to worry about the initial setup
240 if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
241 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
242 debug-print "Cache hit: [${SCONSOPTS}]"
245 _SCONS_CACHE_MAKEOPTS=${*}
247 while [[ ${#} -gt 0 ]]; do
249 # clean, simple to check -- we like that
250 --jobs=*|--keep-going)
251 new_makeopts+=( ${1} )
253 # need to take a look at the next arg and guess
255 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
256 new_makeopts+=( ${1} ${2} )
259 # no value means no limit, let's pass a default instead
260 new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
263 # strip other long options
272 while [[ -n ${str} ]]; do
277 # -j needs to come last
279 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
283 new_optstr+="j $(( $(get_nproc) + 1 ))"
286 # otherwise, everything after -j is treated as an arg
295 if [[ -n ${new_optstr} ]]; then
296 new_makeopts+=( -${new_optstr} )
303 SCONSOPTS=${new_makeopts[*]}
304 _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
305 debug-print "New SCONSOPTS: [${SCONSOPTS}]"
308 # @FUNCTION: use_scons
309 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
311 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
313 # Output a SCons parameter with value depending on the USE flag state.
314 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
315 # <var-name>=<var-opt-false>.
317 # If <var-name> is omitted, <use-flag> will be used instead. However,
318 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
319 # prepended to the name (e.g. noflag).
321 # If <var-opt-true> and/or <var-opt-false> are omitted,
322 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
324 [[ ${EAPI} == [012345] ]] \
325 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
328 local varname=${2:-${flag/\!/no}}
329 local vartrue=${3:-${USE_SCONS_TRUE}}
330 local varfalse=${4:-${USE_SCONS_FALSE}}
332 debug-print-function ${FUNCNAME} "${@}"
334 if [[ ${#} -eq 0 ]]; then
335 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
336 die 'scons-use(): not enough arguments'
339 if use "${flag}"; then
340 echo "${varname}=${vartrue}"
342 echo "${varname}=${varfalse}"