scons-utils.eclass: Revert EAPI 7 pending further changes
[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) ;;
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         # pass ebuild environment variables through!
139         local -x GENTOO_SCONS_ENV_PASSTHROUGH=1
140
141         set -- scons ${SCONSOPTS} ${EXTRA_ESCONS} "${@}"
142         echo "${@}" >&2
143         "${@}"
144         ret=${?}
145
146         if [[ ${ret} -ne 0 ]]; then
147                 case "${EAPI:-0}" in
148                         0|1|2|3) # nonfatal in EAPIs 0 through 3
149                                 ;;
150                         4|5) # 100% fatal in 4 & 5
151                                 die "escons failed."
152                                 ;;
153                         *) # respect nonfatal in 6 onwards
154                                 die -n "escons failed."
155                                 ;;
156                 esac
157         fi
158         return ${ret}
159 }
160
161 # @FUNCTION: _scons_clean_makeopts
162 # @INTERNAL
163 # @USAGE: [makeflags] [...]
164 # @DESCRIPTION:
165 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
166 # an argument) of options not supported by SCons and make sure --jobs
167 # gets an argument. Output the resulting flag list (suitable
168 # for an assignment to SCONSOPTS).
169 _scons_clean_makeopts() {
170         local new_makeopts=()
171
172         debug-print-function ${FUNCNAME} "${@}"
173
174         if [[ ${#} -eq 0 ]]; then
175                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
176                 set -- ${MAKEOPTS}
177         else
178                 # unquote if necessary
179                 set -- ${*}
180         fi
181
182         # empty MAKEOPTS give out empty SCONSOPTS
183         # thus, we do need to worry about the initial setup
184         if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
185                 SCONSOPTS=${_SCONS_CACHE_SCONSOPTS}
186                 debug-print "Cache hit: [${SCONSOPTS}]"
187                 return
188         fi
189         _SCONS_CACHE_MAKEOPTS=${*}
190
191         while [[ ${#} -gt 0 ]]; do
192                 case ${1} in
193                         # clean, simple to check -- we like that
194                         --jobs=*|--keep-going)
195                                 new_makeopts+=( ${1} )
196                                 ;;
197                         # need to take a look at the next arg and guess
198                         --jobs)
199                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
200                                         new_makeopts+=( ${1} ${2} )
201                                         shift
202                                 else
203                                         # no value means no limit, let's pass a default instead
204                                         new_makeopts+=( ${1}=$(( $(get_nproc) + 1 )) )
205                                 fi
206                                 ;;
207                         # strip other long options
208                         --*)
209                                 ;;
210                         # short option hell
211                         -*)
212                                 local str new_optstr
213                                 new_optstr=
214                                 str=${1#-}
215
216                                 while [[ -n ${str} ]]; do
217                                         case ${str} in
218                                                 k*)
219                                                         new_optstr+=k
220                                                         ;;
221                                                 # -j needs to come last
222                                                 j)
223                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
224                                                                 new_optstr+="j ${2}"
225                                                                 shift
226                                                         else
227                                                                 new_optstr+="j $(( $(get_nproc) + 1 ))"
228                                                         fi
229                                                         ;;
230                                                 # otherwise, everything after -j is treated as an arg
231                                                 j*)
232                                                         new_optstr+=${str}
233                                                         break
234                                                         ;;
235                                         esac
236                                         str=${str#?}
237                                 done
238
239                                 if [[ -n ${new_optstr} ]]; then
240                                         new_makeopts+=( -${new_optstr} )
241                                 fi
242                                 ;;
243                 esac
244                 shift
245         done
246
247         SCONSOPTS=${new_makeopts[*]}
248         _SCONS_CACHE_SCONSOPTS=${SCONSOPTS}
249         debug-print "New SCONSOPTS: [${SCONSOPTS}]"
250 }
251
252 # @FUNCTION: use_scons
253 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
254 # @DESCRIPTION:
255 # DEPRECATED, EAPI 0..5 ONLY: use usex instead
256 #
257 # Output a SCons parameter with value depending on the USE flag state.
258 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
259 # <var-name>=<var-opt-false>.
260 #
261 # If <var-name> is omitted, <use-flag> will be used instead. However,
262 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
263 # prepended to the name (e.g. noflag).
264 #
265 # If <var-opt-true> and/or <var-opt-false> are omitted,
266 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
267 use_scons() {
268         [[ ${EAPI} == [012345] ]] \
269                 || die "${FUNCNAME} is banned in EAPI ${EAPI}, use usex instead"
270
271         local flag=${1}
272         local varname=${2:-${flag/\!/no}}
273         local vartrue=${3:-${USE_SCONS_TRUE}}
274         local varfalse=${4:-${USE_SCONS_FALSE}}
275
276         debug-print-function ${FUNCNAME} "${@}"
277
278         if [[ ${#} -eq 0 ]]; then
279                 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
280                 die 'scons-use(): not enough arguments'
281         fi
282
283         if use "${flag}"; then
284                 echo "${varname}=${vartrue}"
285         else
286                 echo "${varname}=${varfalse}"
287         fi
288 }