scons-utils.eclass: Enable EAPI 6
[gentoo.git] / eclass / scons-utils.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: scons-utils.eclass
6 # @MAINTAINER:
7 # mgorny@gentoo.org
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 # 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:
17 #
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.
22 #
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.
27 #
28 # @EXAMPLE:
29 # @CODE
30 # inherit scons-utils toolchain-funcs
31 #
32 # EAPI=5
33 #
34 # src_configure() {
35 #       MYSCONS=(
36 #               CC="$(tc-getCC)"
37 #               ENABLE_NLS=$(usex nls)
38 #       )
39 # }
40 #
41 # src_compile() {
42 #       escons "${MYSCONS[@]}"
43 # }
44 #
45 # src_install() {
46 #       # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
47 #       escons "${MYSCONS[@]}" DESTDIR="${D}" install
48 # }
49 # @CODE
50
51 # -- public variables --
52
53 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
54 # @DEFAULT_UNSET
55 # @DESCRIPTION:
56 # The minimal version of SCons required for the build to work.
57
58 # @VARIABLE: myesconsargs
59 # @DEFAULT_UNSET
60 # @DESCRIPTION:
61 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
62 #
63 # List of package-specific options to pass to all SCons calls. Supposed to be
64 # set in src_configure().
65
66 # @ECLASS-VARIABLE: SCONSOPTS
67 # @DEFAULT_UNSET
68 # @DESCRIPTION:
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.
72
73 # @ECLASS-VARIABLE: EXTRA_ESCONS
74 # @DEFAULT_UNSET
75 # @DESCRIPTION:
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
78 # and not in ebuilds!
79
80 # @ECLASS-VARIABLE: USE_SCONS_TRUE
81 # @DESCRIPTION:
82 # DEPRECATED: use usex instead
83 #
84 # The default value for truth in scons-use() (1 by default).
85 : ${USE_SCONS_TRUE:=1}
86
87 # @ECLASS-VARIABLE: USE_SCONS_FALSE
88 # @DESCRIPTION:
89 # DEPRECATED: use usex instead
90 #
91 # The default value for false in scons-use() (0 by default).
92 : ${USE_SCONS_FALSE:=0}
93
94 # -- EAPI support check --
95
96 case ${EAPI:-0} in
97         0|1|2|3|4|5|6) ;;
98         *) die "EAPI ${EAPI} unsupported."
99 esac
100
101 # -- ebuild variables setup --
102
103 if [[ -n ${SCONS_MIN_VERSION} ]]; then
104         DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
105 else
106         DEPEND="dev-util/scons"
107 fi
108
109 # -- public functions --
110
111 # @FUNCTION: escons
112 # @USAGE: [<args>...]
113 # @DESCRIPTION:
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.
116 escons() {
117         local ret
118
119         debug-print-function ${FUNCNAME} "${@}"
120
121         # Use myesconsargs in EAPI 5 and older
122         if [[ ${EAPI} == [012345] ]]; then
123                 set -- "${myesconsargs[@]}" "${@}"
124         fi
125
126         # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
127         if [[ ! ${SCONSOPTS+set} ]]; then
128                 local SCONSOPTS
129                 _scons_clean_makeopts
130         fi
131
132         set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
133         echo "${@}" >&2
134         "${@}"
135         ret=${?}
136
137         if [[ ${ret} -ne 0 ]]; then
138                 case "${EAPI:-0}" in
139                         0|1|2|3) # nonfatal in EAPIs 0 through 3
140                                 ;;
141                         4|5) # 100% fatal in 4 & 5
142                                 die "escons failed."
143                                 ;;
144                         *) # respect nonfatal in 6 onwards
145                                 die -n "escons failed."
146                                 ;;
147                 esac
148         fi
149         return ${ret}
150 }
151
152 # @FUNCTION: _scons_get_default_jobs
153 # @INTERNAL
154 # @DESCRIPTION:
155 # Output the default number of jobs, used if -j is used without
156 # argument. Tries to figure out the number of logical CPUs, falling
157 # back to hardcoded constant.
158 _scons_get_default_jobs() {
159         local nproc
160
161         if type -P nproc &>/dev/null; then
162                 # GNU
163                 nproc=$(nproc)
164         elif type -P python &>/dev/null; then
165                 # fallback to python2.6+
166                 # note: this may fail (raise NotImplementedError)
167                 nproc=$(python -c 'import multiprocessing; print(multiprocessing.cpu_count());' 2>/dev/null)
168         fi
169
170         if [[ ${nproc} ]]; then
171                 echo $(( nproc + 1 ))
172         else
173                 # random default
174                 echo 5
175         fi
176 }
177
178 # @FUNCTION: _scons_clean_makeopts
179 # @INTERNAL
180 # @USAGE: [makeflags] [...]
181 # @DESCRIPTION:
182 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
183 # an argument) of options not supported by SCons and make sure --jobs
184 # gets an argument. Output the resulting flag list (suitable
185 # for an assignment to SCONSOPTS).
186 _scons_clean_makeopts() {
187         local new_makeopts=()
188
189         debug-print-function ${FUNCNAME} "${@}"
190
191         if [[ ${#} -eq 0 ]]; then
192                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
193                 set -- ${MAKEOPTS}
194         else
195                 # unquote if necessary
196                 set -- ${*}
197         fi
198
199         # empty MAKEOPTS give out empty SCONSOPTS
200         # thus, we do need to worry about the initial setup
201         if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
202                 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
203                 debug-print "Cache hit: [${SCONSOPTS}]"
204                 return
205         fi
206         _SCONS_CACHE_MAKEOPTS=${*}
207
208         while [[ ${#} -gt 0 ]]; do
209                 case ${1} in
210                         # clean, simple to check -- we like that
211                         --jobs=*|--keep-going)
212                                 new_makeopts+=( ${1} )
213                                 ;;
214                         # need to take a look at the next arg and guess
215                         --jobs)
216                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
217                                         new_makeopts+=( ${1} ${2} )
218                                         shift
219                                 else
220                                         # no value means no limit, let's pass a random int
221                                         new_makeopts+=( ${1}=$(_scons_get_default_jobs) )
222                                 fi
223                                 ;;
224                         # strip other long options
225                         --*)
226                                 ;;
227                         # short option hell
228                         -*)
229                                 local str new_optstr
230                                 new_optstr=
231                                 str=${1#-}
232
233                                 while [[ -n ${str} ]]; do
234                                         case ${str} in
235                                                 k*)
236                                                         new_optstr+=k
237                                                         ;;
238                                                 # -j needs to come last
239                                                 j)
240                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
241                                                                 new_optstr+="j ${2}"
242                                                                 shift
243                                                         else
244                                                                 new_optstr+="j $(_scons_get_default_jobs)"
245                                                         fi
246                                                         ;;
247                                                 # otherwise, everything after -j is treated as an arg
248                                                 j*)
249                                                         new_optstr+=${str}
250                                                         break
251                                                         ;;
252                                         esac
253                                         str=${str#?}
254                                 done
255
256                                 if [[ -n ${new_optstr} ]]; then
257                                         new_makeopts+=( -${new_optstr} )
258                                 fi
259                                 ;;
260                 esac
261                 shift
262         done
263
264         SCONSOPTS=${new_makeopts[*]}
265         _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
266         debug-print "New SCONSOPTS: [${SCONSOPTS}]"
267 }
268
269 # @FUNCTION: use_scons
270 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
271 # @DESCRIPTION:
272 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
273 #
274 # Output a SCons parameter with value depending on the USE flag state.
275 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
276 # <var-name>=<var-opt-false>.
277 #
278 # If <var-name> is omitted, <use-flag> will be used instead. However,
279 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
280 # prepended to the name (e.g. noflag).
281 #
282 # If <var-opt-true> and/or <var-opt-false> are omitted,
283 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
284 use_scons() {
285         [[ ${EAPI} == [012345] ]] \
286                 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
287
288         local flag=${1}
289         local varname=${2:-${flag/\!/no}}
290         local vartrue=${3:-${USE_SCONS_TRUE}}
291         local varfalse=${4:-${USE_SCONS_FALSE}}
292
293         debug-print-function ${FUNCNAME} "${@}"
294
295         if [[ ${#} -eq 0 ]]; then
296                 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
297                 die 'scons-use(): not enough arguments'
298         fi
299
300         if use "${flag}"; then
301                 echo "${varname}=${vartrue}"
302         else
303                 echo "${varname}=${varfalse}"
304         fi
305 }