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