1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: python-single-r1.eclass
6 # Python team <python@gentoo.org>
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.
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
18 # This eclass sets correct IUSE. It also provides PYTHON_DEPS
19 # and PYTHON_REQUIRED_USE that need to be added to appropriate ebuild
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_MULTI_USEDEP placeholder.
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).
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
36 # For more information, please see the wiki:
37 # https://wiki.gentoo.org/wiki/Project:Python/python-single-r1
41 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
44 # EAPI=5 is required for sane USE_EXPAND dependencies
47 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
51 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
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.'
59 inherit python-utils-r1
63 EXPORT_FUNCTIONS pkg_setup
65 # @ECLASS-VARIABLE: PYTHON_COMPAT
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
74 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
77 # Please note that you can also use bash brace expansion if you like:
79 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
82 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
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.
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_TARGETS and PYTHON_SINGLE_TARGET is ignored,
93 # and the implementation in PYTHON_COMPAT_OVERRIDE is built instead.
94 # Dependencies need to be satisfied manually.
98 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
101 # @ECLASS-VARIABLE: PYTHON_REQ_USE
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
109 # This should be set before calling `inherit'.
113 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
116 # It will cause the Python dependencies to look like:
118 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
121 # @ECLASS-VARIABLE: PYTHON_DEPS
123 # This is an eclass-generated Python dependency string for all
124 # implementations listed in PYTHON_COMPAT.
126 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
130 # RDEPEND="${PYTHON_DEPS}
132 # DEPEND="${RDEPEND}"
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] )
142 # @ECLASS-VARIABLE: PYTHON_USEDEP
144 # DEPRECATED. Use PYTHON_SINGLE_USEDEP or python_gen_cond_dep with
145 # PYTHON_MULTI_USEDEP placeholder instead.
147 # This is an eclass-generated USE-dependency string which can be used to
148 # depend on another Python package being built for the same Python
151 # The generate USE-flag list is compatible with packages using python-r1,
152 # python-single-r1 and python-distutils-ng eclasses. It must not be used
153 # on packages using python.eclass.
157 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
162 # python_targets_python2_7(-)?,python_single_target_python3_4(+)?
165 # @ECLASS-VARIABLE: PYTHON_SINGLE_USEDEP
167 # This is an eclass-generated USE-dependency string which can be used to
168 # depend on another python-single-r1 package being built for the same
169 # Python implementations.
171 # If you need to depend on a multi-impl (python-r1) package, use
172 # python_gen_cond_dep with PYTHON_MULTI_USEDEP placeholder instead.
176 # RDEPEND="dev-python/foo[${PYTHON_SINGLE_USEDEP}]"
181 # python_single_target_python3_4(-)?
184 # @ECLASS-VARIABLE: PYTHON_MULTI_USEDEP
186 # This is a placeholder variable supported by python_gen_cond_dep,
187 # in order to depend on python-r1 packages built for the same Python
192 # RDEPEND="$(python_gen_cond_dep '
193 # dev-python/foo[${PYTHON_MULTI_USEDEP}]
199 # python_targets_python3_4(-)
202 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
204 # This is an eclass-generated required-use expression which ensures the following
205 # when more than one python implementation is possible:
206 # 1. Exactly one PYTHON_SINGLE_TARGET value has been enabled.
207 # 2. The selected PYTHON_SINGLE_TARGET value is enabled in PYTHON_TARGETS.
209 # This expression should be utilized in an ebuild by including it in
210 # REQUIRED_USE, optionally behind a use flag.
214 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
219 # python_single_target_python2_7? ( python_targets_python2_7 )
220 # python_single_target_python3_3? ( python_targets_python3_3 )
221 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
224 _python_single_set_globals() {
227 local flags_mt=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_targets_}" )
228 local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
229 local unflags=( "${_PYTHON_UNSUPPORTED_IMPLS[@]/#/-python_single_target_}" )
231 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
232 # if only one implementation is supported, use IUSE defaults
233 # to avoid requesting the user to enable it
234 IUSE="+${flags_mt[0]} +${flags[0]}"
236 IUSE="${flags_mt[*]} ${flags[*]}"
239 local requse="^^ ( ${flags[*]} )"
240 local optflags="${flags_mt[@]/%/(-)?},${unflags[@]/%/(-)},${flags[@]/%/(+)?}"
241 local usedep="${optflags// /,}"
242 local single_flags="${flags[@]/%/(-)?}"
243 local single_usedep=${single_flags// /,}
245 local deps= i PYTHON_PKG_DEP
246 for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
247 # The chosen targets need to be in PYTHON_TARGETS as well.
248 # This is in order to enforce correct dependencies on packages
249 # supporting multiple implementations.
250 requse+=" python_single_target_${i}? ( python_targets_${i} )"
252 python_export "${i}" PYTHON_PKG_DEP
253 # 1) well, python-exec would suffice as an RDEP
254 # but no point in making this overcomplex, BDEP doesn't hurt anyone
255 # 2) python-exec should be built with all targets forced anyway
256 # but if new targets were added, we may need to force a rebuild
257 deps+="python_single_target_${i}? (
259 >=dev-lang/python-exec-2:=[python_targets_${i}]
263 if [[ ${PYTHON_DEPS+1} ]]; then
264 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
265 eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
266 eerror "Before: ${PYTHON_DEPS}"
267 eerror "Now : ${deps}"
268 die "PYTHON_DEPS integrity check failed"
271 # these two are formality -- they depend on PYTHON_COMPAT only
272 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
273 eerror "PYTHON_REQUIRED_USE have changed between inherits!"
274 eerror "Before: ${PYTHON_REQUIRED_USE}"
275 eerror "Now : ${requse}"
276 die "PYTHON_REQUIRED_USE integrity check failed"
279 if [[ ${PYTHON_USEDEP} != "${usedep}" ]]; then
280 eerror "PYTHON_USEDEP have changed between inherits!"
281 eerror "Before: ${PYTHON_USEDEP}"
282 eerror "Now : ${usedep}"
283 die "PYTHON_USEDEP integrity check failed"
286 if [[ ${PYTHON_SINGLE_USEDEP} != "${single_usedep}" ]]; then
287 eerror "PYTHON_SINGLE_USEDEP have changed between inherits!"
288 eerror "Before: ${PYTHON_SINGLE_USEDEP}"
289 eerror "Now : ${single_usedep}"
290 die "PYTHON_SINGLE_USEDEP integrity check failed"
294 PYTHON_REQUIRED_USE=${requse}
295 PYTHON_USEDEP=${usedep}
296 PYTHON_SINGLE_USEDEP=${single_usedep}
297 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_USEDEP \
301 _python_single_set_globals
302 unset -f _python_single_set_globals
304 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
306 # @FUNCTION: _python_gen_usedep
308 # @USAGE: <-s|-u> [<pattern>...]
310 # Output a USE dependency string for Python implementations which
311 # are both in PYTHON_COMPAT and match any of the patterns passed
312 # as parameters to the function.
314 # The first argument specifies USE-dependency type: '-s' for new-style
315 # PYTHON_SINGLE_USEDEP, '-u' for backwards-compatible PYTHON_USEDEP.
317 # The patterns can be either fnmatch-style patterns (matched via bash
318 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
319 # appropriately all enabled Python 2/3 implementations (alike
320 # python_is_python3). Remember to escape or quote the fnmatch patterns
321 # to prevent accidental shell filename expansion.
323 # This is an internal function used to implement python_gen_cond_dep
324 # and deprecated python_gen_usedep.
325 _python_gen_usedep() {
326 debug-print-function ${FUNCNAME} "${@}"
330 local impl matches=()
332 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
333 if _python_impl_matches "${impl}" "${@}"; then
337 "python_single_target_${impl}(-)?"
342 "python_targets_${impl}(-)?"
343 "python_single_target_${impl}(+)?"
350 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
352 local out=${matches[@]}
356 # @FUNCTION: python_gen_usedep
357 # @USAGE: <pattern> [...]
359 # DEPRECATED. Please use python_gen_cond_dep instead.
361 # Output a USE dependency string for Python implementations which
362 # are both in PYTHON_COMPAT and match any of the patterns passed
363 # as parameters to the function.
365 # The patterns can be either fnmatch-style patterns (matched via bash
366 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
367 # appropriately all enabled Python 2/3 implementations (alike
368 # python_is_python3). Remember to escape or quote the fnmatch patterns
369 # to prevent accidental shell filename expansion.
371 # When all implementations are requested, please use ${PYTHON_USEDEP}
372 # instead. Please also remember to set an appropriate REQUIRED_USE
373 # to avoid ineffective USE flags.
377 # PYTHON_COMPAT=( python{2_7,3_4} )
378 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
381 # It will cause the dependency to look like:
383 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7(-)?,...] )"
385 python_gen_usedep() {
386 debug-print-function ${FUNCNAME} "${@}"
388 # output only once, during some reasonable phase
389 # (avoid spamming cache regen runs)
390 if [[ ${EBUILD_PHASE} == setup ]]; then
391 eqawarn "python_gen_usedep() is deprecated. Please use python_gen_cond_dep instead."
393 _python_gen_usedep -u "${@}"
396 # @FUNCTION: python_gen_useflags
397 # @USAGE: [<pattern>...]
399 # Output a list of USE flags for Python implementations which
400 # are both in PYTHON_COMPAT and match any of the patterns passed
401 # as parameters to the function.
403 # The patterns can be either fnmatch-style patterns (matched via bash
404 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
405 # appropriately all enabled Python 2/3 implementations (alike
406 # python_is_python3). Remember to escape or quote the fnmatch patterns
407 # to prevent accidental shell filename expansion.
411 # PYTHON_COMPAT=( python{2_7,3_4} )
412 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
415 # It will cause the variable to look like:
417 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
419 python_gen_useflags() {
420 debug-print-function ${FUNCNAME} "${@}"
422 local impl matches=()
424 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
425 if _python_impl_matches "${impl}" "${@}"; then
426 matches+=( "python_single_target_${impl}" )
433 # @FUNCTION: python_gen_cond_dep
434 # @USAGE: <dependency> [<pattern>...]
436 # Output a list of <dependency>-ies made conditional to USE flags
437 # of Python implementations which are both in PYTHON_COMPAT and match
438 # any of the patterns passed as the remaining parameters.
440 # The patterns can be either fnmatch-style patterns (matched via bash
441 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
442 # appropriately all enabled Python 2/3 implementations (alike
443 # python_is_python3). Remember to escape or quote the fnmatch patterns
444 # to prevent accidental shell filename expansion.
446 # In order to enforce USE constraints on the packages, verbatim
447 # '${PYTHON_USEDEP}', '${PYTHON_SINGLE_USEDEP}'
448 # and '${PYTHON_MULTI_USEDEP}' (quoted!) may be placed in the dependency
449 # specification. It will get expanded within the function into a proper
450 # USE dependency string.
454 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
455 # RDEPEND="$(python_gen_cond_dep \
456 # 'dev-python/unittest2[${PYTHON_MULTI_USEDEP}]' python2_7 pypy )"
459 # It will cause the variable to look like:
461 # RDEPEND="python_single_target_python2_7? (
462 # dev-python/unittest2[python_targets_python2_7(-)?,...] )
463 # python_single_target_pypy? (
464 # dev-python/unittest2[python_targets_pypy(-)?,...] )"
466 python_gen_cond_dep() {
467 debug-print-function ${FUNCNAME} "${@}"
469 local impl matches=()
474 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
475 if _python_impl_matches "${impl}" "${@}"; then
476 # substitute ${PYTHON_USEDEP} if used
477 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
478 # the code is run at most once)
479 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
480 local usedep=$(_python_gen_usedep -u "${@}")
481 dep=${dep//\$\{PYTHON_USEDEP\}/${usedep}}
483 if [[ ${dep} == *'${PYTHON_SINGLE_USEDEP}'* ]]; then
484 local usedep=$(_python_gen_usedep -s "${@}")
485 dep=${dep//\$\{PYTHON_SINGLE_USEDEP\}/${usedep}}
487 local multi_usedep="python_targets_${impl}(-)"
489 matches+=( "python_single_target_${impl}? (
490 ${dep//\$\{PYTHON_MULTI_USEDEP\}/${multi_usedep}} )" )
497 # @FUNCTION: python_gen_impl_dep
498 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
500 # Output a dependency on Python implementations with the specified USE
501 # dependency string appended, or no USE dependency string if called
502 # without the argument (or with empty argument). If any implementation
503 # patterns are passed, the output dependencies will be generated only
504 # for the implementations matching them.
506 # The patterns can be either fnmatch-style patterns (matched via bash
507 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
508 # appropriately all enabled Python 2/3 implementations (alike
509 # python_is_python3). Remember to escape or quote the fnmatch patterns
510 # to prevent accidental shell filename expansion.
512 # Use this function when you need to request different USE flags
513 # on the Python interpreter depending on package's USE flags. If you
514 # only need a single set of interpreter USE flags, just set
515 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
519 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
520 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
523 # It will cause the variable to look like:
526 # python_single_target_python2_7? (
527 # dev-lang/python:2.7[xml(+)] )
528 # python_single_target_pypy? (
529 # dev-python/pypy[xml(+)] ) )"
531 python_gen_impl_dep() {
532 debug-print-function ${FUNCNAME} "${@}"
537 local PYTHON_REQ_USE=${1}
540 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
541 if _python_impl_matches "${impl}" "${@}"; then
543 python_export "${impl}" PYTHON_PKG_DEP
544 matches+=( "python_single_target_${impl}? ( ${PYTHON_PKG_DEP} )" )
551 # @FUNCTION: python_setup
553 # Determine what the selected Python implementation is and set
554 # the Python build environment up for it.
556 debug-print-function ${FUNCNAME} "${@}"
560 # support developer override
561 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
562 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
563 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
565 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
566 ewarn "implementation will be used:"
568 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
570 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
572 python_export "${impls[0]}" EPYTHON PYTHON
578 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
579 if use "python_single_target_${impl}"; then
580 if [[ ${EPYTHON} ]]; then
581 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
582 eerror "implementation. Please set it to just one value. If you need"
583 eerror "to override the value for a single package, please use package.env"
584 eerror "or an equivalent solution (man 5 portage)."
586 die "More than one implementation in PYTHON_SINGLE_TARGET."
589 if ! use "python_targets_${impl}"; then
590 eerror "The implementation chosen as PYTHON_SINGLE_TARGET must be added"
591 eerror "to PYTHON_TARGETS as well. This is in order to ensure that"
592 eerror "dependencies are satisfied correctly. We're sorry"
593 eerror "for the inconvenience."
595 die "Build target (${impl}) not in PYTHON_TARGETS."
598 python_export "${impl}" EPYTHON PYTHON
603 if [[ ! ${EPYTHON} ]]; then
604 eerror "No Python implementation selected for the build. Please set"
605 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
606 eerror "of the following values:"
608 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
610 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
614 # @FUNCTION: python-single-r1_pkg_setup
617 python-single-r1_pkg_setup() {
618 debug-print-function ${FUNCNAME} "${@}"
620 [[ ${MERGE_TYPE} != binary ]] && python_setup