1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: python-r1.eclass
7 # Python team <python@gentoo.org>
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @BLURB: A common, simple eclass for Python packages.
13 # A common eclass providing helper functions to build and install
14 # packages supporting being installed for multiple Python
17 # This eclass sets correct IUSE. Modification of REQUIRED_USE has to
18 # be done by the author of the ebuild (but PYTHON_REQUIRED_USE is
19 # provided for convenience, see below). python-r1 exports PYTHON_DEPS
20 # and PYTHON_USEDEP so you can create correct dependencies for your
21 # package easily. It also provides methods to easily run a command for
22 # each enabled Python implementation and duplicate the sources for them.
24 # Please note that python-r1 will always inherit python-utils-r1 as
25 # well. Thus, all the functions defined there can be used
26 # in the packages using python-r1, and there is no need ever to inherit
29 # For more information, please see the wiki:
30 # https://wiki.gentoo.org/wiki/Project:Python/python-r1
34 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
37 # EAPI=4 is only allowed on legacy packages
38 if [[ ${CATEGORY}/${P} == dev-python/pyelftools-0.2[123] ]]; then
40 elif [[ ${CATEGORY}/${P} == sys-apps/file-5.22 ]]; then
42 elif [[ ${CATEGORY}/${P} == sys-apps/i2c-tools-3.1.1 ]]; then
44 elif [[ ${CATEGORY}/${P} == sys-libs/cracklib-2.9.[12] ]]; then
47 die "Unsupported EAPI=${EAPI:-4} (too old, allowed only on restricted set of packages) for ${ECLASS}"
51 # EAPI=5 is required for sane USE_EXPAND dependencies
54 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
58 if [[ ! ${_PYTHON_R1} ]]; then
60 if [[ ${_PYTHON_SINGLE_R1} ]]; then
61 die 'python-r1.eclass can not be used with python-single-r1.eclass.'
62 elif [[ ${_PYTHON_ANY_R1} ]]; then
63 die 'python-r1.eclass can not be used with python-any-r1.eclass.'
66 inherit multibuild python-utils-r1
68 # @ECLASS-VARIABLE: PYTHON_COMPAT
71 # This variable contains a list of Python implementations the package
72 # supports. It must be set before the `inherit' call. It has to be
77 # PYTHON_COMPAT=( python2_7 python3_3 python3_4} )
80 # Please note that you can also use bash brace expansion if you like:
82 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
84 if ! declare -p PYTHON_COMPAT &>/dev/null; then
85 die 'PYTHON_COMPAT not declared.'
88 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
91 # This variable can be used when working with ebuilds to override
92 # the in-ebuild PYTHON_COMPAT. It is a string listing all
93 # the implementations which package will be built for. It need be
94 # specified in the calling environment, and not in ebuilds.
96 # It should be noted that in order to preserve metadata immutability,
97 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
98 # The state of PYTHON_TARGETS is ignored, and all the implementations
99 # in PYTHON_COMPAT_OVERRIDE are built. Dependencies need to be satisfied
104 # PYTHON_COMPAT_OVERRIDE='pypy python3_3' emerge -1v dev-python/foo
107 # @ECLASS-VARIABLE: PYTHON_REQ_USE
110 # The list of USEflags required to be enabled on the chosen Python
111 # implementations, formed as a USE-dependency string. It should be valid
112 # for all implementations in PYTHON_COMPAT, so it may be necessary to
115 # This should be set before calling `inherit'.
119 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
122 # It will cause the Python dependencies to look like:
124 # python_targets_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
127 # @ECLASS-VARIABLE: PYTHON_DEPS
129 # This is an eclass-generated Python dependency string for all
130 # implementations listed in PYTHON_COMPAT.
134 # RDEPEND="${PYTHON_DEPS}
136 # DEPEND="${RDEPEND}"
141 # dev-lang/python-exec:=
142 # python_targets_python2_7? ( dev-lang/python:2.7[gdbm] )
143 # python_targets_pypy? ( virtual/pypy[gdbm] )
146 # @ECLASS-VARIABLE: PYTHON_USEDEP
148 # This is an eclass-generated USE-dependency string which can be used to
149 # depend on another Python package being built for the same Python
152 # The generate USE-flag list is compatible with packages using python-r1
153 # and python-distutils-ng eclasses. It must not be used on packages
154 # using python.eclass.
158 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
163 # python_targets_python2_7(-)?,python_targets_python3_4(-)?
166 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
168 # This is an eclass-generated required-use expression which ensures at
169 # least one Python implementation has been enabled.
171 # This expression should be utilized in an ebuild by including it in
172 # REQUIRED_USE, optionally behind a use flag.
176 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
181 # || ( python_targets_python2_7 python_targets_python3_4 )
184 _python_set_globals() {
188 local i PYTHON_PKG_DEP
189 for i in "${PYTHON_COMPAT[@]}"; do
190 _python_impl_supported "${i}" || continue
192 python_export "${i}" PYTHON_PKG_DEP
193 PYTHON_DEPS+="python_targets_${i}? ( ${PYTHON_PKG_DEP} ) "
198 if [[ ${#impls[@]} -eq 0 ]]; then
199 die "No supported implementation in PYTHON_COMPAT."
202 local flags=( "${impls[@]/#/python_targets_}" )
203 local optflags=${flags[@]/%/(-)?}
205 # A nice QA trick here. Since a python-single-r1 package has to have
206 # at least one PYTHON_SINGLE_TARGET enabled (REQUIRED_USE),
207 # the following check will always fail on those packages. Therefore,
208 # it should prevent developers from mistakenly depending on packages
209 # not supporting multiple Python implementations.
211 local flags_st=( "${impls[@]/#/-python_single_target_}" )
212 optflags+=,${flags_st[@]/%/(-)}
215 PYTHON_REQUIRED_USE="|| ( ${flags[*]} )"
216 PYTHON_USEDEP=${optflags// /,}
218 # 1) well, python-exec would suffice as an RDEP
219 # but no point in making this overcomplex, BDEP doesn't hurt anyone
220 # 2) python-exec should be built with all targets forced anyway
221 # but if new targets were added, we may need to force a rebuild
222 # 3) use whichever python-exec slot installed in EAPI 5. For EAPI 4,
223 # just fix :2 since := deps are not supported.
224 if [[ ${_PYTHON_WANT_PYTHON_EXEC2} == 0 ]]; then
225 die "python-exec:0 is no longer supported, please fix your ebuild to work with python-exec:2"
226 elif [[ ${EAPI} != 4 ]]; then
227 PYTHON_DEPS+=">=dev-lang/python-exec-2:=[${PYTHON_USEDEP}]"
229 PYTHON_DEPS+="dev-lang/python-exec:2[${PYTHON_USEDEP}]"
234 # @FUNCTION: _python_validate_useflags
237 # Enforce the proper setting of PYTHON_TARGETS.
238 _python_validate_useflags() {
239 debug-print-function ${FUNCNAME} "${@}"
243 for i in "${PYTHON_COMPAT[@]}"; do
244 _python_impl_supported "${i}" || continue
246 use "python_targets_${i}" && return 0
249 eerror "No Python implementation selected for the build. Please add one"
250 eerror "of the following values to your PYTHON_TARGETS (in make.conf):"
252 eerror "${PYTHON_COMPAT[@]}"
254 die "No supported Python implementation in PYTHON_TARGETS."
257 # @FUNCTION: python_gen_usedep
258 # @USAGE: <pattern> [...]
260 # Output a USE dependency string for Python implementations which
261 # are both in PYTHON_COMPAT and match any of the patterns passed
262 # as parameters to the function.
264 # Remember to escape or quote the patterns to prevent shell filename
267 # When all implementations are requested, please use ${PYTHON_USEDEP}
268 # instead. Please also remember to set an appropriate REQUIRED_USE
269 # to avoid ineffective USE flags.
273 # PYTHON_COMPAT=( python{2_7,3_4} )
274 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
277 # It will cause the dependency to look like:
279 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7?] )"
281 python_gen_usedep() {
282 debug-print-function ${FUNCNAME} "${@}"
287 for impl in "${PYTHON_COMPAT[@]}"; do
288 _python_impl_supported "${impl}" || continue
291 if [[ ${impl} == ${pattern} ]]; then
293 "python_targets_${impl}(-)?"
294 "-python_single_target_${impl}(-)"
301 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
303 local out=${matches[@]}
307 # @FUNCTION: python_gen_useflags
308 # @USAGE: <pattern> [...]
310 # Output a list of USE flags for Python implementations which
311 # are both in PYTHON_COMPAT and match any of the patterns passed
312 # as parameters to the function.
316 # PYTHON_COMPAT=( python{2_7,3_4} )
317 # REQUIRED_USE="doc? ( || ( $(python_gen_useflags python2*) ) )"
320 # It will cause the variable to look like:
322 # REQUIRED_USE="doc? ( || ( python_targets_python2_7 ) )"
324 python_gen_useflags() {
325 debug-print-function ${FUNCNAME} "${@}"
330 for impl in "${PYTHON_COMPAT[@]}"; do
331 _python_impl_supported "${impl}" || continue
334 if [[ ${impl} == ${pattern} ]]; then
335 matches+=( "python_targets_${impl}" )
344 # @FUNCTION: python_gen_cond_dep
345 # @USAGE: <dependency> <pattern> [...]
347 # Output a list of <dependency>-ies made conditional to USE flags
348 # of Python implementations which are both in PYTHON_COMPAT and match
349 # any of the patterns passed as the remaining parameters.
351 # In order to enforce USE constraints on the packages, verbatim
352 # '${PYTHON_USEDEP}' (quoted!) may be placed in the dependency
353 # specification. It will get expanded within the function into a proper
354 # USE dependency string.
358 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
359 # RDEPEND="$(python_gen_cond_dep \
360 # 'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
363 # It will cause the variable to look like:
365 # RDEPEND="python_targets_python2_7? (
366 # dev-python/unittest2[python_targets_python2_7?] )
367 # python_targets_pypy? (
368 # dev-python/unittest2[python_targets_pypy?] )"
370 python_gen_cond_dep() {
371 debug-print-function ${FUNCNAME} "${@}"
379 for impl in "${PYTHON_COMPAT[@]}"; do
380 _python_impl_supported "${impl}" || continue
383 if [[ ${impl} == ${pattern} ]]; then
384 # substitute ${PYTHON_USEDEP} if used
385 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
386 # the code is run at most once)
387 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
388 local PYTHON_USEDEP=$(python_gen_usedep "${@}")
389 dep=${dep//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
392 matches+=( "python_targets_${impl}? ( ${dep} )" )
401 # @ECLASS-VARIABLE: BUILD_DIR
403 # The current build directory. In global scope, it is supposed to
404 # contain an initial build directory; if unset, it defaults to ${S}.
406 # In functions run by python_foreach_impl(), the BUILD_DIR is locally
407 # set to an implementation-specific build directory. That path is
408 # created through appending a hyphen and the implementation name
409 # to the final component of the initial BUILD_DIR.
413 # ${WORKDIR}/foo-1.3-python2_7
416 # @FUNCTION: python_copy_sources
418 # Create a single copy of the package sources for each enabled Python
421 # The sources are always copied from initial BUILD_DIR (or S if unset)
422 # to implementation-specific build directory matching BUILD_DIR used by
423 # python_foreach_abi().
424 python_copy_sources() {
425 debug-print-function ${FUNCNAME} "${@}"
427 local MULTIBUILD_VARIANTS
430 multibuild_copy_sources
433 # @FUNCTION: _python_obtain_impls
436 # Set up the enabled implementation list.
437 _python_obtain_impls() {
438 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
439 if [[ ! ${_PYTHON_COMPAT_OVERRIDE_WARNED} ]]; then
440 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
441 ewarn "implementations will be enabled:"
443 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
445 ewarn "Dependencies won't be satisfied, and PYTHON_TARGETS will be ignored."
446 _PYTHON_COMPAT_OVERRIDE_WARNED=1
449 MULTIBUILD_VARIANTS=( ${PYTHON_COMPAT_OVERRIDE} )
453 _python_validate_useflags
455 MULTIBUILD_VARIANTS=()
457 for impl in "${_PYTHON_ALL_IMPLS[@]}"; do
458 if has "${impl}" "${PYTHON_COMPAT[@]}" \
459 && use "python_targets_${impl}"
461 MULTIBUILD_VARIANTS+=( "${impl}" )
466 # @FUNCTION: _python_multibuild_wrapper
467 # @USAGE: <command> [<args>...]
470 # Initialize the environment for Python implementation selected
472 _python_multibuild_wrapper() {
473 debug-print-function ${FUNCNAME} "${@}"
475 local -x EPYTHON PYTHON
476 local -x PATH=${PATH} PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
477 python_export "${MULTIBUILD_VARIANT}" EPYTHON PYTHON
483 # @FUNCTION: python_foreach_impl
484 # @USAGE: <command> [<args>...]
486 # Run the given command for each of the enabled Python implementations.
487 # If additional parameters are passed, they will be passed through
490 # The function will return 0 status if all invocations succeed.
491 # Otherwise, the return code from first failing invocation will
494 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
495 # locally, and the former two are exported to the command environment.
496 python_foreach_impl() {
497 debug-print-function ${FUNCNAME} "${@}"
499 local MULTIBUILD_VARIANTS
502 multibuild_foreach_variant _python_multibuild_wrapper "${@}"
505 # @FUNCTION: python_parallel_foreach_impl
506 # @USAGE: <command> [<args>...]
508 # Run the given command for each of the enabled Python implementations.
509 # If additional parameters are passed, they will be passed through
512 # The function will return 0 status if all invocations succeed.
513 # Otherwise, the return code from first failing invocation will
516 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
517 # locally, and the former two are exported to the command environment.
519 # This command used to be the parallel variant of python_foreach_impl.
520 # However, the parallel run support has been removed to simplify
521 # the eclasses and make them more predictable and therefore it is now
522 # only a deprecated alias to python_foreach_impl.
523 python_parallel_foreach_impl() {
524 debug-print-function ${FUNCNAME} "${@}"
526 if [[ ! ${_PYTHON_PARALLEL_WARNED} ]]; then
527 eqawarn "python_parallel_foreach_impl() is no longer meaningful. All runs"
528 eqawarn "are non-parallel now. Please replace the call with python_foreach_impl."
530 _PYTHON_PARALLEL_WARNED=1
533 local MULTIBUILD_VARIANTS
535 multibuild_foreach_variant _python_multibuild_wrapper "${@}"
538 # @FUNCTION: python_setup
539 # @USAGE: [<impl-pattern>...]
541 # Find the best (most preferred) Python implementation that is enabled
542 # and matches at least one of the patterns passed (or '*' if no patterns
543 # passed). Set the Python build environment up for that implementation.
545 # This function needs to be used when Python is being called outside
546 # of python_foreach_impl calls (e.g. for shared processes like doc
547 # building). python_foreach_impl sets up the build environment itself.
549 # If the specific commands support only a subset of Python
550 # implementations, patterns need to be passed to restrict the allowed
555 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
560 # python_setup 'python2*'
566 debug-print-function ${FUNCNAME} "${@}"
568 local best_impl patterns=( "${@-*}" )
571 for pattern in "${patterns[@]}"; do
572 if [[ ${EPYTHON} == ${pattern} ]]; then
577 python_foreach_impl _python_try_impl
579 if [[ ! ${best_impl} ]]; then
580 eerror "${FUNCNAME}: none of the enabled implementation matched the patterns."
581 eerror " patterns: ${@-'(*)'}"
582 eerror "Likely a REQUIRED_USE constraint (possibly USE-conditional) is missing."
583 eerror " suggested: || ( \$(python_gen_useflags ${@}) )"
584 eerror "(remember to quote all the patterns with '')"
585 die "${FUNCNAME}: no enabled implementation satisfy requirements"
588 python_export "${best_impl}" EPYTHON PYTHON
592 # @FUNCTION: python_export_best
593 # @USAGE: [<variable>...]
595 # Find the best (most preferred) Python implementation enabled
596 # and export given variables for it. If no variables are provided,
597 # EPYTHON & PYTHON will be exported.
598 python_export_best() {
599 debug-print-function ${FUNCNAME} "${@}"
601 eqawarn "python_export_best() is deprecated. Please use python_setup instead,"
602 eqawarn "combined with python_export if necessary."
604 [[ ${#} -gt 0 ]] || set -- EPYTHON PYTHON
606 local best MULTIBUILD_VARIANTS
610 best=${MULTIBUILD_VARIANT}
612 multibuild_for_best_variant _python_set_best
614 debug-print "${FUNCNAME}: Best implementation is: ${best}"
615 python_export "${best}" "${@}"
619 # @FUNCTION: python_replicate_script
622 # Copy the given script to variants for all enabled Python
623 # implementations, then replace it with a symlink to the wrapper.
625 # All specified files must start with a 'python' shebang. A file not
626 # having a matching shebang will be refused.
627 python_replicate_script() {
628 debug-print-function ${FUNCNAME} "${@}"
630 _python_replicate_script() {
631 local _PYTHON_FIX_SHEBANG_QUIET=1
633 local PYTHON_SCRIPTDIR
634 python_export PYTHON_SCRIPTDIR
637 exeinto "${PYTHON_SCRIPTDIR#${EPREFIX}}"
641 python_fix_shebang -q \
642 "${files[@]/*\//${D%/}/${PYTHON_SCRIPTDIR}/}"
645 local files=( "${@}" )
646 python_foreach_impl _python_replicate_script
648 # install the wrappers
651 _python_ln_rel "${ED%/}/usr/lib/python-exec/python-exec2" "${f}" || die