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 [[ ${EAPI} == [45] ]] && inherit eutils
67 inherit multibuild python-utils-r1
69 # @ECLASS-VARIABLE: PYTHON_COMPAT
72 # This variable contains a list of Python implementations the package
73 # supports. It must be set before the `inherit' call. It has to be
78 # PYTHON_COMPAT=( python2_7 python3_3 python3_4} )
81 # Please note that you can also use bash brace expansion if you like:
83 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
85 if ! declare -p PYTHON_COMPAT &>/dev/null; then
86 die 'PYTHON_COMPAT not declared.'
88 if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then
89 die 'PYTHON_COMPAT must be an array.'
92 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
95 # This variable can be used when working with ebuilds to override
96 # the in-ebuild PYTHON_COMPAT. It is a string listing all
97 # the implementations which package will be built for. It need be
98 # specified in the calling environment, and not in ebuilds.
100 # It should be noted that in order to preserve metadata immutability,
101 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
102 # The state of PYTHON_TARGETS is ignored, and all the implementations
103 # in PYTHON_COMPAT_OVERRIDE are built. Dependencies need to be satisfied
108 # PYTHON_COMPAT_OVERRIDE='pypy python3_3' emerge -1v dev-python/foo
111 # @ECLASS-VARIABLE: PYTHON_REQ_USE
114 # The list of USEflags required to be enabled on the chosen Python
115 # implementations, formed as a USE-dependency string. It should be valid
116 # for all implementations in PYTHON_COMPAT, so it may be necessary to
119 # This should be set before calling `inherit'.
123 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
126 # It will cause the Python dependencies to look like:
128 # python_targets_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
131 # @ECLASS-VARIABLE: PYTHON_DEPS
133 # This is an eclass-generated Python dependency string for all
134 # implementations listed in PYTHON_COMPAT.
138 # RDEPEND="${PYTHON_DEPS}
140 # DEPEND="${RDEPEND}"
145 # dev-lang/python-exec:=
146 # python_targets_python2_7? ( dev-lang/python:2.7[gdbm] )
147 # python_targets_pypy? ( virtual/pypy[gdbm] )
150 # @ECLASS-VARIABLE: PYTHON_USEDEP
152 # This is an eclass-generated USE-dependency string which can be used to
153 # depend on another Python package being built for the same Python
156 # The generate USE-flag list is compatible with packages using python-r1
157 # and python-distutils-ng eclasses. It must not be used on packages
158 # using python.eclass.
162 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
167 # python_targets_python2_7(-)?,python_targets_python3_4(-)?
170 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
172 # This is an eclass-generated required-use expression which ensures at
173 # least one Python implementation has been enabled.
175 # This expression should be utilized in an ebuild by including it in
176 # REQUIRED_USE, optionally behind a use flag.
180 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
185 # || ( python_targets_python2_7 python_targets_python3_4 )
188 _python_set_globals() {
192 local i PYTHON_PKG_DEP
193 for i in "${PYTHON_COMPAT[@]}"; do
194 _python_impl_supported "${i}" || continue
196 python_export "${i}" PYTHON_PKG_DEP
197 PYTHON_DEPS+="python_targets_${i}? ( ${PYTHON_PKG_DEP} ) "
202 if [[ ${#impls[@]} -eq 0 ]]; then
203 die "No supported implementation in PYTHON_COMPAT."
206 local flags=( "${impls[@]/#/python_targets_}" )
207 local optflags=${flags[@]/%/(-)?}
209 # A nice QA trick here. Since a python-single-r1 package has to have
210 # at least one PYTHON_SINGLE_TARGET enabled (REQUIRED_USE),
211 # the following check will always fail on those packages. Therefore,
212 # it should prevent developers from mistakenly depending on packages
213 # not supporting multiple Python implementations.
215 local flags_st=( "${impls[@]/#/-python_single_target_}" )
216 optflags+=,${flags_st[@]/%/(-)}
219 PYTHON_REQUIRED_USE="|| ( ${flags[*]} )"
220 PYTHON_USEDEP=${optflags// /,}
222 # 1) well, python-exec would suffice as an RDEP
223 # but no point in making this overcomplex, BDEP doesn't hurt anyone
224 # 2) python-exec should be built with all targets forced anyway
225 # but if new targets were added, we may need to force a rebuild
226 # 3) use whichever python-exec slot installed in EAPI 5. For EAPI 4,
227 # just fix :2 since := deps are not supported.
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"
230 elif [[ ${EAPI} != 4 ]]; then
231 PYTHON_DEPS+=">=dev-lang/python-exec-2:=[${PYTHON_USEDEP}]"
233 PYTHON_DEPS+="dev-lang/python-exec:2[${PYTHON_USEDEP}]"
238 # @FUNCTION: _python_validate_useflags
241 # Enforce the proper setting of PYTHON_TARGETS.
242 _python_validate_useflags() {
243 debug-print-function ${FUNCNAME} "${@}"
247 for i in "${PYTHON_COMPAT[@]}"; do
248 _python_impl_supported "${i}" || continue
250 use "python_targets_${i}" && return 0
253 eerror "No Python implementation selected for the build. Please add one"
254 eerror "of the following values to your PYTHON_TARGETS (in make.conf):"
256 eerror "${PYTHON_COMPAT[@]}"
258 die "No supported Python implementation in PYTHON_TARGETS."
261 # @FUNCTION: python_gen_usedep
262 # @USAGE: <pattern> [...]
264 # Output a USE dependency string for Python implementations which
265 # are both in PYTHON_COMPAT and match any of the patterns passed
266 # as parameters to the function.
268 # Remember to escape or quote the patterns to prevent shell filename
271 # When all implementations are requested, please use ${PYTHON_USEDEP}
272 # instead. Please also remember to set an appropriate REQUIRED_USE
273 # to avoid ineffective USE flags.
277 # PYTHON_COMPAT=( python{2_7,3_4} )
278 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
281 # It will cause the dependency to look like:
283 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7?] )"
285 python_gen_usedep() {
286 debug-print-function ${FUNCNAME} "${@}"
291 for impl in "${PYTHON_COMPAT[@]}"; do
292 _python_impl_supported "${impl}" || continue
295 if [[ ${impl} == ${pattern} ]]; then
297 "python_targets_${impl}(-)?"
298 "-python_single_target_${impl}(-)"
305 [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
307 local out=${matches[@]}
311 # @FUNCTION: python_gen_useflags
312 # @USAGE: <pattern> [...]
314 # Output a list of USE flags for Python implementations which
315 # are both in PYTHON_COMPAT and match any of the patterns passed
316 # as parameters to the function.
320 # PYTHON_COMPAT=( python{2_7,3_4} )
321 # REQUIRED_USE="doc? ( || ( $(python_gen_useflags python2*) ) )"
324 # It will cause the variable to look like:
326 # REQUIRED_USE="doc? ( || ( python_targets_python2_7 ) )"
328 python_gen_useflags() {
329 debug-print-function ${FUNCNAME} "${@}"
334 for impl in "${PYTHON_COMPAT[@]}"; do
335 _python_impl_supported "${impl}" || continue
338 if [[ ${impl} == ${pattern} ]]; then
339 matches+=( "python_targets_${impl}" )
348 # @FUNCTION: python_gen_cond_dep
349 # @USAGE: <dependency> <pattern> [...]
351 # Output a list of <dependency>-ies made conditional to USE flags
352 # of Python implementations which are both in PYTHON_COMPAT and match
353 # any of the patterns passed as the remaining parameters.
355 # In order to enforce USE constraints on the packages, verbatim
356 # '${PYTHON_USEDEP}' (quoted!) may be placed in the dependency
357 # specification. It will get expanded within the function into a proper
358 # USE dependency string.
362 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
363 # RDEPEND="$(python_gen_cond_dep \
364 # 'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
367 # It will cause the variable to look like:
369 # RDEPEND="python_targets_python2_7? (
370 # dev-python/unittest2[python_targets_python2_7?] )
371 # python_targets_pypy? (
372 # dev-python/unittest2[python_targets_pypy?] )"
374 python_gen_cond_dep() {
375 debug-print-function ${FUNCNAME} "${@}"
383 for impl in "${PYTHON_COMPAT[@]}"; do
384 _python_impl_supported "${impl}" || continue
387 if [[ ${impl} == ${pattern} ]]; then
388 # substitute ${PYTHON_USEDEP} if used
389 # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
390 # the code is run at most once)
391 if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
392 local PYTHON_USEDEP=$(python_gen_usedep "${@}")
393 dep=${dep//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
396 matches+=( "python_targets_${impl}? ( ${dep} )" )
405 # @ECLASS-VARIABLE: BUILD_DIR
407 # The current build directory. In global scope, it is supposed to
408 # contain an initial build directory; if unset, it defaults to ${S}.
410 # In functions run by python_foreach_impl(), the BUILD_DIR is locally
411 # set to an implementation-specific build directory. That path is
412 # created through appending a hyphen and the implementation name
413 # to the final component of the initial BUILD_DIR.
417 # ${WORKDIR}/foo-1.3-python2_7
420 # @FUNCTION: python_copy_sources
422 # Create a single copy of the package sources for each enabled Python
425 # The sources are always copied from initial BUILD_DIR (or S if unset)
426 # to implementation-specific build directory matching BUILD_DIR used by
427 # python_foreach_abi().
428 python_copy_sources() {
429 debug-print-function ${FUNCNAME} "${@}"
431 local MULTIBUILD_VARIANTS
434 multibuild_copy_sources
437 # @FUNCTION: _python_obtain_impls
440 # Set up the enabled implementation list.
441 _python_obtain_impls() {
442 if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
443 if [[ ! ${_PYTHON_COMPAT_OVERRIDE_WARNED} ]]; then
444 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
445 ewarn "implementations will be enabled:"
447 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
449 ewarn "Dependencies won't be satisfied, and PYTHON_TARGETS will be ignored."
450 _PYTHON_COMPAT_OVERRIDE_WARNED=1
453 MULTIBUILD_VARIANTS=( ${PYTHON_COMPAT_OVERRIDE} )
457 _python_validate_useflags
459 MULTIBUILD_VARIANTS=()
461 for impl in "${_PYTHON_ALL_IMPLS[@]}"; do
462 if has "${impl}" "${PYTHON_COMPAT[@]}" \
463 && use "python_targets_${impl}"
465 MULTIBUILD_VARIANTS+=( "${impl}" )
470 # @FUNCTION: _python_multibuild_wrapper
471 # @USAGE: <command> [<args>...]
474 # Initialize the environment for Python implementation selected
476 _python_multibuild_wrapper() {
477 debug-print-function ${FUNCNAME} "${@}"
479 local -x EPYTHON PYTHON
480 local -x PATH=${PATH} PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
481 python_export "${MULTIBUILD_VARIANT}" EPYTHON PYTHON
487 # @FUNCTION: python_foreach_impl
488 # @USAGE: <command> [<args>...]
490 # Run the given command for each of the enabled Python implementations.
491 # If additional parameters are passed, they will be passed through
494 # The function will return 0 status if all invocations succeed.
495 # Otherwise, the return code from first failing invocation will
498 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
499 # locally, and the former two are exported to the command environment.
500 python_foreach_impl() {
501 debug-print-function ${FUNCNAME} "${@}"
503 local MULTIBUILD_VARIANTS
506 multibuild_foreach_variant _python_multibuild_wrapper "${@}"
509 # @FUNCTION: python_parallel_foreach_impl
510 # @USAGE: <command> [<args>...]
512 # Run the given command for each of the enabled Python implementations.
513 # If additional parameters are passed, they will be passed through
516 # The function will return 0 status if all invocations succeed.
517 # Otherwise, the return code from first failing invocation will
520 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
521 # locally, and the former two are exported to the command environment.
523 # This command used to be the parallel variant of python_foreach_impl.
524 # However, the parallel run support has been removed to simplify
525 # the eclasses and make them more predictable and therefore it is now
526 # only a deprecated alias to python_foreach_impl.
527 python_parallel_foreach_impl() {
528 debug-print-function ${FUNCNAME} "${@}"
530 [[ ${EAPI} == [45] ]] || die "${FUNCNAME} is banned in EAPI ${EAPI}"
532 if [[ ! ${_PYTHON_PARALLEL_WARNED} ]]; then
533 eqawarn "python_parallel_foreach_impl() is no longer meaningful. All runs"
534 eqawarn "are non-parallel now. Please replace the call with python_foreach_impl."
536 _PYTHON_PARALLEL_WARNED=1
539 local MULTIBUILD_VARIANTS
541 multibuild_foreach_variant _python_multibuild_wrapper "${@}"
544 # @FUNCTION: python_setup
545 # @USAGE: [<impl-pattern>...]
547 # Find the best (most preferred) Python implementation that is enabled
548 # and matches at least one of the patterns passed (or '*' if no patterns
549 # passed). Set the Python build environment up for that implementation.
551 # This function needs to be used when Python is being called outside
552 # of python_foreach_impl calls (e.g. for shared processes like doc
553 # building). python_foreach_impl sets up the build environment itself.
555 # If the specific commands support only a subset of Python
556 # implementations, patterns need to be passed to restrict the allowed
561 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
566 # python_setup 'python2*'
572 debug-print-function ${FUNCNAME} "${@}"
574 local best_impl patterns=( "${@-*}" )
577 for pattern in "${patterns[@]}"; do
578 if [[ ${EPYTHON} == ${pattern} ]]; then
583 python_foreach_impl _python_try_impl
585 if [[ ! ${best_impl} ]]; then
586 eerror "${FUNCNAME}: none of the enabled implementation matched the patterns."
587 eerror " patterns: ${@-'(*)'}"
588 eerror "Likely a REQUIRED_USE constraint (possibly USE-conditional) is missing."
589 eerror " suggested: || ( \$(python_gen_useflags ${@}) )"
590 eerror "(remember to quote all the patterns with '')"
591 die "${FUNCNAME}: no enabled implementation satisfy requirements"
594 python_export "${best_impl}" EPYTHON PYTHON
598 # @FUNCTION: python_export_best
599 # @USAGE: [<variable>...]
601 # Find the best (most preferred) Python implementation enabled
602 # and export given variables for it. If no variables are provided,
603 # EPYTHON & PYTHON will be exported.
604 python_export_best() {
605 debug-print-function ${FUNCNAME} "${@}"
607 [[ ${EAPI} == [45] ]] || die "${FUNCNAME} is banned in EAPI ${EAPI}"
609 eqawarn "python_export_best() is deprecated. Please use python_setup instead,"
610 eqawarn "combined with python_export if necessary."
612 [[ ${#} -gt 0 ]] || set -- EPYTHON PYTHON
614 local best MULTIBUILD_VARIANTS
618 best=${MULTIBUILD_VARIANT}
620 multibuild_for_best_variant _python_set_best
622 debug-print "${FUNCNAME}: Best implementation is: ${best}"
623 python_export "${best}" "${@}"
627 # @FUNCTION: python_replicate_script
630 # Copy the given script to variants for all enabled Python
631 # implementations, then replace it with a symlink to the wrapper.
633 # All specified files must start with a 'python' shebang. A file not
634 # having a matching shebang will be refused.
635 python_replicate_script() {
636 debug-print-function ${FUNCNAME} "${@}"
638 _python_replicate_script() {
639 local _PYTHON_FIX_SHEBANG_QUIET=1
641 local PYTHON_SCRIPTDIR
642 python_export PYTHON_SCRIPTDIR
645 exeinto "${PYTHON_SCRIPTDIR#${EPREFIX}}"
649 python_fix_shebang -q \
650 "${files[@]/*\//${D%/}/${PYTHON_SCRIPTDIR}/}"
653 local files=( "${@}" )
654 python_foreach_impl _python_replicate_script
656 # install the wrappers
659 _python_ln_rel "${ED%/}/usr/lib/python-exec/python-exec2" "${f}" || die