kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / confutils.eclass
1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: confutils.eclass
5 # @MAINTAINER:
6 # No maintainer <maintainer-needed@gentoo.org>
7 # @BLURB: utility functions to help with configuring a package
8 # @DESCRIPTION:
9 # The confutils eclass contains functions to handle use flag dependencies and
10 # extended --with-*/--enable-* magic.
11 #
12 # Based on the PHP5 eclass by Stuart Herbert <stuart@stuartherbert.com>
13
14 inherit eutils
15
16 # @VARIABLE: EBUILD_SUPPORTS_SHAREDEXT
17 # @DESCRIPTION:
18 # Set this variable to 1 if your ebuild supports shared extensions. You need to
19 # call confutils_init() in pkg_setup() if you use this variable.
20 if [[ ${EBUILD_SUPPORTS_SHAREDEXT} == 1 ]]; then
21         IUSE="sharedext"
22 fi
23
24 # @FUNCTION: confutils_init
25 # @USAGE: [value]
26 # @DESCRIPTION:
27 # Call this function from your pkg_setup() function to initialize this eclass
28 # if EBUILD_SUPPORTS_SHAREDEXT is enabled. If no value is given `shared' is used
29 # by default.
30 confutils_init() {
31         if [[ ${EBUILD_SUPPORTS_SHAREDEXT} == 1 ]] && use sharedext; then
32                 shared="=${1:-shared}"
33         else
34                 shared=
35         fi
36 }
37
38 # @FUNCTION: confutils_require_one
39 # @USAGE: <flag> [more flags ...]
40 # @DESCRIPTION:
41 # Use this function to ensure exactly one of the specified USE flags have been
42 # enabled
43 confutils_require_one() {
44         local required_flags="$@"
45         local success=0
46
47         for flag in ${required_flags}; do
48                 use ${flag} && ((success++))
49         done
50
51         [[ ${success} -eq 1 ]] && return
52
53         echo
54         eerror "You *must* enable *exactly* one of the following USE flags:"
55         eerror "  ${required_flags}"
56         eerror
57         eerror "You can do this by enabling *one* of these flag in /etc/portage/package.use:"
58
59         set -- ${required_flags}
60         eerror "     =${CATEGORY}/${PN}-${PVR} ${1}"
61         shift
62
63         for flag in $@; do
64                 eerror "  OR =${CATEGORY}/${PN}-${PVR} ${flag}"
65         done
66
67         echo
68         die "Missing or conflicting USE flags"
69 }
70
71 # @FUNCTION: confutils_require_any
72 # @USAGE: <flag> [more flags ...]
73 # @DESCRIPTION:
74 # Use this function to ensure one or more of the specified USE flags have been
75 # enabled
76 confutils_require_any() {
77         local required_flags="$@"
78         local success=0
79
80         for flag in ${required_flags}; do
81                 use ${flag} && success=1
82         done
83
84         [[ ${success} -eq 1 ]] && return
85
86         echo
87         eerror "You *must* enable one or more of the following USE flags:"
88         eerror "  ${required_flags}"
89         eerror
90         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
91         eerror "    =${CATEGORY}/${PN}-${PVR} ${required_flags}"
92         echo
93         die "Missing USE flags"
94 }
95
96 # @FUNCTION: confutils_require_built_with_all
97 # @USAGE: <foreign> <flag> [more flags ...]
98 # @DESCRIPTION:
99 # Use this function to ensure all of the specified USE flags have been enabled
100 # in the specified foreign package
101 confutils_require_built_with_all() {
102         local foreign=$1 && shift
103         local required_flags="$@"
104
105         built_with_use ${foreign} ${required_flags} && return
106
107         echo
108         eerror "You *must* enable all of the following USE flags in ${foreign}:"
109         eerror "  ${required_flags}"
110         eerror
111         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
112         eerror "    ${foreign} ${required_flags}"
113         echo
114         die "Missing USE flags in ${foreign}"
115 }
116
117 # @FUNCTION: confutils_require_built_with_any
118 # @USAGE: <foreign> <flag> [more flags ...]
119 # @DESCRIPTION:
120 # Use this function to ensure one or more of the specified USE flags have been
121 # enabled in the specified foreign package
122 confutils_require_built_with_any() {
123         local foreign=$1 && shift
124         local required_flags="$@"
125         local success=0
126
127         for flag in ${required_flags}; do
128                 built_with_use ${foreign} ${flag} && success=1
129         done
130
131         [[ ${success} -eq 1 ]] && return
132
133         echo
134         eerror "You *must* enable one or more of the following USE flags in ${foreign}:"
135         eerror "  ${required_flags}"
136         eerror
137         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
138         eerror "    ${foreign} ${required_flags}"
139         echo
140         die "Missing USE flags in ${foreign}"
141 }
142
143 # @FUNCTION: confutils_use_conflict
144 # @USAGE: <enabled flag> <conflicting flag> [more conflicting flags ...]
145 # @DESCRIPTION:
146 # Use this function to automatically complain to the user if conflicting USE
147 # flags have been enabled
148 confutils_use_conflict() {
149         use $1 || return
150
151         local my_flag="$1" && shift
152         local my_present=
153         local my_remove=
154
155         for flag in "$@"; do
156                 if use ${flag}; then
157                         my_present="${my_present} ${flag}"
158                         my_remove="${my_remove} -${flag}"
159                 fi
160         done
161
162         [[ -z "${my_present}" ]] && return
163
164         echo
165         eerror "USE flag '${my_flag}' conflicts with these USE flag(s):"
166         eerror "  ${my_present}"
167         eerror
168         eerror "You must disable these conflicting flags before you can emerge this package."
169         eerror "You can do this by disabling these flags in /etc/portage/package.use:"
170         eerror "    =${CATEGORY}/${PN}-${PVR} ${my_remove}"
171         eerror
172         eerror "You could disable this flag instead in /etc/portage/package.use:"
173         eerror "    =${CATEGORY}/${PN}-${PVR} -${my_flag}"
174         echo
175         die "Conflicting USE flags"
176 }
177
178 # @FUNCTION: confutils_use_depend_all
179 # @USAGE: <enabled flag> <needed flag> [more needed flags ...]
180 # @DESCRIPTION:
181 # Use this function to automatically complain to the user if a USE flag depends
182 # on another USE flag that hasn't been enabled
183 confutils_use_depend_all() {
184         use $1 || return
185
186         local my_flag="$1" && shift
187         local my_missing=
188
189         for flag in "$@"; do
190                 use ${flag} || my_missing="${my_missing} ${flag}"
191         done
192
193         [[ -z "${my_missing}" ]] && return
194
195         echo
196         eerror "USE flag '${my_flag}' needs these additional flag(s) set:"
197         eerror "  ${my_missing}"
198         eerror
199         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
200         eerror "    =${CATEGORY}/${PN}-${PVR} ${my_missing}"
201         eerror
202         eerror "You could disable this flag instead in /etc/portage/package.use:"
203         eerror "    =${CATEGORY}/${PN}-${PVR} -${my_flag}"
204         echo
205         die "Need missing USE flags"
206 }
207
208 # @FUNCTION: confutils_use_depend_any
209 # @USAGE: <enabled flag> <needed flag> [more needed flags ...]
210 # @DESCRIPTION:
211 # Use this function to automatically complain to the user if a USE flag depends
212 # on another USE flag that hasn't been enabled
213 confutils_use_depend_any() {
214         use $1 || return
215
216         local my_flag="$1" && shift
217         local my_found=
218         local my_missing=
219
220         for flag in "$@"; do
221                 if use ${flag}; then
222                         my_found="${my_found} ${flag}"
223                 else
224                         my_missing="${my_missing} ${flag}"
225                 fi
226         done
227
228         [[ -n "${my_found}" ]] && return
229
230         echo
231         eerror "USE flag '${my_flag}' needs one or more of these additional flag(s) set:"
232         eerror "  ${my_missing}"
233         eerror
234         eerror "You can do this by enabling one of these flags in /etc/portage/package.use:"
235         eerror "    =${CATEGORY}/${PN}-${PVR} ${my_missing}"
236         eerror
237         eerror "You could disable this flag instead in /etc/portage/package.use:"
238         eerror "    =${CATEGORY}/${PN}-${PVR} -${my_flag}"
239         echo
240         die "Need missing USE flag(s)"
241 }
242
243 # @FUNCTION: confutils_use_depend_built_with_all
244 # @USAGE: <enabled flag> <foreign> <needed flag> [more needed flags ...]
245 # @DESCRIPTION:
246 # Use this function to automatically complain to the user if a USE flag depends
247 # on a USE flag in another package that hasn't been enabled
248 confutils_use_depend_built_with_all() {
249         use $1 || return
250
251         local my_flag="$1" && shift
252         local foreign=$1 && shift
253         local required_flags="$@"
254
255         built_with_use ${foreign} ${required_flags} && return
256
257         echo
258         eerror "USE flag '${my_flag}' needs the following USE flags in ${foreign}:"
259         eerror "  ${required_flags}"
260         eerror
261         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
262         eerror "    ${foreign} ${required_flags}"
263         eerror
264         eerror "You could disable this flag instead in /etc/portage/package.use:"
265         eerror "    =${CATEGORY}/${PN}-${PVR} -${my_flag}"
266         echo
267         die "Missing USE flags in ${foreign}"
268 }
269
270 # @FUNCTION: confutils_use_depend_built_with_any
271 # @USAGE: <enabled flag> <foreign> <needed flag> [more needed flags ...]
272 # @DESCRIPTION:
273 # Use this function to automatically complain to the user if a USE flag depends
274 # on a USE flag in another package that hasn't been enabled
275 confutils_use_depend_built_with_any() {
276         use $1 || return
277
278         local my_flag="$1" && shift
279         local foreign=$1 && shift
280         local required_flags="$@"
281         local success=0
282
283         for flag in ${required_flags}; do
284                 built_with_use ${foreign} ${flag} && success=1
285         done
286
287         [[ ${success} -eq 1 ]] && return
288
289         echo
290         eerror "USE flag '${my_flag}' needs one or more of the following USE flags in ${foreign}:"
291         eerror "  ${required_flags}"
292         eerror
293         eerror "You can do this by enabling these flags in /etc/portage/package.use:"
294         eerror "    ${foreign} ${required_flags}"
295         eerror
296         eerror "You could disable this flag instead in /etc/portage/package.use:"
297         eerror "    =${CATEGORY}/${PN}-${PVR} -${my_flag}"
298         echo
299         die "Missing USE flags in ${foreign}"
300 }
301
302
303 # internal function constructs the configure values for optional shared module
304 # support and extra arguments
305 _confutils_shared_suffix() {
306         local my_shared=
307
308         if [[ "$1" == "1" ]]; then
309                 if [[ -n "${shared}" ]]; then
310                         my_shared="${shared}"
311                         if [[ -n "$2" ]]; then
312                                 my_shared="${my_shared},$2"
313                         fi
314                 elif [[ -n "$2" ]]; then
315                         my_shared="=$2"
316                 fi
317         else
318                 if [[ -n "$2" ]]; then
319                         my_shared="=$2"
320                 fi
321         fi
322
323         echo "${my_shared}"
324 }
325
326 # @FUNCTION: enable_extension_disable
327 # @USAGE: <extension> <flag> [msg]
328 # @DESCRIPTION:
329 # Use this function to disable an extension that is enabled by default.  This is
330 # provided for those rare configure scripts that don't support a --enable for
331 # the corresponding --disable.
332 enable_extension_disable() {
333         local my_msg=${3:-$1}
334
335         if use "$2" ; then
336                 einfo "  Enabling ${my_msg}"
337         else
338                 my_conf="${my_conf} --disable-$1"
339                 einfo "  Disabling ${my_msg}"
340         fi
341 }
342
343 # @FUNCTION: enable_extension_enable
344 # @USAGE: <extension> <flag> [shared] [extra conf] [msg]
345 # @DESCRIPTION:
346 # This function is like use_enable(), except that it knows about enabling
347 # modules as shared libraries, and it supports passing additional data with the
348 # switch.
349 enable_extension_enable() {
350         local my_shared=$(_confutils_shared_suffix $3 $4)
351         local my_msg=${5:-$1}
352
353         if use $2; then
354                 my_conf="${my_conf} --enable-${1}${my_shared}"
355                 einfo "  Enabling ${my_msg}"
356         else
357                 my_conf="${my_conf} --disable-$1"
358                 einfo "  Disabling ${my_msg}"
359         fi
360 }
361
362 # @FUNCTION: enable_extension_enableonly
363 # @USAGE: <extension> <flag> [shared] [extra conf] [msg]
364 # @DESCRIPTION:
365 # This function is like use_enable(), except that it knows about enabling
366 # modules as shared libraries, and it supports passing additional data with the
367 # switch.  This function is provided for those rare configure scripts that support
368 # --enable but not the corresponding --disable.
369 enable_extension_enableonly() {
370         local my_shared=$(_confutils_shared_suffix $3 $4)
371         local my_msg=${5:-$1}
372
373         if use $2 ; then
374                 my_conf="${my_conf} --enable-${1}${my_shared}"
375                 einfo "  Enabling ${my_msg}"
376         else
377                 # note: we deliberately do *not* use a --disable switch here
378                 einfo "  Disabling ${my_msg}"
379         fi
380 }
381
382 # @FUNCTION: enable_extension_without
383 # @USAGE: <extension> <flag> [msg]
384 # @DESCRIPTION:
385 # Use this function to disable an extension that is enabled by default. This
386 # function is provided for those rare configure scripts that support --without
387 # but not the corresponding --with
388 enable_extension_without() {
389         local my_msg=${3:-$1}
390
391         if use "$2"; then
392                 einfo "  Enabling ${my_msg}"
393         else
394                 my_conf="${my_conf} --without-$1"
395                 einfo "  Disabling ${my_msg}"
396         fi
397 }
398
399 # @FUNCTION: enable_extension_with
400 # @USAGE: <extension> <flag> [shared] [extra conf] [msg]
401 # @DESCRIPTION:
402 # This function is like use_with(), except that it knows about enabling modules
403 # as shared libraries, and it supports passing additional data with the switch.
404 enable_extension_with() {
405         local my_shared=$(_confutils_shared_suffix $3 $4)
406         local my_msg=${5:-$1}
407
408         if use $2; then
409                 my_conf="${my_conf} --with-${1}${my_shared}"
410                 einfo "  Enabling ${my_msg}"
411         else
412                 my_conf="${my_conf} --without-$1"
413                 einfo "  Disabling ${my_msg}"
414         fi
415 }
416
417 # @FUNCTION: enable_extension_withonly
418 # @USAGE: <extension> <flag> [shared] [extra conf] [msg]
419 # @DESCRIPTION:
420 # This function is like use_with(), except that it knows about enabling modules
421 # as shared libraries, and it supports passing additional data with the switch.
422 # This function is provided for those rare configure scripts that support --enable
423 # but not the corresponding --disable.
424 enable_extension_withonly() {
425         local my_shared=$(_confutils_shared_suffix $3 $4)
426         local my_msg=${5:-$1}
427
428         if use $2; then
429                 my_conf="${my_conf} --with-${1}${my_shared}"
430                 einfo "  Enabling ${my_msg}"
431         else
432                 # note: we deliberately do *not* use a --without switch here
433                 einfo "  Disabling ${my_msg}"
434         fi
435 }
436
437 # @FUNCTION: enable_extension_enable_built_with
438 # @USAGE: <foreign> <flag> <extension> [shared] [extra conf] [msg]
439 # @DESCRIPTION:
440 # This function is like enable_extension_enable(), except that it
441 # enables/disables modules based on a USE flag in a foreign package.
442 enable_extension_enable_built_with() {
443         local my_shared=$(_confutils_shared_suffix $4 $5)
444         local my_msg=${6:-$3}
445
446         if built_with_use $1 $2; then
447                 my_conf="${my_conf} --enable-${3}${my_shared}"
448                 einfo "  Enabling ${my_msg}"
449         else
450                 my_conf="${my_conf} --disable-$3"
451                 einfo "  Disabling ${my_msg}"
452         fi
453 }
454
455 # @FUNCTION: enable_extension_with_built_with
456 # @USAGE: <foreign> <flag> <extension> [shared] [extra conf] [msg]
457 # @DESCRIPTION:
458 # This function is like enable_extension_with(), except that it
459 # enables/disables modules based on a USE flag in a foreign package.
460 enable_extension_with_built_with() {
461         # legacy workaround
462         if [[ "$4" != "0" && "$4" != "1" ]]; then
463                 enable_extension_with_built_with "$1" "$2" "$3" 0 "$4" "$5"
464                 return
465         fi
466
467         local my_shared=$(_confutils_shared_suffix $4 $5)
468         local my_msg=${6:-$3}
469
470         if built_with_use $1 $2; then
471                 my_conf="${my_conf} --with-${3}${my_shared}"
472                 einfo "  Enabling ${my_msg}"
473         else
474                 my_conf="${my_conf} --disable-$3"
475                 einfo "  Disabling ${my_msg}"
476         fi
477 }