This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid redundant initialisation around Newxz()
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
f4df373d 2
be61d08e 3BEGIN {
9e6fc21f 4 @INC = qw(lib);
ae5391ad 5
9e6fc21f 6 # This needs to be at BEGIN time, before any use of Config
f4df373d 7 # install_lib itself loads and imports Config into main::
9e6fc21f 8 require './install_lib.pl';
be61d08e 9}
f4df373d 10
9e6fc21f 11use strict;
be61d08e 12
16d20bd9 13use Getopt::Long;
354f3b56 14use ExtUtils::Packlist;
a2743834 15use Pod::Man;
3d3a0a8a 16our ( %opts, $packlist );
791b4ad3 17
d6a39ee2 18require './Porting/pod_lib.pl';
1cb2462d 19my %man1 = (map {($_->[0], $_->[1])} @{get_pod_metadata()->{master}});
d6a39ee2 20
cd8bccb5 21$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 22
f0512cd0 23my $patchlevel = substr($],3,2);
16d20bd9 24die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
25 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
26 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 27
f0512cd0 28my $usage =
16d20bd9 29"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 30 --man3dir=/usr/wherever --man3ext=3
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
8cef6e50
NC
41# --strip intentionally does nothing. By permitting installman to accept it
42# without error, the Makefile can pass the same options to installperl and
43# installman, which permits more simplification there than this comment costs.
f0512cd0 44GetOptions( \%opts,
d0265671 45 qw( man1dir=s man1ext=s man3dir=s man3ext=s
8cef6e50 46 destdir:s notify|n help|h|? silent|S verbose|V strip))
16d20bd9 47 || die $usage;
f0512cd0 48die $usage if $opts{help};
02bc0c09 49$opts{destdir} //= '';
16d20bd9 50
3cc5758c
NC
51foreach my $pre (qw(man1 man3)) {
52 $opts{"${pre}dir"} //= $opts{destdir} . $Config{"install${pre}dir"};
53 $opts{"${pre}ext"} //= $Config{"${pre}ext"};
54}
bbb45456 55$opts{verbose} ||= $opts{notify};
16d20bd9
AD
56
57#Sanity checks
58
ae5391ad 59-x "./perl$Config{exe_ext}"
cd8bccb5 60 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 61-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
62 || warn "Perl library directory $Config{'installprivlib'} not found.
63 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 64-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
65 " (Installing anyway.)\n";
66
1ef57a5c 67$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 68
16d20bd9 69# Install the main pod pages.
1cb2462d 70pod2man(\%man1, $opts{man1dir}, $opts{man1ext}, 'pod');
16d20bd9
AD
71
72# Install the pods for library modules.
45a365ec 73{
9bbb230a
NC
74 my $found = pods_to_install();
75 pod2man($found->{$_}, $opts{man3dir}, $opts{man3ext}, 'lib')
76 foreach qw(MODULE PRAGMA);
45a365ec 77}
16d20bd9 78
1fef88e7 79# Install the pods embedded in the installed scripts
bf1ee2ba 80my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
3301e5d1
NC
81my $fh = open_or_die('utils.lst');
82while (<$fh>) {
21dae8a0
SC
83 next if /^#/;
84 chomp;
b3e335c2 85 my ($path, $leaf) = m|^(\S*/(\S+))|;
c67aee7a 86 # Have we already installed the manpage for this? (eg perldoc)
1cb2462d 87 next if $man1{$leaf};
b3e335c2 88 pod2man({$leaf, $path}, $opts{man1dir}, $opts{man1ext});
bf1ee2ba 89 if ($has_man1dir) {
b3e335c2
NC
90 if (my ($link) = m|#.*link\s*=\s*\S+/(\S+)|) {
91 my $old = "$opts{man1dir}/$leaf.$opts{man1ext}";
92 my $new = "$opts{man1dir}/$link.$opts{man1ext}";
bf1ee2ba
JH
93 unlink($new);
94 link($old, $new);
3301e5d1
NC
95 $old =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
96 $new =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
97 $packlist->{$new} = { from => $old, type => 'link' };
bf1ee2ba 98 }
21dae8a0
SC
99 }
100}
3301e5d1 101close $fh or my_die("close 'utils.lst': $!");
1fef88e7 102
a2743834 103sub pod2man {
45a365ec 104 my($modpods, $mandir, $manext, $where) = @_;
16d20bd9 105 if ($mandir eq ' ' or $mandir eq '') {
45a365ec
NC
106 if ($where) {
107 warn "Skipping installation of $where man pages.\n"
108 } else {
b3e335c2 109 warn "Skipping installation of $_ man page.\n"
45a365ec
NC
110 foreach values %$modpods;
111 }
112 return;
16d20bd9
AD
113 }
114
b3e335c2 115 if ($opts{verbose}) {
45a365ec
NC
116 if ($where) {
117 print "installing from $where\n";
b3e335c2 118 } else {
45a365ec
NC
119 print "installing $_\n"
120 foreach sort keys %$modpods;
b3e335c2
NC
121 }
122 }
16d20bd9 123
a01f5661 124 mkpath($mandir);
b3e335c2 125
b3e335c2
NC
126 foreach my $manpage (sort keys %$modpods) {
127 my $mod = $modpods->{$manpage};
cd8bccb5 128
63fae907
AT
129 # Skip files without pod docs
130 my $has_pod;
3301e5d1
NC
131 my $fh = open_or_die($mod);
132 while (my $line = <$fh>) {
133 if ($line =~ /^=head1\b/) {
134 ++$has_pod;
135 last;
136 }
137 }
138 close $fh or my_die("close '$mod': $!");
139 # Sadly it doesn't seem possible to re-use this handle for the call
140 # to parse_from_file() below, as Pod::Man relies on source_filename(),
141 # which Pod::Simple only sets accurately if it opens the file itself.
63fae907
AT
142
143 unless ($has_pod)
144 {
7bc0dab7 145 print "no documentation in $mod\n" if $opts{verbose};
63fae907
AT
146 next;
147 }
148
4fabb596 149 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
107ea3f3 150 $manpage =~ s#::#.#g;
cd8bccb5 151 }
d0265671 152 my $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 153 $manpage = "${mandir}/${manpage}.${manext}";
a2743834 154
571afc3f
SP
155 my $parser = Pod::Man->new( section => $manext,
156 official=> 1,
157 center => 'Perl Programmers Reference Guide'
158 );
5a9231b0
MS
159 my $xmanpage = $manpage;
160 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
0f876784 161 print " $xmanpage\n" unless $opts{silent};
d0265671 162 if (!$opts{notify} && $parser->parse_from_file($mod, $tmp)) {
a2743834 163 if (-s $tmp) {
02ced026
AC
164 # We've already emitted a progress message for this file, if
165 # needed, so ensure safe_rename() doesn't emit another one.
166 local $opts{silent} = 1;
4c432614 167 if (safe_rename($tmp, $manpage)) {
5a9231b0 168 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
169 next;
170 }
171 }
172 unlink($tmp);
6d64b06f 173 }
16d20bd9 174 }
16d20bd9
AD
175}
176
f0512cd0
DC
177$packlist->write() unless $opts{notify};
178print " Installation complete\n" if $opts{verbose};
16d20bd9 179
5a6ebf5f 180# ex: set ts=8 sts=4 sw=4 et: