This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move the common Pod scanning code from installman and buildtoc to pod_lib.pl
[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 10use Getopt::Long;
356fc125 11use File::Path qw(mkpath);
354f3b56 12use ExtUtils::Packlist;
a2743834 13use Pod::Man;
d0265671 14use vars qw(%opts $packlist);
791b4ad3 15
d6a39ee2
NC
16require './Porting/pod_lib.pl';
17my $state = get_pod_metadata();
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
eabd589f 29 --notify --verbose --silent --help
16d20bd9
AD
30 Defaults are:
31 man1dir = $Config{'installman1dir'};
32 man1ext = $Config{'man1ext'};
33 man3dir = $Config{'installman3dir'};
34 man3ext = $Config{'man3ext'};
eabd589f
A
35 --notify (or -n) just lists commands that would be executed.
36 --verbose (or -V) report all progress.
37 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 38
f0512cd0 39GetOptions( \%opts,
d0265671 40 qw( man1dir=s man1ext=s man3dir=s man3ext=s
ae5391ad 41 destdir:s notify n help silent S verbose V))
16d20bd9 42 || die $usage;
f0512cd0 43die $usage if $opts{help};
02bc0c09 44$opts{destdir} //= '';
16d20bd9 45
3cc5758c
NC
46foreach my $pre (qw(man1 man3)) {
47 $opts{"${pre}dir"} //= $opts{destdir} . $Config{"install${pre}dir"};
48 $opts{"${pre}ext"} //= $Config{"${pre}ext"};
49}
f0512cd0 50$opts{silent} ||= $opts{S};
f0512cd0 51$opts{notify} ||= $opts{n};
4ad019ef 52$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9
AD
53
54#Sanity checks
55
ae5391ad 56-x "./perl$Config{exe_ext}"
cd8bccb5 57 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 58-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
59 || warn "Perl library directory $Config{'installprivlib'} not found.
60 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 61-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
62 " (Installing anyway.)\n";
63
1ef57a5c 64$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 65
16d20bd9 66# Install the main pod pages.
d6a39ee2
NC
67pod2man({
68 map {
2ce3647c 69 ($_->[0], $_->[1])
d6a39ee2 70 } @{$state->{master}}
45a365ec 71 }, $opts{man1dir}, $opts{man1ext}, 'pod');
16d20bd9
AD
72
73# Install the pods for library modules.
45a365ec 74{
9bbb230a
NC
75 my $found = pods_to_install();
76 pod2man($found->{$_}, $opts{man3dir}, $opts{man3ext}, 'lib')
77 foreach qw(MODULE PRAGMA);
45a365ec 78}
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+)/;
b3e335c2 87 my ($path, $leaf) = m|^(\S*/(\S+))|;
107ea3f3 88 $leaf =~ s/\.pod\z//;
b3e335c2 89 pod2man({$leaf, $path}, $opts{man1dir}, $opts{man1ext});
bf1ee2ba 90 if ($has_man1dir) {
b3e335c2
NC
91 if (my ($link) = m|#.*link\s*=\s*\S+/(\S+)|) {
92 my $old = "$opts{man1dir}/$leaf.$opts{man1ext}";
93 my $new = "$opts{man1dir}/$link.$opts{man1ext}";
bf1ee2ba
JH
94 unlink($new);
95 link($old, $new);
25207203 96 my $xold = $old;
97 $xold =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
98 my $xnew = $new;
99 $xnew =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
100 $packlist->{$xnew} = { from => $xold, type => 'link' };
bf1ee2ba 101 }
21dae8a0
SC
102 }
103}
1fef88e7 104
a2743834 105sub pod2man {
45a365ec 106 my($modpods, $mandir, $manext, $where) = @_;
16d20bd9 107 if ($mandir eq ' ' or $mandir eq '') {
45a365ec
NC
108 if ($where) {
109 warn "Skipping installation of $where man pages.\n"
110 } else {
b3e335c2 111 warn "Skipping installation of $_ man page.\n"
45a365ec
NC
112 foreach values %$modpods;
113 }
114 return;
16d20bd9
AD
115 }
116
b3e335c2 117 if ($opts{verbose}) {
45a365ec
NC
118 if ($where) {
119 print "installing from $where\n";
b3e335c2 120 } else {
45a365ec
NC
121 print "installing $_\n"
122 foreach sort keys %$modpods;
b3e335c2
NC
123 }
124 }
16d20bd9 125
418918ac 126 mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify}; # In File::Path
b3e335c2 127
b3e335c2
NC
128 foreach my $manpage (sort keys %$modpods) {
129 my $mod = $modpods->{$manpage};
cd8bccb5 130
63fae907
AT
131 # Skip files without pod docs
132 my $has_pod;
133 if (open T, $mod)
134 {
135 local $_;
136 while (<T>)
137 {
5a6ebf5f 138 ++$has_pod and last if /^=head1\b/;
63fae907
AT
139 }
140
141 close T;
142 }
143
144 unless ($has_pod)
145 {
146 warn "no documentation in $mod\n";
147 next;
148 }
149
4fabb596 150 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
107ea3f3 151 $manpage =~ s#::#.#g;
cd8bccb5 152 }
d0265671 153 my $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 154 $manpage = "${mandir}/${manpage}.${manext}";
a2743834 155
571afc3f
SP
156 my $parser = Pod::Man->new( section => $manext,
157 official=> 1,
158 center => 'Perl Programmers Reference Guide'
159 );
5a9231b0
MS
160 my $xmanpage = $manpage;
161 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
162 print " $xmanpage\n";
d0265671 163 if (!$opts{notify} && $parser->parse_from_file($mod, $tmp)) {
a2743834
MS
164 if (-s $tmp) {
165 if (rename($tmp, $manpage)) {
5a9231b0 166 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
167 next;
168 }
169 }
170 unlink($tmp);
6d64b06f 171 }
16d20bd9 172 }
16d20bd9
AD
173}
174
f0512cd0
DC
175$packlist->write() unless $opts{notify};
176print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
177
178exit 0;
16d20bd9 179
cd8bccb5 180sub rename {
f0512cd0 181 my($from,$to) = @_;
55a105fd 182 if (-f $to and not unlink($to)) {
72b3d9b4
GS
183 my($i);
184 for ($i = 1; $i < 50; $i++) {
185 last if CORE::rename($to, "$to.$i");
186 }
19f4563d 187 warn("Cannot rename to '$to.$i': $!"), return 0
72b3d9b4 188 if $i >= 50; # Give up!
cd8bccb5 189 }
55a105fd 190 link($from,$to) || return 0;
191 unlink($from);
16d20bd9 192}
5a6ebf5f
NC
193
194# Local variables:
195# cperl-indent-level: 4
196# indent-tabs-mode: nil
197# End:
198#
199# ex: set ts=8 sts=4 sw=4 et: