This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In pod2man() in installman, transpose the keys and values of %modpods.
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
be61d08e 2BEGIN {
9e6fc21f 3 @INC = qw(lib);
ae5391ad 4
9e6fc21f
NC
5 # This needs to be at BEGIN time, before any use of Config
6 require './install_lib.pl';
be61d08e 7}
9e6fc21f 8use strict;
be61d08e 9
16d20bd9
AD
10use Getopt::Long;
11use File::Find;
354f3b56 12use File::Copy;
356fc125 13use File::Path qw(mkpath);
354f3b56 14use ExtUtils::Packlist;
a2743834 15use Pod::Man;
9e6fc21f
NC
16use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $Is_Darwin $Is_NetWare
17 %opts $packlist);
791b4ad3 18
cd8bccb5 19$ENV{SHELL} = 'sh' if $^O eq 'os2';
16d20bd9 20
f0512cd0 21my $patchlevel = substr($],3,2);
16d20bd9 22die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
23 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
24 if $patchlevel != $Config{'PERL_VERSION'};
16d20bd9 25
f0512cd0 26my $usage =
16d20bd9 27"Usage: installman --man1dir=/usr/wherever --man1ext=1
b5f05010 28 --man3dir=/usr/wherever --man3ext=3
f1745d4f 29 --batchlimit=40
eabd589f 30 --notify --verbose --silent --help
16d20bd9
AD
31 Defaults are:
32 man1dir = $Config{'installman1dir'};
33 man1ext = $Config{'man1ext'};
34 man3dir = $Config{'installman3dir'};
35 man3ext = $Config{'man3ext'};
eabd589f
A
36 --notify (or -n) just lists commands that would be executed.
37 --verbose (or -V) report all progress.
38 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 39
f0512cd0 40GetOptions( \%opts,
f1745d4f 41 qw( man1dir=s man1ext=s man3dir=s man3ext=s batchlimit=i
ae5391ad 42 destdir:s notify n help silent S verbose V))
16d20bd9 43 || die $usage;
f0512cd0 44die $usage if $opts{help};
02bc0c09 45$opts{destdir} //= '';
16d20bd9 46
3cc5758c
NC
47foreach my $pre (qw(man1 man3)) {
48 $opts{"${pre}dir"} //= $opts{destdir} . $Config{"install${pre}dir"};
49 $opts{"${pre}ext"} //= $Config{"${pre}ext"};
50}
f0512cd0 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
ae5391ad 57-x "./perl$Config{exe_ext}"
cd8bccb5 58 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 59-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
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
1ef57a5c 65$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 66
ae5391ad
FR
67# manpages not to be installed
68my %do_not_install = map { ($_ => 1) } qw(
69 Pod/Functions.pm
70 XS/APItest.pm
cf34e658 71 XS/Typemap.pm
ae5391ad 72);
21dae8a0 73
16d20bd9 74# Install the main pod pages.
a2743834 75pod2man('pod', $opts{man1dir}, $opts{man1ext});
16d20bd9
AD
76
77# Install the pods for library modules.
a2743834 78pod2man('lib', $opts{man3dir}, $opts{man3ext});
16d20bd9 79
1fef88e7 80# Install the pods embedded in the installed scripts
bf1ee2ba 81my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
21dae8a0
SC
82open UTILS, "utils.lst" or die "Can't open 'utils.lst': $!";
83while (<UTILS>) {
84 next if /^#/;
85 chomp;
86 $_ = $1 if /#.*pod\s*=\s*(\S+)/;
53fb3a93 87 my ($where, $what) = m|^(\S*)/(\S+)|;
a2743834 88 pod2man($where, $opts{man1dir}, $opts{man1ext}, $what);
bf1ee2ba
JH
89 if ($has_man1dir) {
90 if (my ($where2, $what2) = m|#.*link\s*=\s*(\S+)/(\S+)|) {
91 my $old = "$opts{man1dir}/$what.$opts{man1ext}";
92 my $new = "$opts{man1dir}/$what2.$opts{man1ext}";
93 unlink($new);
94 link($old, $new);
25207203 95 my $xold = $old;
96 $xold =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
97 my $xnew = $new;
98 $xnew =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
99 $packlist->{$xnew} = { from => $xold, type => 'link' };
bf1ee2ba 100 }
21dae8a0
SC
101 }
102}
1fef88e7 103
a2743834 104sub pod2man {
ae5391ad 105 # @script is scripts names if we are installing manpages embedded
f1745d4f
JH
106 # in scripts, () otherwise
107 my($poddir, $mandir, $manext, @script) = @_;
16d20bd9 108 if ($mandir eq ' ' or $mandir eq '') {
f1745d4f
JH
109 if (@script) {
110 warn "Skipping installation of $poddir/$_ man page.\n"
111 foreach @script;
112 } else {
113 warn "Skipping installation of $poddir man pages.\n";
114 }
16d20bd9
AD
115 return;
116 }
117
b48e406f 118 print "installing from $poddir\n" if $opts{verbose};
16d20bd9 119
418918ac 120 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
b48e406f
NC
121 # Make a list of all the .pm and .pod files in the directory. We avoid
122 # chdir because we are running with @INC = '../lib', and modules may wish
123 # to dynamically require Carp::Heavy or other diagnostics warnings.
124 # Hash the names of files we find, keys are names relative to perl build
125 # dir ('.'), values are names relative to $poddir.
126 my %modpods;
f1745d4f 127 if (@script) {
a73d7a24 128 %modpods = (map {$_, "$poddir/$_"} @script);
72b3d9b4
GS
129 }
130 else {
b48e406f
NC
131 File::Find::find({no_chdir=>1,
132 wanted => sub {
133 # $_ is $File::Find::name when using no_chdir
134 if (-f $_ and /\.p(?:m|od)$/) {
135 my $fullname = $_;
136 s!^\Q$poddir\E/!!;
0beff067
RGS
137 # perlfaq manpages are installed in section 1,
138 # so skip when searching files for section 3
139 return if m(perlfaq.?\.pod|perlglossary.pod);
a73d7a24 140 $modpods{$_} = $fullname;
b48e406f
NC
141 }
142 }},
143 $poddir);
1fef88e7 144 }
f1745d4f 145 my @to_process;
a73d7a24
NC
146 foreach my $manpage (sort keys %modpods) {
147 my $mod = $modpods{$manpage};
cd8bccb5
PP
148 my $tmp;
149 # Skip .pm files that have corresponding .pod files, and Functions.pm.
150 next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
ae5391ad
FR
151 next if $mod =~ m:/t/:; # no pods from test directories
152 next if $do_not_install{$manpage};
cd8bccb5 153
63fae907
AT
154 # Skip files without pod docs
155 my $has_pod;
156 if (open T, $mod)
157 {
158 local $_;
159 while (<T>)
160 {
5a6ebf5f 161 ++$has_pod and last if /^=head1\b/;
63fae907
AT
162 }
163
164 close T;
165 }
166
167 unless ($has_pod)
168 {
169 warn "no documentation in $mod\n";
170 next;
171 }
172
16d20bd9
AD
173 # Convert name from File/Basename.pm to File::Basename.3 format,
174 # if necessary.
175 $manpage =~ s#\.p(m|od)$##;
4fabb596 176 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 177 $manpage =~ s#/#.#g;
72b3d9b4
GS
178 }
179 else {
cd8bccb5
PP
180 $manpage =~ s#/#::#g;
181 }
182 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 183 $manpage = "${mandir}/${manpage}.${manext}";
f1745d4f
JH
184 push @to_process, [$mod, $tmp, $manpage];
185 }
a2743834 186
a2743834
MS
187 foreach my $page (@to_process) {
188 my($pod, $tmp, $manpage) = @$page;
189
571afc3f
SP
190 my $parser = Pod::Man->new( section => $manext,
191 official=> 1,
192 center => 'Perl Programmers Reference Guide'
193 );
5a9231b0
MS
194 my $xmanpage = $manpage;
195 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
196 print " $xmanpage\n";
a2743834
MS
197 if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
198 if (-s $tmp) {
199 if (rename($tmp, $manpage)) {
5a9231b0 200 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
201 next;
202 }
203 }
204 unlink($tmp);
6d64b06f 205 }
16d20bd9 206 }
16d20bd9
AD
207}
208
f0512cd0
DC
209$packlist->write() unless $opts{notify};
210print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
211
212exit 0;
16d20bd9 213
cd8bccb5 214sub rename {
f0512cd0 215 my($from,$to) = @_;
55a105fd 216 if (-f $to and not unlink($to)) {
72b3d9b4
GS
217 my($i);
218 for ($i = 1; $i < 50; $i++) {
219 last if CORE::rename($to, "$to.$i");
220 }
19f4563d 221 warn("Cannot rename to '$to.$i': $!"), return 0
72b3d9b4 222 if $i >= 50; # Give up!
cd8bccb5 223 }
55a105fd 224 link($from,$to) || return 0;
225 unlink($from);
16d20bd9 226}
5a6ebf5f
NC
227
228# Local variables:
229# cperl-indent-level: 4
230# indent-tabs-mode: nil
231# End:
232#
233# ex: set ts=8 sts=4 sw=4 et: