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