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