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

Contents of /eclass/distutils-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.70 - (show annotations) (download)
Tue Apr 30 05:36:19 2013 UTC (14 months, 2 weeks ago) by mgorny
Branch: MAIN
Changes since 1.69: +5 -3 lines
Use bash built-ins rather than external tools.

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

  ViewVC Help
Powered by ViewVC 1.1.20