perl-functions.eclass: add perl_get_raw_vendorlib
[gentoo.git] / eclass / perl-functions.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: perl-functions.eclass
5 # @MAINTAINER:
6 # perl@gentoo.org
7 # @AUTHOR:
8 # Seemant Kulleen <seemant@gentoo.org>
9 # Andreas K. Huettel <dilfridge@gentoo.org>
10 # @BLURB: helper functions eclass for perl modules
11 # @DESCRIPTION:
12 # The perl-functions eclass is designed to allow easier installation of perl
13 # modules, and their incorporation into the Gentoo Linux system.
14 # It provides helper functions, no phases or variable manipulation in
15 # global scope.
16
17 [[ ${CATEGORY} == "perl-core" ]] && inherit alternatives
18
19 case "${EAPI:-0}" in
20         5|6)
21                 ;;
22         *)
23                 die "EAPI=${EAPI} is not supported by perl-functions.eclass"
24                 ;;
25 esac
26
27 perlinfo_done=false
28
29 # @FUNCTION: perl_set_version
30 # @USAGE: perl_set_version
31 # @DESCRIPTION:
32 # Extract version information and installation paths from the current Perl
33 # interpreter.
34 #
35 # This sets the following variables: PERL_VERSION, SITE_ARCH, SITE_LIB,
36 # ARCH_LIB, VENDOR_LIB, VENDOR_ARCH
37 #
38 # This function used to be called perlinfo as well.
39 perl_set_version() {
40         debug-print-function $FUNCNAME "$@"
41         debug-print "$FUNCNAME: perlinfo_done=${perlinfo_done}"
42         ${perlinfo_done} && return 0
43         perlinfo_done=true
44
45         local f version install{{site,vendor}{arch,lib},archlib}
46         eval "$(perl -V:{version,install{{site,vendor}{arch,lib},archlib}} )"
47         PERL_VERSION=${version}
48         SITE_ARCH=${installsitearch}
49         SITE_LIB=${installsitelib}
50         ARCH_LIB=${installarchlib}
51         VENDOR_LIB=${installvendorlib}
52         VENDOR_ARCH=${installvendorarch}
53 }
54
55 # @FUNCTION: perl_delete_localpod
56 # @USAGE: perl_delete_localpod
57 # @DESCRIPTION:
58 # Remove stray perllocal.pod files in the temporary install directory D.
59 #
60 # This function used to be called fixlocalpod as well.
61 perl_delete_localpod() {
62         debug-print-function $FUNCNAME "$@"
63
64         find "${D}" -type f -name perllocal.pod -delete
65         find "${D}" -depth -mindepth 1 -type d -empty -delete
66 }
67
68 # @FUNCTION: perl_fix_osx_extra
69 # @USAGE: perl_fix_osx_extra
70 # @DESCRIPTION:
71 # Look through ${S} for AppleDouble encoded files and get rid of them.
72 perl_fix_osx_extra() {
73         debug-print-function $FUNCNAME "$@"
74
75         local f
76         find "${S}" -type f -name "._*" -print0 | while read -rd '' f ; do
77                 einfo "Removing AppleDouble encoded Macintosh file: ${f#${S}/}"
78                 rm -f "${f}"
79                 f=${f#${S}/}
80                 grep -q "${f}" "${S}"/MANIFEST && \
81                         elog "AppleDouble encoded Macintosh file in MANIFEST: ${f#${S}/}"
82         done
83 }
84
85 # @FUNCTION: perl_delete_module_manpages
86 # @USAGE: perl_delete_module_manpages
87 # @DESCRIPTION:
88 # Bump off manpages installed by the current module such as *.3pm files as well
89 # as empty directories.
90 perl_delete_module_manpages() {
91         debug-print-function $FUNCNAME "$@"
92
93         if [[ -d "${ED}"/usr/share/man ]] ; then
94                 find "${ED}"/usr/share/man -type f -name "*.3pm" -delete
95                 find "${ED}"/usr/share/man -depth -type d -empty -delete
96         fi
97 }
98
99 # @FUNCTION: perl_delete_packlist
100 # @USAGE: perl_delete_packlist
101 # @DESCRIPTION:
102 # Look through ${D} for .packlist files, empty .bs files and empty directories,
103 # and get rid of items found.
104 perl_delete_packlist() {
105         debug-print-function $FUNCNAME "$@"
106         perl_set_version
107         if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
108                 find "${D}/${VENDOR_ARCH}" -type f -a -name .packlist -delete
109                 perl_delete_emptybsdir
110         fi
111 }
112
113 # @FUNCTION: perl_delete_emptybsdir
114 # @USAGE: perl_delete_emptybsdir
115 # @DESCRIPTION:
116 # Look through ${D} for empty .bs files and empty directories,
117 # and get rid of items found.
118 perl_delete_emptybsdir() {
119         debug-print-function $FUNCNAME "$@"
120         perl_set_version
121         if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
122                 find "${D}/${VENDOR_ARCH}" -type f \
123                         -a -name '*.bs' -a -empty -delete
124                 find "${D}" -depth -mindepth 1 -type d -empty -delete
125         fi
126 }
127
128 # @FUNCTION: perl_fix_packlist
129 # @USAGE: perl_fix_packlist
130 # @DESCRIPTION:
131 # Look through ${D} for .packlist text files containing the temporary installation
132 # folder (i.e. ${D}). If the pattern is found, silently replace it with `/'.
133 # Remove duplicate entries; then validate all entries in the packlist against ${D}
134 # and prune entries that do not correspond to installed files.
135 perl_fix_packlist() {
136         debug-print-function $FUNCNAME "$@"
137
138         local packlist_temp="${T}/.gentoo_packlist_temp"
139         find "${D}" -type f -name '.packlist' -print0 | while read -rd '' f ; do
140                 if file "${f}" | grep -q -i " text" ; then
141                         einfo "Fixing packlist file /${f#${D}}"
142
143                         # remove the temporary build dir path
144                         sed -i -e "s:${D}:/:g" "${f}"
145
146                         # remove duplicate entries
147                         sort -u "${f}" > "${packlist_temp}"
148                         mv "${packlist_temp}" "${f}"
149
150                         # remove files that dont exist
151                         cat "${f}" | while read -r entry; do
152                                 if [ ! -e "${D}/${entry}" ]; then
153                                         einfo "Pruning surplus packlist entry ${entry}"
154                                         grep -v -x -F "${entry}" "${f}" > "${packlist_temp}"
155                                         mv "${packlist_temp}" "${f}"
156                                 fi
157                         done
158                 fi
159         done
160 }
161
162 # @FUNCTION: perl_remove_temppath
163 # @USAGE: perl_remove_temppath
164 # @DESCRIPTION:
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 "$@"
169
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}"
174                 fi
175         done
176 }
177
178 # @FUNCTION: perl_rm_files
179 # @USAGE: perl_rm_files "file_1" "file_2"
180 # @DESCRIPTION:
181 # Remove certain files from a Perl release and remove them from the MANIFEST
182 # while we're there.
183 #
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.
189 #
190 # Removing from MANIFEST also avoids needless log messages warning
191 # users about files "missing from their kit".
192 perl_rm_files() {
193         debug-print-function $FUNCNAME "$@"
194         local skipfile="${T}/.gentoo_makefile_skip"
195         local manifile="${S}/MANIFEST"
196         local manitemp="${T}/.gentoo_manifest_temp"
197         oldifs="$IFS"
198         IFS="\n"
199         for filename in "$@"; do
200                 einfo "Removing un-needed ${filename}";
201                 # Remove the file
202                 rm -f "${S}/${filename}"
203                 [[ -e "${manifile}" ]] && echo "${filename}" >> "${skipfile}"
204         done
205         if [[ -e "${manifile}" && -e "${skipfile}" ]]; then
206                 einfo "Fixing Manifest"
207                 grep -v -F -f "${skipfile}" "${manifile}" > "${manitemp}"
208                 mv -f -- "${manitemp}" "${manifile}"
209                 rm -- "${skipfile}";
210         fi
211         IFS="$oldifs"
212 }
213
214 # @FUNCTION: perl_link_duallife_scripts
215 # @USAGE: perl_link_duallife_scripts
216 # @DESCRIPTION:
217 # Moves files and generates symlinks so dual-life packages installing scripts do not
218 # lead to file collisions. Mainly for use in pkg_postinst and pkg_postrm, and makes
219 # only sense for perl-core packages.
220 perl_link_duallife_scripts() {
221         debug-print-function $FUNCNAME "$@"
222         if [[ ${CATEGORY} != perl-core ]] || ! has_version ">=dev-lang/perl-5.8.8-r8" ; then
223                 return 0
224         fi
225
226         local i ff
227         if has "${EBUILD_PHASE:-none}" "postinst" "postrm" ; then
228                 for i in "${DUALLIFESCRIPTS[@]}" ; do
229                         alternatives_auto_makesym "/${i}" "/${i}-[0-9]*"
230                 done
231                 for i in "${DUALLIFEMAN[@]}" ; do
232                         ff=`echo "${EROOT}"/${i%.1}-${PV}-${P}.1*`
233                         ff=${ff##*.1}
234                         alternatives_auto_makesym "/${i}${ff}" "/${i%.1}-[0-9]*"
235                 done
236         else
237                 pushd "${ED}" > /dev/null
238                 for i in $(find usr/bin -maxdepth 1 -type f 2>/dev/null) ; do
239                         mv ${i}{,-${PV}-${P}} || die
240                         #DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i##*/}
241                         DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i}
242                 done
243                 for i in $(find usr/share/man/man1 -maxdepth 1 -type f 2>/dev/null) ; do
244                         mv ${i} ${i%.1}-${PV}-${P}.1 || die
245                         DUALLIFEMAN[${#DUALLIFEMAN[*]}]=${i}
246                 done
247                 popd > /dev/null
248         fi
249 }
250
251 # @FUNCTION: perl_check_env
252 # @USAGE: perl_check_env
253 # @DESCRIPTION:
254 # Checks a blacklist of known-suspect ENV values that can be accidentally set by users
255 # doing personal perl work, which may accidentally leak into portage and break the
256 # system perl installaton.
257 # Dies if any of the suspect fields are found, and tell the user what needs to be unset.
258 # There's a workaround, but you'll have to read the code for it.
259 perl_check_env() {
260         local errored value;
261
262         for i in PERL_MM_OPT PERL5LIB PERL5OPT PERL_MB_OPT PERL_CORE PERLPREFIX; do
263                 # Next unless match
264                 [ -v $i ] || continue;
265
266                 # Warn only once, and warn only when one of the bad values are set.
267                 # record failure here.
268                 if [ ${errored:-0} == 0 ]; then
269                         if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
270                                 elog "perl-module.eclass: Suspicious environment values found.";
271                         else
272                                 eerror "perl-module.eclass: Suspicious environment values found.";
273                         fi
274                 fi
275                 errored=1
276
277                 # Read ENV Value
278                 value=${!i};
279
280                 # Print ENV name/value pair
281                 if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
282                         elog "    $i=\"$value\"";
283                 else
284                         eerror "    $i=\"$value\"";
285                 fi
286         done
287
288         # Return if there were no failures
289         [ ${errored:-0} == 0 ] && return;
290
291         # Return if user knows what they're doing
292         if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
293                 elog "Continuing anyway, seems you know what you're doing."
294                 return
295         fi
296
297         eerror "Your environment settings may lead to undefined behavior and/or build failures."
298         die "Please fix your environment ( ~/.bashrc, package.env, ... ), see above for details."
299 }
300
301 # @FUNCTION: perl_doexamples
302 # @USAGE: perl_doexamples "file_1" "file_2"
303 # @DESCRIPTION:
304 # Install example files ready-to-run.
305 # Is called under certain circumstances in perl-module.eclass src_install
306 # (see the documentation there).
307 #
308 perl_doexamples() {
309         debug-print-function $FUNCNAME "$@"
310
311         einfo "Installing examples into /usr/share/doc/${PF}/examples"
312
313         # no compression since we want ready-to-run scripts
314         docompress -x /usr/share/doc/${PF}/examples
315
316         docinto examples/
317         dodoc -r $@
318
319         # is there a way to undo "docinto" ?
320 }
321
322 # @FUNCTION: perl_has_module
323 # @USAGE: perl_has_module "Test::Tester"
324 # @DESCRIPTION:
325 # Query the installed system Perl to see if a given module is installed.
326 # This does **not** load the module in question, only anticipates if it *might* load.
327 #
328 # This is primarily for the purposes of dependency weakening so that conditional
329 # behaviour can be triggered without adding dependencies to portage which would confuse
330 # a dependency resolver.
331 #
332 # returns 'true' if the module is available, returns error if the module is not available
333
334 perl_has_module() {
335         debug-print-function $FUNCNAME "$@"
336
337         [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
338         [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
339
340         perl -we 'my $mn = $ARGV[0];
341                 $mn =~ s{(::|\x{27})}{/}g;
342                 for(@INC){
343                         next if ref $_;
344                         exit 0 if -r $_ . q[/] . $mn . q[.pm]
345                 }
346                 exit 1' "$@";
347 }
348
349 # @FUNCTION: perl_has_module_version
350 # @USAGE: perl_has_module_version "Test::Tester" "0.017"
351 # @DESCRIPTION:
352 # Query the installed system Perl to see if a given module is installed
353 # and is at least a given version.
354 #
355 # This requires more caution to use than perl_has_module as it requires
356 # loading the module in question to determine version compatibility,
357 # which can be SLOW, and can have side effects (ie: compilation fails in
358 # require due to some dependency, resulting in a "Fail")
359 #
360 # Also take care to note the module version is a *minimum*, *must* be
361 # written in upstream versions format and should be a a legal upstream version
362 #
363 # returns a true exit code if the module is both available and is at least
364 # the specified version
365
366 perl_has_module_version() {
367         debug-print-function $FUNCNAME "$@"
368
369         [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
370         [[ $# -gt 1 ]] || die "${FUNCNAME}: No module version provided"
371         [[ $# -lt 3 ]] || die "${FUNCNAME}: Too many parameters ($#)"
372
373         perl -we 'my $mn = $ARGV[0];
374                 $mn =~ s{(::|\x{27})}{/}g;
375                 exit ( eval {
376                         require qq[${mn}.pm];
377                         $ARGV[0]->VERSION($ARGV[1]);
378                         1
379                 } ? 0 : 1 )' "$@"
380 }
381
382 # @FUNCTION: perl_get_module_version
383 # @USAGE: MODVER=$(perl_get_module_version "Test::Simple")
384 # @DESCRIPTION:
385 # Query the installed system perl to report the version of the installed
386 # module.
387 #
388 # Note this should be strictly for diagnostic purposes to the end user,
389 # and may be of selective use in pkg_info to enhance
390 # emerge --info reports.
391 #
392 # Anything that does version comparisons **must not** use the return value
393 # from this function
394 #
395 # Also note that this **must** at least attempt load the module in
396 # question as part of its operation, and is subsequently prone to SLOWness.
397 #
398 # Return codes return error in both compilation-failure and not-installed cases.
399
400 perl_get_module_version() {
401         debug-print-function $FUNCNAME "$@"
402
403         [[ $# -gt 0 ]] || die "${FUNCNAME}: No module name provided"
404         [[ $# -lt 2 ]] || die "${FUNCNAME}: Too many parameters ($#)"
405
406         if ! perl_has_module "$@" ; then
407                 echo "(Not Installed)";
408                 return 1;
409         fi
410
411         # Todo: What do we do if require fails? spew to stderr
412         # or stay silent?
413
414         perl -we 'my $mn = $ARGV[0];
415                 $mn =~ s{(::|\x{27})}{/}g;
416                 local $@;
417                 eval { require qq[${mn}.pm]; 1 } or do {
418                         print q[(Compilation failed in require)];
419                         exit 1;
420                 };
421                 my $stash = \%{ $ARGV[0] . q[::] };
422                 if ( not exists $stash->{VERSION} ) {
423                         print q[(No VERSION property)];
424                         exit 0;
425                 }
426                 if ( not defined ${$stash->{VERSION}} ) {
427                         print q[(undef)];
428                         exit 0;
429                 }
430                 print ${$stash->{VERSION}};
431                 exit 0; ' "$@"
432 }
433
434 # @FUNCTION: perl_get_raw_vendorlib
435 # @USAGE: perl_get_raw_vendorlib
436 # @DESCRIPTION:
437 # Convenience function to optimise for a common case without double-handling
438 # variables everywhere.
439 #
440 # Note: Will include EPREFIX where relevant
441 perl_get_raw_vendorlib() {
442         debug-print-function $FUNCNAME "$@"
443
444         [[ $# -lt 1 ]] || die "${FUNCNAME}: Too many parameters ($#)"
445
446         perl -MConfig \
447                 -e'exists $Config{$ARGV[0]} || die qq{No such Config key "$ARGV[0]"};
448                    print $Config{$ARGV[0]};
449                    exit 0' -- "installvendorlib" || die "Can't extract installvendorlib from Perl Configuration"
450 }