dev-util/qbs: version bump
[gentoo.git] / eclass / distutils-r1.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: distutils-r1.eclass
6 # @MAINTAINER:
7 # Python team <python@gentoo.org>
8 # @AUTHOR:
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # Based on the work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @BLURB: A simple eclass to build Python packages using distutils.
12 # @DESCRIPTION:
13 # A simple eclass providing functions to build Python packages using
14 # the distutils build system. It exports phase functions for all
15 # the src_* phases. Each of the phases runs two pseudo-phases:
16 # python_..._all() (e.g. python_prepare_all()) once in ${S}, then
17 # python_...() (e.g. python_prepare()) for each implementation
18 # (see: python_foreach_impl() in python-r1).
19 #
20 # In distutils-r1_src_prepare(), the 'all' function is run before
21 # per-implementation ones (because it creates the implementations),
22 # per-implementation functions are run in a random order.
23 #
24 # In remaining phase functions, the per-implementation functions are run
25 # before the 'all' one, and they are ordered from the least to the most
26 # preferred implementation (so that 'better' files overwrite 'worse'
27 # ones).
28 #
29 # If the ebuild doesn't specify a particular pseudo-phase function,
30 # the default one will be used (distutils-r1_...). Defaults are provided
31 # for all per-implementation pseudo-phases, python_prepare_all()
32 # and python_install_all(); whenever writing your own pseudo-phase
33 # functions, you should consider calling the defaults (and especially
34 # distutils-r1_python_prepare_all).
35 #
36 # Please note that distutils-r1 sets RDEPEND and DEPEND unconditionally
37 # for you.
38 #
39 # Also, please note that distutils-r1 will always inherit python-r1
40 # as well. Thus, all the variables defined and documented there are
41 # relevant to the packages using distutils-r1.
42 #
43 # For more information, please see the wiki:
44 # https://wiki.gentoo.org/wiki/Project:Python/distutils-r1
45
46 case "${EAPI:-0}" in
47         0|1|2|3)
48                 die "Unsupported EAPI=${EAPI:-0} (too old) for ${ECLASS}"
49                 ;;
50         4|5)
51                 ;;
52         *)
53                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
54                 ;;
55 esac
56
57 # @ECLASS-VARIABLE: DISTUTILS_OPTIONAL
58 # @DEFAULT_UNSET
59 # @DESCRIPTION:
60 # If set to a non-null value, distutils part in the ebuild will
61 # be considered optional. No dependencies will be added and no phase
62 # functions will be exported.
63 #
64 # If you enable DISTUTILS_OPTIONAL, you have to set proper dependencies
65 # for your package (using ${PYTHON_DEPS}) and to either call
66 # distutils-r1 default phase functions or call the build system
67 # manually.
68
69 # @ECLASS-VARIABLE: DISTUTILS_SINGLE_IMPL
70 # @DEFAULT_UNSET
71 # @DESCRIPTION:
72 # If set to a non-null value, the ebuild will support setting a single
73 # Python implementation only. It will effectively replace the python-r1
74 # eclass inherit with python-single-r1.
75 #
76 # Note that inheriting python-single-r1 will cause pkg_setup()
77 # to be exported. It must be run in order for the eclass functions
78 # to function properly.
79
80 if [[ ! ${_DISTUTILS_R1} ]]; then
81
82 inherit eutils toolchain-funcs
83
84 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
85         inherit multiprocessing python-r1
86 else
87         inherit python-single-r1
88 fi
89
90 fi
91
92 if [[ ! ${DISTUTILS_OPTIONAL} ]]; then
93         EXPORT_FUNCTIONS src_prepare src_configure src_compile src_test src_install
94 fi
95
96 if [[ ! ${_DISTUTILS_R1} ]]; then
97
98 if [[ ! ${DISTUTILS_OPTIONAL} ]]; then
99         RDEPEND=${PYTHON_DEPS}
100         DEPEND=${PYTHON_DEPS}
101         REQUIRED_USE=${PYTHON_REQUIRED_USE}
102 fi
103
104 # @ECLASS-VARIABLE: PATCHES
105 # @DEFAULT_UNSET
106 # @DESCRIPTION:
107 # An array containing patches to be applied to the sources before
108 # copying them.
109 #
110 # If unset, no custom patches will be applied.
111 #
112 # Please note, however, that at some point the eclass may apply
113 # additional distutils patches/quirks independently of this variable.
114 #
115 # Example:
116 # @CODE
117 # PATCHES=( "${FILESDIR}"/${P}-make-gentoo-happy.patch )
118 # @CODE
119
120 # @ECLASS-VARIABLE: DOCS
121 # @DEFAULT_UNSET
122 # @DESCRIPTION:
123 # An array containing documents installed using dodoc. The files listed
124 # there must exist in the directory from which
125 # distutils-r1_python_install_all() is run (${S} by default).
126 #
127 # If unset, the function will instead look up files matching default
128 # filename pattern list (from the Package Manager Specification),
129 # and install those found.
130 #
131 # Example:
132 # @CODE
133 # DOCS=( NEWS README )
134 # @CODE
135
136 # @ECLASS-VARIABLE: HTML_DOCS
137 # @DEFAULT_UNSET
138 # @DESCRIPTION:
139 # An array containing documents installed using dohtml. The files
140 # and directories listed there must exist in the directory from which
141 # distutils-r1_python_install_all() is run (${S} by default).
142 #
143 # If unset, no HTML docs will be installed.
144 #
145 # Example:
146 # @CODE
147 # HTML_DOCS=( doc/html/. )
148 # @CODE
149
150 # @ECLASS-VARIABLE: EXAMPLES
151 # @DEFAULT_UNSET
152 # @DESCRIPTION:
153 # An array containing examples installed into 'examples' doc
154 # subdirectory. The files and directories listed there must exist
155 # in the directory from which distutils-r1_python_install_all() is run
156 # (${S} by default).
157 #
158 # The 'examples' subdirectory will be marked not to be compressed
159 # automatically.
160 #
161 # If unset, no examples will be installed.
162 #
163 # Example:
164 # @CODE
165 # EXAMPLES=( examples/. demos/. )
166 # @CODE
167
168 # @ECLASS-VARIABLE: DISTUTILS_IN_SOURCE_BUILD
169 # @DEFAULT_UNSET
170 # @DESCRIPTION:
171 # If set to a non-null value, in-source builds will be enabled.
172 # If unset, the default is to use in-source builds when python_prepare()
173 # is declared, and out-of-source builds otherwise.
174 #
175 # If in-source builds are used, the eclass will create a copy of package
176 # sources for each Python implementation in python_prepare_all(),
177 # and work on that copy afterwards.
178 #
179 # If out-of-source builds are used, the eclass will instead work
180 # on the sources directly, prepending setup.py arguments with
181 # 'build --build-base ${BUILD_DIR}' to enforce keeping & using built
182 # files in the specific root.
183
184 # @ECLASS-VARIABLE: DISTUTILS_ALL_SUBPHASE_IMPLS
185 # @DEFAULT_UNSET
186 # @DESCRIPTION:
187 # An array of patterns specifying which implementations can be used
188 # for *_all() sub-phase functions. If undefined, defaults to '*'
189 # (allowing any implementation). If multiple values are specified,
190 # implementations matching any of the patterns will be accepted.
191 #
192 # If the restriction needs to apply conditionally to a USE flag,
193 # the variable should be set conditionally as well (e.g. in an early
194 # phase function or other convenient location).
195 #
196 # Please remember to add a matching || block to REQUIRED_USE,
197 # to ensure that at least one implementation matching the patterns will
198 # be enabled.
199 #
200 # Example:
201 # @CODE
202 # REQUIRED_USE="doc? ( || ( $(python_gen_useflags 'python2*') ) )"
203 #
204 # pkg_setup() {
205 #     use doc && DISTUTILS_ALL_SUBPHASE_IMPLS=( 'python2*' )
206 # }
207 # @CODE
208
209 # @ECLASS-VARIABLE: mydistutilsargs
210 # @DEFAULT_UNSET
211 # @DESCRIPTION:
212 # An array containing options to be passed to setup.py.
213 #
214 # Example:
215 # @CODE
216 # python_configure_all() {
217 #       mydistutilsargs=( --enable-my-hidden-option )
218 # }
219 # @CODE
220
221 # @FUNCTION: esetup.py
222 # @USAGE: [<args>...]
223 # @DESCRIPTION:
224 # Run setup.py using currently selected Python interpreter
225 # (if ${PYTHON} is set; fallback 'python' otherwise).
226 #
227 # setup.py will be passed the following, in order:
228 # 1. ${mydistutilsargs[@]}
229 # 2. additional arguments passed to the esetup.py function.
230 #
231 # Please note that setup.py will respect defaults (unless overriden
232 # via command-line options) from setup.cfg that is created
233 # in distutils-r1_python_compile and in distutils-r1_python_install.
234 #
235 # This command dies on failure.
236 esetup.py() {
237         debug-print-function ${FUNCNAME} "${@}"
238
239         set -- "${PYTHON:-python}" setup.py "${mydistutilsargs[@]}" "${@}"
240
241         echo "${@}" >&2
242         "${@}" || die
243 }
244
245 # @FUNCTION: distutils_install_for_testing
246 # @USAGE: [<args>...]
247 # @DESCRIPTION:
248 # Install the package into a temporary location for running tests.
249 # Update PYTHONPATH appropriately and set TEST_DIR to the test
250 # installation root. The Python packages will be installed in 'lib'
251 # subdir, and scripts in 'scripts' subdir (like in BUILD_DIR).
252 #
253 # Please note that this function should be only used if package uses
254 # namespaces (and therefore proper install needs to be done to enforce
255 # PYTHONPATH) or tests rely on the results of install command.
256 # For most of the packages, tests built in BUILD_DIR are good enough.
257 distutils_install_for_testing() {
258         debug-print-function ${FUNCNAME} "${@}"
259
260         # A few notes:
261         # 1) because of namespaces, we can't use 'install --root',
262         # 2) 'install --home' is terribly broken on pypy, so we need
263         #    to override --install-lib and --install-scripts,
264         # 3) non-root 'install' complains about PYTHONPATH and missing dirs,
265         #    so we need to set it properly and mkdir them,
266         # 4) it runs a bunch of commands which write random files to cwd,
267         #    in order to avoid that, we add the necessary path overrides
268         #    in _distutils-r1_create_setup_cfg.
269
270         TEST_DIR=${BUILD_DIR}/test
271         local bindir=${TEST_DIR}/scripts
272         local libdir=${TEST_DIR}/lib
273         PYTHONPATH=${libdir}:${PYTHONPATH}
274
275         local add_args=(
276                 install
277                         --home="${TEST_DIR}"
278                         --install-lib="${libdir}"
279                         --install-scripts="${bindir}"
280         )
281
282         mkdir -p "${libdir}" || die
283         esetup.py "${add_args[@]}" "${@}"
284 }
285
286 # @FUNCTION: _distutils-r1_disable_ez_setup
287 # @INTERNAL
288 # @DESCRIPTION:
289 # Stub out ez_setup.py and distribute_setup.py to prevent packages
290 # from trying to download a local copy of setuptools.
291 _distutils-r1_disable_ez_setup() {
292         local stub="def use_setuptools(*args, **kwargs): pass"
293         if [[ -f ez_setup.py ]]; then
294                 echo "${stub}" > ez_setup.py || die
295         fi
296         if [[ -f distribute_setup.py ]]; then
297                 echo "${stub}" > distribute_setup.py || die
298         fi
299 }
300
301 # @FUNCTION: distutils-r1_python_prepare_all
302 # @DESCRIPTION:
303 # The default python_prepare_all(). It applies the patches from PATCHES
304 # array, then user patches and finally calls python_copy_sources to
305 # create copies of resulting sources for each Python implementation.
306 #
307 # At some point in the future, it may also apply eclass-specific
308 # distutils patches and/or quirks.
309 distutils-r1_python_prepare_all() {
310         debug-print-function ${FUNCNAME} "${@}"
311
312         [[ ${PATCHES} ]] && epatch "${PATCHES[@]}"
313
314         epatch_user
315
316         # by default, use in-source build if python_prepare() is used
317         if [[ ! ${DISTUTILS_IN_SOURCE_BUILD+1} ]]; then
318                 if declare -f python_prepare >/dev/null; then
319                         DISTUTILS_IN_SOURCE_BUILD=1
320                 fi
321         fi
322
323         _distutils-r1_disable_ez_setup
324
325         if [[ ${DISTUTILS_IN_SOURCE_BUILD} && ! ${DISTUTILS_SINGLE_IMPL} ]]
326         then
327                 # create source copies for each implementation
328                 python_copy_sources
329         fi
330
331         _DISTUTILS_DEFAULT_CALLED=1
332 }
333
334 # @FUNCTION: distutils-r1_python_prepare
335 # @DESCRIPTION:
336 # The default python_prepare(). A no-op.
337 distutils-r1_python_prepare() {
338         debug-print-function ${FUNCNAME} "${@}"
339
340         :
341 }
342
343 # @FUNCTION: distutils-r1_python_configure
344 # @DESCRIPTION:
345 # The default python_configure(). A no-op.
346 distutils-r1_python_configure() {
347         debug-print-function ${FUNCNAME} "${@}"
348
349         :
350 }
351
352 # @FUNCTION: _distutils-r1_create_setup_cfg
353 # @INTERNAL
354 # @DESCRIPTION:
355 # Create implementation-specific configuration file for distutils,
356 # setting proper build-dir (and install-dir) paths.
357 _distutils-r1_create_setup_cfg() {
358         cat > "${HOME}"/.pydistutils.cfg <<-_EOF_ || die
359                 [build]
360                 build-base = ${BUILD_DIR}
361
362                 # using a single directory for them helps us export
363                 # ${PYTHONPATH} and ebuilds find the sources independently
364                 # of whether the package installs extensions or not
365                 #
366                 # note: due to some packages (wxpython) relying on separate
367                 # platlib & purelib dirs, we do not set --build-lib (which
368                 # can not be overriden with --build-*lib)
369                 build-platlib = %(build-base)s/lib
370                 build-purelib = %(build-base)s/lib
371
372                 # make the ebuild writer lives easier
373                 build-scripts = %(build-base)s/scripts
374
375                 [egg_info]
376                 egg-base = ${BUILD_DIR}
377
378                 # this is needed by distutils_install_for_testing since
379                 # setuptools like to create .egg files for install --home.
380                 [bdist_egg]
381                 dist-dir = ${BUILD_DIR}/dist
382         _EOF_
383
384         # we can't refer to ${D} before src_install()
385         if [[ ${EBUILD_PHASE} == install ]]; then
386                 cat >> "${HOME}"/.pydistutils.cfg <<-_EOF_ || die
387
388                         # installation paths -- allow calling extra install targets
389                         # without the default 'install'
390                         [install]
391                         compile = True
392                         optimize = 2
393                         root = ${D}
394                 _EOF_
395
396                 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
397                         cat >> "${HOME}"/.pydistutils.cfg <<-_EOF_ || die
398                                 install-scripts = $(python_get_scriptdir)
399                         _EOF_
400                 fi
401         fi
402 }
403
404 # @FUNCTION: _distutils-r1_copy_egg_info
405 # @INTERNAL
406 # @DESCRIPTION:
407 # Copy egg-info files to the ${BUILD_DIR} (that's going to become
408 # egg-base in esetup.py). This way, we respect whatever's in upstream
409 # egg-info.
410 _distutils-r1_copy_egg_info() {
411         mkdir -p "${BUILD_DIR}" || die
412         # stupid freebsd can't do 'cp -t ${BUILD_DIR} {} +'
413         find -name '*.egg-info' -type d -exec cp -pr {} "${BUILD_DIR}"/ ';' || die
414 }
415
416 # @FUNCTION: distutils-r1_python_compile
417 # @USAGE: [additional-args...]
418 # @DESCRIPTION:
419 # The default python_compile(). Runs 'esetup.py build'. Any parameters
420 # passed to this function will be appended to setup.py invocation,
421 # i.e. passed as options to the 'build' command.
422 #
423 # This phase also sets up initial setup.cfg with build directories
424 # and copies upstream egg-info files if supplied.
425 distutils-r1_python_compile() {
426         debug-print-function ${FUNCNAME} "${@}"
427
428         _distutils-r1_create_setup_cfg
429         _distutils-r1_copy_egg_info
430
431         esetup.py build "${@}"
432 }
433
434 # @FUNCTION: _distutils-r1_wrap_scripts
435 # @USAGE: <path> <bindir>
436 # @INTERNAL
437 # @DESCRIPTION:
438 # Moves and wraps all installed scripts/executables as necessary.
439 _distutils-r1_wrap_scripts() {
440         debug-print-function ${FUNCNAME} "${@}"
441
442         [[ ${#} -eq 2 ]] || die "usage: ${FUNCNAME} <path> <bindir>"
443         local path=${1}
444         local bindir=${2}
445
446         local PYTHON_SCRIPTDIR
447         python_export PYTHON_SCRIPTDIR
448
449         local f python_files=() non_python_files=()
450
451         if [[ -d ${path}${PYTHON_SCRIPTDIR} ]]; then
452                 for f in "${path}${PYTHON_SCRIPTDIR}"/*; do
453                         [[ -d ${f} ]] && die "Unexpected directory: ${f}"
454                         debug-print "${FUNCNAME}: found executable at ${f#${path}/}"
455
456                         local shebang
457                         read -r shebang < "${f}"
458                         if [[ ${shebang} == '#!'*${EPYTHON}* ]]; then
459                                 debug-print "${FUNCNAME}: matching shebang: ${shebang}"
460                                 python_files+=( "${f}" )
461                         else
462                                 debug-print "${FUNCNAME}: non-matching shebang: ${shebang}"
463                                 non_python_files+=( "${f}" )
464                         fi
465
466                         mkdir -p "${path}${bindir}" || die
467                 done
468
469                 for f in "${python_files[@]}"; do
470                         local basename=${f##*/}
471
472                         debug-print "${FUNCNAME}: installing wrapper at ${bindir}/${basename}"
473                         _python_ln_rel "${path}${EPREFIX}"/usr/lib/python-exec/python-exec2 \
474                                 "${path}${bindir}/${basename}" || die
475                 done
476
477                 for f in "${non_python_files[@]}"; do
478                         local basename=${f##*/}
479
480                         debug-print "${FUNCNAME}: moving ${f#${path}/} to ${bindir}/${basename}"
481                         mv "${f}" "${path}${bindir}/${basename}" || die
482                 done
483         fi
484 }
485
486 # @FUNCTION: distutils-r1_python_install
487 # @USAGE: [additional-args...]
488 # @DESCRIPTION:
489 # The default python_install(). Runs 'esetup.py install', doing
490 # intermediate root install and handling script wrapping afterwards.
491 # Any parameters passed to this function will be appended
492 # to the setup.py invocation (i.e. as options to the 'install' command).
493 #
494 # This phase updates the setup.cfg file with install directories.
495 distutils-r1_python_install() {
496         debug-print-function ${FUNCNAME} "${@}"
497
498         local args=( "${@}" )
499
500         # enable compilation for the install phase.
501         local -x PYTHONDONTWRITEBYTECODE=
502
503         # re-create setup.cfg with install paths
504         _distutils-r1_create_setup_cfg
505
506         # python likes to compile any module it sees, which triggers sandbox
507         # failures if some packages haven't compiled their modules yet.
508         addpredict "${EPREFIX}/usr/$(get_libdir)/${EPYTHON}"
509         addpredict /usr/lib/portage/pym
510         addpredict /usr/local # bug 498232
511
512         if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
513                 # user may override --install-scripts
514                 # note: this is poor but distutils argv parsing is dumb
515                 local mydistutilsargs=( "${mydistutilsargs[@]}" )
516                 local scriptdir=${EPREFIX}/usr/bin
517
518                 # construct a list of mydistutilsargs[0] args[0] args[1]...
519                 local arg arg_vars
520                 [[ ${mydistutilsargs[@]} ]] && eval arg_vars+=(
521                         'mydistutilsargs['{0..$(( ${#mydistutilsargs[@]} - 1 ))}']'
522                 )
523                 [[ ${args[@]} ]] && eval arg_vars+=(
524                         'args['{0..$(( ${#args[@]} - 1 ))}']'
525                 )
526
527                 set -- "${arg_vars[@]}"
528                 while [[ ${@} ]]; do
529                         local arg_var=${1}
530                         shift
531                         local a=${!arg_var}
532
533                         case "${a}" in
534                                 --install-scripts=*)
535                                         scriptdir=${a#--install-scripts=}
536                                         unset "${arg_var}"
537                                         ;;
538                                 --install-scripts)
539                                         scriptdir=${!1}
540                                         unset "${arg_var}" "${1}"
541                                         shift
542                                         ;;
543                         esac
544                 done
545         fi
546
547         local root=${D}/_${EPYTHON}
548         [[ ${DISTUTILS_SINGLE_IMPL} ]] && root=${D}
549
550         esetup.py install --root="${root}" "${args[@]}"
551
552         local forbidden_package_names=( examples test tests )
553         local p
554         for p in "${forbidden_package_names[@]}"; do
555                 if [[ -d ${root}$(python_get_sitedir)/${p} ]]; then
556                         die "Package installs '${p}' package which is forbidden and likely a bug in the build system."
557                 fi
558         done
559         if [[ -d ${root}/usr/$(get_libdir)/pypy/share ]]; then
560                 eqawarn "Package installs 'share' in PyPy prefix, see bug #465546."
561         fi
562
563         if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
564                 _distutils-r1_wrap_scripts "${root}" "${scriptdir}"
565                 multibuild_merge_root "${root}" "${D}"
566         fi
567 }
568
569 # @FUNCTION: distutils-r1_python_install_all
570 # @DESCRIPTION:
571 # The default python_install_all(). It installs the documentation.
572 distutils-r1_python_install_all() {
573         debug-print-function ${FUNCNAME} "${@}"
574
575         einstalldocs
576
577         if declare -p EXAMPLES &>/dev/null; then
578                 local INSDESTTREE=/usr/share/doc/${PF}/examples
579                 doins -r "${EXAMPLES[@]}"
580                 docompress -x "${INSDESTTREE}"
581         fi
582
583         _DISTUTILS_DEFAULT_CALLED=1
584 }
585
586 # @FUNCTION: distutils-r1_run_phase
587 # @USAGE: [<argv>...]
588 # @INTERNAL
589 # @DESCRIPTION:
590 # Run the given command.
591 #
592 # If out-of-source builds are used, the phase function is run in source
593 # directory, with BUILD_DIR pointing at the build directory
594 # and PYTHONPATH having an entry for the module build directory.
595 #
596 # If in-source builds are used, the command is executed in the directory
597 # holding the per-implementation copy of sources. BUILD_DIR points
598 # to the 'build' subdirectory.
599 distutils-r1_run_phase() {
600         debug-print-function ${FUNCNAME} "${@}"
601
602         if [[ ${DISTUTILS_IN_SOURCE_BUILD} ]]; then
603                 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
604                         cd "${BUILD_DIR}" || die
605                 fi
606                 local BUILD_DIR=${BUILD_DIR}/build
607         fi
608         local -x PYTHONPATH="${BUILD_DIR}/lib:${PYTHONPATH}"
609
610         # Bug 559644
611         # using PYTHONPATH when the ${BUILD_DIR}/lib is not created yet might lead to
612         # problems in setup.py scripts that try to import modules/packages from that path
613         # during the build process (Python at startup evaluates PYTHONPATH, if the dir is
614         # not valid then associates a NullImporter object to ${BUILD_DIR}/lib storing it
615         # in the sys.path_importer_cache)
616         mkdir -p "${BUILD_DIR}/lib" || die
617
618         # We need separate home for each implementation, for .pydistutils.cfg.
619         if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
620                 local -x HOME=${HOME}/${EPYTHON}
621                 mkdir -p "${HOME}" || die
622         fi
623
624         # Set up build environment, bug #513664.
625         local -x AR=${AR} CC=${CC} CPP=${CPP} CXX=${CXX}
626         tc-export AR CC CPP CXX
627
628         # How to build Python modules in different worlds...
629         local ldopts
630         case "${CHOST}" in
631                 # provided by haubi, 2014-07-08
632                 *-aix*) ldopts='-shared -Wl,-berok';; # good enough
633                 # provided by grobian, 2014-06-22, bug #513664 c7
634                 *-darwin*) ldopts='-bundle -undefined dynamic_lookup';;
635                 *) ldopts='-shared';;
636         esac
637
638         local -x LDSHARED="${CC} ${ldopts}" LDCXXSHARED="${CXX} ${ldopts}"
639
640         "${@}"
641
642         cd "${_DISTUTILS_INITIAL_CWD}" || die
643 }
644
645 # @FUNCTION: _distutils-r1_run_common_phase
646 # @USAGE: [<argv>...]
647 # @INTERNAL
648 # @DESCRIPTION:
649 # Run the given command, restoring the state for a most preferred Python
650 # implementation matching DISTUTILS_ALL_SUBPHASE_IMPLS.
651 #
652 # If in-source build is used, the command will be run in the copy
653 # of sources made for the selected Python interpreter.
654 _distutils-r1_run_common_phase() {
655         local DISTUTILS_ORIG_BUILD_DIR=${BUILD_DIR}
656
657         if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
658                 local best_impl patterns=( "${DISTUTILS_ALL_SUBPHASE_IMPLS[@]-*}" )
659                 _distutils_try_impl() {
660                         local pattern
661                         for pattern in "${patterns[@]}"; do
662                                 if [[ ${EPYTHON} == ${pattern} ]]; then
663                                         best_impl=${MULTIBUILD_VARIANT}
664                                 fi
665                         done
666                 }
667                 python_foreach_impl _distutils_try_impl
668
669                 local PYTHON_COMPAT=( "${best_impl}" )
670         fi
671
672         _distutils-r1_run_foreach_impl "${@}"
673 }
674
675 # @FUNCTION: _distutils-r1_run_foreach_impl
676 # @INTERNAL
677 # @DESCRIPTION:
678 # Run the given phase for each implementation if multiple implementations
679 # are enabled, once otherwise.
680 _distutils-r1_run_foreach_impl() {
681         debug-print-function ${FUNCNAME} "${@}"
682
683         if [[ ${DISTUTILS_NO_PARALLEL_BUILD} ]]; then
684                 eqawarn "DISTUTILS_NO_PARALLEL_BUILD is no longer meaningful. Now all builds"
685                 eqawarn "are non-parallel. Please remove it from the ebuild."
686
687                 unset DISTUTILS_NO_PARALLEL_BUILD # avoid repeated warnings
688         fi
689
690         # store for restoring after distutils-r1_run_phase.
691         local _DISTUTILS_INITIAL_CWD=${PWD}
692         set -- distutils-r1_run_phase "${@}"
693
694         if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
695                 python_foreach_impl "${@}"
696         else
697                 if [[ ! ${EPYTHON} ]]; then
698                         die "EPYTHON unset, python-single-r1_pkg_setup not called?!"
699                 fi
700                 local BUILD_DIR=${BUILD_DIR:-${S}}
701                 BUILD_DIR=${BUILD_DIR%%/}_${EPYTHON}
702
703                 "${@}"
704         fi
705 }
706
707 distutils-r1_src_prepare() {
708         debug-print-function ${FUNCNAME} "${@}"
709
710         local _DISTUTILS_DEFAULT_CALLED
711
712         # common preparations
713         if declare -f python_prepare_all >/dev/null; then
714                 python_prepare_all
715         else
716                 distutils-r1_python_prepare_all
717         fi
718
719         if [[ ! ${_DISTUTILS_DEFAULT_CALLED} ]]; then
720                 eqawarn "QA warning: python_prepare_all() didn't call distutils-r1_python_prepare_all"
721         fi
722
723         if declare -f python_prepare >/dev/null; then
724                 _distutils-r1_run_foreach_impl python_prepare
725         fi
726 }
727
728 distutils-r1_src_configure() {
729         python_export_utf8_locale
730
731         if declare -f python_configure >/dev/null; then
732                 _distutils-r1_run_foreach_impl python_configure
733         fi
734
735         if declare -f python_configure_all >/dev/null; then
736                 _distutils-r1_run_common_phase python_configure_all
737         fi
738 }
739
740 distutils-r1_src_compile() {
741         debug-print-function ${FUNCNAME} "${@}"
742
743         if declare -f python_compile >/dev/null; then
744                 _distutils-r1_run_foreach_impl python_compile
745         else
746                 _distutils-r1_run_foreach_impl distutils-r1_python_compile
747         fi
748
749         if declare -f python_compile_all >/dev/null; then
750                 _distutils-r1_run_common_phase python_compile_all
751         fi
752 }
753
754 _clean_egg_info() {
755         # Work around for setuptools test behavior (bug 534058).
756         # https://bitbucket.org/pypa/setuptools/issue/292
757         rm -rf "${BUILD_DIR}"/lib/*.egg-info
758 }
759
760 distutils-r1_src_test() {
761         debug-print-function ${FUNCNAME} "${@}"
762
763         if declare -f python_test >/dev/null; then
764                 _distutils-r1_run_foreach_impl python_test
765                 _distutils-r1_run_foreach_impl _clean_egg_info
766         fi
767
768         if declare -f python_test_all >/dev/null; then
769                 _distutils-r1_run_common_phase python_test_all
770         fi
771 }
772
773 distutils-r1_src_install() {
774         debug-print-function ${FUNCNAME} "${@}"
775
776         if declare -f python_install >/dev/null; then
777                 _distutils-r1_run_foreach_impl python_install
778         else
779                 _distutils-r1_run_foreach_impl distutils-r1_python_install
780         fi
781
782         local _DISTUTILS_DEFAULT_CALLED
783
784         if declare -f python_install_all >/dev/null; then
785                 _distutils-r1_run_common_phase python_install_all
786         else
787                 _distutils-r1_run_common_phase distutils-r1_python_install_all
788         fi
789
790         if [[ ! ${_DISTUTILS_DEFAULT_CALLED} ]]; then
791                 eqawarn "QA warning: python_install_all() didn't call distutils-r1_python_install_all"
792         fi
793 }
794
795 # -- distutils.eclass functions --
796
797 distutils_get_intermediate_installation_image() {
798         die "${FUNCNAME}() is invalid for distutils-r1"
799 }
800
801 distutils_src_unpack() {
802         die "${FUNCNAME}() is invalid for distutils-r1, and you don't want it in EAPI ${EAPI} anyway"
803 }
804
805 distutils_src_prepare() {
806         die "${FUNCNAME}() is invalid for distutils-r1, you probably want: ${FUNCNAME/_/-r1_}"
807 }
808
809 distutils_src_compile() {
810         die "${FUNCNAME}() is invalid for distutils-r1, you probably want: ${FUNCNAME/_/-r1_}"
811 }
812
813 distutils_src_test() {
814         die "${FUNCNAME}() is invalid for distutils-r1, you probably want: ${FUNCNAME/_/-r1_}"
815 }
816
817 distutils_src_install() {
818         die "${FUNCNAME}() is invalid for distutils-r1, you probably want: ${FUNCNAME/_/-r1_}"
819 }
820
821 distutils_pkg_postinst() {
822         die "${FUNCNAME}() is invalid for distutils-r1, and pkg_postinst is unnecessary"
823 }
824
825 distutils_pkg_postrm() {
826         die "${FUNCNAME}() is invalid for distutils-r1, and pkg_postrm is unnecessary"
827 }
828
829 _DISTUTILS_R1=1
830 fi