/[gentoo-x86]/eclass/distutils-r1.eclass
Gentoo

Contents of /eclass/distutils-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.73 - (show annotations) (download)
Sun Jul 21 19:00:56 2013 UTC (12 months, 4 weeks ago) by mgorny
Branch: MAIN
Changes since 1.72: +2 -3 lines
Replace local+export with "local -x".

1 # Copyright 1999-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/distutils-r1.eclass,v 1.72 2013/07/09 01:57:07 floppym Exp $
4
5 # @ECLASS: distutils-r1
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 python-r1 Developer's Guide:
44 # http://www.gentoo.org/proj/en/Python/python-r1/dev-guide.xml
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
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_NO_PARALLEL_BUILD
185 # @DEFAULT_UNSET
186 # @DESCRIPTION:
187 # If set to a non-null value, the parallel build feature will
188 # be disabled.
189 #
190 # When parallel builds are used, the implementation-specific sub-phases
191 # for selected Python implementation will be run in parallel. This will
192 # increase build efficiency with distutils which does not do parallel
193 # builds.
194 #
195 # This variable can be used to disable the afore-mentioned feature
196 # in case it causes issues with the package.
197
198 # @ECLASS-VARIABLE: mydistutilsargs
199 # @DEFAULT_UNSET
200 # @DESCRIPTION:
201 # An array containing options to be passed to setup.py.
202 #
203 # Example:
204 # @CODE
205 # python_configure_all() {
206 # mydistutilsargs=( --enable-my-hidden-option )
207 # }
208 # @CODE
209
210 # @FUNCTION: esetup.py
211 # @USAGE: [<args>...]
212 # @DESCRIPTION:
213 # Run setup.py using currently selected Python interpreter
214 # (if ${PYTHON} is set; fallback 'python' otherwise).
215 #
216 # setup.py will be passed the following, in order:
217 # 1. ${mydistutilsargs[@]}
218 # 2. The 'build' command and standard build options including ${BUILD_DIR}
219 # 3. Any additional arguments passed to the esetup.py function.
220 #
221 # This command dies on failure.
222 esetup.py() {
223 debug-print-function ${FUNCNAME} "${@}"
224
225 local add_args=()
226 if [[ ${BUILD_DIR} ]]; then
227 add_args+=(
228 build
229 --build-base "${BUILD_DIR}"
230
231 # using a single directory for them helps us export
232 # ${PYTHONPATH} and ebuilds find the sources independently
233 # of whether the package installs extensions or not
234 #
235 # note: due to some packages (wxpython) relying on separate
236 # platlib & purelib dirs, we do not set --build-lib (which
237 # can not be overriden with --build-*lib)
238 --build-platlib "${BUILD_DIR}/lib"
239 --build-purelib "${BUILD_DIR}/lib"
240
241 # make the ebuild writer lives easier
242 --build-scripts "${BUILD_DIR}/scripts"
243 )
244
245 # if setuptools is used, adjust egg_info path as well
246 if "${PYTHON:-python}" setup.py --help egg_info &>/dev/null; then
247 add_args+=(
248 egg_info --egg-base "${BUILD_DIR}"
249 )
250 fi
251 elif [[ ! ${DISTUTILS_IN_SOURCE_BUILD} ]]; then
252 die 'Out-of-source build requested, yet BUILD_DIR unset.'
253 fi
254
255 set -- "${PYTHON:-python}" setup.py \
256 "${mydistutilsargs[@]}" "${add_args[@]}" "${@}"
257
258 echo "${@}" >&2
259 "${@}" || die
260 }
261
262 # @FUNCTION: distutils_install_for_testing
263 # @USAGE: [<args>...]
264 # @DESCRIPTION:
265 # Install the package into a temporary location for running tests.
266 # Update PYTHONPATH appropriately and set TEST_DIR to the test
267 # installation root. The Python packages will be installed in 'lib'
268 # subdir, and scripts in 'scripts' subdir (like in BUILD_DIR).
269 #
270 # Please note that this function should be only used if package uses
271 # namespaces (and therefore proper install needs to be done to enforce
272 # PYTHONPATH) or tests rely on the results of install command.
273 # For most of the packages, tests built in BUILD_DIR are good enough.
274 distutils_install_for_testing() {
275 debug-print-function ${FUNCNAME} "${@}"
276
277 # A few notes:
278 # 1) because of namespaces, we can't use 'install --root',
279 # 2) 'install --home' is terribly broken on pypy, so we need
280 # to override --install-lib and --install-scripts,
281 # 3) non-root 'install' complains about PYTHONPATH and missing dirs,
282 # so we need to set it properly and mkdir them,
283 # 4) it runs a bunch of commands which write random files to cwd,
284 # in order to avoid that, we need to run them ourselves to pass
285 # alternate build paths,
286 # 5) 'install' needs to go before 'bdist_egg' or the latter would
287 # re-set install paths.
288
289 TEST_DIR=${BUILD_DIR}/test
290 local bindir=${TEST_DIR}/scripts
291 local libdir=${TEST_DIR}/lib
292 PYTHONPATH=${libdir}:${PYTHONPATH}
293
294 local add_args=(
295 install
296 --home="${TEST_DIR}"
297 --install-lib="${libdir}"
298 --install-scripts="${bindir}"
299 )
300
301 if "${PYTHON:-python}" setup.py --help bdist_egg &>/dev/null; then
302 add_args+=(
303 bdist_egg --dist-dir="${TEST_DIR}"
304 )
305 fi
306
307 mkdir -p "${libdir}" || die
308 esetup.py "${add_args[@]}"
309 }
310
311 _disable_ez_setup() {
312 local stub="def use_setuptools(*args, **kwargs): pass"
313 [[ -f ez_setup.py ]] && echo "${stub}" > ez_setup.py
314 [[ -f distribute_setup.py ]] && echo "${stub}" > distribute_setup.py
315 }
316
317 # @FUNCTION: distutils-r1_python_prepare_all
318 # @DESCRIPTION:
319 # The default python_prepare_all(). It applies the patches from PATCHES
320 # array, then user patches and finally calls python_copy_sources to
321 # create copies of resulting sources for each Python implementation.
322 #
323 # At some point in the future, it may also apply eclass-specific
324 # distutils patches and/or quirks.
325 distutils-r1_python_prepare_all() {
326 debug-print-function ${FUNCNAME} "${@}"
327
328 [[ ${PATCHES} ]] && epatch "${PATCHES[@]}"
329
330 epatch_user
331
332 # by default, use in-source build if python_prepare() is used
333 if [[ ! ${DISTUTILS_IN_SOURCE_BUILD+1} ]]; then
334 if declare -f python_prepare >/dev/null; then
335 DISTUTILS_IN_SOURCE_BUILD=1
336 fi
337 fi
338
339 # Prevent packages from downloading their own copy of setuptools
340 _disable_ez_setup
341
342 if [[ ${DISTUTILS_IN_SOURCE_BUILD} && ! ${DISTUTILS_SINGLE_IMPL} ]]
343 then
344 # create source copies for each implementation
345 python_copy_sources
346 fi
347 }
348
349 # @FUNCTION: distutils-r1_python_prepare
350 # @DESCRIPTION:
351 # The default python_prepare(). A no-op.
352 distutils-r1_python_prepare() {
353 debug-print-function ${FUNCNAME} "${@}"
354
355 :
356 }
357
358 # @FUNCTION: distutils-r1_python_configure
359 # @DESCRIPTION:
360 # The default python_configure(). A no-op.
361 distutils-r1_python_configure() {
362 debug-print-function ${FUNCNAME} "${@}"
363
364 :
365 }
366
367 # @FUNCTION: distutils-r1_python_compile
368 # @USAGE: [additional-args...]
369 # @DESCRIPTION:
370 # The default python_compile(). Runs 'esetup.py build'. Any parameters
371 # passed to this function will be appended to setup.py invocation,
372 # i.e. passed as options to the 'build' command.
373 distutils-r1_python_compile() {
374 debug-print-function ${FUNCNAME} "${@}"
375
376 esetup.py "${@}"
377 }
378
379 # @FUNCTION: distutils-r1_python_test
380 # @DESCRIPTION:
381 # The default python_test(). A no-op.
382 distutils-r1_python_test() {
383 debug-print-function ${FUNCNAME} "${@}"
384
385 :
386 }
387
388 # @FUNCTION: _distutils-r1_rename_scripts
389 # @USAGE: <path>
390 # @INTERNAL
391 # @DESCRIPTION:
392 # Renames installed Python scripts to be implementation-suffixed.
393 # ${EPYTHON} needs to be set to the implementation name.
394 #
395 # All executable scripts having shebang referencing ${EPYTHON}
396 # in given path will be renamed.
397 _distutils-r1_rename_scripts() {
398 debug-print-function ${FUNCNAME} "${@}"
399
400 local path=${1}
401 [[ ${path} ]] || die "${FUNCNAME}: no path given"
402
403 local f
404 while IFS= read -r -d '' f; do
405 debug-print "${FUNCNAME}: found executable at ${f#${D}/}"
406
407 local shebang
408 read -r shebang < "${f}"
409 if [[ ${shebang} == '#!'*${EPYTHON}* ]]
410 then
411 debug-print "${FUNCNAME}: matching shebang: ${shebang}"
412
413 local newf=${f}-${EPYTHON}
414 debug-print "${FUNCNAME}: renaming to ${newf#${D}/}"
415 mv "${f}" "${newf}" || die
416
417 debug-print "${FUNCNAME}: installing wrapper at ${f#${D}/}"
418 _python_ln_rel "${path}${EPREFIX}"/usr/bin/python-exec "${f}" || die
419 fi
420 done < <(find "${path}" -type f -executable -print0)
421 }
422
423 # @FUNCTION: distutils-r1_python_install
424 # @USAGE: [additional-args...]
425 # @DESCRIPTION:
426 # The default python_install(). Runs 'esetup.py install', appending
427 # the optimization flags. Then renames the installed scripts.
428 # Any parameters passed to this function will be appended
429 # to the setup.py invocation (i.e. as options to the 'install' command).
430 distutils-r1_python_install() {
431 debug-print-function ${FUNCNAME} "${@}"
432
433 local flags
434
435 case "${EPYTHON}" in
436 jython*)
437 flags=(--compile);;
438 *)
439 flags=(--compile -O2);;
440 esac
441 debug-print "${FUNCNAME}: [${EPYTHON}] flags: ${flags}"
442
443 # enable compilation for the install phase.
444 local -x PYTHONDONTWRITEBYTECODE
445
446 # python likes to compile any module it sees, which triggers sandbox
447 # failures if some packages haven't compiled their modules yet.
448 addpredict "$(python_get_sitedir)"
449 addpredict /usr/lib/portage/pym
450
451 local root=${D}/_${EPYTHON}
452 [[ ${DISTUTILS_SINGLE_IMPL} ]] && root=${D}
453
454 esetup.py install "${flags[@]}" --root="${root}" "${@}"
455
456 if [[ -d ${root}$(python_get_sitedir)/tests ]]; then
457 die "Package installs 'tests' package, file collisions likely."
458 fi
459
460 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
461 _distutils-r1_rename_scripts "${root}"
462 multibuild_merge_root "${root}" "${D}"
463 fi
464 }
465
466 # @FUNCTION: distutils-r1_python_install_all
467 # @DESCRIPTION:
468 # The default python_install_all(). It installs the documentation.
469 distutils-r1_python_install_all() {
470 debug-print-function ${FUNCNAME} "${@}"
471
472 if declare -p DOCS &>/dev/null; then
473 # an empty list == don't install anything
474 if [[ ${DOCS[@]} ]]; then
475 dodoc -r "${DOCS[@]}"
476 fi
477 else
478 local f
479 # same list as in PMS
480 for f in README* ChangeLog AUTHORS NEWS TODO CHANGES \
481 THANKS BUGS FAQ CREDITS CHANGELOG; do
482 if [[ -s ${f} ]]; then
483 dodoc "${f}"
484 fi
485 done
486 fi
487
488 if declare -p HTML_DOCS &>/dev/null; then
489 dohtml -r "${HTML_DOCS[@]}"
490 fi
491
492 if declare -p EXAMPLES &>/dev/null; then
493 local INSDESTTREE=/usr/share/doc/${PF}/examples
494 doins -r "${EXAMPLES[@]}"
495 docompress -x "${INSDESTTREE}"
496 fi
497 }
498
499 # @FUNCTION: distutils-r1_run_phase
500 # @USAGE: [<argv>...]
501 # @INTERNAL
502 # @DESCRIPTION:
503 # Run the given command.
504 #
505 # If out-of-source builds are used, the phase function is run in source
506 # directory, with BUILD_DIR pointing at the build directory
507 # and PYTHONPATH having an entry for the module build directory.
508 #
509 # If in-source builds are used, the command is executed in the directory
510 # holding the per-implementation copy of sources. BUILD_DIR points
511 # to the 'build' subdirectory.
512 distutils-r1_run_phase() {
513 debug-print-function ${FUNCNAME} "${@}"
514
515 if [[ ${DISTUTILS_IN_SOURCE_BUILD} ]]; then
516 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
517 pushd "${BUILD_DIR}" >/dev/null || die
518 fi
519 local BUILD_DIR=${BUILD_DIR}/build
520 fi
521 local -x PYTHONPATH="${BUILD_DIR}/lib:${PYTHONPATH}"
522
523 local TMPDIR=${T}/${EPYTHON}
524
525 mkdir -p "${TMPDIR}" || die
526
527 "${@}"
528
529 if [[ ${DISTUTILS_IN_SOURCE_BUILD} && ! ${DISTUTILS_SINGLE_IMPL} ]]
530 then
531 popd >/dev/null || die
532 fi
533 }
534
535 # @FUNCTION: _distutils-r1_run_common_phase
536 # @USAGE: [<argv>...]
537 # @INTERNAL
538 # @DESCRIPTION:
539 # Run the given command, restoring the best-implementation state.
540 #
541 # If in-source build is used, the command will be run in the copy
542 # of sources made for the best Python interpreter.
543 _distutils-r1_run_common_phase() {
544 local DISTUTILS_ORIG_BUILD_DIR=${BUILD_DIR}
545
546 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
547 local MULTIBUILD_VARIANTS
548 _python_obtain_impls
549
550 multibuild_for_best_variant _python_multibuild_wrapper \
551 distutils-r1_run_phase "${@}"
552 else
553 # semi-hack, be careful.
554 _distutils-r1_run_foreach_impl "${@}"
555 fi
556 }
557
558 # @FUNCTION: _distutils-r1_run_foreach_impl
559 # @INTERNAL
560 # @DESCRIPTION:
561 # Run the given phase for each implementation if multiple implementations
562 # are enabled, once otherwise.
563 _distutils-r1_run_foreach_impl() {
564 debug-print-function ${FUNCNAME} "${@}"
565
566 set -- distutils-r1_run_phase "${@}"
567
568 if [[ ! ${DISTUTILS_SINGLE_IMPL} ]]; then
569 if [[ ${DISTUTILS_NO_PARALLEL_BUILD} || ${DISTUTILS_SINGLE_IMPL} ]]
570 then
571 python_foreach_impl "${@}"
572 else
573 python_parallel_foreach_impl "${@}"
574 fi
575 else
576 if [[ ! ${EPYTHON} ]]; then
577 die "EPYTHON unset, python-single-r1_pkg_setup not called?!"
578 fi
579 local BUILD_DIR=${BUILD_DIR:-${S}}
580 BUILD_DIR=${BUILD_DIR%%/}_${EPYTHON}
581
582 "${@}"
583 fi
584 }
585
586 distutils-r1_src_prepare() {
587 debug-print-function ${FUNCNAME} "${@}"
588
589 # common preparations
590 if declare -f python_prepare_all >/dev/null; then
591 python_prepare_all
592 else
593 distutils-r1_python_prepare_all
594 fi
595
596 if declare -f python_prepare >/dev/null; then
597 _distutils-r1_run_foreach_impl python_prepare
598 fi
599 }
600
601 distutils-r1_src_configure() {
602 if declare -f python_configure >/dev/null; then
603 _distutils-r1_run_foreach_impl python_configure
604 fi
605
606 if declare -f python_configure_all >/dev/null; then
607 _distutils-r1_run_common_phase python_configure_all
608 fi
609 }
610
611 distutils-r1_src_compile() {
612 debug-print-function ${FUNCNAME} "${@}"
613
614 if declare -f python_compile >/dev/null; then
615 _distutils-r1_run_foreach_impl python_compile
616 else
617 _distutils-r1_run_foreach_impl distutils-r1_python_compile
618 fi
619
620 if declare -f python_compile_all >/dev/null; then
621 _distutils-r1_run_common_phase python_compile_all
622 fi
623 }
624
625 distutils-r1_src_test() {
626 debug-print-function ${FUNCNAME} "${@}"
627
628 if declare -f python_test >/dev/null; then
629 _distutils-r1_run_foreach_impl python_test
630 fi
631
632 if declare -f python_test_all >/dev/null; then
633 _distutils-r1_run_common_phase python_test_all
634 fi
635 }
636
637 distutils-r1_src_install() {
638 debug-print-function ${FUNCNAME} "${@}"
639
640 if declare -f python_install >/dev/null; then
641 _distutils-r1_run_foreach_impl python_install
642 else
643 _distutils-r1_run_foreach_impl distutils-r1_python_install
644 fi
645
646 if declare -f python_install_all >/dev/null; then
647 _distutils-r1_run_common_phase python_install_all
648 else
649 _distutils-r1_run_common_phase distutils-r1_python_install_all
650 fi
651 }
652
653 _DISTUTILS_R1=1
654 fi

  ViewVC Help
Powered by ViewVC 1.1.20