This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix bug [perl #24212] : improper error recovery in the
[perl5.git] / installman
1 #!./perl -w
2 BEGIN { @INC = qw(lib) }
3 use strict;
4 use Config;
5 use Getopt::Long;
6 use File::Find;
7 use File::Copy;
8 use File::Path qw(mkpath);
9 use ExtUtils::Packlist;
10 use Pod::Man;
11 use subs qw(unlink chmod rename link);
12 use vars qw($packlist);
13
14 if ($Config{d_umask}) {
15     umask(022); # umasks like 077 aren't that useful for installations
16 }
17
18 $ENV{SHELL} = 'sh' if $^O eq 'os2';
19
20 my $ver = $Config{version};     # Not used presently.
21 my $release = substr($],0,3);   # Not used presently.
22 my $patchlevel = substr($],3,2);
23 die "Patchlevel of perl ($patchlevel)",
24     "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
25         if $patchlevel != $Config{'PERL_VERSION'};
26
27 my $usage =
28 "Usage:  installman --man1dir=/usr/wherever --man1ext=1
29                    --man3dir=/usr/wherever --man3ext=3
30                    --batchlimit=40
31                    --notify --verbose --silent --help
32         Defaults are:
33         man1dir = $Config{'installman1dir'};
34         man1ext = $Config{'man1ext'};
35         man3dir = $Config{'installman3dir'};
36         man3ext = $Config{'man3ext'};
37         --notify  (or -n) just lists commands that would be executed.
38         --verbose (or -V) report all progress.
39         --silent  (or -S) be silent. Only report errors.\n";
40
41 my %opts;
42 GetOptions( \%opts,
43             qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
44                 destdir:s notify n help silent S verbose V)) 
45         || die $usage;
46 die $usage if $opts{help};
47
48 $opts{man1dir} = "$opts{destdir}$Config{'installman1dir'}"
49     unless defined($opts{man1dir}); 
50 $opts{man1ext} = $Config{'man1ext'}
51     unless defined($opts{man1ext}); 
52 $opts{man3dir} = "$opts{destdir}$Config{'installman3dir'}"
53     unless defined($opts{man3dir}); 
54 $opts{man3ext} = $Config{'man3ext'}
55     unless defined($opts{man3ext}); 
56 $opts{silent} ||= $opts{S};
57 $opts{notify} ||= $opts{n};
58 $opts{verbose} ||= $opts{V} || $opts{notify};
59
60 #Sanity checks
61
62 -x  "./perl$Config{exe_ext}" 
63   or warn "./perl$Config{exe_ext} not found!  Have you run make?\n";
64 -d  "$opts{destdir}$Config{'installprivlib'}"
65         || warn "Perl library directory $Config{'installprivlib'} not found.
66                 Have you run make install?.  (Installing anyway.)\n";
67 -x "t/perl$Config{exe_ext}"             || warn "WARNING: You've never run 'make test'!!!",
68         "  (Installing anyway.)\n";
69
70 $packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
71
72
73 # Install the main pod pages.
74 pod2man('pod', $opts{man1dir}, $opts{man1ext});
75
76 # Install the pods for library modules.
77 pod2man('lib', $opts{man3dir}, $opts{man3ext});
78
79 # Install the pods embedded in the installed scripts
80 my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
81 open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
82 while (<UTILS>) {
83     next if /^#/;
84     chomp;
85     $_ = $1 if /#.*pod\s*=\s*(\S+)/;
86     my ($where, $what) = m|^(\S*)/(\S+)|;
87     pod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
88     if ($has_man1dir) {
89         if (my ($where2, $what2) = m|#.*link\s*=\s*(\S+)/(\S+)|) {
90             my $old = "$opts{man1dir}/$what.$opts{man1ext}";
91             my $new = "$opts{man1dir}/$what2.$opts{man1ext}";
92             unlink($new);
93             link($old, $new);
94         }
95     }
96 }
97
98 sub pod2man {
99     # @script is scripts names if we are installing manpages embedded 
100     # in scripts, () otherwise
101     my($poddir, $mandir, $manext, @script) = @_;
102     if ($mandir eq ' ' or $mandir eq '') {
103         if (@script) {
104             warn "Skipping installation of $poddir/$_ man page.\n"
105                 foreach @script;
106         } else {
107             warn "Skipping installation of $poddir man pages.\n";
108         }
109         return;
110     }
111
112     print "installing from $poddir\n" if $opts{verbose};
113
114     mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify};  # In File::Path
115     # Make a list of all the .pm and .pod files in the directory.  We avoid
116     # chdir because we are running with @INC = '../lib', and modules may wish
117     # to dynamically require Carp::Heavy or other diagnostics warnings.
118     # Hash the names of files we find, keys are names relative to perl build
119     # dir ('.'), values are names relative to $poddir.
120     my %modpods;
121     if (@script) {
122         %modpods = (map {+"$poddir/$_", $_} @script);
123     }
124     else {
125         File::Find::find({no_chdir=>1,
126                           wanted => sub {
127                               # $_ is $File::Find::name when using no_chdir
128                               if (-f $_ and /\.p(?:m|od)$/) {
129                                   my $fullname = $_;
130                                   s!^\Q$poddir\E/!!;
131                                   $modpods{$fullname} = $_;
132                               }
133                           }},
134                          $poddir);
135     }
136     my @to_process;
137     foreach my $mod (sort keys %modpods) {
138         my $manpage = $modpods{$mod};
139         my $tmp;
140         # Skip .pm files that have corresponding .pod files, and Functions.pm.
141         next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
142         next if $mod =~ m:/t/:; # no pods from test directories 
143         next if ($manpage eq 'Pod/Functions.pm'); #### Used only by pod itself
144
145         # Skip files without pod docs
146         my $has_pod;
147         if (open T, $mod)
148         {
149             local $_;
150             while (<T>)
151             {
152                 ++$has_pod and last if /^=(?:head\d+|item|pod)\b/;
153             }
154
155             close T;
156         }
157
158         unless ($has_pod)
159         {
160             warn "no documentation in $mod\n";
161             next;
162         }
163
164         # Convert name from  File/Basename.pm to File::Basename.3 format,
165         # if necessary.
166         $manpage =~ s#\.p(m|od)$##;
167         if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
168           $manpage =~ s#/#.#g;
169         }
170         else {
171           $manpage =~ s#/#::#g;
172         }
173         $tmp = "${mandir}/${manpage}.tmp";
174         $manpage = "${mandir}/${manpage}.${manext}";
175         push @to_process, [$mod, $tmp, $manpage];
176     }
177
178     my $parser = Pod::Man->new( section => $manext,
179                                 official=> 1,
180                                 center  => 'Perl Programmers Reference Guide'
181                               );
182     foreach my $page (@to_process) {
183         my($pod, $tmp, $manpage) = @$page;
184
185         my $xmanpage = $manpage;
186         $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
187         print "  $xmanpage\n";
188         if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
189             if (-s $tmp) {
190                 if (rename($tmp, $manpage)) {
191                     $packlist->{$xmanpage} = { type => 'file' };
192                     next;
193                 }
194             }
195             unlink($tmp);
196         }
197     }
198 }
199
200 $packlist->write() unless $opts{notify};
201 print "  Installation complete\n" if $opts{verbose};
202
203 exit 0;
204     
205
206 ###############################################################################
207 # Utility subroutines from installperl
208
209 sub unlink {
210     my(@names) = @_;
211     my $cnt = 0;
212
213     foreach my $name (@names) {
214         next unless -e $name;
215         chmod 0777, $name if $^O eq 'os2';
216         print "  unlink $name\n" if $opts{verbose};
217         ( CORE::unlink($name) and ++$cnt 
218             or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
219     }
220     return $cnt;
221 }
222
223 sub link {
224     my($from,$to) = @_;
225     my($success) = 0;
226
227     print "  ln $from $to\n" if $opts{verbose};
228     eval {
229         CORE::link($from, $to)
230             ? $success++
231             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
232               ? die "AFS"  # okay inside eval {}
233               : warn "Couldn't link $from to $to: $!\n"
234           unless $opts{notify};
235     };
236     if ($@) {
237         File::Copy::copy($from, $to)
238             ? $success++
239             : warn "Couldn't copy $from to $to: $!\n"
240           unless $opts{notify};
241     }
242     $success;
243 }
244
245 sub rename {
246     my($from,$to) = @_;
247     if (-f $to and not unlink($to)) {
248         my($i);
249         for ($i = 1; $i < 50; $i++) {
250             last if CORE::rename($to, "$to.$i");
251         }
252         warn("Cannot rename to `$to.$i': $!"), return 0 
253             if $i >= 50;        # Give up!
254     }
255     link($from,$to) || return 0;
256     unlink($from);
257 }
258
259 sub chmod {
260     my($mode,$name) = @_;
261
262     printf "  chmod %o %s\n", $mode, $name if $opts{verbose};
263     CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
264         unless $opts{notify};
265 }
266
267 sub samepath {
268     my($p1, $p2) = @_;
269     my($dev1, $ino1, $dev2, $ino2);
270
271     if ($p1 ne $p2) {
272         ($dev1, $ino1) = stat($p1);
273         ($dev2, $ino2) = stat($p2);
274         ($dev1 == $dev2 && $ino1 == $ino2);
275     }
276     else {
277         1;
278     }
279 }