sys-process/glances: 3.1.4.1-r1 amd64 stable, bug #720368
[gentoo.git] / eclass / scons-utils.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: scons-utils.eclass
5 # @MAINTAINER:
6 # mgorny@gentoo.org
7 # @SUPPORTED_EAPIS: 0 1 2 3 4 5 6 7
8 # @BLURB: helper functions to deal with SCons buildsystem
9 # @DESCRIPTION:
10 # This eclass provides a set of function to help developers sanely call
11 # dev-util/scons and pass parameters to it.
12 #
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
20 #
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:
25 #
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.
30 #
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.
35 #
36 # @EXAMPLE:
37 # @CODE
38 # PYTHON_COMPAT=( python2_7 )
39 # inherit python-any-r1 scons-utils toolchain-funcs
40 #
41 # EAPI=5
42 #
43 # src_configure() {
44 #       MYSCONS=(
45 #               CC="$(tc-getCC)"
46 #               ENABLE_NLS=$(usex nls)
47 #       )
48 # }
49 #
50 # src_compile() {
51 #       escons "${MYSCONS[@]}"
52 # }
53 #
54 # src_install() {
55 #       # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
56 #       escons "${MYSCONS[@]}" DESTDIR="${D}" install
57 # }
58 # @CODE
59
60 # -- public variables --
61
62 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
63 # @DEFAULT_UNSET
64 # @DESCRIPTION:
65 # The minimal version of SCons required for the build to work.
66
67 # @VARIABLE: myesconsargs
68 # @DEFAULT_UNSET
69 # @DESCRIPTION:
70 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
71 #
72 # List of package-specific options to pass to all SCons calls. Supposed to be
73 # set in src_configure().
74
75 # @ECLASS-VARIABLE: SCONSOPTS
76 # @DEFAULT_UNSET
77 # @DESCRIPTION:
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.
81
82 # @ECLASS-VARIABLE: EXTRA_ESCONS
83 # @DEFAULT_UNSET
84 # @DESCRIPTION:
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
87 # and not in ebuilds!
88
89 # @ECLASS-VARIABLE: USE_SCONS_TRUE
90 # @DESCRIPTION:
91 # DEPRECATED: use usex instead
92 #
93 # The default value for truth in scons-use() (1 by default).
94 : ${USE_SCONS_TRUE:=1}
95
96 # @ECLASS-VARIABLE: USE_SCONS_FALSE
97 # @DESCRIPTION:
98 # DEPRECATED: use usex instead
99 #
100 # The default value for false in scons-use() (0 by default).
101 : ${USE_SCONS_FALSE:=0}
102
103 # -- EAPI support check --
104
105 case ${EAPI:-0} in
106         0|1|2|3|4|5|6|7) ;;
107         *) die "EAPI ${EAPI} unsupported."
108 esac
109
110 inherit multiprocessing
111
112 # -- ebuild variables setup --
113
114 if [[ -n ${SCONS_MIN_VERSION} ]]; then
115         SCONS_DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
116 else
117         SCONS_DEPEND="dev-util/scons"
118 fi
119
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}]")"
123
124         scons-utils_python_check_deps() {
125                 has_version "${SCONS_DEPEND}[${PYTHON_USEDEP}]"
126         }
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
130         BDEPEND="
131                 $(python_gen_cond_dep "${SCONS_DEPEND}[\${PYTHON_MULTI_USEDEP}]")
132                 ${PYTHON_DEPS}"
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
140         BDEPEND=""
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"
148 fi
149
150 if [[ ${EAPI:-0} == [0123456] ]]; then
151         DEPEND=${BDEPEND}
152         unset BDEPEND
153 fi
154
155 # -- public functions --
156
157 # @FUNCTION: escons
158 # @USAGE: [<args>...]
159 # @DESCRIPTION:
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.
162 escons() {
163         local ret
164
165         debug-print-function ${FUNCNAME} "${@}"
166
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()."
175                         else # python-r1
176                                 eerror "Please ensure that python_setup is called before escons, or that escons"
177                                 eerror "is used within python_foreach_impl as appropriate."
178                         fi
179                         die "EPYTHON unset in escons"
180                 else
181                         local -x EPYTHON=python2.7
182                 fi
183         fi
184
185         # Use myesconsargs in EAPI 5 and older
186         if [[ ${EAPI} == [012345] ]]; then
187                 set -- "${myesconsargs[@]}" "${@}"
188         fi
189
190         # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
191         if [[ ! ${SCONSOPTS+set} ]]; then
192                 local SCONSOPTS
193                 _scons_clean_makeopts
194         fi
195
196         # pass ebuild environment variables through!
197         local -x GENTOO_SCONS_ENV_PASSTHROUGH=1
198
199         set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
200         echo "${@}" >&2
201         "${@}"
202         ret=${?}
203
204         if [[ ${ret} -ne 0 ]]; then
205                 case "${EAPI:-0}" in
206                         0|1|2|3) # nonfatal in EAPIs 0 through 3
207                                 ;;
208                         4|5) # 100% fatal in 4 & 5
209                                 die "escons failed."
210                                 ;;
211                         *) # respect nonfatal in 6 onwards
212                                 die -n "escons failed."
213                                 ;;
214                 esac
215         fi
216         return ${ret}
217 }
218
219 # @FUNCTION: _scons_clean_makeopts
220 # @INTERNAL
221 # @USAGE: [makeflags] [...]
222 # @DESCRIPTION:
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=()
229
230         debug-print-function ${FUNCNAME} "${@}"
231
232         if [[ ${#} -eq 0 ]]; then
233                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
234                 set -- ${MAKEOPTS}
235         else
236                 # unquote if necessary
237                 set -- ${*}
238         fi
239
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}]"
245                 return
246         fi
247         _SCONS_CACHE_MAKEOPTS=${*}
248
249         while [[ ${#} -gt 0 ]]; do
250                 case ${1} in
251                         # clean, simple to check -- we like that
252                         --jobs=*|--keep-going)
253                                 new_makeopts+=( ${1} )
254                                 ;;
255                         # need to take a look at the next arg and guess
256                         --jobs)
257                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
258                                         new_makeopts+=( ${1} ${2} )
259                                         shift
260                                 else
261                                         # no value means no limit, let's pass a default instead
262                                         new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
263                                 fi
264                                 ;;
265                         # strip other long options
266                         --*)
267                                 ;;
268                         # short option hell
269                         -*)
270                                 local str new_optstr
271                                 new_optstr=
272                                 str=${1#-}
273
274                                 while [[ -n ${str} ]]; do
275                                         case ${str} in
276                                                 k*)
277                                                         new_optstr+=k
278                                                         ;;
279                                                 # -j needs to come last
280                                                 j)
281                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
282                                                                 new_optstr+="j ${2}"
283                                                                 shift
284                                                         else
285                                                                 new_optstr+="j $(( $(get_nproc) + 1 ))"
286                                                         fi
287                                                         ;;
288                                                 # otherwise, everything after -j is treated as an arg
289                                                 j*)
290                                                         new_optstr+=${str}
291                                                         break
292                                                         ;;
293                                         esac
294                                         str=${str#?}
295                                 done
296
297                                 if [[ -n ${new_optstr} ]]; then
298                                         new_makeopts+=( -${new_optstr} )
299                                 fi
300                                 ;;
301                 esac
302                 shift
303         done
304
305         SCONSOPTS=${new_makeopts[*]}
306         _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
307         debug-print "New SCONSOPTS: [${SCONSOPTS}]"
308 }
309
310 # @FUNCTION: use_scons
311 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
312 # @DESCRIPTION:
313 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
314 #
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>.
318 #
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).
322 #
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.
325 use_scons() {
326         [[ ${EAPI} == [012345] ]] \
327                 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
328
329         local flag=${1}
330         local varname=${2:-${flag/\!/no}}
331         local vartrue=${3:-${USE_SCONS_TRUE}}
332         local varfalse=${4:-${USE_SCONS_FALSE}}
333
334         debug-print-function ${FUNCNAME} "${@}"
335
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'
339         fi
340
341         if use "${flag}"; then
342                 echo "${varname}=${vartrue}"
343         else
344                 echo "${varname}=${varfalse}"
345         fi
346 }