python-single-r1.eclass 16 KB

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