]> git.donarmstrong.com Git - debhelper.git/blob - dh_perl
r346: some perl 5.6 issues
[debhelper.git] / dh_perl
1 #!/usr/bin/perl -w
2 #
3 # Find dependencies on perl stuff
4 # Remove .packlist files
5
6 use Debian::Debhelper::Dh_Lib;
7 init();
8
9 my $ext = '';
10 my $lib_dir = 'usr/lib/perl5';
11
12 # Figure out the version of perl. If $ENV{PERL} is set, query the perl binary
13 # it points to, otherwise query perl directly.
14 my $version=sprintf("%.3f", $]);
15 if (defined $ENV{PERL}) {
16         $version=`$ENV{PERL} -e 'printf "%.3f", \$]'`;
17 }
18
19 # Cleaning the paths given on the command line
20 foreach (@ARGV) {
21         s#/$##;
22         s#^/##;
23 }
24
25 # If -d is given, then we'll try to depend on one of the perl-5.00X-base 
26 # package instead of perl-5.00X
27 $ext='-base' if ($dh{'D_FLAG'});
28
29 foreach $PACKAGE (@{$dh{DOPACKAGES}}) {
30         $TMP=tmpdir($PACKAGE);
31         $EXT=pkgext($PACKAGE);
32
33         my ($file, $v, $arch);
34         my $dep_arch = '';
35         my $dep = '';
36         my $found = 0;
37
38         # Check also for alternate locations given on the command line
39         my $dirs = '';
40         foreach ($lib_dir, @ARGV) {
41                 $dirs .= "$TMP/$_ " if (-d "$TMP/$_");
42         }
43         my $re = '(?:' . join('|', ($lib_dir, @ARGV)) . ')';
44
45         # Look for perl modules and check where they are installed
46         if ($dirs) {
47             foreach $file (split(/\n/,`find $dirs -type f \\( -name "*.pm" -or -name "*.so" \\)`)) {
48                 $found++;
49                 if ($file =~ m<^$TMP/$re/(\d\.\d{3})/([^/]+)/>) {
50                         $v = $1;
51                         $arch = $2;
52                         check_module_version ($v, $version);
53                         $v .= '-thread' if ($arch =~ /-thread/); 
54                         $dep_arch = add_deps ($dep_arch, "perl-$v");
55                 } elsif ($file =~ m<^$TMP/$re/(\d.\d{3})/>) {
56                         $v = $1;
57                         check_module_version ($v, $version);
58                         $dep_arch = add_deps ($dep_arch, "perl-$v");
59                 }
60             }
61         }
62
63         if ($found and not $dep_arch) {
64                 $dep = "perl5$ext";
65         } elsif ($dep_arch) {
66                 $dep = $dep_arch;
67         }
68
69         # Look for perl scripts
70         my ($ff, $newdep);
71         foreach $file (split(/\n/,`find $TMP -type f \\( -name "*.pl" -or -perm +111 \\)`)) {
72                 $ff=`file -b $file`;
73                 if ($ff =~ /perl/) {
74                         $newdep = dep_from_script ($file);
75                         $dep = add_deps ($dep, $newdep) if $newdep;
76                 }
77         }
78
79         # Remove .packlist files and eventually some empty directories
80         if (not $dh{'K_FLAG'}) {
81                 foreach $file (split(/\n/,`find $TMP -type f -name .packlist`))
82                 {
83                         unlink($file);
84                         # Get the directory name
85                         while ($file =~ s#/[^/]+$##){
86                                 last if (not -d $file);
87                                 last if (not rmdir $file);
88                         }
89                 }
90         }
91
92         next unless $dep;
93
94         if (-e "debian/${EXT}substvars") {
95                 open (IN, "<debian/${EXT}substvars");
96                 my @lines=grep { ! /^perl:Depends=/ } <IN>;
97                 close IN;
98                 open (OUT, ">debian/${EXT}substvars");
99                 print OUT @lines;
100         } else {
101                 open (OUT, ">debian/${EXT}substvars");
102         }
103         print OUT "perl:Depends=$dep\n";
104         close OUT;
105 }
106
107 sub add_deps {
108         my ($dep, $new) = @_;
109         
110         # If the $new-base package can exist then add $ext to $new
111         $new = "$new$ext" if ($new =~ m/^(?:perl5|perl-\d\.\d{3})$/);
112         
113         # If $new = perl5 or perl5-thread check if perl-X.XXX(-thread)?
114         # is not already in the dependencies
115         if ($new eq "perl5") {
116                 return $dep if ($dep =~ m/(^|\s)perl-5\.\d{3}(\s|,|$)/);
117         } elsif ($new eq "perl5-thread") {
118                 return $dep if ($dep =~ m/(^|\s)perl-5\.\d{3}-thread(\s|,|$)/);
119         }
120         
121         if (not $dep) {
122                 $dep = $new;
123         } else {
124                 $dep .= ", $new" unless ($dep =~ m/(^|\s)$new(\s|,|$)/);
125         }
126
127         return $dep;
128 }
129
130 sub check_module_version {
131         my ($v1, $v2) = @_;
132         unless ($v1 eq $v2) {
133                 warning("A module has been found in perl-$v1 arch directory. But perl-$v2 is the perl currently used ...\n");
134         }
135 }
136
137 sub dep_from_script {
138         my $file = shift;
139         my ($line, $perl, $dep);
140         open (SCRIPT, "<$file") || die "Can't open $file: $!\n";
141         $line = <SCRIPT>;
142         close (SCRIPT);
143         if ($line =~ m<^#!\s*/usr/bin/(perl\S*)(?:\s+|$)>) {
144                 $perl = $1;
145                 if ($perl eq "perl") {
146                         $dep = "perl5";
147                 } elsif ($perl eq "perl-thread") {
148                         $dep = "perl5-thread";
149                 } elsif ($perl =~ m/^perl-\d\.\d{3}(?:-thread)?$/) {
150                         $dep = $perl;
151                 } elsif ($perl =~ m/^perl(\d\.\d{3})(\d\d)$/) {
152                         # Should never happen but ...
153                         $dep = "perl-$1 (=$1.$2)";
154                 }
155         }
156         return $dep;
157 }