net-fs/openafs-kernel: remove vulnerable versions
[gentoo.git] / eclass / scons-utils.eclass
1 # Copyright 1999-2012 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 # @EXAMPLE:
13 #
14 # @CODE
15 # inherit scons-utils toolchain-funcs
16 #
17 # EAPI=4
18 #
19 # src_configure() {
20 #       myesconsargs=(
21 #               CC="$(tc-getCC)"
22 #               $(use_scons nls ENABLE_NLS)
23 #       )
24 # }
25 #
26 # src_compile() {
27 #       escons
28 # }
29 #
30 # src_install() {
31 #       # note: this can be DESTDIR, INSTALL_ROOT, ... depending on package
32 #       escons DESTDIR="${D}" install
33 # }
34 # @CODE
35
36 # -- public variables --
37
38 # @ECLASS-VARIABLE: SCONS_MIN_VERSION
39 # @DEFAULT_UNSET
40 # @DESCRIPTION:
41 # The minimal version of SCons required for the build to work.
42
43 # @VARIABLE: myesconsargs
44 # @DEFAULT_UNSET
45 # @DESCRIPTION:
46 # List of package-specific options to pass to all SCons calls. Supposed to be
47 # set in src_configure().
48
49 # @ECLASS-VARIABLE: SCONSOPTS
50 # @DEFAULT_UNSET
51 # @DESCRIPTION:
52 # The default set of options to pass to scons. Similar to MAKEOPTS,
53 # supposed to be set in make.conf. If unset, escons() will use cleaned
54 # up MAKEOPTS instead.
55
56 # @ECLASS-VARIABLE: EXTRA_ESCONS
57 # @DEFAULT_UNSET
58 # @DESCRIPTION:
59 # The additional parameters to pass to SCons whenever escons() is used.
60 # Much like EXTRA_EMAKE, this is not supposed to be used in make.conf
61 # and not in ebuilds!
62
63 # @ECLASS-VARIABLE: USE_SCONS_TRUE
64 # @DESCRIPTION:
65 # The default value for truth in scons-use() (1 by default).
66 : ${USE_SCONS_TRUE:=1}
67
68 # @ECLASS-VARIABLE: USE_SCONS_FALSE
69 # @DESCRIPTION:
70 # The default value for false in scons-use() (0 by default).
71 : ${USE_SCONS_FALSE:=0}
72
73 # -- EAPI support check --
74
75 case ${EAPI:-0} in
76         0|1|2|3|4|5) ;;
77         *) die "EAPI ${EAPI} unsupported."
78 esac
79
80 # -- ebuild variables setup --
81
82 if [[ -n ${SCONS_MIN_VERSION} ]]; then
83         DEPEND=">=dev-util/scons-${SCONS_MIN_VERSION}"
84 else
85         DEPEND="dev-util/scons"
86 fi
87
88 # -- public functions --
89
90 # @FUNCTION: escons
91 # @USAGE: [scons-arg] ...
92 # @DESCRIPTION:
93 # Call scons, passing the supplied arguments, ${myesconsargs[@]},
94 # filtered ${MAKEOPTS}, ${EXTRA_ESCONS}. Similar to emake. Like emake,
95 # this function does die on failure in EAPI 4 (unless called nonfatal).
96 escons() {
97         local ret
98
99         debug-print-function ${FUNCNAME} "${@}"
100
101         # if SCONSOPTS are _unset_, use cleaned MAKEOPTS
102         set -- scons ${SCONSOPTS-$(scons_clean_makeopts)} ${EXTRA_ESCONS} \
103                 "${myesconsargs[@]}" "${@}"
104         echo "${@}" >&2
105         "${@}"
106         ret=${?}
107
108         [[ ${ret} -ne 0 ]] && has "${EAPI:-0}" 4 5 && die "escons failed."
109         return ${ret}
110 }
111
112 # @FUNCTION: scons_clean_makeopts
113 # @USAGE: [makeflags] [...]
114 # @DESCRIPTION:
115 # Strip the supplied makeflags (or ${MAKEOPTS} if called without
116 # an argument) of options not supported by SCons and make sure --jobs
117 # gets an argument. Output the resulting flag list (suitable
118 # for an assignment to SCONSOPTS).
119 scons_clean_makeopts() {
120         local new_makeopts
121
122         debug-print-function ${FUNCNAME} "${@}"
123
124         if [[ ${#} -eq 0 ]]; then
125                 debug-print "Using MAKEOPTS: [${MAKEOPTS}]"
126                 set -- ${MAKEOPTS}
127         else
128                 # unquote if necessary
129                 set -- ${*}
130         fi
131
132         # empty MAKEOPTS give out empty SCONSOPTS
133         # thus, we do need to worry about the initial setup
134         if [[ ${*} = ${_SCONS_CACHE_MAKEOPTS} ]]; then
135                 set -- ${_SCONS_CACHE_SCONSOPTS}
136                 debug-print "Cache hit: [${*}]"
137                 echo ${*}
138                 return
139         fi
140         export _SCONS_CACHE_MAKEOPTS=${*}
141
142         while [[ ${#} -gt 0 ]]; do
143                 case ${1} in
144                         # clean, simple to check -- we like that
145                         --jobs=*|--keep-going)
146                                 new_makeopts=${new_makeopts+${new_makeopts} }${1}
147                                 ;;
148                         # need to take a look at the next arg and guess
149                         --jobs)
150                                 if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
151                                         new_makeopts="${new_makeopts+${new_makeopts} }${1} ${2}"
152                                         shift
153                                 else
154                                         # no value means no limit, let's pass a random int
155                                         new_makeopts=${new_makeopts+${new_makeopts} }${1}=5
156                                 fi
157                                 ;;
158                         # strip other long options
159                         --*)
160                                 ;;
161                         # short option hell
162                         -*)
163                                 local str new_optstr
164                                 new_optstr=
165                                 str=${1#-}
166
167                                 while [[ -n ${str} ]]; do
168                                         case ${str} in
169                                                 k*)
170                                                         new_optstr=${new_optstr}k
171                                                         ;;
172                                                 # -j needs to come last
173                                                 j)
174                                                         if [[ ${#} -gt 1 && ${2} =~ ^[0-9]+$ ]]; then
175                                                                 new_optstr="${new_optstr}j ${2}"
176                                                                 shift
177                                                         else
178                                                                 new_optstr="${new_optstr}j 5"
179                                                         fi
180                                                         ;;
181                                                 # otherwise, everything after -j is treated as an arg
182                                                 j*)
183                                                         new_optstr=${new_optstr}${str}
184                                                         break
185                                                         ;;
186                                         esac
187                                         str=${str#?}
188                                 done
189
190                                 if [[ -n ${new_optstr} ]]; then
191                                         new_makeopts=${new_makeopts+${new_makeopts} }-${new_optstr}
192                                 fi
193                                 ;;
194                 esac
195                 shift
196         done
197
198         set -- ${new_makeopts}
199         export _SCONS_CACHE_SCONSOPTS=${*}
200         debug-print "New SCONSOPTS: [${*}]"
201         echo ${*}
202 }
203
204 # @FUNCTION: use_scons
205 # @USAGE: <use-flag> [var-name] [var-opt-true] [var-opt-false]
206 # @DESCRIPTION:
207 # Output a SCons parameter with value depending on the USE flag state.
208 # If the USE flag is set, output <var-name>=<var-opt-true>; otherwise
209 # <var-name>=<var-opt-false>.
210 #
211 # If <var-name> is omitted, <use-flag> will be used instead. However,
212 # if <use-flag> starts with an exclamation mark (!flag), 'no' will be
213 # prepended to the name (e.g. noflag).
214 #
215 # If <var-opt-true> and/or <var-opt-false> are omitted,
216 # ${USE_SCONS_TRUE} and/or ${USE_SCONS_FALSE} will be used instead.
217 use_scons() {
218         local flag=${1}
219         local varname=${2:-${flag/\!/no}}
220         local vartrue=${3:-${USE_SCONS_TRUE}}
221         local varfalse=${4:-${USE_SCONS_FALSE}}
222
223         debug-print-function ${FUNCNAME} "${@}"
224
225         if [[ ${#} -eq 0 ]]; then
226                 eerror "Usage: scons-use <use-flag> [var-name] [var-opt-true] [var-opt-false]"
227                 die 'scons-use(): not enough arguments'
228         fi
229
230         if use "${flag}"; then
231                 echo "${varname}=${vartrue}"
232         else
233                 echo "${varname}=${varfalse}"
234         fi
235 }