This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
4f9d469f6b04c0a5f791224cf59cafdb11da027e
[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 require File::Path;
12 use ExtUtils::Packlist;
13 use Pod::Man;
14 use vars qw(%opts $packlist);
15
16 require './Porting/pod_lib.pl';
17 my %man1 = (map {($_->[0], $_->[1])} @{get_pod_metadata()->{master}});
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                    --notify --verbose --silent --help
30         Defaults are:
31         man1dir = $Config{'installman1dir'};
32         man1ext = $Config{'man1ext'};
33         man3dir = $Config{'installman3dir'};
34         man3ext = $Config{'man3ext'};
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";
38
39 # --strip intentionally does nothing. By permitting installman to accept it
40 # without error, the Makefile can pass the same options to installperl and
41 # installman, which permits more simplification there than this comment costs.
42 GetOptions( \%opts,
43             qw( man1dir=s man1ext=s man3dir=s man3ext=s
44                 destdir:s notify|n help|h|? silent|S verbose|V strip))
45         || die $usage;
46 die $usage if $opts{help};
47 $opts{destdir} //= '';
48
49 foreach my $pre (qw(man1 man3)) {
50     $opts{"${pre}dir"} //= $opts{destdir} . $Config{"install${pre}dir"};
51     $opts{"${pre}ext"} //= $Config{"${pre}ext"};
52 }
53 $opts{verbose} ||= $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 # Install the main pod pages.
68 pod2man(\%man1, $opts{man1dir}, $opts{man1ext}, 'pod');
69
70 # Install the pods for library modules.
71 {
72     my $found = pods_to_install();
73     pod2man($found->{$_}, $opts{man3dir}, $opts{man3ext}, 'lib')
74         foreach qw(MODULE PRAGMA);
75 }
76
77 # Install the pods embedded in the installed scripts
78 my $has_man1dir = $opts{man1dir} ne '' && -d $opts{man1dir};
79 my $fh = open_or_die('utils.lst');
80 while (<$fh>) {
81     next if /^#/;
82     chomp;
83     my ($path, $leaf) = m|^(\S*/(\S+))|;
84     # Have we already installed the manpage for this? (eg perldoc, a2p)
85     next if $man1{$leaf};
86     pod2man({$leaf, $path}, $opts{man1dir}, $opts{man1ext});
87     if ($has_man1dir) {
88         if (my ($link) = m|#.*link\s*=\s*\S+/(\S+)|) {
89             my $old = "$opts{man1dir}/$leaf.$opts{man1ext}";
90             my $new = "$opts{man1dir}/$link.$opts{man1ext}";
91             unlink($new);
92             link($old, $new);
93             $old =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
94             $new =~ s/^\Q$opts{destdir}\E// if $opts{destdir};
95             $packlist->{$new} = { from => $old, type => 'link' };
96         }
97     }
98 }
99 close $fh or my_die("close 'utils.lst': $!");
100
101 sub pod2man {
102     my($modpods, $mandir, $manext, $where) = @_;
103     if ($mandir eq ' ' or $mandir eq '') {
104         if ($where) {
105             warn "Skipping installation of $where man pages.\n"
106         } else {
107             warn "Skipping installation of $_ man page.\n"
108                 foreach values %$modpods;
109         }
110         return;
111     }
112
113     if ($opts{verbose}) {
114         if ($where) {
115             print "installing from $where\n";
116         } else {
117             print "installing $_\n"
118                 foreach sort keys %$modpods;
119         }
120     }
121
122     File::Path::mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify};
123
124     foreach my $manpage (sort keys %$modpods) {
125         my $mod = $modpods->{$manpage};
126
127         # Skip files without pod docs
128         my $has_pod;
129         my $fh = open_or_die($mod);
130         while (my $line = <$fh>) {
131             if ($line =~ /^=head1\b/) {
132                 ++$has_pod;
133                 last;
134             }
135         }
136         close $fh or my_die("close '$mod': $!");
137         # Sadly it doesn't seem possible to re-use this handle for the call
138         # to parse_from_file() below, as Pod::Man relies on source_filename(),
139         # which Pod::Simple only sets accurately if it opens the file itself.
140
141         unless ($has_pod)
142         {
143             warn "no documentation in $mod\n" unless $opts{silent};
144             next;
145         }
146
147         if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
148             $manpage =~ s#::#.#g;
149         }
150         my $tmp = "${mandir}/${manpage}.tmp";
151         $manpage = "${mandir}/${manpage}.${manext}";
152
153         my $parser = Pod::Man->new( section => $manext,
154                                     official=> 1,
155                                     center  => 'Perl Programmers Reference Guide'
156                                   );
157         my $xmanpage = $manpage;
158         $xmanpage =~ s/^\Q$opts{'destdir'}\E// if $opts{'destdir'};
159         print "  $xmanpage\n" unless $opts{silent};
160         if (!$opts{notify} && $parser->parse_from_file($mod, $tmp)) {
161             if (-s $tmp) {
162                 if (rename($tmp, $manpage)) {
163                     $packlist->{$xmanpage} = { type => 'file' };
164                     next;
165                 }
166             }
167             unlink($tmp);
168         }
169     }
170 }
171
172 $packlist->write() unless $opts{notify};
173 print "  Installation complete\n" if $opts{verbose};
174
175 sub rename {
176     my($from,$to) = @_;
177     if (-f $to and not unlink($to)) {
178         my($i);
179         for ($i = 1; $i < 50; $i++) {
180             last if CORE::rename($to, "$to.$i");
181         }
182         warn("Cannot rename to '$to.$i': $!"), return 0
183             if $i >= 50;        # Give up!
184     }
185     link($from,$to) || return 0;
186     unlink($from);
187 }
188
189 # Local variables:
190 # cperl-indent-level: 4
191 # indent-tabs-mode: nil
192 # End:
193 #
194 # ex: set ts=8 sts=4 sw=4 et: