perl-functions.eclass: Add new function perl_doexamples
[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 # $Id$
4
5 # @ECLASS: perl-functions.eclass
6 # @MAINTAINER:
7 # perl@gentoo.org
8 # @AUTHOR:
9 # Seemant Kulleen <seemant@gentoo.org>
10 # Andreas K. Huettel <dilfridge@gentoo.org>
11 # @BLURB: helper functions eclass for perl modules
12 # @DESCRIPTION:
13 # The perl-functions eclass is designed to allow easier installation of perl
14 # modules, and their incorporation into the Gentoo Linux system.
15 # It provides helper functions, no phases or variable manipulation in
16 # global scope.
17
18 [[ ${CATEGORY} == "perl-core" ]] && inherit alternatives
19
20 case "${EAPI:-0}" in
21         5|6)
22                 ;;
23         *)
24                 die "EAPI=${EAPI} is not supported by perl-functions.eclass"
25                 ;;
26 esac
27
28 perlinfo_done=false
29
30 # @FUNCTION: perl_set_version
31 # @USAGE: perl_set_version
32 # @DESCRIPTION:
33 # Extract version information and installation paths from the current Perl 
34 # interpreter. 
35 #
36 # This sets the following variables: PERL_VERSION, SITE_ARCH, SITE_LIB, 
37 # ARCH_LIB, VENDOR_LIB, VENDOR_ARCH
38 #
39 # This function used to be called perlinfo as well.
40 perl_set_version() {
41         debug-print-function $FUNCNAME "$@"
42         debug-print "$FUNCNAME: perlinfo_done=${perlinfo_done}"
43         ${perlinfo_done} && return 0
44         perlinfo_done=true
45
46         local f version install{{site,vendor}{arch,lib},archlib}
47         eval "$(perl -V:{version,install{{site,vendor}{arch,lib},archlib}} )"
48         PERL_VERSION=${version}
49         SITE_ARCH=${installsitearch}
50         SITE_LIB=${installsitelib}
51         ARCH_LIB=${installarchlib}
52         VENDOR_LIB=${installvendorlib}
53         VENDOR_ARCH=${installvendorarch}
54 }
55
56 # @FUNCTION: perl_delete_localpod
57 # @USAGE: perl_delete_localpod
58 # @DESCRIPTION:
59 # Remove stray perllocal.pod files in the temporary install directory D.
60 #
61 # This function used to be called fixlocalpod as well.
62 perl_delete_localpod() {
63         debug-print-function $FUNCNAME "$@"
64
65         find "${D}" -type f -name perllocal.pod -delete
66         find "${D}" -depth -mindepth 1 -type d -empty -delete
67 }
68
69 # @FUNCTION: perl_fix_osx_extra
70 # @USAGE: perl_fix_osx_extra
71 # @DESCRIPTION:
72 # Look through ${S} for AppleDouble encoded files and get rid of them.
73 perl_fix_osx_extra() {
74         debug-print-function $FUNCNAME "$@"
75
76         local f
77         find "${S}" -type f -name "._*" -print0 | while read -rd '' f ; do
78                 einfo "Removing AppleDouble encoded Macintosh file: ${f#${S}/}"
79                 rm -f "${f}"
80                 f=${f#${S}/}
81                 grep -q "${f}" "${S}"/MANIFEST && \
82                         elog "AppleDouble encoded Macintosh file in MANIFEST: ${f#${S}/}"
83         done
84 }
85
86 # @FUNCTION: perl_delete_module_manpages
87 # @USAGE: perl_delete_module_manpages
88 # @DESCRIPTION:
89 # Bump off manpages installed by the current module such as *.3pm files as well
90 # as empty directories.
91 perl_delete_module_manpages() {
92         debug-print-function $FUNCNAME "$@"
93
94         if [[ -d "${ED}"/usr/share/man ]] ; then
95                 find "${ED}"/usr/share/man -type f -name "*.3pm" -delete
96                 find "${ED}"/usr/share/man -depth -type d -empty -delete
97         fi
98 }
99
100 # @FUNCTION: perl_delete_packlist
101 # @USAGE: perl_delete_packlist
102 # @DESCRIPTION:
103 # Look through ${D} for .packlist files, empty .bs files and empty directories,
104 # and get rid of items found.
105 perl_delete_packlist() {
106         debug-print-function $FUNCNAME "$@"
107         perl_set_version
108         if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
109                 find "${D}/${VENDOR_ARCH}" -type f -a -name .packlist -delete
110                 perl_delete_emptybsdir
111         fi
112 }
113
114 # @FUNCTION: perl_delete_emptybsdir
115 # @USAGE: perl_delete_emptybsdir
116 # @DESCRIPTION:
117 # Look through ${D} for empty .bs files and empty directories,
118 # and get rid of items found.
119 perl_delete_emptybsdir() {
120         debug-print-function $FUNCNAME "$@"
121         perl_set_version
122         if [[ -d ${D}/${VENDOR_ARCH} ]] ; then
123                 find "${D}/${VENDOR_ARCH}" -type f \
124                         -a -name '*.bs' -a -empty -delete
125                 find "${D}" -depth -mindepth 1 -type d -empty -delete
126         fi
127 }
128
129 # @FUNCTION: perl_fix_packlist
130 # @USAGE: perl_fix_packlist
131 # @DESCRIPTION:
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 "$@"
138
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}}"
143
144                         # remove the temporary build dir path
145                         sed -i -e "s:${D}:/:g" "${f}"
146
147                         # remove duplicate entries
148                         sort -u "${f}" > "${packlist_temp}"
149                         mv "${packlist_temp}" "${f}"
150
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}"
157                                 fi
158                         done
159                 fi
160         done
161 }
162
163 # @FUNCTION: perl_remove_temppath
164 # @USAGE: perl_remove_temppath
165 # @DESCRIPTION:
166 # Look through ${D} for text files containing the temporary installation
167 # folder (i.e. ${D}). If the pattern is found, replace it with `/' and warn.
168 perl_remove_temppath() {
169         debug-print-function $FUNCNAME "$@"
170
171         find "${D}" -type f -not -name '*.so' -print0 | while read -rd '' f ; do
172                 if file "${f}" | grep -q -i " text" ; then
173                         grep -q "${D}" "${f}" && ewarn "QA: File contains a temporary path ${f}"
174                         sed -i -e "s:${D}:/:g" "${f}"
175                 fi
176         done
177 }
178
179 # @FUNCTION: perl_rm_files
180 # @USAGE: perl_rm_files "file_1" "file_2"
181 # @DESCRIPTION:
182 # Remove certain files from a Perl release and remove them from the MANIFEST
183 # while we're there.
184 #
185 # Most useful in src_prepare for nuking bad tests, and is highly recommended
186 # for any tests like 'pod.t', 'pod-coverage.t' or 'kwalitee.t', as what they
187 # test is completely irrelevant to end users, and frequently fail simply
188 # because the authors of Test::Pod... changed their recommendations, and thus
189 # failures are only useful feedback to Authors, not users.
190 #
191 # Removing from MANIFEST also avoids needless log messages warning
192 # users about files "missing from their kit".
193 perl_rm_files() {
194         debug-print-function $FUNCNAME "$@"
195         local skipfile="${T}/.gentoo_makefile_skip"
196         local manifile="${S}/MANIFEST"
197         local manitemp="${T}/.gentoo_manifest_temp"
198         oldifs="$IFS"
199         IFS="\n"
200         for filename in "$@"; do
201                 einfo "Removing un-needed ${filename}";
202                 # Remove the file
203                 rm -f "${S}/${filename}"
204                 [[ -e "${manifile}" ]] && echo "${filename}" >> "${skipfile}"
205         done
206         if [[ -e "${manifile}" && -e "${skipfile}" ]]; then
207                 einfo "Fixing Manifest"
208                 grep -v -F -f "${skipfile}" "${manifile}" > "${manitemp}"
209                 mv -f -- "${manitemp}" "${manifile}"
210                 rm -- "${skipfile}";
211         fi
212         IFS="$oldifs"
213 }
214
215 # @FUNCTION: perl_link_duallife_scripts
216 # @USAGE: perl_link_duallife_scripts
217 # @DESCRIPTION:
218 # Moves files and generates symlinks so dual-life packages installing scripts do not
219 # lead to file collisions. Mainly for use in pkg_postinst and pkg_postrm, and makes 
220 # only sense for perl-core packages.
221 perl_link_duallife_scripts() {
222         debug-print-function $FUNCNAME "$@"
223         if [[ ${CATEGORY} != perl-core ]] || ! has_version ">=dev-lang/perl-5.8.8-r8" ; then
224                 return 0
225         fi
226
227         local i ff
228         if has "${EBUILD_PHASE:-none}" "postinst" "postrm" ; then
229                 for i in "${DUALLIFESCRIPTS[@]}" ; do
230                         alternatives_auto_makesym "/${i}" "/${i}-[0-9]*"
231                 done
232                 for i in "${DUALLIFEMAN[@]}" ; do
233                         ff=`echo "${EROOT}"/${i%.1}-${PV}-${P}.1*`
234                         ff=${ff##*.1}
235                         alternatives_auto_makesym "/${i}${ff}" "/${i%.1}-[0-9]*"
236                 done
237         else
238                 pushd "${ED}" > /dev/null
239                 for i in $(find usr/bin -maxdepth 1 -type f 2>/dev/null) ; do
240                         mv ${i}{,-${PV}-${P}} || die
241                         #DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i##*/}
242                         DUALLIFESCRIPTS[${#DUALLIFESCRIPTS[*]}]=${i}
243                 done
244                 for i in $(find usr/share/man/man1 -maxdepth 1 -type f 2>/dev/null) ; do
245                         mv ${i} ${i%.1}-${PV}-${P}.1 || die
246                         DUALLIFEMAN[${#DUALLIFEMAN[*]}]=${i}
247                 done
248                 popd > /dev/null
249         fi
250 }
251
252 # @FUNCTION: perl_check_env
253 # @USAGE: perl_check_env
254 # @DESCRIPTION:
255 # Checks a blacklist of known-suspect ENV values that can be accidentally set by users
256 # doing personal perl work, which may accidentally leak into portage and break the
257 # system perl installaton.
258 # Dies if any of the suspect fields are found, and tell the user what needs to be unset.
259 # There's a workaround, but you'll have to read the code for it.
260 perl_check_env() {
261         local errored value;
262
263         for i in PERL_MM_OPT PERL5LIB PERL5OPT PERL_MB_OPT PERL_CORE PERLPREFIX; do
264                 # Next unless match
265                 [ -v $i ] || continue;
266
267                 # Warn only once, and warn only when one of the bad values are set.
268                 # record failure here.
269                 if [ ${errored:-0} == 0 ]; then
270                         if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
271                                 elog "perl-module.eclass: Suspicious environment values found.";
272                         else
273                                 eerror "perl-module.eclass: Suspicious environment values found.";
274                         fi
275                 fi
276                 errored=1
277
278                 # Read ENV Value
279                 eval "value=\$$i";
280
281                 # Print ENV name/value pair
282                 if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
283                         elog "    $i=\"$value\"";
284                 else
285                         eerror "    $i=\"$value\"";
286                 fi
287         done
288
289         # Return if there were no failures
290         [ ${errored:-0} == 0 ] && return;
291
292         # Return if user knows what they're doing
293         if [ -n "${I_KNOW_WHAT_I_AM_DOING}" ]; then
294                 elog "Continuing anyway, seems you know what you're doing."
295                 return
296         fi
297
298         eerror "Your environment settings may lead to undefined behavior and/or build failures."
299         die "Please fix your environment ( ~/.bashrc, package.env, ... ), see above for details."
300 }
301
302 # @FUNCTION: perl_doexamples
303 # @USAGE: perl_doexamples "file_1" "file_2"
304 # @DESCRIPTION:
305 # Install example files ready-to-run.
306 # Is called under certain circumstances in perl-module.eclass src_install
307 # (see the documentation there).
308 #
309 perl_doexamples() {
310         debug-print-function $FUNCNAME "$@"
311
312         einfo "Installing examples into /usr/share/doc/${PF}/examples"
313
314         # no compression since we want ready-to-run scripts
315         docompress -x /usr/share/doc/${PF}/examples
316
317         docinto examples/
318         dodoc -r $@
319
320         # is there a way to undo "docinto" ?
321 }