This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Part Integrate mainline
[perl5.git] / installman
1 #!./perl -w
2 BEGIN { @INC = ('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 subs qw(unlink chmod rename link);
11 use vars qw($packlist @modpods);
12 require Cwd;
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         batchlimit is maximum number of pod files per invocation of pod2man
38         --notify  (or -n) just lists commands that would be executed.
39         --verbose (or -V) report all progress.
40         --silent  (or -S) be silent. Only report errors.\n";
41
42 my %opts;
43 GetOptions( \%opts,
44             qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
45                 notify n help silent S verbose V)) 
46         || die $usage;
47 die $usage if $opts{help};
48
49 $opts{man1dir} = $Config{'installman1dir'}
50     unless defined($opts{man1dir}); 
51 $opts{man1ext} = $Config{'man1ext'}
52     unless defined($opts{man1ext}); 
53 $opts{man3dir} = $Config{'installman3dir'}
54     unless defined($opts{man3dir}); 
55 $opts{man3ext} = $Config{'man3ext'}
56     unless defined($opts{man3ext}); 
57 $opts{batchlimit} ||= 40;
58 $opts{silent} ||= $opts{S};
59 $opts{notify} ||= $opts{n};
60 $opts{verbose} ||= $opts{V} || $opts{notify};
61
62 #Sanity checks
63
64 -x  "./perl$Config{exe_ext}" 
65   or warn "./perl$Config{exe_ext} not found!  Have you run make?\n";
66 -d  $Config{'installprivlib'}
67         || warn "Perl library directory $Config{'installprivlib'} not found.
68                 Have you run make install?.  (Installing anyway.)\n";
69 -x "t/perl$Config{exe_ext}"             || warn "WARNING: You've never run 'make test'!!!",
70         "  (Installing anyway.)\n";
71
72 $packlist = ExtUtils::Packlist->new("$Config{installarchlib}/.packlist");
73
74
75 # Install the main pod pages.
76 runpod2man('pod', $opts{man1dir}, $opts{man1ext});
77
78 # Install the pods for library modules.
79 runpod2man('lib', $opts{man3dir}, $opts{man3ext});
80
81 # Install the pods embedded in the installed scripts
82 open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
83 while (<UTILS>) {
84     next if /^#/;
85     chomp;
86     $_ = $1 if /#.*pod\s*=\s*(\S+)/;
87     my ($where, $what) = m|^(.*?)/(\S+)|;
88     runpod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
89     if (($where, $what) = m|#.*link\s*=\s*(\S+)/(\S+)|) {
90         runpod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
91     }
92 }
93
94 sub runpod2man {
95     # @script is scripts names if we are installing manpages embedded 
96     # in scripts, () otherwise
97     my($poddir, $mandir, $manext, @script) = @_;
98
99     my($downdir); # can't just use .. when installing xsubpp manpage
100
101     $downdir = $poddir;
102     $downdir =~ s:[^/]+:..:g;
103     my($builddir) = Cwd::getcwd();
104
105     if ($mandir eq ' ' or $mandir eq '') {
106         if (@script) {
107             warn "Skipping installation of $poddir/$_ man page.\n"
108                 foreach @script;
109         } else {
110             warn "Skipping installation of $poddir man pages.\n";
111         }
112         return;
113     }
114
115     print "chdir $poddir\n" if $opts{verbose};
116     chdir $poddir || die "Unable to cd to $poddir directory!\n$!\n";
117
118     # We insist on using the current version of pod2man in case there
119     # are enhancements or changes from previous installed versions.
120     # The error message doesn't include the '..' because the user
121     # won't be aware that we've chdir to $poddir.
122     -r  "$downdir/pod/pod2man" || die "Executable pod/pod2man not found.\n";
123
124     # We want to be sure to use the current perl.  We can't rely on
125     # the installed perl because it might not be actually installed
126     # yet. (The user may have set the $install* Configure variables 
127     # to point to some temporary home, from which the executable gets
128     # installed by occult means.)
129     my $pod2man = "$downdir/perl -I $downdir/lib $downdir/pod/pod2man --section=$manext --official";
130
131     mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify};  # In File::Path
132     # Make a list of all the .pm and .pod files in the directory.  We will
133     # always run pod2man from the lib directory and feed it the full pathname
134     # of the pod.  This might be useful for pod2man someday.
135     if (@script) {
136         @modpods = @script;
137     }
138     else {
139         @modpods = ();
140         File::Find::find(\&lsmodpods, '.');
141     }
142     my @to_process;
143     foreach my $mod (@modpods) {
144         my $manpage = $mod;
145         my $tmp;
146         # Skip .pm files that have corresponding .pod files, and Functions.pm.
147         next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
148         next if ($mod eq 'Pod/Functions.pm');   #### Used only by pod itself
149
150         # Convert name from  File/Basename.pm to File::Basename.3 format,
151         # if necessary.
152         $manpage =~ s#\.p(m|od)$##;
153         if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
154           $manpage =~ s#/#.#g;
155         }
156         else {
157           $manpage =~ s#/#::#g;
158         }
159         $tmp = "${mandir}/${manpage}.tmp";
160         $manpage = "${mandir}/${manpage}.${manext}";
161         push @to_process, [$mod, $tmp, $manpage];
162     }
163     # Don't do all pods in same command to avoid busting command line limits
164     while (my @this_batch = splice @to_process, 0, $opts{batchlimit}) {
165         my $cmd = join " ", $pod2man, map "$$_[0] $$_[1]", @this_batch;
166         if (&cmd($cmd) == 0 && !$opts{notify}) {
167             foreach (@this_batch) {
168                 my (undef, $tmp, $manpage) = @$_;
169                 if (-s $tmp) {
170                     if (rename($tmp, $manpage)) {
171                         $packlist->{$manpage} = { type => 'file' };
172                         next;
173                     }
174                 }
175                 unless ($opts{notify}) {
176                     unlink($tmp);
177                 }
178             }
179         }
180     }
181     chdir "$builddir" || die "Unable to cd back to $builddir directory!\n$!\n";
182     print "  chdir $builddir\n" if $opts{verbose};
183 }
184
185 sub lsmodpods {
186     my $dir  = $File::Find::dir;
187     my $name = $File::Find::name;
188     if (-f $_) {
189         $name =~ s#^\./##;
190         push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
191     }
192 }
193
194 $packlist->write() unless $opts{notify};
195 print "  Installation complete\n" if $opts{verbose};
196
197 exit 0;
198     
199
200 ###############################################################################
201 # Utility subroutines from installperl
202
203 sub cmd {
204     my ($cmd) = @_;
205     print "  $cmd\n" if $opts{verbose};
206     unless ($opts{notify}) {
207         if ($Config{d_fork}) {
208             fork ? wait : exec $cmd;  # Allow user to ^C out of command.
209         }
210         else {
211             system $cmd;
212         }
213         warn "Command failed!!\n" if $?;
214     }
215     return $? != 0;
216 }
217
218 sub unlink {
219     my(@names) = @_;
220     my $cnt = 0;
221
222     foreach my $name (@names) {
223         next unless -e $name;
224         chmod 0777, $name if $^O eq 'os2';
225         print "  unlink $name\n" if $opts{verbose};
226         ( CORE::unlink($name) and ++$cnt 
227             or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
228     }
229     return $cnt;
230 }
231
232 sub link {
233     my($from,$to) = @_;
234     my($success) = 0;
235
236     print $opts{verbose} ? "  ln $from $to\n" : "  $to\n" unless $opts{silent};
237     eval {
238         CORE::link($from, $to)
239             ? $success++
240             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
241               ? die "AFS"  # okay inside eval {}
242               : warn "Couldn't link $from to $to: $!\n"
243           unless $opts{notify};
244     };
245     if ($@) {
246         File::Copy::copy($from, $to)
247             ? $success++
248             : warn "Couldn't copy $from to $to: $!\n"
249           unless $opts{notify};
250     }
251     $success;
252 }
253
254 sub rename {
255     my($from,$to) = @_;
256     if (-f $to and not unlink($to)) {
257         my($i);
258         for ($i = 1; $i < 50; $i++) {
259             last if CORE::rename($to, "$to.$i");
260         }
261         warn("Cannot rename to `$to.$i': $!"), return 0 
262             if $i >= 50;        # Give up!
263     }
264     link($from,$to) || return 0;
265     unlink($from);
266 }
267
268 sub chmod {
269     my($mode,$name) = @_;
270
271     printf "  chmod %o %s\n", $mode, $name if $opts{verbose};
272     CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
273         unless $opts{notify};
274 }
275
276 sub samepath {
277     my($p1, $p2) = @_;
278     my($dev1, $ino1, $dev2, $ino2);
279
280     if ($p1 ne $p2) {
281         ($dev1, $ino1) = stat($p1);
282         ($dev2, $ino2) = stat($p2);
283         ($dev1 == $dev2 && $ino1 == $ino2);
284     }
285     else {
286         1;
287     }
288 }