dev-cpp/pangomm: stable 2.42.1 for hppa, bug #717144
[gentoo.git] / eclass / python-single-r1.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: python-single-r1.eclass
5 # @MAINTAINER:
6 # Python team <python@gentoo.org>
7 # @AUTHOR:
8 # Author: Michał Górny <mgorny@gentoo.org>
9 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
10 # @SUPPORTED_EAPIS: 5 6 7
11 # @BLURB: An eclass for Python packages not installed for multiple implementations.
12 # @DESCRIPTION:
13 # An extension of the python-r1 eclass suite for packages which
14 # don't support being installed for multiple Python implementations.
15 # This mostly includes tools embedding Python and packages using foreign
16 # build systems.
17 #
18 # This eclass sets correct IUSE.  It also provides PYTHON_DEPS
19 # and PYTHON_REQUIRED_USE that need to be added to appropriate ebuild
20 # metadata variables.
21 #
22 # The eclass exports PYTHON_SINGLE_USEDEP that is suitable for depending
23 # on other packages using the eclass.  Dependencies on packages using
24 # python-r1 should be created via python_gen_cond_dep() function,
25 # using PYTHON_USEDEP placeholder.
26 #
27 # Please note that packages support multiple Python implementations
28 # (using python-r1 eclass) can not depend on packages not supporting
29 # them (using this eclass).
30 #
31 # Please note that python-single-r1 will always inherit python-utils-r1
32 # as well. Thus, all the functions defined there can be used
33 # in the packages using python-single-r1, and there is no need ever
34 # to inherit both.
35 #
36 # For more information, please see the Python Guide:
37 # https://dev.gentoo.org/~mgorny/python-guide/
38
39 case "${EAPI:-0}" in
40         0|1|2|3|4)
41                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
42                 ;;
43         5|6|7)
44                 # EAPI=5 is required for sane USE_EXPAND dependencies
45                 ;;
46         *)
47                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
48                 ;;
49 esac
50
51 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
52
53 if [[ ${_PYTHON_R1} ]]; then
54         die 'python-single-r1.eclass can not be used with python-r1.eclass.'
55 elif [[ ${_PYTHON_ANY_R1} ]]; then
56         die 'python-single-r1.eclass can not be used with python-any-r1.eclass.'
57 fi
58
59 inherit python-utils-r1
60
61 fi
62
63 EXPORT_FUNCTIONS pkg_setup
64
65 # @ECLASS-VARIABLE: PYTHON_COMPAT
66 # @REQUIRED
67 # @DESCRIPTION:
68 # This variable contains a list of Python implementations the package
69 # supports. It must be set before the `inherit' call. It has to be
70 # an array.
71 #
72 # Example:
73 # @CODE
74 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
75 # @CODE
76 #
77 # Please note that you can also use bash brace expansion if you like:
78 # @CODE
79 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
80 # @CODE
81
82 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
83 # @INTERNAL
84 # @DESCRIPTION:
85 # This variable can be used when working with ebuilds to override
86 # the in-ebuild PYTHON_COMPAT. It is a string naming the implementation
87 # which package will be built for. It needs to be specified
88 # in the calling environment, and not in ebuilds.
89 #
90 # It should be noted that in order to preserve metadata immutability,
91 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
92 # The state of PYTHON_SINGLE_TARGET is ignored, and the implementation
93 # in PYTHON_COMPAT_OVERRIDE is built instead.  Dependencies need to be
94 # satisfied manually.
95 #
96 # Example:
97 # @CODE
98 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
99 # @CODE
100
101 # @ECLASS-VARIABLE: PYTHON_REQ_USE
102 # @DEFAULT_UNSET
103 # @DESCRIPTION:
104 # The list of USEflags required to be enabled on the chosen Python
105 # implementations, formed as a USE-dependency string. It should be valid
106 # for all implementations in PYTHON_COMPAT, so it may be necessary to
107 # use USE defaults.
108 #
109 # This should be set before calling `inherit'.
110 #
111 # Example:
112 # @CODE
113 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
114 # @CODE
115 #
116 # It will cause the Python dependencies to look like:
117 # @CODE
118 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
119 # @CODE
120
121 # @ECLASS-VARIABLE: PYTHON_DEPS
122 # @DESCRIPTION:
123 # This is an eclass-generated Python dependency string for all
124 # implementations listed in PYTHON_COMPAT.
125 #
126 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
127 #
128 # Example use:
129 # @CODE
130 # RDEPEND="${PYTHON_DEPS}
131 #       dev-foo/mydep"
132 # DEPEND="${RDEPEND}"
133 # @CODE
134 #
135 # Example value:
136 # @CODE
137 # dev-lang/python-exec:=
138 # python_single_target_python2_7? ( dev-lang/python:2.7[gdbm] )
139 # python_single_target_pypy? ( dev-python/pypy[gdbm] )
140 # @CODE
141
142 # @ECLASS-VARIABLE: PYTHON_SINGLE_USEDEP
143 # @DESCRIPTION:
144 # This is an eclass-generated USE-dependency string which can be used to
145 # depend on another python-single-r1 package being built for the same
146 # Python implementations.
147 #
148 # If you need to depend on a multi-impl (python-r1) package, use
149 # python_gen_cond_dep with PYTHON_USEDEP placeholder instead.
150 #
151 # Example use:
152 # @CODE
153 # RDEPEND="dev-python/foo[${PYTHON_SINGLE_USEDEP}]"
154 # @CODE
155 #
156 # Example value:
157 # @CODE
158 # python_single_target_python3_4(-)?
159 # @CODE
160
161 # @ECLASS-VARIABLE: PYTHON_USEDEP
162 # @DESCRIPTION:
163 # This is a placeholder variable supported by python_gen_cond_dep,
164 # in order to depend on python-r1 packages built for the same Python
165 # implementations.
166 #
167 # Example use:
168 # @CODE
169 # RDEPEND="$(python_gen_cond_dep '
170 #     dev-python/foo[${PYTHON_USEDEP}]
171 #   ')"
172 # @CODE
173 #
174 # Example value:
175 # @CODE
176 # python_targets_python3_4(-)
177 # @CODE
178
179 # @ECLASS-VARIABLE: PYTHON_MULTI_USEDEP
180 # @DESCRIPTION:
181 # This is a backwards-compatibility placeholder.  Use PYTHON_USEDEP
182 # instead.
183
184 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
185 # @DESCRIPTION:
186 # This is an eclass-generated required-use expression which ensures
187 # that exactly one PYTHON_SINGLE_TARGET value has been enabled.
188 #
189 # This expression should be utilized in an ebuild by including it in
190 # REQUIRED_USE, optionally behind a use flag.
191 #
192 # Example use:
193 # @CODE
194 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
195 # @CODE
196 #
197 # Example value:
198 # @CODE
199 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
200 # @CODE
201
202 _python_single_set_globals() {
203         _python_set_impls
204
205         local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
206
207         if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
208                 # if only one implementation is supported, use IUSE defaults
209                 # to avoid requesting the user to enable it
210                 IUSE="+${flags[0]}"
211         else
212                 IUSE="${flags[*]}"
213         fi
214
215         local requse="^^ ( ${flags[*]} )"
216         local single_flags="${flags[@]/%/(-)?}"
217         local single_usedep=${single_flags// /,}
218
219         local deps= i PYTHON_PKG_DEP
220         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
221                 _python_export "${i}" PYTHON_PKG_DEP
222                 # 1) well, python-exec would suffice as an RDEP
223                 # but no point in making this overcomplex, BDEP doesn't hurt anyone
224                 # 2) python-exec should be built with all targets forced anyway
225                 # but if new targets were added, we may need to force a rebuild
226                 deps+="python_single_target_${i}? (
227                         ${PYTHON_PKG_DEP}
228                         >=dev-lang/python-exec-2:=[python_targets_${i}]
229                 ) "
230         done
231
232         if [[ ${PYTHON_DEPS+1} ]]; then
233                 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
234                         eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
235                         eerror "Before: ${PYTHON_DEPS}"
236                         eerror "Now   : ${deps}"
237                         die "PYTHON_DEPS integrity check failed"
238                 fi
239
240                 # these two are formality -- they depend on PYTHON_COMPAT only
241                 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
242                         eerror "PYTHON_REQUIRED_USE have changed between inherits!"
243                         eerror "Before: ${PYTHON_REQUIRED_USE}"
244                         eerror "Now   : ${requse}"
245                         die "PYTHON_REQUIRED_USE integrity check failed"
246                 fi
247
248                 if [[ ${PYTHON_SINGLE_USEDEP} != "${single_usedep}" ]]; then
249                         eerror "PYTHON_SINGLE_USEDEP have changed between inherits!"
250                         eerror "Before: ${PYTHON_SINGLE_USEDEP}"
251                         eerror "Now   : ${single_usedep}"
252                         die "PYTHON_SINGLE_USEDEP integrity check failed"
253                 fi
254         else
255                 PYTHON_DEPS=${deps}
256                 PYTHON_REQUIRED_USE=${requse}
257                 PYTHON_USEDEP='%PYTHON_USEDEP-NEEDS-TO-BE-USED-IN-PYTHON_GEN_COND_DEP%'
258                 PYTHON_SINGLE_USEDEP=${single_usedep}
259                 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_SINGLE_USEDEP \
260                         PYTHON_USEDEP
261         fi
262 }
263 _python_single_set_globals
264 unset -f _python_single_set_globals
265
266 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
267
268 # @FUNCTION: _python_gen_usedep
269 # @INTERNAL
270 # @USAGE: [<pattern>...]
271 # @DESCRIPTION:
272 # Output a USE dependency string for Python implementations which
273 # are both in PYTHON_COMPAT and match any of the patterns passed
274 # as parameters to the function.
275 #
276 # The patterns can be either fnmatch-style patterns (matched via bash
277 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
278 # appropriately all enabled Python 2/3 implementations (alike
279 # python_is_python3). Remember to escape or quote the fnmatch patterns
280 # to prevent accidental shell filename expansion.
281 #
282 # This is an internal function used to implement python_gen_cond_dep.
283 _python_gen_usedep() {
284         debug-print-function ${FUNCNAME} "${@}"
285
286         local impl matches=()
287
288         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
289                 if _python_impl_matches "${impl}" "${@}"; then
290                         matches+=(
291                                 "python_single_target_${impl}(-)?"
292                         )
293                 fi
294         done
295
296         [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
297
298         local out=${matches[@]}
299         echo "${out// /,}"
300 }
301
302 # @FUNCTION: python_gen_useflags
303 # @USAGE: [<pattern>...]
304 # @DESCRIPTION:
305 # Output a list of USE flags for Python implementations which
306 # are both in PYTHON_COMPAT and match any of the patterns passed
307 # as parameters to the function.
308 #
309 # The patterns can be either fnmatch-style patterns (matched via bash
310 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
311 # appropriately all enabled Python 2/3 implementations (alike
312 # python_is_python3). Remember to escape or quote the fnmatch patterns
313 # to prevent accidental shell filename expansion.
314 #
315 # Example:
316 # @CODE
317 # PYTHON_COMPAT=( python{2_7,3_4} )
318 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
319 # @CODE
320 #
321 # It will cause the variable to look like:
322 # @CODE
323 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
324 # @CODE
325 python_gen_useflags() {
326         debug-print-function ${FUNCNAME} "${@}"
327
328         local impl matches=()
329
330         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
331                 if _python_impl_matches "${impl}" "${@}"; then
332                         matches+=( "python_single_target_${impl}" )
333                 fi
334         done
335
336         echo "${matches[@]}"
337 }
338
339 # @FUNCTION: python_gen_cond_dep
340 # @USAGE: <dependency> [<pattern>...]
341 # @DESCRIPTION:
342 # Output a list of <dependency>-ies made conditional to USE flags
343 # of Python implementations which are both in PYTHON_COMPAT and match
344 # any of the patterns passed as the remaining parameters.
345 #
346 # The patterns can be either fnmatch-style patterns (matched via bash
347 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
348 # appropriately all enabled Python 2/3 implementations (alike
349 # python_is_python3). Remember to escape or quote the fnmatch patterns
350 # to prevent accidental shell filename expansion.
351 #
352 # In order to enforce USE constraints on the packages, verbatim
353 # '${PYTHON_SINGLE_USEDEP}' and '${PYTHON_USEDEP}' (quoted!) may
354 # be placed in the dependency specification. It will get expanded within
355 # the function into a proper USE dependency string.
356 #
357 # Example:
358 # @CODE
359 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
360 # RDEPEND="$(python_gen_cond_dep \
361 #   'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
362 # @CODE
363 #
364 # It will cause the variable to look like:
365 # @CODE
366 # RDEPEND="python_single_target_python2_7? (
367 #     dev-python/unittest2[python_targets_python2_7(-)?,...] )
368 #       python_single_target_pypy? (
369 #     dev-python/unittest2[python_targets_pypy(-)?,...] )"
370 # @CODE
371 python_gen_cond_dep() {
372         debug-print-function ${FUNCNAME} "${@}"
373
374         local impl matches=()
375
376         local dep=${1}
377         shift
378
379         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
380                 if _python_impl_matches "${impl}" "${@}"; then
381                         # substitute ${PYTHON_SINGLE_USEDEP} if used
382                         # (since python_gen_usedep() will not return
383                         #  ${PYTHON_SINGLE_USEDEP}, the code is run at most once)
384                         if [[ ${dep} == *'${PYTHON_SINGLE_USEDEP}'* ]]; then
385                                 local usedep=$(_python_gen_usedep "${@}")
386                                 dep=${dep//\$\{PYTHON_SINGLE_USEDEP\}/${usedep}}
387                         fi
388                         local multi_usedep="python_targets_${impl}(-)"
389
390                         local subdep=${dep//\$\{PYTHON_MULTI_USEDEP\}/${multi_usedep}}
391                         matches+=( "python_single_target_${impl}? (
392                                 ${subdep//\$\{PYTHON_USEDEP\}/${multi_usedep}} )" )
393                 fi
394         done
395
396         echo "${matches[@]}"
397 }
398
399 # @FUNCTION: python_gen_impl_dep
400 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
401 # @DESCRIPTION:
402 # Output a dependency on Python implementations with the specified USE
403 # dependency string appended, or no USE dependency string if called
404 # without the argument (or with empty argument). If any implementation
405 # patterns are passed, the output dependencies will be generated only
406 # for the implementations matching them.
407 #
408 # The patterns can be either fnmatch-style patterns (matched via bash
409 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
410 # appropriately all enabled Python 2/3 implementations (alike
411 # python_is_python3). Remember to escape or quote the fnmatch patterns
412 # to prevent accidental shell filename expansion.
413 #
414 # Use this function when you need to request different USE flags
415 # on the Python interpreter depending on package's USE flags. If you
416 # only need a single set of interpreter USE flags, just set
417 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
418 #
419 # Example:
420 # @CODE
421 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
422 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
423 # @CODE
424 #
425 # It will cause the variable to look like:
426 # @CODE
427 # RDEPEND="foo? (
428 #   python_single_target_python2_7? (
429 #     dev-lang/python:2.7[xml(+)] )
430 #       python_single_target_pypy? (
431 #     dev-python/pypy[xml(+)] ) )"
432 # @CODE
433 python_gen_impl_dep() {
434         debug-print-function ${FUNCNAME} "${@}"
435
436         local impl
437         local matches=()
438
439         local PYTHON_REQ_USE=${1}
440         shift
441
442         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
443                 if _python_impl_matches "${impl}" "${@}"; then
444                         local PYTHON_PKG_DEP
445                         _python_export "${impl}" PYTHON_PKG_DEP
446                         matches+=( "python_single_target_${impl}? ( ${PYTHON_PKG_DEP} )" )
447                 fi
448         done
449
450         echo "${matches[@]}"
451 }
452
453 # @FUNCTION: python_setup
454 # @DESCRIPTION:
455 # Determine what the selected Python implementation is and set
456 # the Python build environment up for it.
457 python_setup() {
458         debug-print-function ${FUNCNAME} "${@}"
459
460         unset EPYTHON
461
462         # support developer override
463         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
464                 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
465                 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
466
467                 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
468                 ewarn "implementation will be used:"
469                 ewarn
470                 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
471                 ewarn
472                 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
473
474                 _python_export "${impls[0]}" EPYTHON PYTHON
475                 _python_wrapper_setup
476                 einfo "Using ${EPYTHON} to build"
477                 return
478         fi
479
480         local impl
481         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
482                 if use "python_single_target_${impl}"; then
483                         if [[ ${EPYTHON} ]]; then
484                                 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
485                                 eerror "implementation. Please set it to just one value. If you need"
486                                 eerror "to override the value for a single package, please use package.env"
487                                 eerror "or an equivalent solution (man 5 portage)."
488                                 echo
489                                 die "More than one implementation in PYTHON_SINGLE_TARGET."
490                         fi
491
492                         _python_export "${impl}" EPYTHON PYTHON
493                         _python_wrapper_setup
494                         einfo "Using ${EPYTHON} to build"
495                 fi
496         done
497
498         if [[ ! ${EPYTHON} ]]; then
499                 eerror "No Python implementation selected for the build. Please set"
500                 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
501                 eerror "of the following values:"
502                 eerror
503                 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
504                 echo
505                 die "No supported Python implementation in PYTHON_SINGLE_TARGET."
506         fi
507 }
508
509 # @FUNCTION: python-single-r1_pkg_setup
510 # @DESCRIPTION:
511 # Runs python_setup.
512 python-single-r1_pkg_setup() {
513         debug-print-function ${FUNCNAME} "${@}"
514
515         [[ ${MERGE_TYPE} != binary ]] && python_setup
516 }
517
518 _PYTHON_SINGLE_R1=1
519 fi