This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
It is possible to have no hosts database at all. Pointed out in
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
16d20bd9 2BEGIN { @INC = ('lib') }
f0512cd0 3use strict;
16d20bd9
AD
4use Config;
5use Getopt::Long;
6use File::Find;
354f3b56 7use File::Copy;
356fc125 8use File::Path qw(mkpath);
354f3b56 9use ExtUtils::Packlist;
cd8bccb5 10use subs qw(unlink chmod rename link);
f0512cd0 11use vars qw($packlist @modpods);
16d20bd9
AD
12require Cwd;
13
cd8bccb5 14$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 15
f0512cd0
DC
16my $ver = $Config{version}; # Not used presently.
17my $release = substr($],0,3); # Not used presently.
18my $patchlevel = substr($],3,2);
16d20bd9 19die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
20 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
21 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 22
f0512cd0 23my $usage =
16d20bd9 24"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 25 --man3dir=/usr/wherever --man3ext=3
eabd589f 26 --notify --verbose --silent --help
16d20bd9
AD
27 Defaults are:
28 man1dir = $Config{'installman1dir'};
29 man1ext = $Config{'man1ext'};
30 man3dir = $Config{'installman3dir'};
31 man3ext = $Config{'man3ext'};
eabd589f
A
32 --notify (or -n) just lists commands that would be executed.
33 --verbose (or -V) report all progress.
34 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 35
f0512cd0
DC
36my %opts;
37GetOptions( \%opts,
38 qw( man1dir=s man1ext=s man3dir=s man3ext=s
39 notify n help silent S verbose V))
16d20bd9 40 || die $usage;
f0512cd0 41die $usage if $opts{help};
16d20bd9 42
f0512cd0
DC
43$opts{man1dir} = $Config{'installman1dir'}
44 unless defined($opts{man1dir});
45$opts{man1ext} = $Config{'man1ext'}
46 unless defined($opts{man1ext});
47$opts{man3dir} = $Config{'installman3dir'}
48 unless defined($opts{man3dir});
49$opts{man3ext} = $Config{'man3ext'}
50 unless defined($opts{man3ext});
51$opts{silent} ||= $opts{S};
f0512cd0 52$opts{notify} ||= $opts{n};
4ad019ef 53$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9
AD
54
55#Sanity checks
56
cd8bccb5
PP
57-x "./perl$Config{exe_ext}"
58 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
16d20bd9
AD
59-d $Config{'installprivlib'}
60 || warn "Perl library directory $Config{'installprivlib'} not found.
61 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 62-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
63 " (Installing anyway.)\n";
64
354f3b56
AB
65$packlist = ExtUtils::Packlist->new("$Config{installarchlib}/.packlist");
66
16d20bd9 67# Install the main pod pages.
f0512cd0 68runpod2man('pod', $opts{man1dir}, $opts{man1ext});
16d20bd9
AD
69
70# Install the pods for library modules.
f0512cd0 71runpod2man('lib', $opts{man3dir}, $opts{man3ext});
16d20bd9 72
1fef88e7 73# Install the pods embedded in the installed scripts
f0512cd0
DC
74runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'c2ph');
75runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'h2ph');
76runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'h2xs');
77runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perlcc');
78runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perldoc');
79runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perlbug');
80runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'pl2pm');
81runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'splain');
82runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'dprofpp');
83runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 's2p');
84runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 'a2p.pod');
85runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 'find2perl');
86runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2man');
87runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2html');
88runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2text');
89runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2usage');
90runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'podchecker');
91runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'podselect');
1fef88e7
JM
92
93# It would probably be better to have this page linked
94# to the c2ph man page. Or, this one could say ".so man1/c2ph.1",
f0512cd0
DC
95# but then it would have to pay attention to $opts{man1dir} and $opts{man1ext}.
96runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'pstruct');
1fef88e7 97
f0512cd0 98runpod2man('lib/ExtUtils', $opts{man1dir}, $opts{man1ext}, 'xsubpp');
1fef88e7 99
16d20bd9 100sub runpod2man {
1fef88e7
JM
101 # $script is script name if we are installing a manpage embedded
102 # in a script, undef otherwise
103 my($poddir, $mandir, $manext, $script) = @_;
104
105 my($downdir); # can't just use .. when installing xsubpp manpage
106
107 $downdir = $poddir;
108 $downdir =~ s:[^/]+:..:g;
16d20bd9
AD
109 my($builddir) = Cwd::getcwd();
110
111 if ($mandir eq ' ' or $mandir eq '') {
e31a4ff1 112 warn "Skipping installation of ",
1fef88e7 113 ($script ? "$poddir/$script man page" : "$poddir man pages"), ".\n";
16d20bd9
AD
114 return;
115 }
116
f0512cd0 117 print "chdir $poddir\n" if $opts{verbose};
1fef88e7 118 chdir $poddir || die "Unable to cd to $poddir directory!\n$!\n";
16d20bd9
AD
119
120 # We insist on using the current version of pod2man in case there
121 # are enhancements or changes from previous installed versions.
e50aee73
AD
122 # The error message doesn't include the '..' because the user
123 # won't be aware that we've chdir to $poddir.
1fef88e7 124 -r "$downdir/pod/pod2man" || die "Executable pod/pod2man not found.\n";
e50aee73
AD
125
126 # We want to be sure to use the current perl. We can't rely on
127 # the installed perl because it might not be actually installed
128 # yet. (The user may have set the $install* Configure variables
129 # to point to some temporary home, from which the executable gets
130 # installed by occult means.)
f0512cd0 131 my $pod2man = "$downdir/perl -I $downdir/lib $downdir/pod/pod2man --section=$manext --official";
16d20bd9 132
418918ac 133 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
16d20bd9
AD
134 # Make a list of all the .pm and .pod files in the directory. We will
135 # always run pod2man from the lib directory and feed it the full pathname
136 # of the pod. This might be useful for pod2man someday.
1fef88e7
JM
137 if ($script) {
138 @modpods = ($script);
72b3d9b4
GS
139 }
140 else {
1fef88e7 141 @modpods = ();
f0512cd0 142 File::Find::find(\&lsmodpods, '.');
1fef88e7 143 }
f0512cd0
DC
144 foreach my $mod (@modpods) {
145 my $manpage = $mod;
cd8bccb5
PP
146 my $tmp;
147 # Skip .pm files that have corresponding .pod files, and Functions.pm.
148 next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
149 next if ($mod eq 'Pod/Functions.pm'); #### Used only by pod itself
150
16d20bd9
AD
151 # Convert name from File/Basename.pm to File::Basename.3 format,
152 # if necessary.
153 $manpage =~ s#\.p(m|od)$##;
4fabb596 154 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 155 $manpage =~ s#/#.#g;
72b3d9b4
GS
156 }
157 else {
cd8bccb5
PP
158 $manpage =~ s#/#::#g;
159 }
160 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 161 $manpage = "${mandir}/${manpage}.${manext}";
f0512cd0 162 if (&cmd("$pod2man $mod > $tmp") == 0 && !$opts{notify} && -s $tmp) {
72b3d9b4
GS
163 if (rename($tmp, $manpage)) {
164 $packlist->{$manpage} = { type => 'file' };
165 next;
166 }
6d64b06f 167 }
f0512cd0 168 unless ($opts{notify}) {
72b3d9b4 169 unlink($tmp);
6d64b06f 170 }
16d20bd9
AD
171 }
172 chdir "$builddir" || die "Unable to cd back to $builddir directory!\n$!\n";
f0512cd0 173 print " chdir $builddir\n" if $opts{verbose};
16d20bd9
AD
174}
175
176sub lsmodpods {
177 my $dir = $File::Find::dir;
178 my $name = $File::Find::name;
179 if (-f $_) {
180 $name =~ s#^\./##;
181 push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
182 }
183}
184
f0512cd0
DC
185$packlist->write() unless $opts{notify};
186print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
187
188exit 0;
189
190
191###############################################################################
192# Utility subroutines from installperl
193
194sub cmd {
f0512cd0
DC
195 my ($cmd) = @_;
196 print " $cmd\n" if $opts{verbose};
197 unless ($opts{notify}) {
b29d8d13
PP
198 if ($Config{d_fork}) {
199 fork ? wait : exec $cmd; # Allow user to ^C out of command.
200 }
201 else {
202 system $cmd;
203 }
cb1a09d0 204 warn "Command failed!!\n" if $?;
16d20bd9 205 }
cb1a09d0 206 return $? != 0;
16d20bd9
AD
207}
208
cd8bccb5 209sub unlink {
f0512cd0 210 my(@names) = @_;
cd8bccb5
PP
211 my $cnt = 0;
212
f0512cd0 213 foreach my $name (@names) {
72b3d9b4
GS
214 next unless -e $name;
215 chmod 0777, $name if $^O eq 'os2';
f0512cd0 216 print " unlink $name\n" if $opts{verbose};
72b3d9b4 217 ( CORE::unlink($name) and ++$cnt
f0512cd0 218 or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
cd8bccb5
PP
219 }
220 return $cnt;
221}
222
16d20bd9 223sub link {
354f3b56
AB
224 my($from,$to) = @_;
225 my($success) = 0;
16d20bd9 226
f0512cd0 227 print $opts{verbose} ? " ln $from $to\n" : " $to\n" unless $opts{silent};
354f3b56
AB
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"
f0512cd0 234 unless $opts{notify};
354f3b56
AB
235 };
236 if ($@) {
237 File::Copy::copy($from, $to)
238 ? $success++
239 : warn "Couldn't copy $from to $to: $!\n"
f0512cd0 240 unless $opts{notify};
354f3b56
AB
241 }
242 $success;
cd8bccb5
PP
243}
244
245sub rename {
f0512cd0 246 my($from,$to) = @_;
18689f1d 247 if (-f $to and not &unlink($to)) {
72b3d9b4
GS
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!
cd8bccb5 254 }
18689f1d
AD
255 &link($from,$to) || return 0;
256 &unlink($from)
16d20bd9
AD
257}
258
259sub chmod {
f0512cd0 260 my($mode,$name) = @_;
16d20bd9 261
f0512cd0 262 printf " chmod %o %s\n", $mode, $name if $opts{verbose};
cd8bccb5 263 CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
f0512cd0 264 unless $opts{notify};
16d20bd9
AD
265}
266
16d20bd9 267sub samepath {
f0512cd0
DC
268 my($p1, $p2) = @_;
269 my($dev1, $ino1, $dev2, $ino2);
16d20bd9
AD
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}