]> git.donarmstrong.com Git - debhelper.git/blob - dh_perl
dh_perl: Remove empty directories created by MakeMaker.
[debhelper.git] / dh_perl
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_perl - calculates perl dependencies and cleans up after MakeMaker
6
7 =cut
8
9 use strict;
10 use Config;
11 use File::Find;
12 use Debian::Debhelper::Dh_Lib;
13
14 =head1 SYNOPSIS
15
16 B<dh_perl> [S<I<debhelper options>>] [B<-d>] [S<I<library dirs ...>>]
17
18 =head1 DESCRIPTION
19
20 dh_perl is a debhelper program that is responsible for generating
21 the ${perl:Depends} substitutions and adding them to substvars files.
22
23 The program will look at perl scripts and modules in your package,
24 and will use this information to generate a dependency on perl or
25 perlapi. The dependency will be substituted into your package's control
26 file wherever you place the token "${perl:Depends}".
27
28 dh_perl also cleans up empty directories that MakeMaker can generate when
29 installing perl modules.
30
31 =head1 OPTIONS
32
33 =over 4
34
35 =item B<-d>
36
37 In some specific cases you may want to depend on perl-base rather than the
38 full perl package. If so, you can pass the -d option to make dh_perl generate
39 a dependency on the correct base package. This is only necessary for some
40 packages that are included in the base system.
41
42 Note that this flag may cause no dependency on perl-base to be generated at
43 all. perl-base is Essential, so its dependency can be left out, unless a
44 versioned dependency is needed.
45
46 =item B<-V>
47
48 By default, scripts and architecture independent modules don't depend
49 on any specific version of perl.  The -V option causes the current
50 version of the perl (or perl-base with -d) package to be specified.
51
52 =item I<library dirs>
53
54 If your package installs perl modules in non-standard
55 directories, you can make dh_perl check those directories by passing their
56 names on the command line. It will only check the vendorlib and vendorarch
57 directories by default.
58
59 =back
60
61 =head1 CONFORMS TO
62
63 Debian policy, version 3.0.1
64
65 Perl policy, version 1.18
66
67 =cut
68
69 init();
70
71 my $vendorlib = substr $Config{vendorlib}, 1;
72 my $vendorarch = substr $Config{vendorarch}, 1;
73
74 # the installation dir for arch-indep modules changed to
75 # /usr/share/perl5 in this version:
76 my $min_version = '5.6.0-16';
77
78 # Cleaning the paths given on the command line
79 foreach (@ARGV) {
80         s#/$##;
81         s#^/##;
82 }
83
84 my $perl = 'perl';
85 # If -d is given, then the dependency is on perl-base rather than perl.
86 $perl .= '-base' if $dh{D_FLAG};
87 my $version;
88
89 # dependency types
90 use constant PROGRAM   => 1;
91 use constant PM_MODULE => 2;
92 use constant XS_MODULE => 4;
93
94 foreach my $package (@{$dh{DOPACKAGES}}) {
95         my $tmp=tmpdir($package);
96
97         delsubstvar($package, "perl:Depends"); # for idempotency
98         
99         # Check also for alternate locations given on the command line
100         my @dirs = grep -d, map "$tmp/$_", $vendorlib, $vendorarch, @ARGV;
101
102         # Look for perl modules and check where they are installed
103         my $deps = 0;
104         find sub {
105                 return unless -f;
106                 $deps |= PM_MODULE if /\.pm$/;
107                 $deps |= XS_MODULE if /\.so$/;
108         }, @dirs if @dirs;
109
110         # find scripts
111         find sub {
112                 return unless -f and (-x or /\.pl$/);
113                 local *F;
114                 return unless open F, $_;
115                 if (read F, local $_, 32 and m%^#!\s*(/usr/bin/perl|/usr/bin/env\s+perl)\s%) {
116                         $deps |= PROGRAM;
117                 }
118                 close F;
119         }, $tmp;
120
121         if ($deps) {
122                 my $version="";
123                 if ($deps & XS_MODULE or $dh{V_FLAG_SET}) {
124                         ($version) = `dpkg -s $perl` =~ /^Version:\s*(\S+)/m
125                                 unless $version;
126                         $version = ">= $version";
127                 }
128                 elsif ($deps & PM_MODULE) {
129                         $version = ">= $min_version";
130                 }
131                 
132                 # no need to depend on an un-versioned perl-base -- it's
133                 # essential
134                 addsubstvar($package, "perl:Depends", $perl, $version)
135                         unless $perl eq 'perl-base' && ! length($version);
136
137                 # add perlapi-<ver> for XS modules
138                 addsubstvar($package, "perl:Depends", "perlapi-$Config{version}")
139                         if $deps & XS_MODULE;
140         }
141
142         # MakeMaker always makes lib and share dirs, but typically
143         # only one directory is installed into.
144         foreach my $dir ("$tmp/usr/share/perl5", "$tmp/usr/lib/perl5") {
145                 if (-d $dir) {
146                         doit("rmdir", "--ignore-fail-on-non-empty", "--parents",
147                                 "$dir");
148                 }
149         }
150 }
151
152 =head1 SEE ALSO
153
154 L<debhelper(7)>
155
156 This program is a part of debhelper.
157
158 =head1 AUTHOR
159
160 Brendan O'Dea <bod@debian.org>
161
162 =cut