1 # Copyright 1999-2015 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 # @BLURB: An eclass for Python packages not installed for multiple implementations.
12 # An extension of the python-r1 eclass suite for packages which
13 # don't support being installed for multiple Python implementations.
14 # This mostly includes tools embedding Python.
16 # This eclass extends the IUSE and REQUIRED_USE set by python-r1
17 # to request the PYTHON_SINGLE_TARGET when the inheriting ebuild
18 # can be supported by more than one Python implementation. It also
19 # replaces PYTHON_USEDEP and PYTHON_DEPS with a more suitable form.
21 # Please note that packages support multiple Python implementations
22 # (using python-r1 eclass) can not depend on packages not supporting
23 # them (using this eclass).
25 # Please note that python-single-r1 will always inherit python-utils-r1
26 # as well. Thus, all the functions defined there can be used
27 # in the packages using python-single-r1, and there is no need ever
30 # For more information, please see the wiki:
31 # https://wiki.gentoo.org/wiki/Project:Python/python-single-r1
35 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
38 # EAPI=5 is required for sane USE_EXPAND dependencies
41 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
45 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
47 if [[ ${_PYTHON_R1} ]]; then
48 die 'python-single-r1.eclass can not be used with python-r1.eclass.'
49 elif [[ ${_PYTHON_ANY_R1} ]]; then
50 die 'python-single-r1.eclass can not be used with python-any-r1.eclass.'
53 inherit python-utils-r1
57 EXPORT_FUNCTIONS pkg_setup
59 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
61 # @ECLASS-VARIABLE: PYTHON_COMPAT
64 # This variable contains a list of Python implementations the package
65 # supports. It must be set before the `inherit' call. It has to be
70 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
73 # Please note that you can also use bash brace expansion if you like:
75 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
78 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
81 # This variable can be used when working with ebuilds to override
82 # the in-ebuild PYTHON_COMPAT. It is a string naming the implementation
83 # which package will be built for. It needs to be specified
84 # in the calling environment, and not in ebuilds.
86 # It should be noted that in order to preserve metadata immutability,
87 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
88 # The state of PYTHON_TARGETS and PYTHON_SINGLE_TARGET is ignored,
89 # and the implementation in PYTHON_COMPAT_OVERRIDE is built instead.
90 # Dependencies need to be satisfied manually.
94 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
97 # @ECLASS-VARIABLE: PYTHON_REQ_USE
100 # The list of USEflags required to be enabled on the chosen Python
101 # implementations, formed as a USE-dependency string. It should be valid
102 # for all implementations in PYTHON_COMPAT, so it may be necessary to
105 # This should be set before calling `inherit'.
109 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
112 # It will cause the Python dependencies to look like:
114 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
117 # @ECLASS-VARIABLE: PYTHON_DEPS
119 # This is an eclass-generated Python dependency string for all
120 # implementations listed in PYTHON_COMPAT.
122 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
126 # RDEPEND="${PYTHON_DEPS}
128 # DEPEND="${RDEPEND}"
133 # dev-lang/python-exec:=
134 # python_single_target_python2_7? ( dev-lang/python:2.7[gdbm] )
135 # python_single_target_pypy? ( virtual/pypy[gdbm] )
138 # @ECLASS-VARIABLE: PYTHON_USEDEP
140 # This is an eclass-generated USE-dependency string which can be used to
141 # depend on another Python package being built for the same Python
144 # The generate USE-flag list is compatible with packages using python-r1,
145 # python-single-r1 and python-distutils-ng eclasses. It must not be used
146 # on packages using python.eclass.
150 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
155 # python_targets_python2_7(-)?,python_single_target_python3_4(+)?
158 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
160 # This is an eclass-generated required-use expression which ensures the following
161 # when more than one python implementation is possible:
162 # 1. Exactly one PYTHON_SINGLE_TARGET value has been enabled.
163 # 2. The selected PYTHON_SINGLE_TARGET value is enabled in PYTHON_TARGETS.
165 # This expression should be utilized in an ebuild by including it in
166 # REQUIRED_USE, optionally behind a use flag.
170 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
175 # python_single_target_python2_7? ( python_targets_python2_7 )
176 # python_single_target_python3_3? ( python_targets_python3_3 )
177 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
180 _python_single_set_globals() {
184 local i PYTHON_PKG_DEP
186 local flags_mt=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_targets_}" )
187 local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
188 local unflags=( "${_PYTHON_UNSUPPORTED_IMPLS[@]/#/-python_single_target_}" )
190 local optflags=${flags_mt[@]/%/(-)?},${unflags[@]/%/(-)}
192 IUSE="${flags_mt[*]}"
194 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
195 # There is only one supported implementation; set IUSE and other
196 # variables without PYTHON_SINGLE_TARGET.
197 PYTHON_REQUIRED_USE="${flags_mt[*]}"
198 python_export "${_PYTHON_SUPPORTED_IMPLS[0]}" PYTHON_PKG_DEP
199 PYTHON_DEPS="${flags_mt[*]}? ( ${PYTHON_PKG_DEP} ) "
200 # Force on the python_single_target_* flag for this impl, so
201 # that any dependencies that inherit python-single-r1 and
202 # happen to have multiple implementations will still need
203 # to bound by the implementation used by this package.
204 optflags+=,${flags[0]/%/(+)}
206 # Multiple supported implementations; honor PYTHON_SINGLE_TARGET.
208 PYTHON_REQUIRED_USE="^^ ( ${flags[*]} )"
209 # Ensure deps honor the same python_single_target_* flag as is set
211 optflags+=,${flags[@]/%/(+)?}
213 for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
214 # The chosen targets need to be in PYTHON_TARGETS as well.
215 # This is in order to enforce correct dependencies on packages
216 # supporting multiple implementations.
217 PYTHON_REQUIRED_USE+=" python_single_target_${i}? ( python_targets_${i} )"
219 python_export "${i}" PYTHON_PKG_DEP
220 PYTHON_DEPS+="python_single_target_${i}? ( ${PYTHON_PKG_DEP} ) "
223 PYTHON_USEDEP=${optflags// /,}
225 # 1) well, python-exec would suffice as an RDEP
226 # but no point in making this overcomplex, BDEP doesn't hurt anyone
227 # 2) python-exec should be built with all targets forced anyway
228 # but if new targets were added, we may need to force a rebuild
229 # 3) use whichever python-exec slot installed in EAPI 5. For EAPI 4,
230 # just fix :2 since := deps are not supported.
231 if [[ ${_PYTHON_WANT_PYTHON_EXEC2} == 0 ]]; then
232 die "python-exec:0 is no longer supported, please fix your ebuild to work with python-exec:2"
233 elif [[ ${EAPI} != 4 ]]; then
234 PYTHON_DEPS+=">=dev-lang/python-exec-2:=[${PYTHON_USEDEP}]"
236 PYTHON_DEPS+="dev-lang/python-exec:2[${PYTHON_USEDEP}]"
238 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_USEDEP
240 _python_single_set_globals
241 unset -f _python_single_set_globals
243 # @FUNCTION: python_gen_usedep
244 # @USAGE: <pattern> [...]
246 # Output a USE dependency string for Python implementations which
247 # are both in PYTHON_COMPAT and match any of the patterns passed
248 # as parameters to the function.
250 # Remember to escape or quote the patterns to prevent shell filename
253 # When all implementations are requested, please use ${PYTHON_USEDEP}
254 # instead. Please also remember to set an appropriate REQUIRED_USE
255 # to avoid ineffective USE flags.
259 # PYTHON_COMPAT=( python{2_7,3_4} )
260 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
263 # It will cause the dependency to look like:
265 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7(-)?,...] )"
267 python_gen_usedep() {
268 debug-print-function ${FUNCNAME} "${@}"
273 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
275 if [[ ${impl} == ${pattern} ]]; then
277 "python_targets_${impl}(-)?"
278 "python_single_target_${impl}(+)?"
285 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
287 local out=${matches[@]}
291 # @FUNCTION: python_gen_useflags
292 # @USAGE: <pattern> [...]
294 # Output a list of USE flags for Python implementations which
295 # are both in PYTHON_COMPAT and match any of the patterns passed
296 # as parameters to the function.
300 # PYTHON_COMPAT=( python{2_7,3_4} )
301 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
304 # It will cause the variable to look like:
306 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
308 python_gen_useflags() {
309 debug-print-function ${FUNCNAME} "${@}"
311 local flag_prefix impl pattern
314 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
315 flag_prefix=python_targets
317 flag_prefix=python_single_target
320 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
322 if [[ ${impl} == ${pattern} ]]; then
323 matches+=( "${flag_prefix}_${impl}" )
332 # @FUNCTION: python_gen_cond_dep
333 # @USAGE: <dependency> <pattern> [...]
335 # Output a list of <dependency>-ies made conditional to USE flags
336 # of Python implementations which are both in PYTHON_COMPAT and match
337 # any of the patterns passed as the remaining parameters.
339 # In order to enforce USE constraints on the packages, verbatim
340 # '${PYTHON_USEDEP}' (quoted!) may be placed in the dependency
341 # specification. It will get expanded within the function into a proper
342 # USE dependency string.
346 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
347 # RDEPEND="$(python_gen_cond_dep \
348 # 'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
351 # It will cause the variable to look like:
353 # RDEPEND="python_single_target_python2_7? (
354 # dev-python/unittest2[python_targets_python2_7(-)?,...] )
355 # python_single_target_pypy? (
356 # dev-python/unittest2[python_targets_pypy(-)?,...] )"
358 python_gen_cond_dep() {
359 debug-print-function ${FUNCNAME} "${@}"
361 local flag_prefix impl pattern
364 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
365 flag_prefix=python_targets
367 flag_prefix=python_single_target
373 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
375 if [[ ${impl} == ${pattern} ]]; then
376 # substitute ${PYTHON_USEDEP} if used
377 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
378 # the code is run at most once)
379 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
380 local usedep=$(python_gen_usedep "${@}")
381 dep=${dep//\$\{PYTHON_USEDEP\}/${usedep}}
384 matches+=( "${flag_prefix}_${impl}? ( ${dep} )" )
393 # @FUNCTION: python_gen_impl_dep
394 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
396 # Output a dependency on Python implementations with the specified USE
397 # dependency string appended, or no USE dependency string if called
398 # without the argument (or with empty argument). If any implementation
399 # patterns are passed, the output dependencies will be generated only
400 # for the implementations matching them.
402 # Use this function when you need to request different USE flags
403 # on the Python interpreter depending on package's USE flags. If you
404 # only need a single set of interpreter USE flags, just set
405 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
409 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
410 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
413 # It will cause the variable to look like:
416 # python_single_target_python2_7? (
417 # dev-lang/python:2.7[xml(+)] )
418 # python_single_target_pypy? (
419 # dev-python/pypy[xml(+)] ) )"
421 python_gen_impl_dep() {
422 debug-print-function ${FUNCNAME} "${@}"
427 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
428 flag_prefix=python_targets
430 flag_prefix=python_single_target
433 local PYTHON_REQ_USE=${1}
436 local patterns=( "${@-*}" )
437 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
438 for pattern in "${patterns[@]}"; do
439 if [[ ${impl} == ${pattern} ]]; then
441 python_export "${impl}" PYTHON_PKG_DEP
442 matches+=( "${flag_prefix}_${impl}? ( ${PYTHON_PKG_DEP} )" )
451 # @FUNCTION: python_setup
453 # Determine what the selected Python implementation is and set
454 # the Python build environment up for it.
456 debug-print-function ${FUNCNAME} "${@}"
460 # support developer override
461 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
462 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
463 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
465 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
466 ewarn "implementation will be used:"
468 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
470 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
472 python_export "${impls[0]}" EPYTHON PYTHON
477 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
478 if use "python_targets_${_PYTHON_SUPPORTED_IMPLS[0]}"; then
479 # Only one supported implementation, enable it explicitly
480 python_export "${_PYTHON_SUPPORTED_IMPLS[0]}" EPYTHON PYTHON
485 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
486 if use "python_single_target_${impl}"; then
487 if [[ ${EPYTHON} ]]; then
488 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
489 eerror "implementation. Please set it to just one value. If you need"
490 eerror "to override the value for a single package, please use package.env"
491 eerror "or an equivalent solution (man 5 portage)."
493 die "More than one implementation in PYTHON_SINGLE_TARGET."
496 if ! use "python_targets_${impl}"; then
497 eerror "The implementation chosen as PYTHON_SINGLE_TARGET must be added"
498 eerror "to PYTHON_TARGETS as well. This is in order to ensure that"
499 eerror "dependencies are satisfied correctly. We're sorry"
500 eerror "for the inconvenience."
502 die "Build target (${impl}) not in PYTHON_TARGETS."
505 python_export "${impl}" EPYTHON PYTHON
511 if [[ ! ${EPYTHON} ]]; then
512 eerror "No Python implementation selected for the build. Please set"
513 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
514 eerror "the PYTHON_TARGETS variable in your make.conf to include one"
516 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
518 eerror "of the following values:"
520 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
522 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
526 # @FUNCTION: python-single-r1_pkg_setup
529 python-single-r1_pkg_setup() {
530 debug-print-function ${FUNCNAME} "${@}"
532 [[ ${MERGE_TYPE} != binary ]] && python_setup