This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge common code from installperl and installman into install_lib.pl
[perl5.git] / installman
CommitLineData
f0512cd0 1#!./perl -w
be61d08e 2BEGIN {
9e6fc21f
NC
3 @INC = qw(lib);
4
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
DC
21my $ver = $Config{version}; # Not used presently.
22my $release = substr($],0,3); # Not used presently.
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
f1745d4f 31 --batchlimit=40
eabd589f 32 --notify --verbose --silent --help
16d20bd9
AD
33 Defaults are:
34 man1dir = $Config{'installman1dir'};
35 man1ext = $Config{'man1ext'};
36 man3dir = $Config{'installman3dir'};
37 man3ext = $Config{'man3ext'};
eabd589f
A
38 --notify (or -n) just lists commands that would be executed.
39 --verbose (or -V) report all progress.
40 --silent (or -S) be silent. Only report errors.\n";
16d20bd9 41
f0512cd0 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};
02bc0c09 47$opts{destdir} //= '';
16d20bd9 48
5a9231b0 49$opts{man1dir} = "$opts{destdir}$Config{'installman1dir'}"
f0512cd0
DC
50 unless defined($opts{man1dir});
51$opts{man1ext} = $Config{'man1ext'}
52 unless defined($opts{man1ext});
5a9231b0 53$opts{man3dir} = "$opts{destdir}$Config{'installman3dir'}"
f0512cd0
DC
54 unless defined($opts{man3dir});
55$opts{man3ext} = $Config{'man3ext'}
56 unless defined($opts{man3ext});
57$opts{silent} ||= $opts{S};
f0512cd0 58$opts{notify} ||= $opts{n};
4ad019ef 59$opts{verbose} ||= $opts{V} || $opts{notify};
16d20bd9
AD
60
61#Sanity checks
62
cd8bccb5
PP
63-x "./perl$Config{exe_ext}"
64 or warn "./perl$Config{exe_ext} not found! Have you run make?\n";
5a9231b0 65-d "$opts{destdir}$Config{'installprivlib'}"
16d20bd9
AD
66 || warn "Perl library directory $Config{'installprivlib'} not found.
67 Have you run make install?. (Installing anyway.)\n";
cd8bccb5 68-x "t/perl$Config{exe_ext}" || warn "WARNING: You've never run 'make test'!!!",
16d20bd9
AD
69 " (Installing anyway.)\n";
70
1ef57a5c 71$packlist = ExtUtils::Packlist->new("$opts{destdir}$Config{installarchlib}/.packlist");
354f3b56 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 {
f1745d4f
JH
105 # @script is scripts names if we are installing manpages embedded
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) {
b48e406f 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/!!;
137 $modpods{$fullname} = $_;
138 }
139 }},
140 $poddir);
1fef88e7 141 }
f1745d4f 142 my @to_process;
b48e406f
NC
143 foreach my $mod (sort keys %modpods) {
144 my $manpage = $modpods{$mod};
cd8bccb5
PP
145 my $tmp;
146 # Skip .pm files that have corresponding .pod files, and Functions.pm.
147 next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
732876f5 148 next if $mod =~ m:/t/:; # no pods from test directories
b48e406f 149 next if ($manpage eq 'Pod/Functions.pm'); #### Used only by pod itself
cd8bccb5 150
63fae907
AT
151 # Skip files without pod docs
152 my $has_pod;
153 if (open T, $mod)
154 {
155 local $_;
156 while (<T>)
157 {
158 ++$has_pod and last if /^=(?:head\d+|item|pod)\b/;
159 }
160
161 close T;
162 }
163
164 unless ($has_pod)
165 {
166 warn "no documentation in $mod\n";
167 next;
168 }
169
16d20bd9
AD
170 # Convert name from File/Basename.pm to File::Basename.3 format,
171 # if necessary.
172 $manpage =~ s#\.p(m|od)$##;
4fabb596 173 if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
cd8bccb5 174 $manpage =~ s#/#.#g;
72b3d9b4
GS
175 }
176 else {
cd8bccb5
PP
177 $manpage =~ s#/#::#g;
178 }
179 $tmp = "${mandir}/${manpage}.tmp";
16d20bd9 180 $manpage = "${mandir}/${manpage}.${manext}";
f1745d4f
JH
181 push @to_process, [$mod, $tmp, $manpage];
182 }
a2743834 183
a2743834
MS
184 foreach my $page (@to_process) {
185 my($pod, $tmp, $manpage) = @$page;
186
571afc3f
SP
187 my $parser = Pod::Man->new( section => $manext,
188 official=> 1,
189 center => 'Perl Programmers Reference Guide'
190 );
5a9231b0
MS
191 my $xmanpage = $manpage;
192 $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
193 print " $xmanpage\n";
a2743834
MS
194 if (!$opts{notify} && $parser->parse_from_file($pod, $tmp)) {
195 if (-s $tmp) {
196 if (rename($tmp, $manpage)) {
5a9231b0 197 $packlist->{$xmanpage} = { type => 'file' };
a2743834
MS
198 next;
199 }
200 }
201 unlink($tmp);
6d64b06f 202 }
16d20bd9 203 }
16d20bd9
AD
204}
205
f0512cd0
DC
206$packlist->write() unless $opts{notify};
207print " Installation complete\n" if $opts{verbose};
16d20bd9
AD
208
209exit 0;
16d20bd9 210
cd8bccb5 211sub rename {
f0512cd0 212 my($from,$to) = @_;
55a105fd 213 if (-f $to and not unlink($to)) {
72b3d9b4
GS
214 my($i);
215 for ($i = 1; $i < 50; $i++) {
216 last if CORE::rename($to, "$to.$i");
217 }
218 warn("Cannot rename to `$to.$i': $!"), return 0
219 if $i >= 50; # Give up!
cd8bccb5 220 }
55a105fd 221 link($from,$to) || return 0;
222 unlink($from);
16d20bd9 223}