scons-utils.eclass: escons, invert EAPI check to cover future EAPIs
[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 # List of package-specific options to pass to all SCons calls. Supposed to be
62 # set in src_configure().
63
64 # @ECLASS-VARIABLE: SCONSOPTS
65 # @DEFAULT_UNSET
66 # @DESCRIPTION:
67 # The default set of options to pass to scons. Similar to MAKEOPTS,
68 # supposed to be set in make.conf. If unset, escons() will use cleaned
69 # up MAKEOPTS instead.
70
71 # @ECLASS-VARIABLE: EXTRA_ESCONS
72 # @DEFAULT_UNSET
73 # @DESCRIPTION:
74 # The additional parameters to pass to SCons whenever escons() is used.
75 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
76 # and not in ebuilds!
77
78 # @ECLASS-VARIABLE: USE_SCONS_TRUE
79 # @DESCRIPTION:
80 # The default value for truth in scons-use() (1 by default).
81 : ${USE_SCONS_TRUE:=1}
82
83 # @ECLASS-VARIABLE: USE_SCONS_FALSE
84 # @DESCRIPTION:
85 # The default value for false in scons-use() (0 by default).
86 : ${USE_SCONS_FALSE:=0}
87
88 # -- EAPI support check --
89
90 case ${EAPI:-0} in
91         0|1|2|3|4|5) ;;
92         *) die "EAPI ${EAPI} unsupported."
93 esac
94
95 # -- ebuild variables setup --
96
97 if [[ -n ${SCONS_MIN_VERSION} ]]; then
98         DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
99 else
100         DEPEND="dev-util/scons"
101 fi
102
103 # -- public functions --
104
105 # @FUNCTION: escons
106 # @USAGE: [scons-arg] ...
107 # @DESCRIPTION:
108 # Call scons, passing the supplied arguments, ${myesconsargs[@]},
109 # filtered ${MAKEOPTS}, ${EXTRA_ESCONS}. Similar to emake. Like emake,
110 # this function does die on failure in EAPI 4.
111 escons() {
112         local ret
113
114         debug-print-function ${FUNCNAME} "${@}"
115
116         # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
117         set -- scons ${SCONSOPTS-$(scons_clean_makeopts)} ${EXTRA_ESCONS} \
118                 "${myesconsargs[@]}" "${@}"
119         echo "${@}" >&2
120         "${@}"
121         ret=${?}
122
123         if [[ ${ret} -ne 0 ]]; then
124                 [[ ${EAPI:-0} != [0123] ]] && die "escons failed."
125         fi
126         return ${ret}
127 }
128
129 # @FUNCTION: scons_clean_makeopts
130 # @USAGE: [makeflags] [...]
131 # @DESCRIPTION:
132 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
133 # an argument) of options not supported by SCons and make sure --jobs
134 # gets an argument. Output the resulting flag list (suitable
135 # for an assignment to SCONSOPTS).
136 scons_clean_makeopts() {
137         local new_makeopts
138
139         debug-print-function ${FUNCNAME} "${@}"
140
141         if [[ ${#} -eq 0 ]]; then
142                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
143                 set -- ${MAKEOPTS}
144         else
145                 # unquote if necessary
146                 set -- ${*}
147         fi
148
149         # empty MAKEOPTS give out empty SCONSOPTS
150         # thus, we do need to worry about the initial setup
151         if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
152                 set -- ${_SCONS_CACHE_SCONSOPTS}
153                 debug-print "Cache hit: [${*}]"
154                 echo ${*}
155                 return
156         fi
157         export _SCONS_CACHE_MAKEOPTS=${*}
158
159         while [[ ${#} -gt 0 ]]; do
160                 case ${1} in
161                         # clean, simple to check -- we like that
162                         --jobs=*|--keep-going)
163                                 new_makeopts=${new_makeopts+${new_makeopts} }${1}
164                                 ;;
165                         # need to take a look at the next arg and guess
166                         --jobs)
167                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
168                                         new_makeopts="${new_makeopts+${new_makeopts} }${1} ${2}"
169                                         shift
170                                 else
171                                         # no value means no limit, let's pass a random int
172                                         new_makeopts=${new_makeopts+${new_makeopts} }${1}=5
173                                 fi
174                                 ;;
175                         # strip other long options
176                         --*)
177                                 ;;
178                         # short option hell
179                         -*)
180                                 local str new_optstr
181                                 new_optstr=
182                                 str=${1#-}
183
184                                 while [[ -n ${str} ]]; do
185                                         case ${str} in
186                                                 k*)
187                                                         new_optstr=${new_optstr}k
188                                                         ;;
189                                                 # -j needs to come last
190                                                 j)
191                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
192                                                                 new_optstr="${new_optstr}j ${2}"
193                                                                 shift
194                                                         else
195                                                                 new_optstr="${new_optstr}j 5"
196                                                         fi
197                                                         ;;
198                                                 # otherwise, everything after -j is treated as an arg
199                                                 j*)
200                                                         new_optstr=${new_optstr}${str}
201                                                         break
202                                                         ;;
203                                         esac
204                                         str=${str#?}
205                                 done
206
207                                 if [[ -n ${new_optstr} ]]; then
208                                         new_makeopts=${new_makeopts+${new_makeopts} }-${new_optstr}
209                                 fi
210                                 ;;
211                 esac
212                 shift
213         done
214
215         set -- ${new_makeopts}
216         export _SCONS_CACHE_SCONSOPTS=${*}
217         debug-print "New SCONSOPTS: [${*}]"
218         echo ${*}
219 }
220
221 # @FUNCTION: use_scons
222 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
223 # @DESCRIPTION:
224 # Output a SCons parameter with value depending on the USE flag state.
225 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
226 # <var-name>=<var-opt-false>.
227 #
228 # If <var-name> is omitted, <use-flag> will be used instead. However,
229 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
230 # prepended to the name (e.g. noflag).
231 #
232 # If <var-opt-true> and/or <var-opt-false> are omitted,
233 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
234 use_scons() {
235         local flag=${1}
236         local varname=${2:-${flag/\!/no}}
237         local vartrue=${3:-${USE_SCONS_TRUE}}
238         local varfalse=${4:-${USE_SCONS_FALSE}}
239
240         debug-print-function ${FUNCNAME} "${@}"
241
242         if [[ ${#} -eq 0 ]]; then
243                 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
244                 die 'scons-use(): not enough arguments'
245         fi
246
247         if use "${flag}"; then
248                 echo "${varname}=${vartrue}"
249         else
250                 echo "${varname}=${varfalse}"
251         fi
252 }