Drop $Id$ per council decision in bug #611234.
[gentoo.git] / eclass / python-single-r1.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: python-single-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 # @BLURB: An eclass for Python packages not installed for multiple implementations.
11 # @DESCRIPTION:
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.
15 #
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.
20 #
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).
24 #
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
28 # to inherit both.
29 #
30 # For more information, please see the wiki:
31 # https://wiki.gentoo.org/wiki/Project:Python/python-single-r1
32
33 case "${EAPI:-0}" in
34         0|1|2|3|4)
35                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
36                 ;;
37         5|6)
38                 # EAPI=5 is required for sane USE_EXPAND dependencies
39                 ;;
40         *)
41                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
42                 ;;
43 esac
44
45 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
46
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.'
51 fi
52
53 inherit python-utils-r1
54
55 fi
56
57 EXPORT_FUNCTIONS pkg_setup
58
59 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
60
61 # @ECLASS-VARIABLE: PYTHON_COMPAT
62 # @REQUIRED
63 # @DESCRIPTION:
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
66 # an array.
67 #
68 # Example:
69 # @CODE
70 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
71 # @CODE
72 #
73 # Please note that you can also use bash brace expansion if you like:
74 # @CODE
75 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
76 # @CODE
77
78 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
79 # @INTERNAL
80 # @DESCRIPTION:
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.
85 #
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.
91 #
92 # Example:
93 # @CODE
94 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
95 # @CODE
96
97 # @ECLASS-VARIABLE: PYTHON_REQ_USE
98 # @DEFAULT_UNSET
99 # @DESCRIPTION:
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
103 # use USE defaults.
104 #
105 # This should be set before calling `inherit'.
106 #
107 # Example:
108 # @CODE
109 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
110 # @CODE
111 #
112 # It will cause the Python dependencies to look like:
113 # @CODE
114 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
115 # @CODE
116
117 # @ECLASS-VARIABLE: PYTHON_DEPS
118 # @DESCRIPTION:
119 # This is an eclass-generated Python dependency string for all
120 # implementations listed in PYTHON_COMPAT.
121 #
122 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
123 #
124 # Example use:
125 # @CODE
126 # RDEPEND="${PYTHON_DEPS}
127 #       dev-foo/mydep"
128 # DEPEND="${RDEPEND}"
129 # @CODE
130 #
131 # Example value:
132 # @CODE
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] )
136 # @CODE
137
138 # @ECLASS-VARIABLE: PYTHON_USEDEP
139 # @DESCRIPTION:
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
142 # implementations.
143 #
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.
147 #
148 # Example use:
149 # @CODE
150 # RDEPEND="dev-python/foo[${PYTHON_USEDEP}]"
151 # @CODE
152 #
153 # Example value:
154 # @CODE
155 # python_targets_python2_7(-)?,python_single_target_python3_4(+)?
156 # @CODE
157
158 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
159 # @DESCRIPTION:
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.
164 #
165 # This expression should be utilized in an ebuild by including it in
166 # REQUIRED_USE, optionally behind a use flag.
167 #
168 # Example use:
169 # @CODE
170 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
171 # @CODE
172 #
173 # Example value:
174 # @CODE
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 )
178 # @CODE
179
180 _python_single_set_globals() {
181         _python_set_impls
182
183         PYTHON_DEPS=
184         local i PYTHON_PKG_DEP
185
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_}" )
189
190         local optflags=${flags_mt[@]/%/(-)?},${unflags[@]/%/(-)}
191
192         IUSE="${flags_mt[*]}"
193
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]/%/(+)}
205         else
206                 # Multiple supported implementations; honor PYTHON_SINGLE_TARGET.
207                 IUSE+=" ${flags[*]}"
208                 PYTHON_REQUIRED_USE="^^ ( ${flags[*]} )"
209                 # Ensure deps honor the same python_single_target_* flag as is set
210                 # on this package.
211                 optflags+=,${flags[@]/%/(+)?}
212
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} )"
218
219                         python_export "${i}" PYTHON_PKG_DEP
220                         PYTHON_DEPS+="python_single_target_${i}? ( ${PYTHON_PKG_DEP} ) "
221                 done
222         fi
223         PYTHON_USEDEP=${optflags// /,}
224
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}]"
235         else
236                 PYTHON_DEPS+="dev-lang/python-exec:2[${PYTHON_USEDEP}]"
237         fi
238         readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_USEDEP
239 }
240 _python_single_set_globals
241 unset -f _python_single_set_globals
242
243 # @FUNCTION: python_gen_usedep
244 # @USAGE: <pattern> [...]
245 # @DESCRIPTION:
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.
249 #
250 # Remember to escape or quote the patterns to prevent shell filename
251 # expansion.
252 #
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.
256 #
257 # Example:
258 # @CODE
259 # PYTHON_COMPAT=( python{2_7,3_4} )
260 # DEPEND="doc? ( dev-python/epydoc[$(python_gen_usedep 'python2*')] )"
261 # @CODE
262 #
263 # It will cause the dependency to look like:
264 # @CODE
265 # DEPEND="doc? ( dev-python/epydoc[python_targets_python2_7(-)?,...] )"
266 # @CODE
267 python_gen_usedep() {
268         debug-print-function ${FUNCNAME} "${@}"
269
270         local impl pattern
271         local matches=()
272
273         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
274                 for pattern; do
275                         if [[ ${impl} == ${pattern} ]]; then
276                                 matches+=(
277                                         "python_targets_${impl}(-)?"
278                                         "python_single_target_${impl}(+)?"
279                                 )
280                                 break
281                         fi
282                 done
283         done
284
285         [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
286
287         local out=${matches[@]}
288         echo "${out// /,}"
289 }
290
291 # @FUNCTION: python_gen_useflags
292 # @USAGE: <pattern> [...]
293 # @DESCRIPTION:
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.
297 #
298 # Example:
299 # @CODE
300 # PYTHON_COMPAT=( python{2_7,3_4} )
301 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
302 # @CODE
303 #
304 # It will cause the variable to look like:
305 # @CODE
306 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
307 # @CODE
308 python_gen_useflags() {
309         debug-print-function ${FUNCNAME} "${@}"
310
311         local flag_prefix impl pattern
312         local matches=()
313
314         if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
315                 flag_prefix=python_targets
316         else
317                 flag_prefix=python_single_target
318         fi
319
320         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
321                 for pattern; do
322                         if [[ ${impl} == ${pattern} ]]; then
323                                 matches+=( "${flag_prefix}_${impl}" )
324                                 break
325                         fi
326                 done
327         done
328
329         echo "${matches[@]}"
330 }
331
332 # @FUNCTION: python_gen_cond_dep
333 # @USAGE: <dependency> <pattern> [...]
334 # @DESCRIPTION:
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.
338 #
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.
343 #
344 # Example:
345 # @CODE
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 )"
349 # @CODE
350 #
351 # It will cause the variable to look like:
352 # @CODE
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(-)?,...] )"
357 # @CODE
358 python_gen_cond_dep() {
359         debug-print-function ${FUNCNAME} "${@}"
360
361         local flag_prefix impl pattern
362         local matches=()
363
364         if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
365                 flag_prefix=python_targets
366         else
367                 flag_prefix=python_single_target
368         fi
369
370         local dep=${1}
371         shift
372
373         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
374                 for pattern; 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}}
382                                 fi
383
384                                 matches+=( "${flag_prefix}_${impl}? ( ${dep} )" )
385                                 break
386                         fi
387                 done
388         done
389
390         echo "${matches[@]}"
391 }
392
393 # @FUNCTION: python_gen_impl_dep
394 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
395 # @DESCRIPTION:
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.
401 #
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.
406 #
407 # Example:
408 # @CODE
409 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
410 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
411 # @CODE
412 #
413 # It will cause the variable to look like:
414 # @CODE
415 # RDEPEND="foo? (
416 #   python_single_target_python2_7? (
417 #     dev-lang/python:2.7[xml(+)] )
418 #       python_single_target_pypy? (
419 #     dev-python/pypy[xml(+)] ) )"
420 # @CODE
421 python_gen_impl_dep() {
422         debug-print-function ${FUNCNAME} "${@}"
423
424         local impl pattern
425         local matches=()
426
427         if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
428                 flag_prefix=python_targets
429         else
430                 flag_prefix=python_single_target
431         fi
432
433         local PYTHON_REQ_USE=${1}
434         shift
435
436         local patterns=( "${@-*}" )
437         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
438                 for pattern in "${patterns[@]}"; do
439                         if [[ ${impl} == ${pattern} ]]; then
440                                 local PYTHON_PKG_DEP
441                                 python_export "${impl}" PYTHON_PKG_DEP
442                                 matches+=( "${flag_prefix}_${impl}? ( ${PYTHON_PKG_DEP} )" )
443                                 break
444                         fi
445                 done
446         done
447
448         echo "${matches[@]}"
449 }
450
451 # @FUNCTION: python_setup
452 # @DESCRIPTION:
453 # Determine what the selected Python implementation is and set
454 # the Python build environment up for it.
455 python_setup() {
456         debug-print-function ${FUNCNAME} "${@}"
457
458         unset EPYTHON
459
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"
464
465                 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
466                 ewarn "implementation will be used:"
467                 ewarn
468                 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
469                 ewarn
470                 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
471
472                 python_export "${impls[0]}" EPYTHON PYTHON
473                 python_wrapper_setup
474                 return
475         fi
476
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
481                         python_wrapper_setup
482                 fi
483         else
484                 local impl
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)."
492                                         echo
493                                         die "More than one implementation in PYTHON_SINGLE_TARGET."
494                                 fi
495
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."
501                                         echo
502                                         die "Build target (${impl}) not in PYTHON_TARGETS."
503                                 fi
504
505                                 python_export "${impl}" EPYTHON PYTHON
506                                 python_wrapper_setup
507                         fi
508                 done
509         fi
510
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"
515                 else
516                         eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
517                 fi
518                 eerror "of the following values:"
519                 eerror
520                 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
521                 echo
522                 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
523         fi
524 }
525
526 # @FUNCTION: python-single-r1_pkg_setup
527 # @DESCRIPTION:
528 # Runs python_setup.
529 python-single-r1_pkg_setup() {
530         debug-print-function ${FUNCNAME} "${@}"
531
532         [[ ${MERGE_TYPE} != binary ]] && python_setup
533 }
534
535 _PYTHON_SINGLE_R1=1
536 fi