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? ( virtual/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 deps+="python_single_target_${i}? ( ${PYTHON_PKG_DEP} ) "
256 # 1) well, python-exec would suffice as an RDEP
257 # but no point in making this overcomplex, BDEP doesn't hurt anyone
258 # 2) python-exec should be built with all targets forced anyway
259 # but if new targets were added, we may need to force a rebuild
260 deps+=">=dev-lang/python-exec-2:=[${usedep}]"
262 if [[ ${PYTHON_DEPS+1} ]]; then
263 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
264 eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
265 eerror "Before: ${PYTHON_DEPS}"
266 eerror "Now : ${deps}"
267 die "PYTHON_DEPS integrity check failed"
270 # these two are formality -- they depend on PYTHON_COMPAT only
271 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
272 eerror "PYTHON_REQUIRED_USE have changed between inherits!"
273 eerror "Before: ${PYTHON_REQUIRED_USE}"
274 eerror "Now : ${requse}"
275 die "PYTHON_REQUIRED_USE integrity check failed"
278 if [[ ${PYTHON_USEDEP} != "${usedep}" ]]; then
279 eerror "PYTHON_USEDEP have changed between inherits!"
280 eerror "Before: ${PYTHON_USEDEP}"
281 eerror "Now : ${usedep}"
282 die "PYTHON_USEDEP integrity check failed"
285 if [[ ${PYTHON_SINGLE_USEDEP} != "${single_usedep}" ]]; then
286 eerror "PYTHON_SINGLE_USEDEP have changed between inherits!"
287 eerror "Before: ${PYTHON_SINGLE_USEDEP}"
288 eerror "Now : ${single_usedep}"
289 die "PYTHON_SINGLE_USEDEP integrity check failed"
293 PYTHON_REQUIRED_USE=${requse}
294 PYTHON_USEDEP=${usedep}
295 PYTHON_SINGLE_USEDEP=${single_usedep}
296 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_USEDEP \
300 _python_single_set_globals
301 unset -f _python_single_set_globals
303 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
305 # @FUNCTION: _python_gen_usedep
307 # @USAGE: <-s|-u> [<pattern>...]
309 # Output a USE dependency string for Python implementations which
310 # are both in PYTHON_COMPAT and match any of the patterns passed
311 # as parameters to the function.
313 # The first argument specifies USE-dependency type: '-s' for new-style
314 # PYTHON_SINGLE_USEDEP, '-u' for backwards-compatible PYTHON_USEDEP.
316 # The patterns can be either fnmatch-style patterns (matched via bash
317 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
318 # appropriately all enabled Python 2/3 implementations (alike
319 # python_is_python3). Remember to escape or quote the fnmatch patterns
320 # to prevent accidental shell filename expansion.
322 # This is an internal function used to implement python_gen_cond_dep
323 # and deprecated python_gen_usedep.
324 _python_gen_usedep() {
325 debug-print-function ${FUNCNAME} "${@}"
329 local impl matches=()
331 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
332 if _python_impl_matches "${impl}" "${@}"; then
336 "python_single_target_${impl}(-)?"
341 "python_targets_${impl}(-)?"
342 "python_single_target_${impl}(+)?"
349 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
351 local out=${matches[@]}
355 # @FUNCTION: python_gen_usedep
356 # @USAGE: <pattern> [...]
358 # DEPRECATED. Please use python_gen_cond_dep instead.
360 # Output a USE dependency string for Python implementations which
361 # are both in PYTHON_COMPAT and match any of the patterns passed
362 # as parameters to the function.
364 # The patterns can be either fnmatch-style patterns (matched via bash
365 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
366 # appropriately all enabled Python 2/3 implementations (alike
367 # python_is_python3). Remember to escape or quote the fnmatch patterns
368 # to prevent accidental shell filename expansion.
370 # When all implementations are requested, please use ${PYTHON_USEDEP}
371 # instead. Please also remember to set an appropriate REQUIRED_USE
372 # to avoid ineffective USE flags.
376 # PYTHON_COMPAT=( python{2_7,3_4} )
377 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
380 # It will cause the dependency to look like:
382 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7(-)?,...] )"
384 python_gen_usedep() {
385 debug-print-function ${FUNCNAME} "${@}"
387 # output only once, during some reasonable phase
388 # (avoid spamming cache regen runs)
389 if [[ ${EBUILD_PHASE} == setup ]]; then
390 eqawarn "python_gen_usedep() is deprecated. Please use python_gen_cond_dep instead."
392 _python_gen_usedep -u "${@}"
395 # @FUNCTION: python_gen_useflags
396 # @USAGE: [<pattern>...]
398 # Output a list of USE flags for Python implementations which
399 # are both in PYTHON_COMPAT and match any of the patterns passed
400 # as parameters to the function.
402 # The patterns can be either fnmatch-style patterns (matched via bash
403 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
404 # appropriately all enabled Python 2/3 implementations (alike
405 # python_is_python3). Remember to escape or quote the fnmatch patterns
406 # to prevent accidental shell filename expansion.
410 # PYTHON_COMPAT=( python{2_7,3_4} )
411 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
414 # It will cause the variable to look like:
416 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
418 python_gen_useflags() {
419 debug-print-function ${FUNCNAME} "${@}"
421 local impl matches=()
423 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
424 if _python_impl_matches "${impl}" "${@}"; then
425 matches+=( "python_single_target_${impl}" )
432 # @FUNCTION: python_gen_cond_dep
433 # @USAGE: <dependency> [<pattern>...]
435 # Output a list of <dependency>-ies made conditional to USE flags
436 # of Python implementations which are both in PYTHON_COMPAT and match
437 # any of the patterns passed as the remaining parameters.
439 # The patterns can be either fnmatch-style patterns (matched via bash
440 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
441 # appropriately all enabled Python 2/3 implementations (alike
442 # python_is_python3). Remember to escape or quote the fnmatch patterns
443 # to prevent accidental shell filename expansion.
445 # In order to enforce USE constraints on the packages, verbatim
446 # '${PYTHON_USEDEP}', '${PYTHON_SINGLE_USEDEP}'
447 # and '${PYTHON_MULTI_USEDEP}' (quoted!) may be placed in the dependency
448 # specification. It will get expanded within the function into a proper
449 # USE dependency string.
453 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
454 # RDEPEND="$(python_gen_cond_dep \
455 # 'dev-python/unittest2[${PYTHON_MULTI_USEDEP}]' python2_7 pypy )"
458 # It will cause the variable to look like:
460 # RDEPEND="python_single_target_python2_7? (
461 # dev-python/unittest2[python_targets_python2_7(-)?,...] )
462 # python_single_target_pypy? (
463 # dev-python/unittest2[python_targets_pypy(-)?,...] )"
465 python_gen_cond_dep() {
466 debug-print-function ${FUNCNAME} "${@}"
468 local impl matches=()
473 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
474 if _python_impl_matches "${impl}" "${@}"; then
475 # substitute ${PYTHON_USEDEP} if used
476 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
477 # the code is run at most once)
478 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
479 local usedep=$(_python_gen_usedep -u "${@}")
480 dep=${dep//\$\{PYTHON_USEDEP\}/${usedep}}
482 if [[ ${dep} == *'${PYTHON_SINGLE_USEDEP}'* ]]; then
483 local usedep=$(_python_gen_usedep -s "${@}")
484 dep=${dep//\$\{PYTHON_SINGLE_USEDEP\}/${usedep}}
486 local multi_usedep="python_targets_${impl}(-)"
488 matches+=( "python_single_target_${impl}? (
489 ${dep//\$\{PYTHON_MULTI_USEDEP\}/${multi_usedep}} )" )
496 # @FUNCTION: python_gen_impl_dep
497 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
499 # Output a dependency on Python implementations with the specified USE
500 # dependency string appended, or no USE dependency string if called
501 # without the argument (or with empty argument). If any implementation
502 # patterns are passed, the output dependencies will be generated only
503 # for the implementations matching them.
505 # The patterns can be either fnmatch-style patterns (matched via bash
506 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
507 # appropriately all enabled Python 2/3 implementations (alike
508 # python_is_python3). Remember to escape or quote the fnmatch patterns
509 # to prevent accidental shell filename expansion.
511 # Use this function when you need to request different USE flags
512 # on the Python interpreter depending on package's USE flags. If you
513 # only need a single set of interpreter USE flags, just set
514 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
518 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
519 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
522 # It will cause the variable to look like:
525 # python_single_target_python2_7? (
526 # dev-lang/python:2.7[xml(+)] )
527 # python_single_target_pypy? (
528 # dev-python/pypy[xml(+)] ) )"
530 python_gen_impl_dep() {
531 debug-print-function ${FUNCNAME} "${@}"
536 local PYTHON_REQ_USE=${1}
539 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
540 if _python_impl_matches "${impl}" "${@}"; then
542 python_export "${impl}" PYTHON_PKG_DEP
543 matches+=( "python_single_target_${impl}? ( ${PYTHON_PKG_DEP} )" )
550 # @FUNCTION: python_setup
552 # Determine what the selected Python implementation is and set
553 # the Python build environment up for it.
555 debug-print-function ${FUNCNAME} "${@}"
559 # support developer override
560 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
561 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
562 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
564 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
565 ewarn "implementation will be used:"
567 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
569 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
571 python_export "${impls[0]}" EPYTHON PYTHON
577 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
578 if use "python_single_target_${impl}"; then
579 if [[ ${EPYTHON} ]]; then
580 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
581 eerror "implementation. Please set it to just one value. If you need"
582 eerror "to override the value for a single package, please use package.env"
583 eerror "or an equivalent solution (man 5 portage)."
585 die "More than one implementation in PYTHON_SINGLE_TARGET."
588 if ! use "python_targets_${impl}"; then
589 eerror "The implementation chosen as PYTHON_SINGLE_TARGET must be added"
590 eerror "to PYTHON_TARGETS as well. This is in order to ensure that"
591 eerror "dependencies are satisfied correctly. We're sorry"
592 eerror "for the inconvenience."
594 die "Build target (${impl}) not in PYTHON_TARGETS."
597 python_export "${impl}" EPYTHON PYTHON
602 if [[ ! ${EPYTHON} ]]; then
603 eerror "No Python implementation selected for the build. Please set"
604 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
605 eerror "of the following values:"
607 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
609 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
613 # @FUNCTION: python-single-r1_pkg_setup
616 python-single-r1_pkg_setup() {
617 debug-print-function ${FUNCNAME} "${@}"
619 [[ ${MERGE_TYPE} != binary ]] && python_setup