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