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

Contents of /eclass/distutils-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (show annotations) (download)
Sun Jan 27 16:39:23 2013 UTC (21 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.47: +90 -26 lines
Support using distutils-r1 along with python-single-r1.

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

  ViewVC Help
Powered by ViewVC 1.1.20