python-any-r1.eclass 10 KB

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