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