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