scons-utils.eclass: Support 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 # Please note that SCons is more like a 'build system creation kit',
13 # and requires a lot of upstream customization to be used sanely.
14 # You will often need to request fixes upstream and/or patch the build
15 # system. In particular:
16 #
17 # 1. There are no 'standard' variables. To respect CC, CXX, CFLAGS,
18 # CXXFLAGS, CPPFLAGS, LDFLAGS, upstream needs to define appropriate
19 # variables explicitly. In some cases, upstreams respect envvars,
20 # in others you need to pass them as options.
21 #
22 # 2. SCons scrubs out environment by default and replaces it with some
23 # pre-defined values. To respect environment variables such as PATH,
24 # Upstreams need to explicitly get them from os.environ and copy them
25 # to the build environment.
26 #
27 # @EXAMPLE:
28 # @CODE
29 # inherit scons-utils toolchain-funcs
30 #
31 # EAPI=5
32 #
33 # src_configure() {
34 #       MYSCONS=(
35 #               CC="$(tc-getCC)"
36 #               ENABLE_NLS=$(usex nls)
37 #       )
38 # }
39 #
40 # src_compile() {
41 #       escons "${MYSCONS[@]}"
42 # }
43 #
44 # src_install() {
45 #       # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
46 #       escons "${MYSCONS[@]}" DESTDIR="${D}" install
47 # }
48 # @CODE
49
50 # -- public variables --
51
52 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
53 # @DEFAULT_UNSET
54 # @DESCRIPTION:
55 # The minimal version of SCons required for the build to work.
56
57 # @VARIABLE: myesconsargs
58 # @DEFAULT_UNSET
59 # @DESCRIPTION:
60 # DEPRECATED, EAPI 0..5 ONLY: pass options to escons instead
61 #
62 # List of package-specific options to pass to all SCons calls. Supposed to be
63 # set in src_configure().
64
65 # @ECLASS-VARIABLE: SCONSOPTS
66 # @DEFAULT_UNSET
67 # @DESCRIPTION:
68 # The default set of options to pass to scons. Similar to MAKEOPTS,
69 # supposed to be set in make.conf. If unset, escons() will use cleaned
70 # up MAKEOPTS instead.
71
72 # @ECLASS-VARIABLE: EXTRA_ESCONS
73 # @DEFAULT_UNSET
74 # @DESCRIPTION:
75 # The additional parameters to pass to SCons whenever escons() is used.
76 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
77 # and not in ebuilds!
78
79 # @ECLASS-VARIABLE: USE_SCONS_TRUE
80 # @DESCRIPTION:
81 # DEPRECATED: use usex instead
82 #
83 # The default value for truth in scons-use() (1 by default).
84 : ${USE_SCONS_TRUE:=1}
85
86 # @ECLASS-VARIABLE: USE_SCONS_FALSE
87 # @DESCRIPTION:
88 # DEPRECATED: use usex instead
89 #
90 # The default value for false in scons-use() (0 by default).
91 : ${USE_SCONS_FALSE:=0}
92
93 # -- EAPI support check --
94
95 case ${EAPI:-0} in
96         0|1|2|3|4|5|6|7) ;;
97         *) die "EAPI ${EAPI} unsupported."
98 esac
99
100 inherit multiprocessing
101
102 # -- ebuild variables setup --
103
104 if [[ -n ${SCONS_MIN_VERSION} ]]; then
105         BDEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
106 else
107         BDEPEND="dev-util/scons"
108 fi
109
110 if [[ ${EAPI:-0} == [0123456] ]]; then
111         DEPEND=${BDEPEND}
112         unset BDEPEND
113 fi
114
115 # -- public functions --
116
117 # @FUNCTION: escons
118 # @USAGE: [<args>...]
119 # @DESCRIPTION:
120 # Call scons, passing the supplied arguments. Like emake, this function
121 # does die on failure in EAPI 4. Respects nonfatal in EAPI 6 and newer.
122 escons() {
123         local ret
124
125         debug-print-function ${FUNCNAME} "${@}"
126
127         # Use myesconsargs in EAPI 5 and older
128         if [[ ${EAPI} == [012345] ]]; then
129                 set -- "${myesconsargs[@]}" "${@}"
130         fi
131
132         # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
133         if [[ ! ${SCONSOPTS+set} ]]; then
134                 local SCONSOPTS
135                 _scons_clean_makeopts
136         fi
137
138         set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
139         echo "${@}" >&2
140         "${@}"
141         ret=${?}
142
143         if [[ ${ret} -ne 0 ]]; then
144                 case "${EAPI:-0}" in
145                         0|1|2|3) # nonfatal in EAPIs 0 through 3
146                                 ;;
147                         4|5) # 100% fatal in 4 & 5
148                                 die "escons failed."
149                                 ;;
150                         *) # respect nonfatal in 6 onwards
151                                 die -n "escons failed."
152                                 ;;
153                 esac
154         fi
155         return ${ret}
156 }
157
158 # @FUNCTION: _scons_clean_makeopts
159 # @INTERNAL
160 # @USAGE: [makeflags] [...]
161 # @DESCRIPTION:
162 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
163 # an argument) of options not supported by SCons and make sure --jobs
164 # gets an argument. Output the resulting flag list (suitable
165 # for an assignment to SCONSOPTS).
166 _scons_clean_makeopts() {
167         local new_makeopts=()
168
169         debug-print-function ${FUNCNAME} "${@}"
170
171         if [[ ${#} -eq 0 ]]; then
172                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
173                 set -- ${MAKEOPTS}
174         else
175                 # unquote if necessary
176                 set -- ${*}
177         fi
178
179         # empty MAKEOPTS give out empty SCONSOPTS
180         # thus, we do need to worry about the initial setup
181         if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
182                 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
183                 debug-print "Cache hit: [${SCONSOPTS}]"
184                 return
185         fi
186         _SCONS_CACHE_MAKEOPTS=${*}
187
188         while [[ ${#} -gt 0 ]]; do
189                 case ${1} in
190                         # clean, simple to check -- we like that
191                         --jobs=*|--keep-going)
192                                 new_makeopts+=( ${1} )
193                                 ;;
194                         # need to take a look at the next arg and guess
195                         --jobs)
196                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
197                                         new_makeopts+=( ${1} ${2} )
198                                         shift
199                                 else
200                                         # no value means no limit, let's pass a default instead
201                                         new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
202                                 fi
203                                 ;;
204                         # strip other long options
205                         --*)
206                                 ;;
207                         # short option hell
208                         -*)
209                                 local str new_optstr
210                                 new_optstr=
211                                 str=${1#-}
212
213                                 while [[ -n ${str} ]]; do
214                                         case ${str} in
215                                                 k*)
216                                                         new_optstr+=k
217                                                         ;;
218                                                 # -j needs to come last
219                                                 j)
220                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
221                                                                 new_optstr+="j ${2}"
222                                                                 shift
223                                                         else
224                                                                 new_optstr+="j $(( $(get_nproc) + 1 ))"
225                                                         fi
226                                                         ;;
227                                                 # otherwise, everything after -j is treated as an arg
228                                                 j*)
229                                                         new_optstr+=${str}
230                                                         break
231                                                         ;;
232                                         esac
233                                         str=${str#?}
234                                 done
235
236                                 if [[ -n ${new_optstr} ]]; then
237                                         new_makeopts+=( -${new_optstr} )
238                                 fi
239                                 ;;
240                 esac
241                 shift
242         done
243
244         SCONSOPTS=${new_makeopts[*]}
245         _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
246         debug-print "New SCONSOPTS: [${SCONSOPTS}]"
247 }
248
249 # @FUNCTION: use_scons
250 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
251 # @DESCRIPTION:
252 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
253 #
254 # Output a SCons parameter with value depending on the USE flag state.
255 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
256 # <var-name>=<var-opt-false>.
257 #
258 # If <var-name> is omitted, <use-flag> will be used instead. However,
259 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
260 # prepended to the name (e.g. noflag).
261 #
262 # If <var-opt-true> and/or <var-opt-false> are omitted,
263 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
264 use_scons() {
265         [[ ${EAPI} == [012345] ]] \
266                 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
267
268         local flag=${1}
269         local varname=${2:-${flag/\!/no}}
270         local vartrue=${3:-${USE_SCONS_TRUE}}
271         local varfalse=${4:-${USE_SCONS_FALSE}}
272
273         debug-print-function ${FUNCNAME} "${@}"
274
275         if [[ ${#} -eq 0 ]]; then
276                 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
277                 die 'scons-use(): not enough arguments'
278         fi
279
280         if use "${flag}"; then
281                 echo "${varname}=${vartrue}"
282         else
283                 echo "${varname}=${varfalse}"
284         fi
285 }