1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: perl-functions.eclass
8 # Seemant Kulleen <seemant@gentoo.org>
9 # Andreas K. Huettel <dilfridge@gentoo.org>
10 # Kent Fredric <kentnl@gentoo.org>
11 # @SUPPORTED_EAPIS: 5 6
12 # @BLURB: helper functions eclass for perl modules
14 # The perl-functions eclass is designed to allow easier installation of perl
15 # modules, and their incorporation into the Gentoo Linux system.
16 # It provides helper functions, no phases or variable manipulation in
19 [[ ${CATEGORY} == "perl-core" ]] && inherit alternatives
25 die "EAPI=${EAPI} is not supported by perl-functions.eclass"
31 # @FUNCTION: perl_set_version
33 # Extract version information and installation paths from the current Perl
36 # This sets the following variables: PERL_VERSION, SITE_ARCH, SITE_LIB,
37 # ARCH_LIB, VENDOR_LIB, VENDOR_ARCH
39 # This function used to be called perlinfo as well.
47 debug-print-function $FUNCNAME "$@"
48 debug-print "$FUNCNAME: perlinfo_done=${perlinfo_done}"
49 ${perlinfo_done} && return 0
52 local f version install{{site,vendor}{arch,lib},archlib}
53 eval "$(perl -V:{version,install{{site,vendor}{arch,lib},archlib}} )"
54 PERL_VERSION=${version}
55 SITE_ARCH=${installsitearch}
56 SITE_LIB=${installsitelib}
57 ARCH_LIB=${installarchlib}
58 VENDOR_LIB=${installvendorlib}
59 VENDOR_ARCH=${installvendorarch}
62 # @FUNCTION: perl_delete_localpod
64 # Remove stray perllocal.pod files in the temporary install directory D.
66 # This function used to be called fixlocalpod as well.
67 perl_delete_localpod() {
68 debug-print-function $FUNCNAME "$@"
70 find "${D}" -type f -name perllocal.pod -delete
71 find "${D}" -depth -mindepth 1 -type d -empty -delete
74 # @FUNCTION: perl_fix_osx_extra
76 # Look through ${S} for AppleDouble encoded files and get rid of them.
77 perl_fix_osx_extra() {
78 debug-print-function $FUNCNAME "$@"
81 find "${S}" -type f -name "._*" -print0 | while read -rd '' f ; do
82 einfo "Removing AppleDouble encoded Macintosh file: ${f#${S}/}"
85 grep -q "${f}" "${S}"/MANIFEST && \
86 elog "AppleDouble encoded Macintosh file in MANIFEST: ${f#${S}/}"
90 # @FUNCTION: perl_delete_module_manpages
92 # Bump off manpages installed by the current module such as *.3pm files as well
93 # as empty directories.
94 perl_delete_module_manpages() {
95 debug-print-function $FUNCNAME "$@"
97 if [[ -d "${ED}"/usr/share/man ]] ; then
98 find "${ED}"/usr/share/man -type f -name "*.3pm" -delete
99 find "${ED}"/usr/share/man -depth -type d -empty -delete
103 # @FUNCTION: perl_delete_packlist
105 # Look through ${D} for .packlist files, empty .bs files and empty directories,
106 # and get rid of items found.
107 perl_delete_packlist() {
108 debug-print-function $FUNCNAME "$@"
110 if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
111 find "${D}/${VENDOR_ARCH}" -type f -a -name .packlist -delete
112 perl_delete_emptybsdir
116 # @FUNCTION: perl_delete_emptybsdir
118 # Look through ${D} for empty .bs files and empty directories,
119 # and get rid of items found.
120 perl_delete_emptybsdir() {
121 debug-print-function $FUNCNAME "$@"
123 if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
124 find "${D}/${VENDOR_ARCH}" -type f \
125 -a -name '*.bs' -a -empty -delete
126 find "${D}" -depth -mindepth 1 -type d -empty -delete
130 # @FUNCTION: perl_fix_packlist
132 # Look through ${D} for .packlist text files containing the temporary installation
133 # folder (i.e. ${D}). If the pattern is found, silently replace it with `/'.
134 # Remove duplicate entries; then validate all entries in the packlist against ${D}
135 # and prune entries that do not correspond to installed files.
136 perl_fix_packlist() {
137 debug-print-function $FUNCNAME "$@"
139 local packlist_temp="${T}/.gentoo_packlist_temp"
140 find "${D}" -type f -name '.packlist' -print0 | while read -rd '' f ; do
141 if file "${f}" | grep -q -i " text" ; then
142 einfo "Fixing packlist file /${f#${D}}"
144 # remove the temporary build dir path
145 sed -i -e "s:${D}:/:g" "${f}"
147 # remove duplicate entries
148 sort -u "${f}" > "${packlist_temp}"
149 mv "${packlist_temp}" "${f}"
151 # remove files that dont exist
152 cat "${f}" | while read -r entry; do
153 if [ ! -e "${D}/${entry}" ]; then
154 einfo "Pruning surplus packlist entry ${entry}"
155 grep -v -x -F "${entry}" "${f}" > "${packlist_temp}"
156 mv "${packlist_temp}" "${f}"
163 # @FUNCTION: perl_remove_temppath
165 # Look through ${D} for text files containing the temporary installation
166 # folder (i.e. ${D}). If the pattern is found, replace it with `/' and warn.
167 perl_remove_temppath() {
168 debug-print-function $FUNCNAME "$@"
170 find "${D}" -type f -not -name '*.so' -print0 | while read -rd '' f ; do
171 if file "${f}" | grep -q -i " text" ; then
172 grep -q "${D}" "${f}" && ewarn "QA: File contains a temporary path ${f}"
173 sed -i -e "s:${D}:/:g" "${f}"
178 # @FUNCTION: perl_rm_files
179 # @USAGE: <list of files>
181 # Remove certain files from a Perl release and remove them from the MANIFEST
184 # Most useful in src_prepare for nuking bad tests, and is highly recommended
185 # for any tests like 'pod.t', 'pod-coverage.t' or 'kwalitee.t', as what they
186 # test is completely irrelevant to end users, and frequently fail simply
187 # because the authors of Test::Pod... changed their recommendations, and thus
188 # failures are only useful feedback to Authors, not users.
190 # Removing from MANIFEST also avoids needless log messages warning
191 # users about files "missing from their kit".
196 # perl_rm_files t/pod{,-coverage}.t
197 # perl-module_src_test
201 debug-print-function $FUNCNAME "$@"
202 local skipfile="${T}/.gentoo_makefile_skip"
203 local manifile="${S}/MANIFEST"
204 local manitemp="${T}/.gentoo_manifest_temp"
207 for filename in "$@"; do
208 einfo "Removing un-needed ${filename}";
210 rm -f "${S}/${filename}"
211 [[ -e "${manifile}" ]] && echo "${filename}" >> "${skipfile}"
213 if [[ -e "${manifile}" && -e "${skipfile}" ]]; then
214 einfo "Fixing Manifest"
215 grep -v -F -f "${skipfile}" "${manifile}" > "${manitemp}"
216 mv -f -- "${manitemp}" "${manifile}"
222 # @FUNCTION: perl_link_duallife_scripts
224 # Moves files and generates symlinks so dual-life packages installing scripts do not
225 # lead to file collisions. Mainly for use in pkg_postinst and pkg_postrm, and makes
226 # only sense for perl-core packages.
227 perl_link_duallife_scripts() {
228 debug-print-function $FUNCNAME "$@"
229 if [[ ${CATEGORY} != perl-core ]] || ! has_version ">=dev-lang/perl-5.8.8-r8" ; then
234 if has "${EBUILD_PHASE:-none}" "postinst" "postrm" ; then
235 for i in "${DUALLIFESCRIPTS[@]}" ; do
236 alternatives_auto_makesym "/${i}" "/${i}-[0-9]*"
238 for i in "${DUALLIFEMAN[@]}" ; do
239 ff=`echo "${EROOT}"/${i%.1}-${PV}-${P}.1*`
241 alternatives_auto_makesym "/${i}${ff}" "/${i%.1}-[0-9]*"
244 pushd "${ED}" > /dev/null
245 for i in $(find usr/bin -maxdepth 1 -type f 2>/dev/null) ; do
246 mv ${i}{,-${PV}-${P}} || die
247 #DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i##*/}
248 DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i}
250 for i in $(find usr/share/man/man1 -maxdepth 1 -type f 2>/dev/null) ; do
251 mv ${i} ${i%.1}-${PV}-${P}.1 || die
252 DUALLIFEMAN[${#DUALLIFEMAN[*]}]=${i}
258 # @FUNCTION: perl_check_env
260 # Checks a blacklist of known-suspect ENV values that can be accidentally set by users
261 # doing personal perl work, which may accidentally leak into portage and break the
262 # system perl installaton.
263 # Dies if any of the suspect fields are found, and tell the user what needs to be unset.
264 # There's a workaround, but you'll have to read the code for it.
268 for i in PERL_MM_OPT PERL5LIB PERL5OPT PERL_MB_OPT PERL_CORE PERLPREFIX; do
270 [ -v $i ] || continue;
272 # Warn only once, and warn only when one of the bad values are set.
273 # record failure here.
274 if [ ${errored:-0} == 0 ]; then
275 if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
276 elog "perl-module.eclass: Suspicious environment values found.";
278 eerror "perl-module.eclass: Suspicious environment values found.";
286 # Print ENV name/value pair
287 if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
288 elog " $i=\"$value\"";
290 eerror " $i=\"$value\"";
294 # Return if there were no failures
295 [ ${errored:-0} == 0 ] && return;
297 # Return if user knows what they're doing
298 if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
299 elog "Continuing anyway, seems you know what you're doing."
303 eerror "Your environment settings may lead to undefined behavior and/or build failures."
304 die "Please fix your environment ( ~/.bashrc, package.env, ... ), see above for details."
307 # @FUNCTION: perl_doexamples
308 # @USAGE: <list of files or globs>
310 # Install example files ready-to-run.
311 # Is called under certain circumstances in perl-module.eclass src_install
312 # (see the documentation there).
317 # perl-module_src_install
318 # use examples && perl_doexamples "eg/*"
322 debug-print-function $FUNCNAME "$@"
324 einfo "Installing examples into /usr/share/doc/${PF}/examples"
326 # no compression since we want ready-to-run scripts
327 docompress -x /usr/share/doc/${PF}/examples
330 # Lack of quoting here is important in order to support glob expansion
331 # in DIST_EXAMPLES=( ), which is defined before source extraction occurs
334 # is there a way to undo "docinto" ?
337 # @FUNCTION: perl_has_module
338 # @USAGE: <module name>
339 # @RETURN: 0 if available, non-zero otherwise
341 # Query the installed system Perl to see if a given module is installed.
342 # This does **not** load the module in question, only anticipates if it *might* load.
344 # This is primarily for the purposes of dependency weakening so that conditional
345 # behaviour can be triggered without adding dependencies to portage which would confuse
346 # a dependency resolver.
348 # returns 'true' if the module is available, returns error if the module is not available
352 # perl_has_module "Test::Tester" && echo "Test::Tester installed"
356 debug-print-function $FUNCNAME "$@"
358 [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
359 [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
361 perl -we 'my $mn = $ARGV[0];
362 $mn =~ s{(::|\x{27})}{/}g;
365 exit 0 if -r $_ . q[/] . $mn . q[.pm]
370 # @FUNCTION: perl_has_module_version
371 # @USAGE: <module name> <minimum upstream version>
372 # @RETURN: 0 if satisfied, non-zero otherwise
374 # Query the installed system Perl to see if a given module is installed
375 # and is at least a given version.
377 # This requires more caution to use than perl_has_module as it requires
378 # loading the module in question to determine version compatibility,
379 # which can be SLOW, and can have side effects (ie: compilation fails in
380 # require due to some dependency, resulting in a "Fail")
382 # Also take care to note the module version is a *minimum*, *must* be
383 # written in upstream versions format and should be a a legal upstream version
385 # returns a true exit code if the module is both available and is at least
386 # the specified version
390 # perl_has_module_version "Test::Tester" "0.017" \
391 # && echo "Test::Tester 0.017 or greater installed"
393 perl_has_module_version() {
394 debug-print-function $FUNCNAME "$@"
396 [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
397 [[ $# -gt 1 ]] || die "${FUNCNAME}: No module version provided"
398 [[ $# -lt 3 ]] || die "${FUNCNAME}: Too many parameters ($#)"
400 perl -we 'my $mn = $ARGV[0];
401 $mn =~ s{(::|\x{27})}{/}g;
403 require qq[${mn}.pm];
404 $ARGV[0]->VERSION($ARGV[1]);
409 # @FUNCTION: perl_get_module_version
410 # @USAGE: <module name>
411 # @RETURN: 0 if module available, non-zero if error
413 # Query the installed system perl to report the version of the installed
416 # Note this should be strictly for diagnostic purposes to the end user,
417 # and may be of selective use in pkg_info to enhance
418 # emerge --info reports.
420 # Anything that does version comparisons **must not** use the return value
423 # Also note that this **must** at least attempt load the module in
424 # question as part of its operation, and is subsequently prone to SLOWness.
426 # Return codes return error in both compilation-failure and not-installed cases.
430 # MODVER=$(perl_get_module_version "Test::Simple") \
431 # || die "Test::Simple not installed: $MODVER"
434 perl_get_module_version() {
435 debug-print-function $FUNCNAME "$@"
437 [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
438 [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
440 if ! perl_has_module "$@" ; then
441 echo "(Not Installed)";
445 # Todo: What do we do if require fails? spew to stderr
448 perl -we 'my $mn = $ARGV[0];
449 $mn =~ s{(::|\x{27})}{/}g;
451 eval { require qq[${mn}.pm]; 1 } or do {
452 print q[(Compilation failed in require)];
455 my $stash = \%{ $ARGV[0] . q[::] };
456 if ( not exists $stash->{VERSION} ) {
457 print q[(No VERSION property)];
460 if ( not defined ${$stash->{VERSION}} ) {
464 print ${$stash->{VERSION}};
468 # @FUNCTION: perl_get_raw_vendorlib
470 # Convenience function to optimise for a common case without double-handling
471 # variables everywhere.
473 # Note: Will include EPREFIX where relevant
477 # my_raw_vendorlib="$(perl_get_raw_vendorlib)"
480 perl_get_raw_vendorlib() {
481 debug-print-function $FUNCNAME "$@"
483 [[ $# -lt 1 ]] || die "${FUNCNAME}: Too many parameters ($#)"
486 -e'exists $Config{$ARGV[0]} || die qq{No such Config key "$ARGV[0]"};
487 print $Config{$ARGV[0]};
488 exit 0' -- "installvendorlib" || die "Can't extract installvendorlib from Perl Configuration"
491 # @FUNCTION: perl_get_vendorlib
493 # Convenience helper for returning Perls' vendor install root
494 # without EPREFIXing.
498 # my_vendorlib="$(perl_get_vendorlib)"
501 perl_get_vendorlib() {
502 debug-print-function $FUNCNAME "$@"
504 [[ $# -lt 1 ]] || die "${FUNCNAME}: Too many parameters ($#)"
506 # Requires perl 5.14 for /r attribute of s///
507 # Just in case somebody out there is stuck in a time warp: upgrade perl first
508 perl -M5.014 -MConfig \
509 -e'exists $Config{$ARGV[0]} || die qq{No such Config key "$ARGV[0]"};
510 print $Config{$ARGV[0]} =~ s{\A\Q$ARGV[1]\E}{}r;
511 exit 0' -- "installvendorlib" "$EPREFIX" || die "Can't extract installvendorlib from Perl Configuration"
514 # @FUNCTION: perl_domodule
515 # @USAGE: [-C <target>] [-r] <files>
517 # Installs files in paths where they can be found in the default
520 # Note: Should only be used in src_install or pkg_preinst
521 # anywhere else will do the wrong thing or die.
523 # The contents of the <files> list are copied into Perls Vendor library path
526 # # install perl/File.pm as Samba::File
528 # perl_domodule -C Samba File.pm
530 # # install perl/ recursively under VENDORLIB/Samba/
532 # perl_domodule -C Samba -r .
538 # The subdirectory relative to the Perl VENDOR_LIB
543 # Install directories recursively ( see doins )
545 # list of .pm files to install to VENDORLIB
549 local target_prefix=""
553 local recursive="false"
557 while [[ $# -gt 0 ]] ; do
560 [[ -z "${2}" || "${2:0:1}" == "-" ]] && die "${FUNCNAME}: -C|--target-prefix expects an argument, got \"$2\"!"
561 target_prefix="${2}";
567 [[ -z "${1}" || "${1:0:1}" == "-" ]] && die "${FUNCNAME}: Unknown argument \"${1}\"!"
573 if [[ "true" == $recursive ]]; then
576 for file in "${files[@]}"; do
577 [[ -e "${file}" ]] || die "$FUNCNAME: Argument \"${file}\" is not an existing file"
578 [[ "false" == ${recursive} && -d "${file}" ]] && die "$FUNCNAME: Argument \"${file}\" is a directory ( needs -r parameter )"
581 target="$(perl_get_vendorlib)"
583 # Extend target if target_prefix is set
584 [[ -z "${target_prefix}" ]] || target="${target%/}/${target_prefix#/}"
586 insinto "/${target#/}"
587 doins "${doins_opts[@]}" "${files[@]}"