app-emulation/vagrant: drop ruby23 comepletely
[gentoo.git] / eclass / python-any-r1.eclass
1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: python-any-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: 0 1 2 3 4 5 6 7
11 # @BLURB: An eclass for packages having build-time dependency on Python.
12 # @DESCRIPTION:
13 # A minimal eclass for packages which need any Python interpreter
14 # installed without a need for explicit choice and invariability.
15 # This usually involves packages requiring Python at build-time
16 # but having no other relevance to it.
17 #
18 # This eclass provides a minimal PYTHON_DEPS variable with a dependency
19 # string on any of the supported Python implementations. It also exports
20 # pkg_setup() which finds the best supported implementation and sets it
21 # as the active one.
22 #
23 # Optionally, you can define a python_check_deps() function. It will
24 # be called by the eclass with EPYTHON set to each matching Python
25 # implementation and it is expected to check whether the implementation
26 # fulfills the package requirements. You can use the locally exported
27 # PYTHON_USEDEP to check USE-dependencies of relevant packages. It
28 # should return a true value (0) if the Python implementation fulfills
29 # the requirements, a false value (non-zero) otherwise.
30 #
31 # Please note that python-any-r1 will always inherit python-utils-r1
32 # as well. Thus, all the functions defined there can be used in the
33 # packages using python-any-r1, and there is no need ever to inherit
34 # both.
35 #
36 # For more information, please see the wiki:
37 # https://wiki.gentoo.org/wiki/Project:Python/python-any-r1
38
39 case "${EAPI:-0}" in
40         0|1|2|3|4|5|6|7)
41                 ;;
42         *)
43                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
44                 ;;
45 esac
46
47 if [[ ! ${_PYTHON_ANY_R1} ]]; then
48
49 if [[ ${_PYTHON_R1} ]]; then
50         die 'python-any-r1.eclass can not be used with python-r1.eclass.'
51 elif [[ ${_PYTHON_SINGLE_R1} ]]; then
52         die 'python-any-r1.eclass can not be used with python-single-r1.eclass.'
53 fi
54
55 inherit python-utils-r1
56
57 fi
58
59 EXPORT_FUNCTIONS pkg_setup
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=( python{2_5,2_6,2_7} )
71 # @CODE
72
73 # @ECLASS-VARIABLE: PYTHON_COMPAT_OVERRIDE
74 # @INTERNAL
75 # @DESCRIPTION:
76 # This variable can be used when working with ebuilds to override
77 # the in-ebuild PYTHON_COMPAT. It is a string naming the implementation
78 # which will be used to build the package. It needs to be specified
79 # in the calling environment, and not in ebuilds.
80 #
81 # It should be noted that in order to preserve metadata immutability,
82 # PYTHON_COMPAT_OVERRIDE does not affect dependencies. The value of
83 # EPYTHON and eselect-python preferences are ignored. Dependencies need
84 # to be satisfied manually.
85 #
86 # Example:
87 # @CODE
88 # PYTHON_COMPAT_OVERRIDE='pypy' emerge -1v dev-python/bar
89 # @CODE
90
91 # @ECLASS-VARIABLE: PYTHON_REQ_USE
92 # @DEFAULT_UNSET
93 # @DESCRIPTION:
94 # The list of USEflags required to be enabled on the Python
95 # implementations, formed as a USE-dependency string. It should be valid
96 # for all implementations in PYTHON_COMPAT, so it may be necessary to
97 # use USE defaults.
98 #
99 # Example:
100 # @CODE
101 # PYTHON_REQ_USE="gdbm,ncurses(-)?"
102 # @CODE
103 #
104 # It will cause the Python dependencies to look like:
105 # @CODE
106 # || ( dev-lang/python:X.Y[gdbm,ncurses(-)?] ... )
107 # @CODE
108
109 # @ECLASS-VARIABLE: PYTHON_DEPS
110 # @DESCRIPTION:
111 # This is an eclass-generated Python dependency string for all
112 # implementations listed in PYTHON_COMPAT.
113 #
114 # Any of the supported interpreters will satisfy the dependency.
115 #
116 # Example use:
117 # @CODE
118 # DEPEND="${RDEPEND}
119 #       ${PYTHON_DEPS}"
120 # @CODE
121 #
122 # Example value:
123 # @CODE
124 # || ( dev-lang/python:2.7[gdbm]
125 #       dev-lang/python:2.6[gdbm] )
126 # @CODE
127
128 # @ECLASS-VARIABLE: PYTHON_USEDEP
129 # @DESCRIPTION:
130 # An eclass-generated USE-dependency string for the currently tested
131 # implementation. It is set locally for python_check_deps() call.
132 #
133 # The generate USE-flag list is compatible with packages using python-r1,
134 # python-single-r1 and python-distutils-ng eclasses. It must not be used
135 # on packages using python.eclass.
136 #
137 # Example use:
138 # @CODE
139 # python_check_deps() {
140 #       has_version "dev-python/foo[${PYTHON_USEDEP}]"
141 # }
142 # @CODE
143 #
144 # Example value:
145 # @CODE
146 # python_targets_python2_7(-)?,python_single_target_python2_7(+)?
147 # @CODE
148
149 _python_any_set_globals() {
150         local usestr deps i PYTHON_PKG_DEP
151         [[ ${PYTHON_REQ_USE} ]] && usestr="[${PYTHON_REQ_USE}]"
152
153         _python_set_impls
154
155         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
156                 python_export "${i}" PYTHON_PKG_DEP
157
158                 # note: need to strip '=' slot operator for || deps
159                 deps="${PYTHON_PKG_DEP%=} ${deps}"
160         done
161         deps="|| ( ${deps})"
162
163         if [[ ${PYTHON_DEPS+1} ]]; then
164                 if [[ ${PYTHON_DEPS} != "${deps}" ]]; then
165                         eerror "PYTHON_DEPS have changed between inherits (PYTHON_REQ_USE?)!"
166                         eerror "Before: ${PYTHON_DEPS}"
167                         eerror "Now   : ${deps}"
168                         die "PYTHON_DEPS integrity check failed"
169                 fi
170         else
171                 PYTHON_DEPS=${deps}
172                 readonly PYTHON_DEPS
173         fi
174 }
175 _python_any_set_globals
176 unset -f _python_any_set_globals
177
178 if [[ ! ${_PYTHON_ANY_R1} ]]; then
179
180 # @FUNCTION: python_gen_any_dep
181 # @USAGE: <dependency-block>
182 # @DESCRIPTION:
183 # Generate an any-of dependency that enforces a version match between
184 # the Python interpreter and Python packages. <dependency-block> needs
185 # to list one or more dependencies with verbatim '${PYTHON_USEDEP}'
186 # references (quoted!) that will get expanded inside the function.
187 #
188 # This should be used along with an appropriate python_check_deps()
189 # that checks which of the any-of blocks were matched.
190 #
191 # Example use:
192 # @CODE
193 # DEPEND="$(python_gen_any_dep '
194 #       dev-python/foo[${PYTHON_USEDEP}]
195 #       || ( dev-python/bar[${PYTHON_USEDEP}]
196 #               dev-python/baz[${PYTHON_USEDEP}] )')"
197 #
198 # python_check_deps() {
199 #       has_version "dev-python/foo[${PYTHON_USEDEP}]" \
200 #               && { has_version "dev-python/bar[${PYTHON_USEDEP}]" \
201 #                       || has_version "dev-python/baz[${PYTHON_USEDEP}]"; }
202 # }
203 # @CODE
204 #
205 # Example value:
206 # @CODE
207 # || (
208 #       (
209 #               dev-lang/python:2.7
210 #               dev-python/foo[python_targets_python2_7(-)?,python_single_target_python2_7(+)?]
211 #               || ( dev-python/bar[python_targets_python2_7(-)?,python_single_target_python2_7(+)?]
212 #                       dev-python/baz[python_targets_python2_7(-)?,python_single_target_python2_7(+)?] )
213 #       )
214 #       (
215 #               dev-lang/python:3.3
216 #               dev-python/foo[python_targets_python3_3(-)?,python_single_target_python3_3(+)?]
217 #               || ( dev-python/bar[python_targets_python3_3(-)?,python_single_target_python3_3(+)?]
218 #                       dev-python/baz[python_targets_python3_3(-)?,python_single_target_python3_3(+)?] )
219 #       )
220 # )
221 # @CODE
222 python_gen_any_dep() {
223         debug-print-function ${FUNCNAME} "${@}"
224
225         local depstr=${1}
226         [[ ${depstr} ]] || die "No dependency string provided"
227
228         local i PYTHON_PKG_DEP out=
229         for i in "${_PYTHON_SUPPORTED_IMPLS[@]}"; do
230                 local PYTHON_USEDEP="python_targets_${i}(-),python_single_target_${i}(+)"
231                 python_export "${i}" PYTHON_PKG_DEP
232
233                 local i_depstr=${depstr//\$\{PYTHON_USEDEP\}/${PYTHON_USEDEP}}
234                 # note: need to strip '=' slot operator for || deps
235                 out="( ${PYTHON_PKG_DEP%=} ${i_depstr} ) ${out}"
236         done
237         echo "|| ( ${out})"
238 }
239
240 # @FUNCTION: _python_EPYTHON_supported
241 # @USAGE: <epython>
242 # @INTERNAL
243 # @DESCRIPTION:
244 # Check whether the specified implementation is supported by package
245 # (specified in PYTHON_COMPAT). Calls python_check_deps() if declared.
246 _python_EPYTHON_supported() {
247         debug-print-function ${FUNCNAME} "${@}"
248
249         local EPYTHON=${1}
250         local i=${EPYTHON/./_}
251
252         case "${i}" in
253                 python*|jython*|pypy*)
254                         ;;
255                 *)
256                         ewarn "Invalid EPYTHON: ${EPYTHON}"
257                         return 1
258                         ;;
259         esac
260
261         if has "${i}" "${_PYTHON_SUPPORTED_IMPLS[@]}"; then
262                 if python_is_installed "${i}"; then
263                         if declare -f python_check_deps >/dev/null; then
264                                 local PYTHON_USEDEP="python_targets_${i}(-),python_single_target_${i}(+)"
265                                 python_check_deps
266                                 return ${?}
267                         fi
268
269                         return 0
270                 fi
271         elif ! has "${i}" "${_PYTHON_ALL_IMPLS[@]}"; then
272                 ewarn "Invalid EPYTHON: ${EPYTHON}"
273         fi
274         return 1
275 }
276
277 # @FUNCTION: python_setup
278 # @DESCRIPTION:
279 # Determine what the best installed (and supported) Python
280 # implementation is, and set the Python build environment up for it.
281 #
282 # This function will call python_check_deps() if defined.
283 python_setup() {
284         debug-print-function ${FUNCNAME} "${@}"
285
286         # support developer override
287         if [[ ${PYTHON_COMPAT_OVERRIDE} ]]; then
288                 local impls=( ${PYTHON_COMPAT_OVERRIDE} )
289                 [[ ${#impls[@]} -eq 1 ]] || die "PYTHON_COMPAT_OVERRIDE must name exactly one implementation for python-any-r1"
290
291                 ewarn "WARNING: PYTHON_COMPAT_OVERRIDE in effect. The following Python"
292                 ewarn "implementation will be used:"
293                 ewarn
294                 ewarn " ${PYTHON_COMPAT_OVERRIDE}"
295                 ewarn
296                 ewarn "Dependencies won't be satisfied, and EPYTHON/eselect-python will be ignored."
297
298                 python_export "${impls[0]}" EPYTHON PYTHON
299                 python_wrapper_setup
300                 return
301         fi
302
303         # first, try ${EPYTHON}... maybe it's good enough for us.
304         if [[ ${EPYTHON} ]]; then
305                 if _python_EPYTHON_supported "${EPYTHON}"; then
306                         python_export EPYTHON PYTHON
307                         python_wrapper_setup
308                         return
309                 fi
310         fi
311
312         # then, try eselect-python
313         local variant i
314         for variant in '' '--python2' '--python3'; do
315                 i=$(eselect python --show ${variant} 2>/dev/null)
316
317                 if [[ ! ${i} ]]; then
318                         # no eselect-python?
319                         break
320                 elif _python_EPYTHON_supported "${i}"; then
321                         python_export "${i}" EPYTHON PYTHON
322                         python_wrapper_setup
323                         return
324                 fi
325         done
326
327         # fallback to best installed impl.
328         # (reverse iteration over _PYTHON_SUPPORTED_IMPLS)
329         for (( i = ${#_PYTHON_SUPPORTED_IMPLS[@]} - 1; i >= 0; i-- )); do
330                 python_export "${_PYTHON_SUPPORTED_IMPLS[i]}" EPYTHON PYTHON
331                 if _python_EPYTHON_supported "${EPYTHON}"; then
332                         python_wrapper_setup
333                         return
334                 fi
335         done
336
337         eerror "No Python implementation found for the build. This is usually"
338         eerror "a bug in the ebuild. Please report it to bugs.gentoo.org"
339         eerror "along with the build log."
340         echo
341         die "No supported Python implementation installed."
342 }
343
344 # @FUNCTION: python-any-r1_pkg_setup
345 # @DESCRIPTION:
346 # Runs python_setup during from-source installs.
347 #
348 # In a binary package installs is a no-op. If you need Python in pkg_*
349 # phases of a binary package, call python_setup directly.
350 python-any-r1_pkg_setup() {
351         debug-print-function ${FUNCNAME} "${@}"
352
353         [[ ${MERGE_TYPE} != binary ]] && python_setup
354 }
355
356 _PYTHON_ANY_R1=1
357 fi