This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
4 bugs in Test::More
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
e8fcf3b9 2BEGIN { @INC = qw(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;
a2743834 10use Pod::Man;
cd8bccb5 11use subs qw(unlink chmod rename link);
b48e406f 12use vars qw($packlist);
16d20bd9 13
791b4ad3
A
14if ($Config{d_umask}) {
15 umask(022); # umasks like 077 aren't that useful for installations
16}
17
cd8bccb5 18$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 19
f0512cd0
DC
20my $ver = $Config{version}; # Not used presently.
21my $release = substr($],0,3); # Not used presently.
22my $patchlevel = substr($],3,2);
16d20bd9 23die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
24 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
25 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 26
f0512cd0 27my $usage =
16d20bd9 28"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 29 --man3dir=/usr/wherever --man3ext=3
f1745d4f 30 --batchlimit=40
eabd589f 31 --notify --verbose --silent --help
16d20bd9
AD
32 Defaults are:
33 man1dir = $Config{'installman1dir'};
34 man1ext = $Config{'man1ext'};
35 man3dir = $Config{'installman3dir'};
36 man3ext = $Config{'man3ext'};
eabd589f
A
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";
16d20bd9 40
f0512cd0
DC
41my %opts;
42GetOptions( \%opts,
f1745d4f 43 qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
ab33fab8 44 destdir:s notify n help silent S verbose V))
16d20bd9 45 || die $usage;
f0512cd0 46die $usage if $opts{help};
16d20bd9 47
5a9231b0 48$opts{man1dir} = "$opts{destdir}$Config{'installman1dir'}"
f0512cd0
DC
49 unless defined($opts{man1dir});
50$opts{man1ext} = $Config{'man1ext'}
51 unless defined($opts{man1ext});
5a9231b0 52$opts{man3dir} = "$opts{destdir}$Config{'installman3dir'}"
f0512cd0
DC
53 unless defined($opts{man3dir});
54$opts{man3ext} = $Config{'man3ext'}
55 unless defined($opts{man3ext});
56$opts{silent} ||= $opts{S};
f0512cd0 57$opts{notify} ||= $opts{n};
4ad019ef 58$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9
AD
59
60#Sanity checks
61
cd8bccb5
PP
62-x "./perl$Config{exe_ext}"
63 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 64-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
65 || warn "Perl library directory $Config{'installprivlib'} not found.
66 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 67-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
68 " (Installing anyway.)\n";
69
1ef57a5c 70$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 71
21dae8a0 72
16d20bd9 73# Install the main pod pages.
a2743834 74pod2man('pod', $opts{man1dir}, $opts{man1ext});
16d20bd9
AD
75
76# Install the pods for library modules.
a2743834 77pod2man('lib', $opts{man3dir}, $opts{man3ext});
16d20bd9 78
1fef88e7 79# Install the pods embedded in the installed scripts
bf1ee2ba 80my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
21dae8a0
SC
81open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
82while (<UTILS>) {
83 next if /^#/;
84 chomp;
85 $_ = $1 if /#.*pod\s*=\s*(\S+)/;
53fb3a93 86 my ($where, $what) = m|^(\S*)/(\S+)|;
a2743834 87 pod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
bf1ee2ba
JH
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 }
21dae8a0
SC
95 }
96}
1fef88e7 97
a2743834 98sub pod2man {
f1745d4f
JH
99 # @script is scripts names if we are installing manpages embedded
100 # in scripts, () otherwise
101 my($poddir, $mandir, $manext, @script) = @_;
16d20bd9 102 if ($mandir eq ' ' or $mandir eq '') {
f1745d4f
JH
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 }
16d20bd9
AD
109 return;
110 }
111
b48e406f 112 print "installing from $poddir\n" if $opts{verbose};
16d20bd9 113
418918ac 114 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
b48e406f
NC
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;
f1745d4f 121 if (@script) {
b48e406f 122 %modpods = (map {+"$poddir/$_", $_} @script);
72b3d9b4
GS
123 }
124 else {
b48e406f
NC
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);
1fef88e7 135 }
f1745d4f 136 my @to_process;
b48e406f
NC
137 foreach my $mod (sort keys %modpods) {
138 my $manpage = $modpods{$mod};
cd8bccb5
PP
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);
732876f5 142 next if $mod =~ m:/t/:; # no pods from test directories
b48e406f 143 next if ($manpage eq 'Pod/Functions.pm'); #### Used only by pod itself
cd8bccb5 144
16d20bd9
AD
145 # Convert name from File/Basename.pm to File::Basename.3 format,
146 # if necessary.
147 $manpage =~ s#\.p(m|od)$##;
4fabb596 148 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 149 $manpage =~ s#/#.#g;
72b3d9b4
GS
150 }
151 else {
cd8bccb5
PP
152 $manpage =~ s#/#::#g;
153 }
154 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 155 $manpage = "${mandir}/${manpage}.${manext}";
f1745d4f
JH
156 push @to_process, [$mod, $tmp, $manpage];
157 }
a2743834
MS
158
159 my $parser = Pod::Man->new( section => $manext,
160 official=> 1,
161 center => 'Perl Programmers Reference Guide'
162 );
163 foreach my $page (@to_process) {
164 my($pod, $tmp, $manpage) = @$page;
165
5a9231b0
MS
166 my $xmanpage = $manpage;
167 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
168 print " $xmanpage\n";
a2743834
MS
169 if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
170 if (-s $tmp) {
171 if (rename($tmp, $manpage)) {
5a9231b0 172 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
173 next;
174 }
175 }
176 unlink($tmp);
6d64b06f 177 }
16d20bd9 178 }
16d20bd9
AD
179}
180
f0512cd0
DC
181$packlist->write() unless $opts{notify};
182print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
183
184exit 0;
185
186
187###############################################################################
188# Utility subroutines from installperl
189
cd8bccb5 190sub unlink {
f0512cd0 191 my(@names) = @_;
cd8bccb5
PP
192 my $cnt = 0;
193
f0512cd0 194 foreach my $name (@names) {
72b3d9b4
GS
195 next unless -e $name;
196 chmod 0777, $name if $^O eq 'os2';
f0512cd0 197 print " unlink $name\n" if $opts{verbose};
72b3d9b4 198 ( CORE::unlink($name) and ++$cnt
f0512cd0 199 or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
cd8bccb5
PP
200 }
201 return $cnt;
202}
203
16d20bd9 204sub link {
354f3b56
AB
205 my($from,$to) = @_;
206 my($success) = 0;
16d20bd9 207
a2743834 208 print " ln $from $to\n" if $opts{verbose};
354f3b56
AB
209 eval {
210 CORE::link($from, $to)
211 ? $success++
212 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
213 ? die "AFS" # okay inside eval {}
214 : warn "Couldn't link $from to $to: $!\n"
f0512cd0 215 unless $opts{notify};
354f3b56
AB
216 };
217 if ($@) {
218 File::Copy::copy($from, $to)
219 ? $success++
220 : warn "Couldn't copy $from to $to: $!\n"
f0512cd0 221 unless $opts{notify};
354f3b56
AB
222 }
223 $success;
cd8bccb5
PP
224}
225
226sub rename {
f0512cd0 227 my($from,$to) = @_;
55a105fd 228 if (-f $to and not unlink($to)) {
72b3d9b4
GS
229 my($i);
230 for ($i = 1; $i < 50; $i++) {
231 last if CORE::rename($to, "$to.$i");
232 }
233 warn("Cannot rename to `$to.$i': $!"), return 0
234 if $i >= 50; # Give up!
cd8bccb5 235 }
55a105fd 236 link($from,$to) || return 0;
237 unlink($from);
16d20bd9
AD
238}
239
240sub chmod {
f0512cd0 241 my($mode,$name) = @_;
16d20bd9 242
f0512cd0 243 printf " chmod %o %s\n", $mode, $name if $opts{verbose};
cd8bccb5 244 CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
f0512cd0 245 unless $opts{notify};
16d20bd9
AD
246}
247
16d20bd9 248sub samepath {
f0512cd0
DC
249 my($p1, $p2) = @_;
250 my($dev1, $ino1, $dev2, $ino2);
16d20bd9
AD
251
252 if ($p1 ne $p2) {
253 ($dev1, $ino1) = stat($p1);
254 ($dev2, $ino2) = stat($p2);
255 ($dev1 == $dev2 && $ino1 == $ino2);
256 }
257 else {
258 1;
259 }
260}