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.
17 # This eclass extends the IUSE and REQUIRED_USE set by python-r1
18 # to request the PYTHON_SINGLE_TARGET when the inheriting ebuild
19 # can be supported by more than one Python implementation. It also
20 # replaces PYTHON_USEDEP and PYTHON_DEPS with a more suitable form.
22 # Please note that packages support multiple Python implementations
23 # (using python-r1 eclass) can not depend on packages not supporting
24 # them (using this eclass).
26 # Please note that python-single-r1 will always inherit python-utils-r1
27 # as well. Thus, all the functions defined there can be used
28 # in the packages using python-single-r1, and there is no need ever
31 # For more information, please see the wiki:
32 # https://wiki.gentoo.org/wiki/Project:Python/python-single-r1
36 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
39 # EAPI=5 is required for sane USE_EXPAND dependencies
42 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
46 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
48 if [[ ${_PYTHON_R1} ]]; then
49 die 'python-single-r1.eclass can not be used with python-r1.eclass.'
50 elif [[ ${_PYTHON_ANY_R1} ]]; then
51 die 'python-single-r1.eclass can not be used with python-any-r1.eclass.'
54 inherit python-utils-r1
58 EXPORT_FUNCTIONS pkg_setup
60 # @ECLASS-VARIABLE: PYTHON_COMPAT
63 # This variable contains a list of Python implementations the package
64 # supports. It must be set before the `inherit' call. It has to be
69 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
72 # Please note that you can also use bash brace expansion if you like:
74 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
77 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
80 # This variable can be used when working with ebuilds to override
81 # the in-ebuild PYTHON_COMPAT. It is a string naming the implementation
82 # which package will be built for. It needs to be specified
83 # in the calling environment, and not in ebuilds.
85 # It should be noted that in order to preserve metadata immutability,
86 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
87 # The state of PYTHON_TARGETS and PYTHON_SINGLE_TARGET is ignored,
88 # and the implementation in PYTHON_COMPAT_OVERRIDE is built instead.
89 # Dependencies need to be satisfied manually.
93 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
96 # @ECLASS-VARIABLE: PYTHON_REQ_USE
99 # The list of USEflags required to be enabled on the chosen Python
100 # implementations, formed as a USE-dependency string. It should be valid
101 # for all implementations in PYTHON_COMPAT, so it may be necessary to
104 # This should be set before calling `inherit'.
108 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
111 # It will cause the Python dependencies to look like:
113 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
116 # @ECLASS-VARIABLE: PYTHON_DEPS
118 # This is an eclass-generated Python dependency string for all
119 # implementations listed in PYTHON_COMPAT.
121 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
125 # RDEPEND="${PYTHON_DEPS}
127 # DEPEND="${RDEPEND}"
132 # dev-lang/python-exec:=
133 # python_single_target_python2_7? ( dev-lang/python:2.7[gdbm] )
134 # python_single_target_pypy? ( virtual/pypy[gdbm] )
137 # @ECLASS-VARIABLE: PYTHON_USEDEP
139 # This is an eclass-generated USE-dependency string which can be used to
140 # depend on another Python package being built for the same Python
143 # The generate USE-flag list is compatible with packages using python-r1,
144 # python-single-r1 and python-distutils-ng eclasses. It must not be used
145 # on packages using python.eclass.
149 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
154 # python_targets_python2_7(-)?,python_single_target_python3_4(+)?
157 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
159 # This is an eclass-generated required-use expression which ensures the following
160 # when more than one python implementation is possible:
161 # 1. Exactly one PYTHON_SINGLE_TARGET value has been enabled.
162 # 2. The selected PYTHON_SINGLE_TARGET value is enabled in PYTHON_TARGETS.
164 # This expression should be utilized in an ebuild by including it in
165 # REQUIRED_USE, optionally behind a use flag.
169 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
174 # python_single_target_python2_7? ( python_targets_python2_7 )
175 # python_single_target_python3_3? ( python_targets_python3_3 )
176 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
179 _python_single_set_globals() {
182 local i PYTHON_PKG_DEP
184 local flags_mt=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_targets_}" )
185 local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
186 local unflags=( "${_PYTHON_UNSUPPORTED_IMPLS[@]/#/-python_single_target_}" )
188 local optflags=${flags_mt[@]/%/(-)?},${unflags[@]/%/(-)}
190 IUSE="${flags_mt[*]}"
192 local deps requse usedep
193 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
194 # There is only one supported implementation; set IUSE and other
195 # variables without PYTHON_SINGLE_TARGET.
196 requse=${flags_mt[*]}
197 python_export "${_PYTHON_SUPPORTED_IMPLS[0]}" PYTHON_PKG_DEP
198 deps="${flags_mt[*]}? ( ${PYTHON_PKG_DEP} ) "
199 # Force on the python_single_target_* flag for this impl, so
200 # that any dependencies that inherit python-single-r1 and
201 # happen to have multiple implementations will still need
202 # to bound by the implementation used by this package.
203 optflags+=,${flags[0]/%/(+)}
205 # Multiple supported implementations; honor PYTHON_SINGLE_TARGET.
207 requse="^^ ( ${flags[*]} )"
208 # Ensure deps honor the same python_single_target_* flag as is set
210 optflags+=,${flags[@]/%/(+)?}
212 for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
213 # The chosen targets need to be in PYTHON_TARGETS as well.
214 # This is in order to enforce correct dependencies on packages
215 # supporting multiple implementations.
216 requse+=" python_single_target_${i}? ( python_targets_${i} )"
218 python_export "${i}" PYTHON_PKG_DEP
219 deps+="python_single_target_${i}? ( ${PYTHON_PKG_DEP} ) "
222 usedep=${optflags// /,}
224 # 1) well, python-exec would suffice as an RDEP
225 # but no point in making this overcomplex, BDEP doesn't hurt anyone
226 # 2) python-exec should be built with all targets forced anyway
227 # but if new targets were added, we may need to force a rebuild
228 if [[ ${_PYTHON_WANT_PYTHON_EXEC2} == 0 ]]; then
229 die "python-exec:0 is no longer supported, please fix your ebuild to work with python-exec:2"
231 deps+=">=dev-lang/python-exec-2:=[${usedep}]"
234 if [[ ${PYTHON_DEPS+1} ]]; then
235 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
236 eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
237 eerror "Before: ${PYTHON_DEPS}"
238 eerror "Now : ${deps}"
239 die "PYTHON_DEPS integrity check failed"
242 # these two are formality -- they depend on PYTHON_COMPAT only
243 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
244 eerror "PYTHON_REQUIRED_USE have changed between inherits!"
245 eerror "Before: ${PYTHON_REQUIRED_USE}"
246 eerror "Now : ${requse}"
247 die "PYTHON_REQUIRED_USE integrity check failed"
250 if [[ ${PYTHON_USEDEP} != "${usedep}" ]]; then
251 eerror "PYTHON_USEDEP have changed between inherits!"
252 eerror "Before: ${PYTHON_USEDEP}"
253 eerror "Now : ${usedep}"
254 die "PYTHON_USEDEP integrity check failed"
258 PYTHON_REQUIRED_USE=${requse}
259 PYTHON_USEDEP=${usedep}
260 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_USEDEP
263 _python_single_set_globals
264 unset -f _python_single_set_globals
266 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
268 # @FUNCTION: python_gen_usedep
269 # @USAGE: <pattern> [...]
271 # Output a USE dependency string for Python implementations which
272 # are both in PYTHON_COMPAT and match any of the patterns passed
273 # as parameters to the function.
275 # The patterns can be either fnmatch-style patterns (matched via bash
276 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
277 # appropriately all enabled Python 2/3 implementations (alike
278 # python_is_python3). Remember to escape or quote the fnmatch patterns
279 # to prevent accidental shell filename expansion.
281 # When all implementations are requested, please use ${PYTHON_USEDEP}
282 # instead. Please also remember to set an appropriate REQUIRED_USE
283 # to avoid ineffective USE flags.
287 # PYTHON_COMPAT=( python{2_7,3_4} )
288 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
291 # It will cause the dependency to look like:
293 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7(-)?,...] )"
295 python_gen_usedep() {
296 debug-print-function ${FUNCNAME} "${@}"
298 local impl matches=()
300 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
301 if _python_impl_matches "${impl}" "${@}"; then
303 "python_targets_${impl}(-)?"
304 "python_single_target_${impl}(+)?"
309 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
311 local out=${matches[@]}
315 # @FUNCTION: python_gen_useflags
316 # @USAGE: <pattern> [...]
318 # Output a list of USE flags for Python implementations which
319 # are both in PYTHON_COMPAT and match any of the patterns passed
320 # as parameters to the function.
322 # The patterns can be either fnmatch-style patterns (matched via bash
323 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
324 # appropriately all enabled Python 2/3 implementations (alike
325 # python_is_python3). Remember to escape or quote the fnmatch patterns
326 # to prevent accidental shell filename expansion.
330 # PYTHON_COMPAT=( python{2_7,3_4} )
331 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
334 # It will cause the variable to look like:
336 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
338 python_gen_useflags() {
339 debug-print-function ${FUNCNAME} "${@}"
341 local flag_prefix impl matches=()
343 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
344 flag_prefix=python_targets
346 flag_prefix=python_single_target
349 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
350 if _python_impl_matches "${impl}" "${@}"; then
351 matches+=( "${flag_prefix}_${impl}" )
358 # @FUNCTION: python_gen_cond_dep
359 # @USAGE: <dependency> <pattern> [...]
361 # Output a list of <dependency>-ies made conditional to USE flags
362 # of Python implementations which are both in PYTHON_COMPAT and match
363 # any of the patterns passed as the remaining parameters.
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 # In order to enforce USE constraints on the packages, verbatim
372 # '${PYTHON_USEDEP}' (quoted!) may be placed in the dependency
373 # specification. It will get expanded within the function into a proper
374 # USE dependency string.
378 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
379 # RDEPEND="$(python_gen_cond_dep \
380 # 'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
383 # It will cause the variable to look like:
385 # RDEPEND="python_single_target_python2_7? (
386 # dev-python/unittest2[python_targets_python2_7(-)?,...] )
387 # python_single_target_pypy? (
388 # dev-python/unittest2[python_targets_pypy(-)?,...] )"
390 python_gen_cond_dep() {
391 debug-print-function ${FUNCNAME} "${@}"
393 local flag_prefix impl matches=()
395 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
396 flag_prefix=python_targets
398 flag_prefix=python_single_target
404 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
405 if _python_impl_matches "${impl}" "${@}"; then
406 # substitute ${PYTHON_USEDEP} if used
407 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
408 # the code is run at most once)
409 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
410 local usedep=$(python_gen_usedep "${@}")
411 dep=${dep//\$\{PYTHON_USEDEP\}/${usedep}}
414 matches+=( "${flag_prefix}_${impl}? ( ${dep} )" )
421 # @FUNCTION: python_gen_impl_dep
422 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
424 # Output a dependency on Python implementations with the specified USE
425 # dependency string appended, or no USE dependency string if called
426 # without the argument (or with empty argument). If any implementation
427 # patterns are passed, the output dependencies will be generated only
428 # for the implementations matching them.
430 # The patterns can be either fnmatch-style patterns (matched via bash
431 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
432 # appropriately all enabled Python 2/3 implementations (alike
433 # python_is_python3). Remember to escape or quote the fnmatch patterns
434 # to prevent accidental shell filename expansion.
436 # Use this function when you need to request different USE flags
437 # on the Python interpreter depending on package's USE flags. If you
438 # only need a single set of interpreter USE flags, just set
439 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
443 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
444 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
447 # It will cause the variable to look like:
450 # python_single_target_python2_7? (
451 # dev-lang/python:2.7[xml(+)] )
452 # python_single_target_pypy? (
453 # dev-python/pypy[xml(+)] ) )"
455 python_gen_impl_dep() {
456 debug-print-function ${FUNCNAME} "${@}"
461 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
462 flag_prefix=python_targets
464 flag_prefix=python_single_target
467 local PYTHON_REQ_USE=${1}
470 local patterns=( "${@-*}" )
471 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
472 if _python_impl_matches "${impl}" "${patterns[@]}"; then
474 python_export "${impl}" PYTHON_PKG_DEP
475 matches+=( "${flag_prefix}_${impl}? ( ${PYTHON_PKG_DEP} )" )
482 # @FUNCTION: python_setup
484 # Determine what the selected Python implementation is and set
485 # the Python build environment up for it.
487 debug-print-function ${FUNCNAME} "${@}"
491 # support developer override
492 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
493 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
494 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
496 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
497 ewarn "implementation will be used:"
499 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
501 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
503 python_export "${impls[0]}" EPYTHON PYTHON
508 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
509 if use "python_targets_${_PYTHON_SUPPORTED_IMPLS[0]}"; then
510 # Only one supported implementation, enable it explicitly
511 python_export "${_PYTHON_SUPPORTED_IMPLS[0]}" EPYTHON PYTHON
516 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
517 if use "python_single_target_${impl}"; then
518 if [[ ${EPYTHON} ]]; then
519 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
520 eerror "implementation. Please set it to just one value. If you need"
521 eerror "to override the value for a single package, please use package.env"
522 eerror "or an equivalent solution (man 5 portage)."
524 die "More than one implementation in PYTHON_SINGLE_TARGET."
527 if ! use "python_targets_${impl}"; then
528 eerror "The implementation chosen as PYTHON_SINGLE_TARGET must be added"
529 eerror "to PYTHON_TARGETS as well. This is in order to ensure that"
530 eerror "dependencies are satisfied correctly. We're sorry"
531 eerror "for the inconvenience."
533 die "Build target (${impl}) not in PYTHON_TARGETS."
536 python_export "${impl}" EPYTHON PYTHON
542 if [[ ! ${EPYTHON} ]]; then
543 eerror "No Python implementation selected for the build. Please set"
544 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
545 eerror "the PYTHON_TARGETS variable in your make.conf to include one"
547 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
549 eerror "of the following values:"
551 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
553 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
557 # @FUNCTION: python-single-r1_pkg_setup
560 python-single-r1_pkg_setup() {
561 debug-print-function ${FUNCNAME} "${@}"
563 [[ ${MERGE_TYPE} != binary ]] && python_setup