python-single-r1.eclass: Remove deprecated PYTHON_USEDEP API
[gentoo.git] / eclass / python-single-r1.eclass
1 # Copyright 1999-2018 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 # @SUPPORTED_EAPIS: 5 6 7
11 # @BLURB: An eclass for Python packages not installed for multiple implementations.
12 # @DESCRIPTION:
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
16 # build systems.
17 #
18 # This eclass sets correct IUSE.  It also provides PYTHON_DEPS
19 # and PYTHON_REQUIRED_USE that need to be added to appropriate ebuild
20 # metadata variables.
21 #
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.
26 #
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).
30 #
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
34 # to inherit both.
35 #
36 # For more information, please see the wiki:
37 # https://wiki.gentoo.org/wiki/Project:Python/python-single-r1
38
39 case "${EAPI:-0}" in
40         0|1|2|3|4)
41                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
42                 ;;
43         5|6|7)
44                 # EAPI=5 is required for sane USE_EXPAND dependencies
45                 ;;
46         *)
47                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
48                 ;;
49 esac
50
51 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
52
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.'
57 fi
58
59 inherit python-utils-r1
60
61 fi
62
63 EXPORT_FUNCTIONS pkg_setup
64
65 # @ECLASS-VARIABLE: PYTHON_COMPAT
66 # @REQUIRED
67 # @DESCRIPTION:
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
70 # an array.
71 #
72 # Example:
73 # @CODE
74 # PYTHON_COMPAT=( python2_7 python3_3 python3_4 )
75 # @CODE
76 #
77 # Please note that you can also use bash brace expansion if you like:
78 # @CODE
79 # PYTHON_COMPAT=( python2_7 python3_{3,4} )
80 # @CODE
81
82 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
83 # @INTERNAL
84 # @DESCRIPTION:
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.
89 #
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_TARGETS and PYTHON_SINGLE_TARGET is ignored,
93 # and the implementation in PYTHON_COMPAT_OVERRIDE is built instead.
94 # Dependencies need to be satisfied manually.
95 #
96 # Example:
97 # @CODE
98 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
99 # @CODE
100
101 # @ECLASS-VARIABLE: PYTHON_REQ_USE
102 # @DEFAULT_UNSET
103 # @DESCRIPTION:
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
107 # use USE defaults.
108 #
109 # This should be set before calling `inherit'.
110 #
111 # Example:
112 # @CODE
113 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
114 # @CODE
115 #
116 # It will cause the Python dependencies to look like:
117 # @CODE
118 # python_single_target_pythonX_Y? ( dev-lang/python:X.Y[gdbm,ncurses(-)?] )
119 # @CODE
120
121 # @ECLASS-VARIABLE: PYTHON_DEPS
122 # @DESCRIPTION:
123 # This is an eclass-generated Python dependency string for all
124 # implementations listed in PYTHON_COMPAT.
125 #
126 # The dependency string is conditional on PYTHON_SINGLE_TARGET.
127 #
128 # Example use:
129 # @CODE
130 # RDEPEND="${PYTHON_DEPS}
131 #       dev-foo/mydep"
132 # DEPEND="${RDEPEND}"
133 # @CODE
134 #
135 # Example value:
136 # @CODE
137 # dev-lang/python-exec:=
138 # python_single_target_python2_7? ( dev-lang/python:2.7[gdbm] )
139 # python_single_target_pypy? ( virtual/pypy[gdbm] )
140 # @CODE
141
142 # @ECLASS-VARIABLE: PYTHON_SINGLE_USEDEP
143 # @DESCRIPTION:
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.
147 #
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.
150 #
151 # Example use:
152 # @CODE
153 # RDEPEND="dev-python/foo[${PYTHON_SINGLE_USEDEP}]"
154 # @CODE
155 #
156 # Example value:
157 # @CODE
158 # python_single_target_python3_4(-)?
159 # @CODE
160
161 # @ECLASS-VARIABLE: PYTHON_MULTI_USEDEP
162 # @DESCRIPTION:
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
165 # implementations.
166 #
167 # Example use:
168 # @CODE
169 # RDEPEND="$(python_gen_cond_dep '
170 #     dev-python/foo[${PYTHON_MULTI_USEDEP}]
171 #   ')"
172 # @CODE
173 #
174 # Example value:
175 # @CODE
176 # python_targets_python3_4(-)
177 # @CODE
178
179 # @ECLASS-VARIABLE: PYTHON_REQUIRED_USE
180 # @DESCRIPTION:
181 # This is an eclass-generated required-use expression which ensures the following
182 # when more than one python implementation is possible:
183 # 1. Exactly one PYTHON_SINGLE_TARGET value has been enabled.
184 # 2. The selected PYTHON_SINGLE_TARGET value is enabled in PYTHON_TARGETS.
185 #
186 # This expression should be utilized in an ebuild by including it in
187 # REQUIRED_USE, optionally behind a use flag.
188 #
189 # Example use:
190 # @CODE
191 # REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
192 # @CODE
193 #
194 # Example value:
195 # @CODE
196 # python_single_target_python2_7? ( python_targets_python2_7 )
197 # python_single_target_python3_3? ( python_targets_python3_3 )
198 # ^^ ( python_single_target_python2_7 python_single_target_python3_3 )
199 # @CODE
200
201 _python_single_set_globals() {
202         _python_set_impls
203
204         local flags_mt=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_targets_}" )
205         local flags=( "${_PYTHON_SUPPORTED_IMPLS[@]/#/python_single_target_}" )
206
207         if [[ ${#_PYTHON_SUPPORTED_IMPLS[@]} -eq 1 ]]; then
208                 # if only one implementation is supported, use IUSE defaults
209                 # to avoid requesting the user to enable it
210                 IUSE="+${flags_mt[0]} +${flags[0]}"
211         else
212                 IUSE="${flags_mt[*]} ${flags[*]}"
213         fi
214
215         local requse="^^ ( ${flags[*]} )"
216         local single_flags="${flags[@]/%/(-)?}"
217         local single_usedep=${single_flags// /,}
218
219         local deps= i PYTHON_PKG_DEP
220         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
221                 # The chosen targets need to be in PYTHON_TARGETS as well.
222                 # This is in order to enforce correct dependencies on packages
223                 # supporting multiple implementations.
224                 requse+=" python_single_target_${i}? ( python_targets_${i} )"
225
226                 python_export "${i}" PYTHON_PKG_DEP
227                 # 1) well, python-exec would suffice as an RDEP
228                 # but no point in making this overcomplex, BDEP doesn't hurt anyone
229                 # 2) python-exec should be built with all targets forced anyway
230                 # but if new targets were added, we may need to force a rebuild
231                 deps+="python_single_target_${i}? (
232                         ${PYTHON_PKG_DEP}
233                         >=dev-lang/python-exec-2:=[python_targets_${i}]
234                 ) "
235         done
236
237         if [[ ${PYTHON_DEPS+1} ]]; then
238                 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
239                         eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
240                         eerror "Before: ${PYTHON_DEPS}"
241                         eerror "Now   : ${deps}"
242                         die "PYTHON_DEPS integrity check failed"
243                 fi
244
245                 # these two are formality -- they depend on PYTHON_COMPAT only
246                 if [[ ${PYTHON_REQUIRED_USE} != ${requse} ]]; then
247                         eerror "PYTHON_REQUIRED_USE have changed between inherits!"
248                         eerror "Before: ${PYTHON_REQUIRED_USE}"
249                         eerror "Now   : ${requse}"
250                         die "PYTHON_REQUIRED_USE integrity check failed"
251                 fi
252
253                 if [[ ${PYTHON_SINGLE_USEDEP} != "${single_usedep}" ]]; then
254                         eerror "PYTHON_SINGLE_USEDEP have changed between inherits!"
255                         eerror "Before: ${PYTHON_SINGLE_USEDEP}"
256                         eerror "Now   : ${single_usedep}"
257                         die "PYTHON_SINGLE_USEDEP integrity check failed"
258                 fi
259         else
260                 PYTHON_DEPS=${deps}
261                 PYTHON_REQUIRED_USE=${requse}
262                 PYTHON_SINGLE_USEDEP=${single_usedep}
263                 readonly PYTHON_DEPS PYTHON_REQUIRED_USE PYTHON_SINGLE_USEDEP
264         fi
265 }
266 _python_single_set_globals
267 unset -f _python_single_set_globals
268
269 if [[ ! ${_PYTHON_SINGLE_R1} ]]; then
270
271 # @FUNCTION: _python_gen_usedep
272 # @INTERNAL
273 # @USAGE: [<pattern>...]
274 # @DESCRIPTION:
275 # Output a USE dependency string for Python implementations which
276 # are both in PYTHON_COMPAT and match any of the patterns passed
277 # as parameters to the function.
278 #
279 # The patterns can be either fnmatch-style patterns (matched via bash
280 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
281 # appropriately all enabled Python 2/3 implementations (alike
282 # python_is_python3). Remember to escape or quote the fnmatch patterns
283 # to prevent accidental shell filename expansion.
284 #
285 # This is an internal function used to implement python_gen_cond_dep.
286 _python_gen_usedep() {
287         debug-print-function ${FUNCNAME} "${@}"
288
289         local impl matches=()
290
291         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
292                 if _python_impl_matches "${impl}" "${@}"; then
293                         matches+=(
294                                 "python_single_target_${impl}(-)?"
295                         )
296                 fi
297         done
298
299         [[ ${matches[@]} ]] || die "No supported implementations match python_gen_usedep patterns: ${@}"
300
301         local out=${matches[@]}
302         echo "${out// /,}"
303 }
304
305 # @FUNCTION: python_gen_useflags
306 # @USAGE: [<pattern>...]
307 # @DESCRIPTION:
308 # Output a list of USE flags for Python implementations which
309 # are both in PYTHON_COMPAT and match any of the patterns passed
310 # as parameters to the function.
311 #
312 # The patterns can be either fnmatch-style patterns (matched via bash
313 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
314 # appropriately all enabled Python 2/3 implementations (alike
315 # python_is_python3). Remember to escape or quote the fnmatch patterns
316 # to prevent accidental shell filename expansion.
317 #
318 # Example:
319 # @CODE
320 # PYTHON_COMPAT=( python{2_7,3_4} )
321 # REQUIRED_USE="doc? ( ^^ ( $(python_gen_useflags 'python2*') ) )"
322 # @CODE
323 #
324 # It will cause the variable to look like:
325 # @CODE
326 # REQUIRED_USE="doc? ( ^^ ( python_single_target_python2_7 ) )"
327 # @CODE
328 python_gen_useflags() {
329         debug-print-function ${FUNCNAME} "${@}"
330
331         local impl matches=()
332
333         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
334                 if _python_impl_matches "${impl}" "${@}"; then
335                         matches+=( "python_single_target_${impl}" )
336                 fi
337         done
338
339         echo "${matches[@]}"
340 }
341
342 # @FUNCTION: python_gen_cond_dep
343 # @USAGE: <dependency> [<pattern>...]
344 # @DESCRIPTION:
345 # Output a list of <dependency>-ies made conditional to USE flags
346 # of Python implementations which are both in PYTHON_COMPAT and match
347 # any of the patterns passed as the remaining parameters.
348 #
349 # The patterns can be either fnmatch-style patterns (matched via bash
350 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
351 # appropriately all enabled Python 2/3 implementations (alike
352 # python_is_python3). Remember to escape or quote the fnmatch patterns
353 # to prevent accidental shell filename expansion.
354 #
355 # In order to enforce USE constraints on the packages, verbatim
356 # '${PYTHON_SINGLE_USEDEP}' and '${PYTHON_MULTI_USEDEP}' (quoted!) may
357 # be placed in the dependency specification. It will get expanded within
358 # the function into a proper USE dependency string.
359 #
360 # Example:
361 # @CODE
362 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
363 # RDEPEND="$(python_gen_cond_dep \
364 #   'dev-python/unittest2[${PYTHON_MULTI_USEDEP}]' python2_7 pypy )"
365 # @CODE
366 #
367 # It will cause the variable to look like:
368 # @CODE
369 # RDEPEND="python_single_target_python2_7? (
370 #     dev-python/unittest2[python_targets_python2_7(-)?,...] )
371 #       python_single_target_pypy? (
372 #     dev-python/unittest2[python_targets_pypy(-)?,...] )"
373 # @CODE
374 python_gen_cond_dep() {
375         debug-print-function ${FUNCNAME} "${@}"
376
377         local impl matches=()
378
379         local dep=${1}
380         shift
381
382         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
383                 if _python_impl_matches "${impl}" "${@}"; then
384                         # substitute ${PYTHON_SINGLE_USEDEP} if used
385                         # (since python_gen_usedep() will not return
386                         #  ${PYTHON_SINGLE_USEDEP}, the code is run at most once)
387                         if [[ ${dep} == *'${PYTHON_SINGLE_USEDEP}'* ]]; then
388                                 local usedep=$(_python_gen_usedep "${@}")
389                                 dep=${dep//\$\{PYTHON_SINGLE_USEDEP\}/${usedep}}
390                         fi
391                         local multi_usedep="python_targets_${impl}(-)"
392
393                         matches+=( "python_single_target_${impl}? (
394                                 ${dep//\$\{PYTHON_MULTI_USEDEP\}/${multi_usedep}} )" )
395                 fi
396         done
397
398         echo "${matches[@]}"
399 }
400
401 # @FUNCTION: python_gen_impl_dep
402 # @USAGE: [<requested-use-flags> [<impl-pattern>...]]
403 # @DESCRIPTION:
404 # Output a dependency on Python implementations with the specified USE
405 # dependency string appended, or no USE dependency string if called
406 # without the argument (or with empty argument). If any implementation
407 # patterns are passed, the output dependencies will be generated only
408 # for the implementations matching them.
409 #
410 # The patterns can be either fnmatch-style patterns (matched via bash
411 # == operator against PYTHON_COMPAT values) or '-2' / '-3' to indicate
412 # appropriately all enabled Python 2/3 implementations (alike
413 # python_is_python3). Remember to escape or quote the fnmatch patterns
414 # to prevent accidental shell filename expansion.
415 #
416 # Use this function when you need to request different USE flags
417 # on the Python interpreter depending on package's USE flags. If you
418 # only need a single set of interpreter USE flags, just set
419 # PYTHON_REQ_USE and use ${PYTHON_DEPS} globally.
420 #
421 # Example:
422 # @CODE
423 # PYTHON_COMPAT=( python{2_7,3_{3,4}} pypy )
424 # RDEPEND="foo? ( $(python_gen_impl_dep 'xml(+)') )"
425 # @CODE
426 #
427 # It will cause the variable to look like:
428 # @CODE
429 # RDEPEND="foo? (
430 #   python_single_target_python2_7? (
431 #     dev-lang/python:2.7[xml(+)] )
432 #       python_single_target_pypy? (
433 #     dev-python/pypy[xml(+)] ) )"
434 # @CODE
435 python_gen_impl_dep() {
436         debug-print-function ${FUNCNAME} "${@}"
437
438         local impl
439         local matches=()
440
441         local PYTHON_REQ_USE=${1}
442         shift
443
444         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
445                 if _python_impl_matches "${impl}" "${@}"; then
446                         local PYTHON_PKG_DEP
447                         python_export "${impl}" PYTHON_PKG_DEP
448                         matches+=( "python_single_target_${impl}? ( ${PYTHON_PKG_DEP} )" )
449                 fi
450         done
451
452         echo "${matches[@]}"
453 }
454
455 # @FUNCTION: python_setup
456 # @DESCRIPTION:
457 # Determine what the selected Python implementation is and set
458 # the Python build environment up for it.
459 python_setup() {
460         debug-print-function ${FUNCNAME} "${@}"
461
462         unset EPYTHON
463
464         # support developer override
465         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
466                 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
467                 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-single-r1"
468
469                 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
470                 ewarn "implementation will be used:"
471                 ewarn
472                 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
473                 ewarn
474                 ewarn "Dependencies won't be satisfied, and PYTHON_SINGLE_TARGET flags will be ignored."
475
476                 python_export "${impls[0]}" EPYTHON PYTHON
477                 python_wrapper_setup
478                 return
479         fi
480
481         local impl
482         for impl in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
483                 if use "python_single_target_${impl}"; then
484                         if [[ ${EPYTHON} ]]; then
485                                 eerror "Your PYTHON_SINGLE_TARGET setting lists more than a single Python"
486                                 eerror "implementation. Please set it to just one value. If you need"
487                                 eerror "to override the value for a single package, please use package.env"
488                                 eerror "or an equivalent solution (man 5 portage)."
489                                 echo
490                                 die "More than one implementation in PYTHON_SINGLE_TARGET."
491                         fi
492
493                         if ! use "python_targets_${impl}"; then
494                                 eerror "The implementation chosen as PYTHON_SINGLE_TARGET must be added"
495                                 eerror "to PYTHON_TARGETS as well. This is in order to ensure that"
496                                 eerror "dependencies are satisfied correctly. We're sorry"
497                                 eerror "for the inconvenience."
498                                 echo
499                                 die "Build target (${impl}) not in PYTHON_TARGETS."
500                         fi
501
502                         python_export "${impl}" EPYTHON PYTHON
503                         python_wrapper_setup
504                 fi
505         done
506
507         if [[ ! ${EPYTHON} ]]; then
508                 eerror "No Python implementation selected for the build. Please set"
509                 eerror "the PYTHON_SINGLE_TARGET variable in your make.conf to one"
510                 eerror "of the following values:"
511                 eerror
512                 eerror "${_PYTHON_SUPPORTED_IMPLS[@]}"
513                 echo
514                 die "No supported Python implementation in PYTHON_SINGLE_TARGET/PYTHON_TARGETS."
515         fi
516 }
517
518 # @FUNCTION: python-single-r1_pkg_setup
519 # @DESCRIPTION:
520 # Runs python_setup.
521 python-single-r1_pkg_setup() {
522         debug-print-function ${FUNCNAME} "${@}"
523
524         [[ ${MERGE_TYPE} != binary ]] && python_setup
525 }
526
527 _PYTHON_SINGLE_R1=1
528 fi