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