This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change 33859 missed $destdir => $opts{destdir}
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
e8fcf3b9 2BEGIN { @INC = qw(lib) }
f0512cd0 3use strict;
be61d08e
RGS
4
5BEGIN {
6 use Config;
7 if ($Config{userelocatableinc}) {
8 # This might be a considered a hack. Need to get information about the
9 # configuration from Config.pm *before* Config.pm expands any .../
10 # prefixes.
11 #
12 # So we set $^X to pretend that we're the already installed perl, so
13 # Config.pm doesits ... expansion off that location.
14
15 my $location = $Config{initialinstalllocation};
16 die <<'OS' unless defined $location;
17$Config{initialinstalllocation} is not defined - can't install a relocatable
18perl without this.
19OS
20 $^X = "$location/perl";
21 # And then remove all trace of ever having loaded Config.pm, so that
22 # it will reload with the revised $^X
23 undef %Config::;
24 delete $INC{"Config.pm"};
25 delete $INC{"Config_heavy.pl"};
26 # You never saw us. We weren't here.
27 }
28}
29
16d20bd9
AD
30use Config;
31use Getopt::Long;
32use File::Find;
354f3b56 33use File::Copy;
356fc125 34use File::Path qw(mkpath);
354f3b56 35use ExtUtils::Packlist;
a2743834 36use Pod::Man;
cd8bccb5 37use subs qw(unlink chmod rename link);
b48e406f 38use vars qw($packlist);
a8f3d2d7
NC
39use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $Is_Darwin $Is_NetWare);
40
41BEGIN {
42 $Is_VMS = $^O eq 'VMS';
43 $Is_W32 = $^O eq 'MSWin32';
44 $Is_OS2 = $^O eq 'os2';
45 $Is_Cygwin = $^O eq 'cygwin';
46 $Is_Darwin = $^O eq 'darwin';
47 if ($Is_VMS) { eval 'use VMS::Filespec;' }
48}
16d20bd9 49
791b4ad3
A
50if ($Config{d_umask}) {
51 umask(022); # umasks like 077 aren't that useful for installations
52}
53
cd8bccb5 54$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 55
f0512cd0
DC
56my $ver = $Config{version}; # Not used presently.
57my $release = substr($],0,3); # Not used presently.
58my $patchlevel = substr($],3,2);
16d20bd9 59die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
60 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
61 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 62
f0512cd0 63my $usage =
16d20bd9 64"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 65 --man3dir=/usr/wherever --man3ext=3
f1745d4f 66 --batchlimit=40
eabd589f 67 --notify --verbose --silent --help
16d20bd9
AD
68 Defaults are:
69 man1dir = $Config{'installman1dir'};
70 man1ext = $Config{'man1ext'};
71 man3dir = $Config{'installman3dir'};
72 man3ext = $Config{'man3ext'};
eabd589f
A
73 --notify (or -n) just lists commands that would be executed.
74 --verbose (or -V) report all progress.
75 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 76
f0512cd0
DC
77my %opts;
78GetOptions( \%opts,
f1745d4f 79 qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
ab33fab8 80 destdir:s notify n help silent S verbose V))
16d20bd9 81 || die $usage;
f0512cd0 82die $usage if $opts{help};
02bc0c09 83$opts{destdir} //= '';
16d20bd9 84
5a9231b0 85$opts{man1dir} = "$opts{destdir}$Config{'installman1dir'}"
f0512cd0
DC
86 unless defined($opts{man1dir});
87$opts{man1ext} = $Config{'man1ext'}
88 unless defined($opts{man1ext});
5a9231b0 89$opts{man3dir} = "$opts{destdir}$Config{'installman3dir'}"
f0512cd0
DC
90 unless defined($opts{man3dir});
91$opts{man3ext} = $Config{'man3ext'}
92 unless defined($opts{man3ext});
93$opts{silent} ||= $opts{S};
f0512cd0 94$opts{notify} ||= $opts{n};
4ad019ef 95$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9
AD
96
97#Sanity checks
98
cd8bccb5
PP
99-x "./perl$Config{exe_ext}"
100 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 101-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
102 || warn "Perl library directory $Config{'installprivlib'} not found.
103 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 104-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
105 " (Installing anyway.)\n";
106
1ef57a5c 107$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 108
21dae8a0 109
16d20bd9 110# Install the main pod pages.
a2743834 111pod2man('pod', $opts{man1dir}, $opts{man1ext});
16d20bd9
AD
112
113# Install the pods for library modules.
a2743834 114pod2man('lib', $opts{man3dir}, $opts{man3ext});
16d20bd9 115
1fef88e7 116# Install the pods embedded in the installed scripts
bf1ee2ba 117my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
21dae8a0
SC
118open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
119while (<UTILS>) {
120 next if /^#/;
121 chomp;
122 $_ = $1 if /#.*pod\s*=\s*(\S+)/;
53fb3a93 123 my ($where, $what) = m|^(\S*)/(\S+)|;
a2743834 124 pod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
bf1ee2ba
JH
125 if ($has_man1dir) {
126 if (my ($where2, $what2) = m|#.*link\s*=\s*(\S+)/(\S+)|) {
127 my $old = "$opts{man1dir}/$what.$opts{man1ext}";
128 my $new = "$opts{man1dir}/$what2.$opts{man1ext}";
129 unlink($new);
130 link($old, $new);
25207203 131 my $xold = $old;
132 $xold =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
133 my $xnew = $new;
134 $xnew =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
135 $packlist->{$xnew} = { from => $xold, type => 'link' };
bf1ee2ba 136 }
21dae8a0
SC
137 }
138}
1fef88e7 139
a2743834 140sub pod2man {
f1745d4f
JH
141 # @script is scripts names if we are installing manpages embedded
142 # in scripts, () otherwise
143 my($poddir, $mandir, $manext, @script) = @_;
16d20bd9 144 if ($mandir eq ' ' or $mandir eq '') {
f1745d4f
JH
145 if (@script) {
146 warn "Skipping installation of $poddir/$_ man page.\n"
147 foreach @script;
148 } else {
149 warn "Skipping installation of $poddir man pages.\n";
150 }
16d20bd9
AD
151 return;
152 }
153
b48e406f 154 print "installing from $poddir\n" if $opts{verbose};
16d20bd9 155
418918ac 156 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
b48e406f
NC
157 # Make a list of all the .pm and .pod files in the directory. We avoid
158 # chdir because we are running with @INC = '../lib', and modules may wish
159 # to dynamically require Carp::Heavy or other diagnostics warnings.
160 # Hash the names of files we find, keys are names relative to perl build
161 # dir ('.'), values are names relative to $poddir.
162 my %modpods;
f1745d4f 163 if (@script) {
b48e406f 164 %modpods = (map {+"$poddir/$_", $_} @script);
72b3d9b4
GS
165 }
166 else {
b48e406f
NC
167 File::Find::find({no_chdir=>1,
168 wanted => sub {
169 # $_ is $File::Find::name when using no_chdir
170 if (-f $_ and /\.p(?:m|od)$/) {
171 my $fullname = $_;
172 s!^\Q$poddir\E/!!;
173 $modpods{$fullname} = $_;
174 }
175 }},
176 $poddir);
1fef88e7 177 }
f1745d4f 178 my @to_process;
b48e406f
NC
179 foreach my $mod (sort keys %modpods) {
180 my $manpage = $modpods{$mod};
cd8bccb5
PP
181 my $tmp;
182 # Skip .pm files that have corresponding .pod files, and Functions.pm.
183 next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
732876f5 184 next if $mod =~ m:/t/:; # no pods from test directories
b48e406f 185 next if ($manpage eq 'Pod/Functions.pm'); #### Used only by pod itself
cd8bccb5 186
63fae907
AT
187 # Skip files without pod docs
188 my $has_pod;
189 if (open T, $mod)
190 {
191 local $_;
192 while (<T>)
193 {
194 ++$has_pod and last if /^=(?:head\d+|item|pod)\b/;
195 }
196
197 close T;
198 }
199
200 unless ($has_pod)
201 {
202 warn "no documentation in $mod\n";
203 next;
204 }
205
16d20bd9
AD
206 # Convert name from File/Basename.pm to File::Basename.3 format,
207 # if necessary.
208 $manpage =~ s#\.p(m|od)$##;
4fabb596 209 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 210 $manpage =~ s#/#.#g;
72b3d9b4
GS
211 }
212 else {
cd8bccb5
PP
213 $manpage =~ s#/#::#g;
214 }
215 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 216 $manpage = "${mandir}/${manpage}.${manext}";
f1745d4f
JH
217 push @to_process, [$mod, $tmp, $manpage];
218 }
a2743834 219
a2743834
MS
220 foreach my $page (@to_process) {
221 my($pod, $tmp, $manpage) = @$page;
222
571afc3f
SP
223 my $parser = Pod::Man->new( section => $manext,
224 official=> 1,
225 center => 'Perl Programmers Reference Guide'
226 );
5a9231b0
MS
227 my $xmanpage = $manpage;
228 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
229 print " $xmanpage\n";
a2743834
MS
230 if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
231 if (-s $tmp) {
232 if (rename($tmp, $manpage)) {
5a9231b0 233 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
234 next;
235 }
236 }
237 unlink($tmp);
6d64b06f 238 }
16d20bd9 239 }
16d20bd9
AD
240}
241
f0512cd0
DC
242$packlist->write() unless $opts{notify};
243print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
244
245exit 0;
16d20bd9
AD
246
247###############################################################################
248# Utility subroutines from installperl
249
cd8bccb5 250sub unlink {
f0512cd0 251 my(@names) = @_;
cd8bccb5
PP
252 my $cnt = 0;
253
a8f3d2d7
NC
254 return scalar(@names) if $Is_VMS;
255
f0512cd0 256 foreach my $name (@names) {
72b3d9b4
GS
257 next unless -e $name;
258 chmod 0777, $name if $^O eq 'os2';
f0512cd0 259 print " unlink $name\n" if $opts{verbose};
72b3d9b4 260 ( CORE::unlink($name) and ++$cnt
f0512cd0 261 or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
cd8bccb5
PP
262 }
263 return $cnt;
264}
265
16d20bd9 266sub link {
354f3b56
AB
267 my($from,$to) = @_;
268 my($success) = 0;
16d20bd9 269
a2743834 270 print " ln $from $to\n" if $opts{verbose};
354f3b56
AB
271 eval {
272 CORE::link($from, $to)
273 ? $success++
274 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
275 ? die "AFS" # okay inside eval {}
a8f3d2d7 276 : die "Couldn't link $from to $to: $!\n"
f0512cd0 277 unless $opts{notify};
354f3b56
AB
278 };
279 if ($@) {
280 File::Copy::copy($from, $to)
281 ? $success++
282 : warn "Couldn't copy $from to $to: $!\n"
f0512cd0 283 unless $opts{notify};
354f3b56
AB
284 }
285 $success;
cd8bccb5
PP
286}
287
288sub rename {
f0512cd0 289 my($from,$to) = @_;
55a105fd 290 if (-f $to and not unlink($to)) {
72b3d9b4
GS
291 my($i);
292 for ($i = 1; $i < 50; $i++) {
293 last if CORE::rename($to, "$to.$i");
294 }
295 warn("Cannot rename to `$to.$i': $!"), return 0
296 if $i >= 50; # Give up!
cd8bccb5 297 }
55a105fd 298 link($from,$to) || return 0;
299 unlink($from);
16d20bd9
AD
300}
301
302sub chmod {
f0512cd0 303 my($mode,$name) = @_;
16d20bd9 304
f0512cd0 305 printf " chmod %o %s\n", $mode, $name if $opts{verbose};
cd8bccb5 306 CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
f0512cd0 307 unless $opts{notify};
16d20bd9
AD
308}
309
16d20bd9 310sub samepath {
f0512cd0
DC
311 my($p1, $p2) = @_;
312 my($dev1, $ino1, $dev2, $ino2);
16d20bd9 313
a8f3d2d7
NC
314 return (lc($p1) eq lc($p2)) if ($Is_W32 || $Is_NetWare);
315
16d20bd9
AD
316 if ($p1 ne $p2) {
317 ($dev1, $ino1) = stat($p1);
318 ($dev2, $ino2) = stat($p2);
319 ($dev1 == $dev2 && $ino1 == $ino2);
320 }
321 else {
322 1;
323 }
324}