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