1 # Copyright 1999-2020 Gentoo Authors
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 Python Guide:
37 # https://dev.gentoo.org/~mgorny/python-guide/
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_SINGLE_TARGET is ignored, and the implementation
93 # in PYTHON_COMPAT_OVERRIDE is built instead. Dependencies need to be
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_SINGLE_USEDEP
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.
148 # If you need to depend on a multi-impl (python-r1) package, use
149 # python_gen_cond_dep with PYTHON_MULTI_USEDEP placeholder instead.
153 # RDEPEND="dev-python/foo[${PYTHON_SINGLE_USEDEP}]"
158 # python_single_target_python3_4(-)?
161 # @ECLASS-VARIABLE: PYTHON_MULTI_USEDEP
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
169 # RDEPEND="$(python_gen_cond_dep '
170 # dev-python/foo[${PYTHON_MULTI_USEDEP}]
176 # python_targets_python3_4(-)
179 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
181 # This is an eclass-generated required-use expression which ensures
182 # that exactly one PYTHON_SINGLE_TARGET value has been enabled.
184 # This expression should be utilized in an ebuild by including it in
185 # REQUIRED_USE, optionally behind a use flag.
189 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
194 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
197 _python_single_set_globals() {
200 local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
202 if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
203 # if only one implementation is supported, use IUSE defaults
204 # to avoid requesting the user to enable it
210 local requse="^^ ( ${flags[*]} )"
211 local single_flags="${flags[@]/%/(-)?}"
212 local single_usedep=${single_flags// /,}
214 local deps= i PYTHON_PKG_DEP
215 for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
216 _python_export "${i}" PYTHON_PKG_DEP
217 # 1) well, python-exec would suffice as an RDEP
218 # but no point in making this overcomplex, BDEP doesn't hurt anyone
219 # 2) python-exec should be built with all targets forced anyway
220 # but if new targets were added, we may need to force a rebuild
221 deps+="python_single_target_${i}? (
223 >=dev-lang/python-exec-2:=[python_targets_${i}]
227 if [[ ${PYTHON_DEPS+1} ]]; then
228 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
229 eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
230 eerror "Before: ${PYTHON_DEPS}"
231 eerror "Now : ${deps}"
232 die "PYTHON_DEPS integrity check failed"
235 # these two are formality -- they depend on PYTHON_COMPAT only
236 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
237 eerror "PYTHON_REQUIRED_USE have changed between inherits!"
238 eerror "Before: ${PYTHON_REQUIRED_USE}"
239 eerror "Now : ${requse}"
240 die "PYTHON_REQUIRED_USE integrity check failed"
243 if [[ ${PYTHON_SINGLE_USEDEP} != "${single_usedep}" ]]; then
244 eerror "PYTHON_SINGLE_USEDEP have changed between inherits!"
245 eerror "Before: ${PYTHON_SINGLE_USEDEP}"
246 eerror "Now : ${single_usedep}"
247 die "PYTHON_SINGLE_USEDEP integrity check failed"
251 PYTHON_REQUIRED_USE=${requse}
252 PYTHON_USEDEP='%PYTHON_USEDEP-HAS-BEEN-REMOVED%'
253 PYTHON_SINGLE_USEDEP=${single_usedep}
254 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_SINGLE_USEDEP \
258 _python_single_set_globals
259 unset -f _python_single_set_globals
261 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
263 # @FUNCTION: _python_gen_usedep
265 # @USAGE: [<pattern>...]
267 # Output a USE dependency string for Python implementations which
268 # are both in PYTHON_COMPAT and match any of the patterns passed
269 # as parameters to the function.
271 # The patterns can be either fnmatch-style patterns (matched via bash
272 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
273 # appropriately all enabled Python 2/3 implementations (alike
274 # python_is_python3). Remember to escape or quote the fnmatch patterns
275 # to prevent accidental shell filename expansion.
277 # This is an internal function used to implement python_gen_cond_dep.
278 _python_gen_usedep() {
279 debug-print-function ${FUNCNAME} "${@}"
281 local impl matches=()
283 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
284 if _python_impl_matches "${impl}" "${@}"; then
286 "python_single_target_${impl}(-)?"
291 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
293 local out=${matches[@]}
297 # @FUNCTION: python_gen_useflags
298 # @USAGE: [<pattern>...]
300 # Output a list of USE flags for Python implementations which
301 # are both in PYTHON_COMPAT and match any of the patterns passed
302 # as parameters to the function.
304 # The patterns can be either fnmatch-style patterns (matched via bash
305 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
306 # appropriately all enabled Python 2/3 implementations (alike
307 # python_is_python3). Remember to escape or quote the fnmatch patterns
308 # to prevent accidental shell filename expansion.
312 # PYTHON_COMPAT=( python{2_7,3_4} )
313 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
316 # It will cause the variable to look like:
318 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
320 python_gen_useflags() {
321 debug-print-function ${FUNCNAME} "${@}"
323 local impl matches=()
325 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
326 if _python_impl_matches "${impl}" "${@}"; then
327 matches+=( "python_single_target_${impl}" )
334 # @FUNCTION: python_gen_cond_dep
335 # @USAGE: <dependency> [<pattern>...]
337 # Output a list of <dependency>-ies made conditional to USE flags
338 # of Python implementations which are both in PYTHON_COMPAT and match
339 # any of the patterns passed as the remaining parameters.
341 # The patterns can be either fnmatch-style patterns (matched via bash
342 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
343 # appropriately all enabled Python 2/3 implementations (alike
344 # python_is_python3). Remember to escape or quote the fnmatch patterns
345 # to prevent accidental shell filename expansion.
347 # In order to enforce USE constraints on the packages, verbatim
348 # '${PYTHON_SINGLE_USEDEP}' and '${PYTHON_MULTI_USEDEP}' (quoted!) may
349 # be placed in the dependency specification. It will get expanded within
350 # the function into a proper USE dependency string.
354 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
355 # RDEPEND="$(python_gen_cond_dep \
356 # 'dev-python/unittest2[${PYTHON_MULTI_USEDEP}]' python2_7 pypy )"
359 # It will cause the variable to look like:
361 # RDEPEND="python_single_target_python2_7? (
362 # dev-python/unittest2[python_targets_python2_7(-)?,...] )
363 # python_single_target_pypy? (
364 # dev-python/unittest2[python_targets_pypy(-)?,...] )"
366 python_gen_cond_dep() {
367 debug-print-function ${FUNCNAME} "${@}"
369 local impl matches=()
374 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
375 if _python_impl_matches "${impl}" "${@}"; then
376 # substitute ${PYTHON_SINGLE_USEDEP} if used
377 # (since python_gen_usedep() will not return
378 # ${PYTHON_SINGLE_USEDEP}, the code is run at most once)
379 if [[ ${dep} == *'${PYTHON_SINGLE_USEDEP}'* ]]; then
380 local usedep=$(_python_gen_usedep "${@}")
381 dep=${dep//\$\{PYTHON_SINGLE_USEDEP\}/${usedep}}
383 local multi_usedep="python_targets_${impl}(-)"
385 matches+=( "python_single_target_${impl}? (
386 ${dep//\$\{PYTHON_MULTI_USEDEP\}/${multi_usedep}} )" )
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 # 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.
408 # Use this function when you need to request different USE flags
409 # on the Python interpreter depending on package's USE flags. If you
410 # only need a single set of interpreter USE flags, just set
411 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
415 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
416 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
419 # It will cause the variable to look like:
422 # python_single_target_python2_7? (
423 # dev-lang/python:2.7[xml(+)] )
424 # python_single_target_pypy? (
425 # dev-python/pypy[xml(+)] ) )"
427 python_gen_impl_dep() {
428 debug-print-function ${FUNCNAME} "${@}"
433 local PYTHON_REQ_USE=${1}
436 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
437 if _python_impl_matches "${impl}" "${@}"; then
439 _python_export "${impl}" PYTHON_PKG_DEP
440 matches+=( "python_single_target_${impl}? ( ${PYTHON_PKG_DEP} )" )
447 # @FUNCTION: python_setup
449 # Determine what the selected Python implementation is and set
450 # the Python build environment up for it.
452 debug-print-function ${FUNCNAME} "${@}"
456 # support developer override
457 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
458 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
459 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
461 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
462 ewarn "implementation will be used:"
464 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
466 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
468 _python_export "${impls[0]}" EPYTHON PYTHON
469 _python_wrapper_setup
470 einfo "Using ${EPYTHON} to build"
475 for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
476 if use "python_single_target_${impl}"; then
477 if [[ ${EPYTHON} ]]; then
478 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
479 eerror "implementation. Please set it to just one value. If you need"
480 eerror "to override the value for a single package, please use package.env"
481 eerror "or an equivalent solution (man 5 portage)."
483 die "More than one implementation in PYTHON_SINGLE_TARGET."
486 _python_export "${impl}" EPYTHON PYTHON
487 _python_wrapper_setup
488 einfo "Using ${EPYTHON} to build"
492 if [[ ! ${EPYTHON} ]]; then
493 eerror "No Python implementation selected for the build. Please set"
494 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
495 eerror "of the following values:"
497 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
499 die "No supported Python implementation in PYTHON_SINGLE_TARGET."
503 # @FUNCTION: python-single-r1_pkg_setup
506 python-single-r1_pkg_setup() {
507 debug-print-function ${FUNCNAME} "${@}"
509 [[ ${MERGE_TYPE} != binary ]] && python_setup