/[gentoo-x86]/eclass/python-utils-r1.eclass
Gentoo

Contents of /eclass/python-utils-r1.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (show annotations) (download)
Tue Apr 30 05:36:19 2013 UTC (19 months, 3 weeks ago) by mgorny
Branch: MAIN
Changes since 1.22: +4 -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/python-utils-r1.eclass,v 1.22 2013/04/14 00:10:00 floppym Exp $
4
5 # @ECLASS: python-utils-r1
6 # @MAINTAINER:
7 # Python team <python@gentoo.org>
8 # @AUTHOR:
9 # Author: Michał Górny <mgorny@gentoo.org>
10 # Based on work of: Krzysztof Pawlik <nelchael@gentoo.org>
11 # @BLURB: Utility functions for packages with Python parts.
12 # @DESCRIPTION:
13 # An utility eclass providing functions to query Python implementations,
14 # install Python modules and scripts.
15 #
16 # This eclass does not set any metadata variables nor export any phase
17 # functions. It can be inherited safely.
18 #
19 # For more information, please see the python-r1 Developer's Guide:
20 # http://www.gentoo.org/proj/en/Python/python-r1/dev-guide.xml
21
22 case "${EAPI:-0}" in
23 0|1|2|3|4|5)
24 # EAPI=4 makes die behavior clear
25 ;;
26 *)
27 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
28 ;;
29 esac
30
31 if [[ ${_PYTHON_ECLASS_INHERITED} ]]; then
32 die 'python-r1 suite eclasses can not be used with python.eclass.'
33 fi
34
35 if [[ ! ${_PYTHON_UTILS_R1} ]]; then
36
37 inherit multilib toolchain-funcs
38
39 # @ECLASS-VARIABLE: _PYTHON_ALL_IMPLS
40 # @INTERNAL
41 # @DESCRIPTION:
42 # All supported Python implementations, most preferred last.
43 _PYTHON_ALL_IMPLS=(
44 jython2_5 jython2_7
45 pypy1_9 pypy2_0
46 python3_1 python3_2 python3_3
47 python2_5 python2_6 python2_7
48 )
49
50 # @FUNCTION: _python_impl_supported
51 # @USAGE: <impl>
52 # @INTERNAL
53 # @DESCRIPTION:
54 # Check whether the implementation <impl> (PYTHON_COMPAT-form)
55 # is still supported.
56 #
57 # Returns 0 if the implementation is valid and supported. If it is
58 # unsupported, returns 1 -- and the caller should ignore the entry.
59 # If it is invalid, dies with an appopriate error messages.
60 _python_impl_supported() {
61 debug-print-function ${FUNCNAME} "${@}"
62
63 [[ ${#} -eq 1 ]] || die "${FUNCNAME}: takes exactly 1 argument (impl)."
64
65 local impl=${1}
66
67 # keep in sync with _PYTHON_ALL_IMPLS!
68 # (not using that list because inline patterns shall be faster)
69 case "${impl}" in
70 python2_[567]|python3_[123]|pypy1_9|pypy2_0|jython2_[57])
71 return 0
72 ;;
73 pypy1_8)
74 return 1
75 ;;
76 *)
77 die "Invalid implementation in PYTHON_COMPAT: ${impl}"
78 esac
79 }
80
81 # @ECLASS-VARIABLE: PYTHON
82 # @DESCRIPTION:
83 # The absolute path to the current Python interpreter.
84 #
85 # Set and exported only in commands run by python_foreach_impl().
86 #
87 # Example value:
88 # @CODE
89 # /usr/bin/python2.6
90 # @CODE
91
92 # @ECLASS-VARIABLE: EPYTHON
93 # @DESCRIPTION:
94 # The executable name of the current Python interpreter.
95 #
96 # This variable is used consistently with python.eclass.
97 #
98 # Set and exported only in commands run by python_foreach_impl().
99 #
100 # Example value:
101 # @CODE
102 # python2.6
103 # @CODE
104
105 # @ECLASS-VARIABLE: PYTHON_SITEDIR
106 # @DESCRIPTION:
107 # The path to Python site-packages directory.
108 #
109 # Set and exported on request using python_export().
110 #
111 # Example value:
112 # @CODE
113 # /usr/lib64/python2.6/site-packages
114 # @CODE
115
116 # @ECLASS-VARIABLE: PYTHON_INCLUDEDIR
117 # @DESCRIPTION:
118 # The path to Python include directory.
119 #
120 # Set and exported on request using python_export().
121 #
122 # Example value:
123 # @CODE
124 # /usr/include/python2.6
125 # @CODE
126
127 # @ECLASS-VARIABLE: PYTHON_LIBPATH
128 # @DESCRIPTION:
129 # The path to Python library.
130 #
131 # Set and exported on request using python_export().
132 # Valid only for CPython.
133 #
134 # Example value:
135 # @CODE
136 # /usr/lib64/libpython2.6.so
137 # @CODE
138
139 # @ECLASS-VARIABLE: PYTHON_CFLAGS
140 # @DESCRIPTION:
141 # Proper C compiler flags for building against Python. Obtained from
142 # pkg-config or python-config.
143 #
144 # Set and exported on request using python_export().
145 # Valid only for CPython. Requires a proper build-time dependency
146 # on the Python implementation and on pkg-config.
147 #
148 # Example value:
149 # @CODE
150 # -I/usr/include/python2.7
151 # @CODE
152
153 # @ECLASS-VARIABLE: PYTHON_LIBS
154 # @DESCRIPTION:
155 # Proper C compiler flags for linking against Python. Obtained from
156 # pkg-config or python-config.
157 #
158 # Set and exported on request using python_export().
159 # Valid only for CPython. Requires a proper build-time dependency
160 # on the Python implementation and on pkg-config.
161 #
162 # Example value:
163 # @CODE
164 # -lpython2.7
165 # @CODE
166
167 # @ECLASS-VARIABLE: PYTHON_PKG_DEP
168 # @DESCRIPTION:
169 # The complete dependency on a particular Python package as a string.
170 #
171 # Set and exported on request using python_export().
172 #
173 # Example value:
174 # @CODE
175 # dev-lang/python:2.7[xml]
176 # @CODE
177
178 # @FUNCTION: python_export
179 # @USAGE: [<impl>] <variables>...
180 # @DESCRIPTION:
181 # Set and export the Python implementation-relevant variables passed
182 # as parameters.
183 #
184 # The optional first parameter may specify the requested Python
185 # implementation (either as PYTHON_TARGETS value, e.g. python2_7,
186 # or an EPYTHON one, e.g. python2.7). If no implementation passed,
187 # the current one will be obtained from ${EPYTHON}.
188 #
189 # The variables which can be exported are: PYTHON, EPYTHON,
190 # PYTHON_SITEDIR. They are described more completely in the eclass
191 # variable documentation.
192 python_export() {
193 debug-print-function ${FUNCNAME} "${@}"
194
195 local impl var
196
197 case "${1}" in
198 python*|jython*)
199 impl=${1/_/.}
200 shift
201 ;;
202 pypy-c*)
203 impl=${1}
204 shift
205 ;;
206 pypy*)
207 local v=${1#pypy}
208 impl=pypy-c${v/_/.}
209 shift
210 ;;
211 *)
212 impl=${EPYTHON}
213 [[ ${impl} ]] || die "python_export: no impl nor EPYTHON"
214 ;;
215 esac
216 debug-print "${FUNCNAME}: implementation: ${impl}"
217
218 for var; do
219 case "${var}" in
220 EPYTHON)
221 export EPYTHON=${impl}
222 debug-print "${FUNCNAME}: EPYTHON = ${EPYTHON}"
223 ;;
224 PYTHON)
225 export PYTHON=${EPREFIX}/usr/bin/${impl}
226 debug-print "${FUNCNAME}: PYTHON = ${PYTHON}"
227 ;;
228 PYTHON_SITEDIR)
229 local dir
230 case "${impl}" in
231 python*)
232 dir=/usr/$(get_libdir)/${impl}
233 ;;
234 jython*)
235 dir=/usr/share/${impl}/Lib
236 ;;
237 pypy*)
238 dir=/usr/$(get_libdir)/${impl/-c/}
239 ;;
240 esac
241
242 export PYTHON_SITEDIR=${EPREFIX}${dir}/site-packages
243 debug-print "${FUNCNAME}: PYTHON_SITEDIR = ${PYTHON_SITEDIR}"
244 ;;
245 PYTHON_INCLUDEDIR)
246 local dir
247 case "${impl}" in
248 python*)
249 dir=/usr/include/${impl}
250 ;;
251 jython*)
252 dir=/usr/share/${impl}/Include
253 ;;
254 pypy*)
255 dir=/usr/$(get_libdir)/${impl/-c/}/include
256 ;;
257 esac
258
259 export PYTHON_INCLUDEDIR=${EPREFIX}${dir}
260 debug-print "${FUNCNAME}: PYTHON_INCLUDEDIR = ${PYTHON_INCLUDEDIR}"
261 ;;
262 PYTHON_LIBPATH)
263 local libname
264 case "${impl}" in
265 python*)
266 libname=lib${impl}
267 ;;
268 *)
269 die "${impl} lacks a dynamic library"
270 ;;
271 esac
272
273 local path=${EPREFIX}/usr/$(get_libdir)
274
275 export PYTHON_LIBPATH=${path}/${libname}$(get_libname)
276 debug-print "${FUNCNAME}: PYTHON_LIBPATH = ${PYTHON_LIBPATH}"
277 ;;
278 PYTHON_CFLAGS)
279 local val
280
281 case "${impl}" in
282 python2.5|python2.6)
283 # old versions support python-config only
284 val=$("${impl}-config" --includes)
285 ;;
286 python*)
287 # python-2.7, python-3.2, etc.
288 val=$($(tc-getPKG_CONFIG) --cflags ${impl/n/n-})
289 ;;
290 *)
291 die "${impl}: obtaining ${var} not supported"
292 ;;
293 esac
294
295 export PYTHON_CFLAGS=${val}
296 debug-print "${FUNCNAME}: PYTHON_CFLAGS = ${PYTHON_CFLAGS}"
297 ;;
298 PYTHON_LIBS)
299 local val
300
301 case "${impl}" in
302 python2.5|python2.6)
303 # old versions support python-config only
304 val=$("${impl}-config" --libs)
305 ;;
306 python*)
307 # python-2.7, python-3.2, etc.
308 val=$($(tc-getPKG_CONFIG) --libs ${impl/n/n-})
309 ;;
310 *)
311 die "${impl}: obtaining ${var} not supported"
312 ;;
313 esac
314
315 export PYTHON_LIBS=${val}
316 debug-print "${FUNCNAME}: PYTHON_LIBS = ${PYTHON_LIBS}"
317 ;;
318 PYTHON_PKG_DEP)
319 local d
320 case ${impl} in
321 python*)
322 PYTHON_PKG_DEP='dev-lang/python';;
323 jython*)
324 PYTHON_PKG_DEP='dev-java/jython';;
325 pypy*)
326 PYTHON_PKG_DEP='dev-python/pypy';;
327 *)
328 die "Invalid implementation: ${impl}"
329 esac
330
331 # slot
332 PYTHON_PKG_DEP+=:${impl##*[a-z-]}
333
334 # use-dep
335 if [[ ${PYTHON_REQ_USE} ]]; then
336 PYTHON_PKG_DEP+=[${PYTHON_REQ_USE}]
337 fi
338
339 export PYTHON_PKG_DEP
340 debug-print "${FUNCNAME}: PYTHON_PKG_DEP = ${PYTHON_PKG_DEP}"
341 ;;
342 *)
343 die "python_export: unknown variable ${var}"
344 esac
345 done
346 }
347
348 # @FUNCTION: python_get_PYTHON
349 # @USAGE: [<impl>]
350 # @DESCRIPTION:
351 # Obtain and print the path to the Python interpreter for the given
352 # implementation. If no implementation is provided, ${EPYTHON} will
353 # be used.
354 #
355 # If you just need to have PYTHON set (and exported), then it is better
356 # to use python_export() directly instead.
357 python_get_PYTHON() {
358 debug-print-function ${FUNCNAME} "${@}"
359
360 python_export "${@}" PYTHON
361 echo "${PYTHON}"
362 }
363
364 # @FUNCTION: python_get_EPYTHON
365 # @USAGE: <impl>
366 # @DESCRIPTION:
367 # Obtain and print the EPYTHON value for the given implementation.
368 #
369 # If you just need to have EPYTHON set (and exported), then it is better
370 # to use python_export() directly instead.
371 python_get_EPYTHON() {
372 debug-print-function ${FUNCNAME} "${@}"
373
374 python_export "${@}" EPYTHON
375 echo "${EPYTHON}"
376 }
377
378 # @FUNCTION: python_get_sitedir
379 # @USAGE: [<impl>]
380 # @DESCRIPTION:
381 # Obtain and print the 'site-packages' path for the given
382 # implementation. If no implementation is provided, ${EPYTHON} will
383 # be used.
384 #
385 # If you just need to have PYTHON_SITEDIR set (and exported), then it is
386 # better to use python_export() directly instead.
387 python_get_sitedir() {
388 debug-print-function ${FUNCNAME} "${@}"
389
390 python_export "${@}" PYTHON_SITEDIR
391 echo "${PYTHON_SITEDIR}"
392 }
393
394 # @FUNCTION: python_get_includedir
395 # @USAGE: [<impl>]
396 # @DESCRIPTION:
397 # Obtain and print the include path for the given implementation. If no
398 # implementation is provided, ${EPYTHON} will be used.
399 #
400 # If you just need to have PYTHON_INCLUDEDIR set (and exported), then it
401 # is better to use python_export() directly instead.
402 python_get_includedir() {
403 debug-print-function ${FUNCNAME} "${@}"
404
405 python_export "${@}" PYTHON_INCLUDEDIR
406 echo "${PYTHON_INCLUDEDIR}"
407 }
408
409 # @FUNCTION: python_get_library_path
410 # @USAGE: [<impl>]
411 # @DESCRIPTION:
412 # Obtain and print the Python library path for the given implementation.
413 # If no implementation is provided, ${EPYTHON} will be used.
414 #
415 # Please note that this function can be used with CPython only. Use
416 # in another implementation will result in a fatal failure.
417 python_get_library_path() {
418 debug-print-function ${FUNCNAME} "${@}"
419
420 python_export "${@}" PYTHON_LIBPATH
421 echo "${PYTHON_LIBPATH}"
422 }
423
424 # @FUNCTION: python_get_CFLAGS
425 # @USAGE: [<impl>]
426 # @DESCRIPTION:
427 # Obtain and print the compiler flags for building against Python,
428 # for the given implementation. If no implementation is provided,
429 # ${EPYTHON} will be used.
430 #
431 # Please note that this function can be used with CPython only.
432 # It requires Python and pkg-config installed, and therefore proper
433 # build-time dependencies need be added to the ebuild.
434 python_get_CFLAGS() {
435 debug-print-function ${FUNCNAME} "${@}"
436
437 python_export "${@}" PYTHON_CFLAGS
438 echo "${PYTHON_CFLAGS}"
439 }
440
441 # @FUNCTION: python_get_LIBS
442 # @USAGE: [<impl>]
443 # @DESCRIPTION:
444 # Obtain and print the compiler flags for linking against Python,
445 # for the given implementation. If no implementation is provided,
446 # ${EPYTHON} will be used.
447 #
448 # Please note that this function can be used with CPython only.
449 # It requires Python and pkg-config installed, and therefore proper
450 # build-time dependencies need be added to the ebuild.
451 python_get_LIBS() {
452 debug-print-function ${FUNCNAME} "${@}"
453
454 python_export "${@}" PYTHON_LIBS
455 echo "${PYTHON_LIBS}"
456 }
457
458 # @FUNCTION: _python_rewrite_shebang
459 # @INTERNAL
460 # @USAGE: [<EPYTHON>] <path>...
461 # @DESCRIPTION:
462 # Replaces 'python' executable in the shebang with the executable name
463 # of the specified interpreter. If no EPYTHON value (implementation) is
464 # used, the current ${EPYTHON} will be used.
465 #
466 # All specified files must start with a 'python' shebang. A file not
467 # having a matching shebang will be refused. The exact shebang style
468 # will be preserved in order not to break anything.
469 #
470 # Example conversions:
471 # @CODE
472 # From: #!/usr/bin/python -R
473 # To: #!/usr/bin/python2.7 -R
474 #
475 # From: #!/usr/bin/env FOO=bar python
476 # To: #!/usr/bin/env FOO=bar python2.7
477 # @CODE
478 _python_rewrite_shebang() {
479 debug-print-function ${FUNCNAME} "${@}"
480
481 local impl
482 case "${1}" in
483 python*|jython*|pypy-c*)
484 impl=${1}
485 shift
486 ;;
487 *)
488 impl=${EPYTHON}
489 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON"
490 ;;
491 esac
492 debug-print "${FUNCNAME}: implementation: ${impl}"
493
494 local f
495 for f; do
496 local from shebang
497 read -r shebang < "${f}"
498 shebang=${shebang%$'\r'}
499 debug-print "${FUNCNAME}: path = ${f}"
500 debug-print "${FUNCNAME}: shebang = ${shebang}"
501
502 if [[ "${shebang} " == *'python '* ]]; then
503 from=python
504 elif [[ "${shebang} " == *'python2 '* ]]; then
505 from=python2
506 elif [[ "${shebang} " == *'python3 '* ]]; then
507 from=python3
508 else
509 eerror "A file does not seem to have a supported shebang:"
510 eerror " file: ${f}"
511 eerror " shebang: ${shebang}"
512 die "${FUNCNAME}: ${f} does not seem to have a valid shebang"
513 fi
514
515 if [[ ${from} == python2 && ${impl} == python3*
516 || ${from} == python3 && ${impl} != python3* ]]; then
517 eerror "A file does have shebang not supporting requested impl:"
518 eerror " file: ${f}"
519 eerror " shebang: ${shebang}"
520 eerror " impl: ${impl}"
521 die "${FUNCNAME}: ${f} does have shebang not supporting ${EPYTHON}"
522 fi
523
524 sed -i -e "1s:${from}:${impl}:" "${f}" || die
525 done
526 }
527
528 # @FUNCTION: _python_ln_rel
529 # @INTERNAL
530 # @USAGE: <from> <to>
531 # @DESCRIPTION:
532 # Create a relative symlink.
533 _python_ln_rel() {
534 debug-print-function ${FUNCNAME} "${@}"
535
536 local from=${1}
537 local to=${2}
538
539 local frpath=${from%/*}/
540 local topath=${to%/*}/
541 local rel_path=
542
543 while [[ ${topath} ]]; do
544 local frseg= toseg=
545
546 while [[ ! ${frseg} && ${frpath} ]]; do
547 frseg=${frpath%%/*}
548 frpath=${frpath#${frseg}/}
549 done
550
551 while [[ ! ${toseg} && ${topath} ]]; do
552 toseg=${topath%%/*}
553 topath=${topath#${toseg}/}
554 done
555
556 if [[ ${frseg} != ${toseg} ]]; then
557 rel_path=../${rel_path}${frseg:+${frseg}/}
558 fi
559 done
560 rel_path+=${frpath}${1##*/}
561
562 debug-print "${FUNCNAME}: ${from} -> ${to}"
563 debug-print "${FUNCNAME}: rel_path = ${rel_path}"
564
565 ln -fs "${rel_path}" "${to}"
566 }
567
568 # @FUNCTION: python_optimize
569 # @USAGE: [<directory>...]
570 # @DESCRIPTION:
571 # Compile and optimize Python modules in specified directories (absolute
572 # paths). If no directories are provided, the default system paths
573 # are used (prepended with ${D}).
574 python_optimize() {
575 debug-print-function ${FUNCNAME} "${@}"
576
577 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
578
579 local PYTHON=${PYTHON}
580 [[ ${PYTHON} ]] || python_export PYTHON
581
582 # Note: python2.6 can't handle passing files to compileall...
583
584 # default to sys.path
585 if [[ ${#} -eq 0 ]]; then
586 local f
587 while IFS= read -r -d '' f; do
588 # 1) accept only absolute paths
589 # (i.e. skip '', '.' or anything like that)
590 # 2) skip paths which do not exist
591 # (python2.6 complains about them verbosely)
592
593 if [[ ${f} == /* && -d ${D}${f} ]]; then
594 set -- "${D}${f}" "${@}"
595 fi
596 done < <("${PYTHON}" -c 'import sys; print("\0".join(sys.path))')
597
598 debug-print "${FUNCNAME}: using sys.path: ${*/%/;}"
599 fi
600
601 local d
602 for d; do
603 # make sure to get a nice path without //
604 local instpath=${d#${D}}
605 instpath=/${instpath##/}
606
607 case "${EPYTHON}" in
608 python*)
609 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${d}"
610 "${PYTHON}" -OO -m compileall -q -f -d "${instpath}" "${d}"
611 ;;
612 *)
613 "${PYTHON}" -m compileall -q -f -d "${instpath}" "${@}"
614 ;;
615 esac
616 done
617 }
618
619 # @ECLASS-VARIABLE: python_scriptroot
620 # @DEFAULT_UNSET
621 # @DESCRIPTION:
622 # The current script destination for python_doscript(). The path
623 # is relative to the installation root (${ED}).
624 #
625 # When unset, ${DESTTREE}/bin (/usr/bin by default) will be used.
626 #
627 # Can be set indirectly through the python_scriptinto() function.
628 #
629 # Example:
630 # @CODE
631 # src_install() {
632 # local python_scriptroot=${GAMES_BINDIR}
633 # python_foreach_impl python_doscript foo
634 # }
635 # @CODE
636
637 # @FUNCTION: python_scriptinto
638 # @USAGE: <new-path>
639 # @DESCRIPTION:
640 # Set the current scriptroot. The new value will be stored
641 # in the 'python_scriptroot' environment variable. The new value need
642 # be relative to the installation root (${ED}).
643 #
644 # Alternatively, you can set the variable directly.
645 python_scriptinto() {
646 debug-print-function ${FUNCNAME} "${@}"
647
648 python_scriptroot=${1}
649 }
650
651 # @FUNCTION: python_doscript
652 # @USAGE: <files>...
653 # @DESCRIPTION:
654 # Install the given scripts into current python_scriptroot,
655 # for the current Python implementation (${EPYTHON}).
656 #
657 # All specified files must start with a 'python' shebang. The shebang
658 # will be converted, the file will be renamed to be EPYTHON-suffixed
659 # and a wrapper will be installed in place of the original name.
660 #
661 # Example:
662 # @CODE
663 # src_install() {
664 # python_foreach_impl python_doscript ${PN}
665 # }
666 # @CODE
667 python_doscript() {
668 debug-print-function ${FUNCNAME} "${@}"
669
670 local f
671 for f; do
672 python_newscript "${f}" "${f##*/}"
673 done
674 }
675
676 # @FUNCTION: python_newscript
677 # @USAGE: <path> <new-name>
678 # @DESCRIPTION:
679 # Install the given script into current python_scriptroot
680 # for the current Python implementation (${EPYTHON}), and name it
681 # <new-name>.
682 #
683 # The file must start with a 'python' shebang. The shebang will be
684 # converted, the file will be renamed to be EPYTHON-suffixed
685 # and a wrapper will be installed in place of the <new-name>.
686 #
687 # Example:
688 # @CODE
689 # src_install() {
690 # python_foreach_impl python_newscript foo.py foo
691 # }
692 # @CODE
693 python_newscript() {
694 debug-print-function ${FUNCNAME} "${@}"
695
696 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
697 [[ ${#} -eq 2 ]] || die "Usage: ${FUNCNAME} <path> <new-name>"
698
699 local d=${python_scriptroot:-${DESTTREE}/bin}
700 local INSDESTTREE INSOPTIONS
701
702 insinto "${d}"
703 insopts -m755
704
705 local f=${1}
706 local barefn=${2}
707
708 local newfn=${barefn}-${EPYTHON}
709
710 debug-print "${FUNCNAME}: ${f} -> ${d}/${newfn}"
711 newins "${f}" "${newfn}" || die
712 _python_rewrite_shebang "${ED}/${d}/${newfn}"
713
714 # install the wrapper
715 _python_ln_rel "${ED}"/usr/bin/python-exec "${ED}/${d}/${barefn}" || die
716 }
717
718 # @ECLASS-VARIABLE: python_moduleroot
719 # @DEFAULT_UNSET
720 # @DESCRIPTION:
721 # The current module root for python_domodule(). The path can be either
722 # an absolute system path (it must start with a slash, and ${ED} will be
723 # prepended to it) or relative to the implementation's site-packages directory
724 # (then it must start with a non-slash character).
725 #
726 # When unset, the modules will be installed in the site-packages root.
727 #
728 # Can be set indirectly through the python_moduleinto() function.
729 #
730 # Example:
731 # @CODE
732 # src_install() {
733 # local python_moduleroot=bar
734 # # installs ${PYTHON_SITEDIR}/bar/baz.py
735 # python_foreach_impl python_domodule baz.py
736 # }
737 # @CODE
738
739 # @FUNCTION: python_moduleinto
740 # @USAGE: <new-path>
741 # @DESCRIPTION:
742 # Set the current module root. The new value will be stored
743 # in the 'python_moduleroot' environment variable. The new value need
744 # be relative to the site-packages root.
745 #
746 # Alternatively, you can set the variable directly.
747 python_moduleinto() {
748 debug-print-function ${FUNCNAME} "${@}"
749
750 python_moduleroot=${1}
751 }
752
753 # @FUNCTION: python_domodule
754 # @USAGE: <files>...
755 # @DESCRIPTION:
756 # Install the given modules (or packages) into the current
757 # python_moduleroot. The list can mention both modules (files)
758 # and packages (directories). All listed files will be installed
759 # for all enabled implementations, and compiled afterwards.
760 #
761 # Example:
762 # @CODE
763 # src_install() {
764 # # (${PN} being a directory)
765 # python_foreach_impl python_domodule ${PN}
766 # }
767 # @CODE
768 python_domodule() {
769 debug-print-function ${FUNCNAME} "${@}"
770
771 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
772
773 local d
774 if [[ ${python_moduleroot} == /* ]]; then
775 # absolute path
776 d=${python_moduleroot}
777 else
778 # relative to site-packages
779 local PYTHON_SITEDIR=${PYTHON_SITEDIR}
780 [[ ${PYTHON_SITEDIR} ]] || python_export PYTHON_SITEDIR
781
782 d=${PYTHON_SITEDIR#${EPREFIX}}/${python_moduleroot}
783 fi
784
785 local INSDESTTREE
786
787 insinto "${d}"
788 doins -r "${@}" || die
789
790 python_optimize "${ED}/${d}"
791 }
792
793 # @FUNCTION: python_doheader
794 # @USAGE: <files>...
795 # @DESCRIPTION:
796 # Install the given headers into the implementation-specific include
797 # directory. This function is unconditionally recursive, i.e. you can
798 # pass directories instead of files.
799 #
800 # Example:
801 # @CODE
802 # src_install() {
803 # python_foreach_impl python_doheader foo.h bar.h
804 # }
805 # @CODE
806 python_doheader() {
807 debug-print-function ${FUNCNAME} "${@}"
808
809 [[ ${EPYTHON} ]] || die 'No Python implementation set (EPYTHON is null).'
810
811 local d PYTHON_INCLUDEDIR=${PYTHON_INCLUDEDIR}
812 [[ ${PYTHON_INCLUDEDIR} ]] || python_export PYTHON_INCLUDEDIR
813
814 d=${PYTHON_INCLUDEDIR#${EPREFIX}}
815
816 local INSDESTTREE
817
818 insinto "${d}"
819 doins -r "${@}" || die
820 }
821
822 # @FUNCTION: python_wrapper_setup
823 # @USAGE: <path> [<impl>]
824 # @DESCRIPTION:
825 # Create proper 'python' executable and pkg-config wrappers
826 # (if available) in the directory named by <path>. Set up PATH
827 # and PKG_CONFIG_PATH appropriately.
828 #
829 # The wrappers will be created for implementation named by <impl>,
830 # or for one named by ${EPYTHON} if no <impl> passed.
831 #
832 # If the named directory contains a python symlink already, it will
833 # be assumed to contain proper wrappers already and only environment
834 # setup will be done. If wrapper update is requested, the directory
835 # shall be removed first.
836 python_wrapper_setup() {
837 debug-print-function ${FUNCNAME} "${@}"
838
839 local workdir=${1}
840 local impl=${2:-${EPYTHON}}
841
842 [[ ${workdir} ]] || die "${FUNCNAME}: no workdir specified."
843 [[ ${impl} ]] || die "${FUNCNAME}: no impl nor EPYTHON specified."
844
845 if [[ ! -x ${workdir}/bin/python ]]; then
846 mkdir -p "${workdir}"/{bin,pkgconfig} || die
847
848 # Clean up, in case we were supposed to do a cheap update.
849 rm -f "${workdir}"/bin/python{,2,3,-config}
850 rm -f "${workdir}"/bin/2to3
851 rm -f "${workdir}"/pkgconfig/python{,2,3}.pc
852
853 local EPYTHON PYTHON
854 python_export "${impl}" EPYTHON PYTHON
855
856 local pyver
857 if [[ ${EPYTHON} == python3* ]]; then
858 pyver=3
859 else # includes pypy & jython
860 pyver=2
861 fi
862
863 # Python interpreter
864 ln -s "${PYTHON}" "${workdir}"/bin/python || die
865 ln -s python "${workdir}"/bin/python${pyver} || die
866
867 local nonsupp=()
868
869 # CPython-specific
870 if [[ ${EPYTHON} == python* ]]; then
871 ln -s "${PYTHON}-config" "${workdir}"/bin/python-config || die
872
873 # Python 2.6+.
874 if [[ ${EPYTHON} != python2.5 ]]; then
875 ln -s "${PYTHON/python/2to3-}" "${workdir}"/bin/2to3 || die
876 else
877 nonsupp+=( 2to3 )
878 fi
879
880 # Python 2.7+.
881 if [[ ${EPYTHON} != python2.[56] ]]; then
882 ln -s "${EPREFIX}"/usr/$(get_libdir)/pkgconfig/${EPYTHON/n/n-}.pc \
883 "${workdir}"/pkgconfig/python.pc || die
884 else
885 # XXX?
886 ln -s /dev/null "${workdir}"/pkgconfig/python.pc || die
887 fi
888 ln -s python.pc "${workdir}"/pkgconfig/python${pyver}.pc || die
889 else
890 nonsupp+=( 2to3 python-config )
891 fi
892
893 local x
894 for x in "${nonsupp[@]}"; do
895 echo >"${workdir}"/bin/${x} <<__EOF__ || die
896 #!/bin/sh
897 echo "${x} is not supported by ${EPYTHON}" >&2
898 exit 1
899 __EOF__
900 chmod +x "${workdir}"/bin/${x} || die
901 done
902
903 # Now, set the environment.
904 # But note that ${workdir} may be shared with something else,
905 # and thus already on top of PATH.
906 if [[ ${PATH##:*} != ${workdir}/bin ]]; then
907 PATH=${workdir}/bin${PATH:+:${PATH}}
908 fi
909 if [[ ${PKG_CONFIG_PATH##:*} != ${workdir}/pkgconfig ]]; then
910 PKG_CONFIG_PATH=${workdir}/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
911 fi
912 export PATH PKG_CONFIG_PATH
913 fi
914 }
915
916 _PYTHON_UTILS_R1=1
917 fi

  ViewVC Help
Powered by ViewVC 1.1.20