app-eselect/eselect-postgresql: [QA] Fix BadHomepage
[gentoo.git] / eclass / python-r1.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: python-r1.eclass
5 # @MAINTAINER:
6 # Python team <python@gentoo.org>
7 # @AUTHOR:
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: A common, simple eclass for Python packages.
12 # @DESCRIPTION:
13 # A common eclass providing helper functions to build and install
14 # packages supporting being installed for multiple Python
15 # implementations.
16 #
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.
23 #
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
27 # both.
28 #
29 # For more information, please see the wiki:
30 # https://wiki.gentoo.org/wiki/Project:Python/python-r1
31
32 case "${EAPI:-0}" in
33         0|1|2|3|4)
34                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
35                 ;;
36         5|6|7)
37                 # EAPI=5 is required for sane USE_EXPAND dependencies
38                 ;;
39         *)
40                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
41                 ;;
42 esac
43
44 if [[ ! ${_PYTHON_R1} ]]; then
45
46 if [[ ${_PYTHON_SINGLE_R1} ]]; then
47         die 'python-r1.eclass can not be used with python-single-r1.eclass.'
48 elif [[ ${_PYTHON_ANY_R1} ]]; then
49         die 'python-r1.eclass can not be used with python-any-r1.eclass.'
50 fi
51
52 [[ ${EAPI} == [45] ]] && inherit eutils
53 inherit multibuild python-utils-r1
54
55 fi
56
57 # @ECLASS-VARIABLE: PYTHON_COMPAT
58 # @REQUIRED
59 # @DESCRIPTION:
60 # This variable contains a list of Python implementations the package
61 # supports. It must be set before the `inherit' call. It has to be
62 # an array.
63 #
64 # Example:
65 # @CODE
66 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
67 # @CODE
68 #
69 # Please note that you can also use bash brace expansion if you like:
70 # @CODE
71 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
72 # @CODE
73
74 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
75 # @INTERNAL
76 # @DESCRIPTION:
77 # This variable can be used when working with ebuilds to override
78 # the in-ebuild PYTHON_COMPAT. It is a string listing all
79 # the implementations which package will be built for. It need be
80 # specified in the calling environment, and not in ebuilds.
81 #
82 # It should be noted that in order to preserve metadata immutability,
83 # PYTHON_COMPAT_OVERRIDE does not affect IUSE nor dependencies.
84 # The state of PYTHON_TARGETS is ignored, and all the implementations
85 # in PYTHON_COMPAT_OVERRIDE are built. Dependencies need to be satisfied
86 # manually.
87 #
88 # Example:
89 # @CODE
90 # PYTHON_COMPAT_OVERRIDE='pypy python3_3' emerge -1v dev-python/foo
91 # @CODE
92
93 # @ECLASS-VARIABLE: PYTHON_REQ_USE
94 # @DEFAULT_UNSET
95 # @DESCRIPTION:
96 # The list of USEflags required to be enabled on the chosen Python
97 # implementations, formed as a USE-dependency string. It should be valid
98 # for all implementations in PYTHON_COMPAT, so it may be necessary to
99 # use USE defaults.
100 #
101 # This should be set before calling `inherit'.
102 #
103 # Example:
104 # @CODE
105 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
106 # @CODE
107 #
108 # It will cause the Python dependencies to look like:
109 # @CODE
110 # python_targets_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
111 # @CODE
112
113 # @ECLASS-VARIABLE: PYTHON_DEPS
114 # @DESCRIPTION:
115 # This is an eclass-generated Python dependency string for all
116 # implementations listed in PYTHON_COMPAT.
117 #
118 # Example use:
119 # @CODE
120 # RDEPEND="${PYTHON_DEPS}
121 #       dev-foo/mydep"
122 # DEPEND="${RDEPEND}"
123 # @CODE
124 #
125 # Example value:
126 # @CODE
127 # dev-lang/python-exec:=
128 # python_targets_python2_7? ( dev-lang/python:2.7[gdbm] )
129 # python_targets_pypy? ( dev-python/pypy[gdbm] )
130 # @CODE
131
132 # @ECLASS-VARIABLE: PYTHON_USEDEP
133 # @DESCRIPTION:
134 # This is an eclass-generated USE-dependency string which can be used to
135 # depend on another Python package being built for the same Python
136 # implementations.
137 #
138 # The generate USE-flag list is compatible with packages using python-r1
139 # and python-distutils-ng eclasses. It must not be used on packages
140 # using python.eclass.
141 #
142 # Example use:
143 # @CODE
144 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
145 # @CODE
146 #
147 # Example value:
148 # @CODE
149 # python_targets_python2_7(-)?,python_targets_python3_4(-)?
150 # @CODE
151
152 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
153 # @DESCRIPTION:
154 # This is an eclass-generated required-use expression which ensures at
155 # least one Python implementation has been enabled.
156 #
157 # This expression should be utilized in an ebuild by including it in
158 # REQUIRED_USE, optionally behind a use flag.
159 #
160 # Example use:
161 # @CODE
162 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
163 # @CODE
164 #
165 # Example value:
166 # @CODE
167 # || ( python_targets_python2_7 python_targets_python3_4 )
168 # @CODE
169
170 _python_set_globals() {
171         local deps i PYTHON_PKG_DEP
172
173         _python_set_impls
174
175         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
176                 python_export "${i}" PYTHON_PKG_DEP
177                 deps+="python_targets_${i}? ( ${PYTHON_PKG_DEP} ) "
178         done
179
180         local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_targets_}" )
181         local optflags=${flags[@]/%/(-)?}
182
183         # A nice QA trick here. Since a python-single-r1 package has to have
184         # at least one PYTHON_SINGLE_TARGET enabled (REQUIRED_USE),
185         # the following check will always fail on those packages. Therefore,
186         # it should prevent developers from mistakenly depending on packages
187         # not supporting multiple Python implementations.
188
189         local flags_st=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/-python_single_target_}" )
190         optflags+=,${flags_st[@]/%/(-)}
191         local requse="|| ( ${flags[*]} )"
192         local usedep=${optflags// /,}
193
194         # 1) well, python-exec would suffice as an RDEP
195         # but no point in making this overcomplex, BDEP doesn't hurt anyone
196         # 2) python-exec should be built with all targets forced anyway
197         # but if new targets were added, we may need to force a rebuild
198         deps+=">=dev-lang/python-exec-2:=[${usedep}]"
199
200         if [[ ${PYTHON_DEPS+1} ]]; then
201                 # IUSE is magical, so we can't really check it
202                 # (but we verify PYTHON_COMPAT already)
203
204                 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
205                         eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
206                         eerror "Before: ${PYTHON_DEPS}"
207                         eerror "Now   : ${deps}"
208                         die "PYTHON_DEPS integrity check failed"
209                 fi
210
211                 # these two are formality -- they depend on PYTHON_COMPAT only
212                 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
213                         eerror "PYTHON_REQUIRED_USE have changed between inherits!"
214                         eerror "Before: ${PYTHON_REQUIRED_USE}"
215                         eerror "Now   : ${requse}"
216                         die "PYTHON_REQUIRED_USE integrity check failed"
217                 fi
218
219                 if [[ ${PYTHON_USEDEP} != "${usedep}" ]]; then
220                         eerror "PYTHON_USEDEP have changed between inherits!"
221                         eerror "Before: ${PYTHON_USEDEP}"
222                         eerror "Now   : ${usedep}"
223                         die "PYTHON_USEDEP integrity check failed"
224                 fi
225         else
226                 IUSE=${flags[*]}
227
228                 PYTHON_DEPS=${deps}
229                 PYTHON_REQUIRED_USE=${requse}
230                 PYTHON_USEDEP=${usedep}
231                 readonly PYTHON_DEPS PYTHON_REQUIRED_USE
232         fi
233 }
234 _python_set_globals
235 unset -f _python_set_globals
236
237 if [[ ! ${_PYTHON_R1} ]]; then
238
239 # @FUNCTION: _python_validate_useflags
240 # @INTERNAL
241 # @DESCRIPTION:
242 # Enforce the proper setting of PYTHON_TARGETS, if PYTHON_COMPAT_OVERRIDE
243 # is not in effect. If it is, just warn that the flags will be ignored.
244 _python_validate_useflags() {
245         debug-print-function ${FUNCNAME} "${@}"
246
247         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
248                 if [[ ! ${_PYTHON_COMPAT_OVERRIDE_WARNED} ]]; then
249                         ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
250                         ewarn "implementations will be enabled:"
251                         ewarn
252                         ewarn " ${PYTHON_COMPAT_OVERRIDE}"
253                         ewarn
254                         ewarn "Dependencies won't be satisfied, and PYTHON_TARGETS will be ignored."
255                         _PYTHON_COMPAT_OVERRIDE_WARNED=1
256                 fi
257                 # we do not use flags with PCO
258                 return
259         fi
260
261         local i
262
263         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
264                 use "python_targets_${i}" && return 0
265         done
266
267         eerror "No Python implementation selected for the build. Please add one"
268         eerror "of the following values to your PYTHON_TARGETS (in make.conf):"
269         eerror
270         eerror "${PYTHON_COMPAT[@]}"
271         echo
272         die "No supported Python implementation in PYTHON_TARGETS."
273 }
274
275 # @FUNCTION: _python_gen_usedep
276 # @INTERNAL
277 # @USAGE: [<pattern>...]
278 # @DESCRIPTION:
279 # Output a USE dependency string for Python implementations which
280 # are both in PYTHON_COMPAT and match any of the patterns passed
281 # as parameters to the function.
282 #
283 # The patterns can be either fnmatch-style patterns (matched via bash
284 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
285 # appropriately all enabled Python 2/3 implementations (alike
286 # python_is_python3). Remember to escape or quote the fnmatch patterns
287 # to prevent accidental shell filename expansion.
288 #
289 # This is an internal function used to implement python_gen_cond_dep
290 # and deprecated python_gen_usedep.
291 _python_gen_usedep() {
292         debug-print-function ${FUNCNAME} "${@}"
293
294         local impl matches=()
295
296         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
297                 if _python_impl_matches "${impl}" "${@}"; then
298                         matches+=(
299                                 "python_targets_${impl}(-)?"
300                                 "-python_single_target_${impl}(-)"
301                         )
302                 fi
303         done
304
305         [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
306
307         local out=${matches[@]}
308         echo "${out// /,}"
309 }
310
311 # @FUNCTION: python_gen_usedep
312 # @USAGE: <pattern> [...]
313 # @DESCRIPTION:
314 # DEPRECATED.  Please use python_gen_cond_dep instead.
315 #
316 # Output a USE dependency string for Python implementations which
317 # are both in PYTHON_COMPAT and match any of the patterns passed
318 # as parameters to the function.
319 #
320 # The patterns can be either fnmatch-style patterns (matched via bash
321 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
322 # appropriately all enabled Python 2/3 implementations (alike
323 # python_is_python3). Remember to escape or quote the fnmatch patterns
324 # to prevent accidental shell filename expansion.
325 #
326 # When all implementations are requested, please use ${PYTHON_USEDEP}
327 # instead. Please also remember to set an appropriate REQUIRED_USE
328 # to avoid ineffective USE flags.
329 #
330 # Example:
331 # @CODE
332 # PYTHON_COMPAT=( python{2_7,3_4} )
333 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
334 # @CODE
335 #
336 # It will cause the dependency to look like:
337 # @CODE
338 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7?] )"
339 # @CODE
340 python_gen_usedep() {
341         debug-print-function ${FUNCNAME} "${@}"
342
343         # output only once, during some reasonable phase
344         # (avoid spamming cache regen runs)
345         if [[ ${EBUILD_PHASE} == setup ]]; then
346                 eqawarn "python_gen_usedep() is deprecated. Please use python_gen_cond_dep instead."
347         fi
348         _python_gen_usedep "${@}"
349 }
350
351 # @FUNCTION: python_gen_useflags
352 # @USAGE: [<pattern>...]
353 # @DESCRIPTION:
354 # Output a list of USE flags for Python implementations which
355 # are both in PYTHON_COMPAT and match any of the patterns passed
356 # as parameters to the function.
357 #
358 # The patterns can be either fnmatch-style patterns (matched via bash
359 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
360 # appropriately all enabled Python 2/3 implementations (alike
361 # python_is_python3). Remember to escape or quote the fnmatch patterns
362 # to prevent accidental shell filename expansion.
363 #
364 # Example:
365 # @CODE
366 # PYTHON_COMPAT=( python{2_7,3_4} )
367 # REQUIRED_USE="doc? ( || ( $(python_gen_useflags python2*) ) )"
368 # @CODE
369 #
370 # It will cause the variable to look like:
371 # @CODE
372 # REQUIRED_USE="doc? ( || ( python_targets_python2_7 ) )"
373 # @CODE
374 python_gen_useflags() {
375         debug-print-function ${FUNCNAME} "${@}"
376
377         local impl matches=()
378
379         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
380                 if _python_impl_matches "${impl}" "${@}"; then
381                         matches+=( "python_targets_${impl}" )
382                 fi
383         done
384
385         echo "${matches[@]}"
386 }
387
388 # @FUNCTION: python_gen_cond_dep
389 # @USAGE: <dependency> [<pattern>...]
390 # @DESCRIPTION:
391 # Output a list of <dependency>-ies made conditional to USE flags
392 # of Python implementations which are both in PYTHON_COMPAT and match
393 # any of the patterns passed as the remaining parameters.
394 #
395 # The patterns can be either fnmatch-style patterns (matched via bash
396 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
397 # appropriately all enabled Python 2/3 implementations (alike
398 # python_is_python3). Remember to escape or quote the fnmatch patterns
399 # to prevent accidental shell filename expansion.
400 #
401 # In order to enforce USE constraints on the packages, verbatim
402 # '${PYTHON_USEDEP}' (quoted!) may be placed in the dependency
403 # specification. It will get expanded within the function into a proper
404 # USE dependency string.
405 #
406 # Example:
407 # @CODE
408 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
409 # RDEPEND="$(python_gen_cond_dep \
410 #   'dev-python/unittest2[${PYTHON_USEDEP}]' python2_7 pypy )"
411 # @CODE
412 #
413 # It will cause the variable to look like:
414 # @CODE
415 # RDEPEND="python_targets_python2_7? (
416 #     dev-python/unittest2[python_targets_python2_7?] )
417 #       python_targets_pypy? (
418 #     dev-python/unittest2[python_targets_pypy?] )"
419 # @CODE
420 python_gen_cond_dep() {
421         debug-print-function ${FUNCNAME} "${@}"
422
423         local impl matches=()
424         local dep=${1}
425         shift
426
427         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
428                 if _python_impl_matches "${impl}" "${@}"; then
429                         # substitute ${PYTHON_USEDEP} if used
430                         # (since python_gen_usedep() will not return ${PYTHON_USEDEP}
431                         #  the code is run at most once)
432                         if [[ ${dep} == *'${PYTHON_USEDEP}'* ]]; then
433                                 local usedep=$(_python_gen_usedep "${@}")
434                                 dep=${dep//\$\{PYTHON_USEDEP\}/${usedep}}
435                         fi
436
437                         matches+=( "python_targets_${impl}? ( ${dep} )" )
438                 fi
439         done
440
441         echo "${matches[@]}"
442 }
443
444 # @FUNCTION: python_gen_impl_dep
445 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
446 # @DESCRIPTION:
447 # Output a dependency on Python implementations with the specified USE
448 # dependency string appended, or no USE dependency string if called
449 # without the argument (or with empty argument). If any implementation
450 # patterns are passed, the output dependencies will be generated only
451 # for the implementations matching them.
452 #
453 # The patterns can be either fnmatch-style patterns (matched via bash
454 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
455 # appropriately all enabled Python 2/3 implementations (alike
456 # python_is_python3). Remember to escape or quote the fnmatch patterns
457 # to prevent accidental shell filename expansion.
458 #
459 # Use this function when you need to request different USE flags
460 # on the Python interpreter depending on package's USE flags. If you
461 # only need a single set of interpreter USE flags, just set
462 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
463 #
464 # Example:
465 # @CODE
466 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
467 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
468 # @CODE
469 #
470 # It will cause the variable to look like:
471 # @CODE
472 # RDEPEND="foo? (
473 #   python_targets_python2_7? (
474 #     dev-lang/python:2.7[xml(+)] )
475 #       python_targets_pypy? (
476 #     dev-python/pypy[xml(+)] ) )"
477 # @CODE
478 python_gen_impl_dep() {
479         debug-print-function ${FUNCNAME} "${@}"
480
481         local impl matches=()
482         local PYTHON_REQ_USE=${1}
483         shift
484
485         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
486                 if _python_impl_matches "${impl}" "${@}"; then
487                         local PYTHON_PKG_DEP
488                         python_export "${impl}" PYTHON_PKG_DEP
489                         matches+=( "python_targets_${impl}? ( ${PYTHON_PKG_DEP} )" )
490                 fi
491         done
492
493         echo "${matches[@]}"
494 }
495
496 # @FUNCTION: python_gen_any_dep
497 # @USAGE: <dependency-block> [<impl-pattern>...]
498 # @DESCRIPTION:
499 # Generate an any-of dependency that enforces a version match between
500 # the Python interpreter and Python packages. <dependency-block> needs
501 # to list one or more dependencies with verbatim '${PYTHON_USEDEP}'
502 # references (quoted!) that will get expanded inside the function.
503 # Optionally, patterns may be specified to restrict the dependency
504 # to a subset of Python implementations supported by the ebuild.
505 #
506 # The patterns can be either fnmatch-style patterns (matched via bash
507 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
508 # appropriately all enabled Python 2/3 implementations (alike
509 # python_is_python3). Remember to escape or quote the fnmatch patterns
510 # to prevent accidental shell filename expansion.
511 #
512 # This should be used along with an appropriate python_check_deps()
513 # that checks which of the any-of blocks were matched, and python_setup
514 # call that enables use of the matched implementation.
515 #
516 # Example use:
517 # @CODE
518 # DEPEND="$(python_gen_any_dep '
519 #       dev-python/foo[${PYTHON_USEDEP}]
520 #       || ( dev-python/bar[${PYTHON_USEDEP}]
521 #               dev-python/baz[${PYTHON_USEDEP}] )' -2)"
522 #
523 # python_check_deps() {
524 #       has_version "dev-python/foo[${PYTHON_USEDEP}]" \
525 #               && { has_version "dev-python/bar[${PYTHON_USEDEP}]" \
526 #                       || has_version "dev-python/baz[${PYTHON_USEDEP}]"; }
527 # }
528 #
529 # src_compile() {
530 #       python_foreach_impl usual_code
531 #
532 #       # some common post-build task that requires Python 2
533 #       python_setup -2
534 #       emake frobnicate
535 # }
536 # @CODE
537 #
538 # Example value:
539 # @CODE
540 # || (
541 #       (
542 #               dev-lang/python:2.7
543 #               dev-python/foo[python_targets_python2_7(-)?,python_single_target_python2_7(+)?]
544 #               || ( dev-python/bar[python_targets_python2_7(-)?,python_single_target_python2_7(+)?]
545 #                       dev-python/baz[python_targets_python2_7(-)?,python_single_target_python2_7(+)?] )
546 #       )
547 #       (
548 #               dev-lang/python:3.3
549 #               dev-python/foo[python_targets_python3_3(-)?,python_single_target_python3_3(+)?]
550 #               || ( dev-python/bar[python_targets_python3_3(-)?,python_single_target_python3_3(+)?]
551 #                       dev-python/baz[python_targets_python3_3(-)?,python_single_target_python3_3(+)?] )
552 #       )
553 # )
554 # @CODE
555 python_gen_any_dep() {
556         debug-print-function ${FUNCNAME} "${@}"
557
558         local depstr=${1}
559         [[ ${depstr} ]] || die "No dependency string provided"
560         shift
561
562         local i PYTHON_PKG_DEP out=
563         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
564                 if _python_impl_matches "${i}" "${@}"; then
565                         local PYTHON_USEDEP="python_targets_${i}(-),python_single_target_${i}(+)"
566                         python_export "${i}" PYTHON_PKG_DEP
567
568                         local i_depstr=${depstr//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
569                         # note: need to strip '=' slot operator for || deps
570                         out="( ${PYTHON_PKG_DEP/:0=/:0} ${i_depstr} ) ${out}"
571                 fi
572         done
573         echo "|| ( ${out})"
574 }
575
576 # @ECLASS-VARIABLE: BUILD_DIR
577 # @DESCRIPTION:
578 # The current build directory. In global scope, it is supposed to
579 # contain an initial build directory; if unset, it defaults to ${S}.
580 #
581 # In functions run by python_foreach_impl(), the BUILD_DIR is locally
582 # set to an implementation-specific build directory. That path is
583 # created through appending a hyphen and the implementation name
584 # to the final component of the initial BUILD_DIR.
585 #
586 # Example value:
587 # @CODE
588 # ${WORKDIR}/foo-1.3-python2_7
589 # @CODE
590
591 # @FUNCTION: python_copy_sources
592 # @DESCRIPTION:
593 # Create a single copy of the package sources for each enabled Python
594 # implementation.
595 #
596 # The sources are always copied from initial BUILD_DIR (or S if unset)
597 # to implementation-specific build directory matching BUILD_DIR used by
598 # python_foreach_abi().
599 python_copy_sources() {
600         debug-print-function ${FUNCNAME} "${@}"
601
602         local MULTIBUILD_VARIANTS
603         _python_obtain_impls
604
605         multibuild_copy_sources
606 }
607
608 # @FUNCTION: _python_obtain_impls
609 # @INTERNAL
610 # @DESCRIPTION:
611 # Set up the enabled implementation list.
612 _python_obtain_impls() {
613         _python_validate_useflags
614
615         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
616                 MULTIBUILD_VARIANTS=( ${PYTHON_COMPAT_OVERRIDE} )
617                 return
618         fi
619
620         MULTIBUILD_VARIANTS=()
621
622         local impl
623         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
624                 has "${impl}" "${PYTHON_COMPAT[@]}" && \
625                 use "python_targets_${impl}" && MULTIBUILD_VARIANTS+=( "${impl}" )
626         done
627 }
628
629 # @FUNCTION: _python_multibuild_wrapper
630 # @USAGE: <command> [<args>...]
631 # @INTERNAL
632 # @DESCRIPTION:
633 # Initialize the environment for Python implementation selected
634 # for multibuild.
635 _python_multibuild_wrapper() {
636         debug-print-function ${FUNCNAME} "${@}"
637
638         local -x EPYTHON PYTHON
639         local -x PATH=${PATH} PKG_CONFIG_PATH=${PKG_CONFIG_PATH}
640         python_export "${MULTIBUILD_VARIANT}" EPYTHON PYTHON
641         python_wrapper_setup
642
643         "${@}"
644 }
645
646 # @FUNCTION: python_foreach_impl
647 # @USAGE: <command> [<args>...]
648 # @DESCRIPTION:
649 # Run the given command for each of the enabled Python implementations.
650 # If additional parameters are passed, they will be passed through
651 # to the command.
652 #
653 # The function will return 0 status if all invocations succeed.
654 # Otherwise, the return code from first failing invocation will
655 # be returned.
656 #
657 # For each command being run, EPYTHON, PYTHON and BUILD_DIR are set
658 # locally, and the former two are exported to the command environment.
659 python_foreach_impl() {
660         debug-print-function ${FUNCNAME} "${@}"
661
662         local MULTIBUILD_VARIANTS
663         _python_obtain_impls
664
665         multibuild_foreach_variant _python_multibuild_wrapper "${@}"
666 }
667
668 # @FUNCTION: python_setup
669 # @USAGE: [<impl-pattern>...]
670 # @DESCRIPTION:
671 # Find the best (most preferred) Python implementation that is suitable
672 # for running common Python code. Set the Python build environment up
673 # for that implementation. This function has two modes of operation:
674 # pure and any-of dep.
675 #
676 # The pure mode is used if python_check_deps() function is not declared.
677 # In this case, an implementation is considered suitable if it is
678 # supported (in PYTHON_COMPAT), enabled (via USE flags) and matches
679 # at least one of the patterns passed (or '*' if no patterns passed).
680 #
681 # Implementation restrictions in the pure mode need to be accompanied
682 # by appropriate REQUIRED_USE constraints. Otherwise, the eclass may
683 # fail at build time due to unsatisfied dependencies.
684 #
685 # The any-of dep mode is used if python_check_deps() is declared.
686 # In this mode, an implementation is considered suitable if it is
687 # supported, matches at least one of the patterns and python_check_deps()
688 # has successful return code. USE flags are not considered.
689 #
690 # The python_check_deps() function in the any-of mode needs to be
691 # accompanied by appropriate any-of dependencies.
692 #
693 # The patterns can be either fnmatch-style patterns (matched via bash
694 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
695 # appropriately all enabled Python 2/3 implementations (alike
696 # python_is_python3). Remember to escape or quote the fnmatch patterns
697 # to prevent accidental shell filename expansion.
698 #
699 # This function needs to be used when Python is being called outside
700 # of python_foreach_impl calls (e.g. for shared processes like doc
701 # building). python_foreach_impl sets up the build environment itself.
702 #
703 # Pure mode example:
704 # @CODE
705 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
706 # REQUIRED_USE="doc? ( $(python_gen_useflags 'python2*') )"
707 #
708 # src_compile() {
709 #   #...
710 #   if use doc; then
711 #     python_setup 'python2*'
712 #     make doc
713 #   fi
714 # }
715 # @CODE
716 #
717 # Any-of mode example:
718 # @CODE
719 # DEPEND="doc? (
720 #       $(python_gen_any_dep 'dev-python/epydoc[${PYTHON_USEDEP}]' 'python2*') )"
721 #
722 # python_check_deps() {
723 #       has_version "dev-python/epydoc[${PYTHON_USEDEP}]"
724 # }
725 #
726 # src_compile() {
727 #   #...
728 #   if use doc; then
729 #     python_setup 'python2*'
730 #     make doc
731 #   fi
732 # }
733 # @CODE
734 python_setup() {
735         debug-print-function ${FUNCNAME} "${@}"
736
737         _python_validate_useflags
738         local pycompat=( "${PYTHON_COMPAT[@]}" )
739         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
740                 pycompat=( ${PYTHON_COMPAT_OVERRIDE} )
741         fi
742
743         local has_check_deps
744         declare -f python_check_deps >/dev/null && has_check_deps=1
745
746         # (reverse iteration -- newest impl first)
747         local found
748         for (( i = ${#_PYTHON_SUPPORTED_IMPLS[@]} - 1; i >= 0; i-- )); do
749                 local impl=${_PYTHON_SUPPORTED_IMPLS[i]}
750
751                 # check PYTHON_COMPAT[_OVERRIDE]
752                 has "${impl}" "${pycompat[@]}" || continue
753
754                 # match USE flags only if override is not in effect
755                 # and python_check_deps() is not defined
756                 if [[ ! ${PYTHON_COMPAT_OVERRIDE} && ! ${has_check_deps} ]]; then
757                         use "python_targets_${impl}" || continue
758                 fi
759
760                 # check patterns
761                 _python_impl_matches "${impl}" "${@}" || continue
762
763                 python_export "${impl}" EPYTHON PYTHON
764
765                 # if python_check_deps() is declared, switch into any-of mode
766                 if [[ ${has_check_deps} ]]; then
767                         # first check if the interpreter is installed
768                         python_is_installed "${impl}" || continue
769                         # then run python_check_deps
770                         local PYTHON_USEDEP="python_targets_${impl}(-),python_single_target_${impl}(+)"
771                         python_check_deps || continue
772                 fi
773
774                 found=1
775                 break
776         done
777
778         if [[ ! ${found} ]]; then
779                 eerror "${FUNCNAME}: none of the enabled implementation matched the patterns."
780                 eerror "  patterns: ${@-'(*)'}"
781                 eerror "Likely a REQUIRED_USE constraint (possibly USE-conditional) is missing."
782                 eerror "  suggested: || ( \$(python_gen_useflags ${@}) )"
783                 eerror "(remember to quote all the patterns with '')"
784                 die "${FUNCNAME}: no enabled implementation satisfy requirements"
785         fi
786
787         python_wrapper_setup
788 }
789
790 # @FUNCTION: python_replicate_script
791 # @USAGE: <path>...
792 # @DESCRIPTION:
793 # Copy the given script to variants for all enabled Python
794 # implementations, then replace it with a symlink to the wrapper.
795 #
796 # All specified files must start with a 'python' shebang. A file not
797 # having a matching shebang will be refused.
798 python_replicate_script() {
799         debug-print-function ${FUNCNAME} "${@}"
800
801         _python_replicate_script() {
802                 local _PYTHON_FIX_SHEBANG_QUIET=1
803
804                 local PYTHON_SCRIPTDIR
805                 python_export PYTHON_SCRIPTDIR
806
807                 (
808                         exeopts -m 0755
809                         exeinto "${PYTHON_SCRIPTDIR#${EPREFIX}}"
810                         doexe "${files[@]}"
811                 )
812
813                 python_fix_shebang -q \
814                         "${files[@]/*\//${D%/}/${PYTHON_SCRIPTDIR}/}"
815         }
816
817         local files=( "${@}" )
818         python_foreach_impl _python_replicate_script
819         unset -f _python_replicate_script
820
821         # install the wrappers
822         local f
823         for f; do
824                 _python_ln_rel "${ED%/}/usr/lib/python-exec/python-exec2" "${f}" || die
825         done
826 }
827
828 _PYTHON_R1=1
829 fi