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