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