This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5db.pl [Was: Re: Debugger question]
[perl5.git] / installman
1 #!./perl -w
2 BEGIN { @INC = ('lib') }
3 use strict;
4 use Config;
5 use Getopt::Long;
6 use File::Find;
7 use File::Copy;
8 use File::Path qw(mkpath);
9 use ExtUtils::Packlist;
10 use subs qw(unlink chmod rename link);
11 use vars qw($packlist @modpods);
12 require Cwd;
13
14 $ENV{SHELL} = 'sh' if $^O eq 'os2';
15
16 my $ver = $Config{version};     # Not used presently.
17 my $release = substr($],0,3);   # Not used presently.
18 my $patchlevel = substr($],3,2);
19 die "Patchlevel of perl ($patchlevel)",
20     "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
21         if $patchlevel != $Config{'PERL_VERSION'};
22
23 my $usage =
24 "Usage:  installman --man1dir=/usr/wherever --man1ext=1
25                    --man3dir=/usr/wherever --man3ext=3
26                    --notify --verbose --silent --help
27         Defaults are:
28         man1dir = $Config{'installman1dir'};
29         man1ext = $Config{'man1ext'};
30         man3dir = $Config{'installman3dir'};
31         man3ext = $Config{'man3ext'};
32         --notify  (or -n) just lists commands that would be executed.
33         --verbose (or -V) report all progress.
34         --silent  (or -S) be silent. Only report errors.\n";
35
36 my %opts;
37 GetOptions( \%opts,
38             qw( man1dir=s man1ext=s man3dir=s man3ext=s
39                 notify n help silent S verbose V)) 
40         || die $usage;
41 die $usage if $opts{help};
42
43 $opts{man1dir} = $Config{'installman1dir'}
44     unless defined($opts{man1dir}); 
45 $opts{man1ext} = $Config{'man1ext'}
46     unless defined($opts{man1ext}); 
47 $opts{man3dir} = $Config{'installman3dir'}
48     unless defined($opts{man3dir}); 
49 $opts{man3ext} = $Config{'man3ext'}
50     unless defined($opts{man3ext}); 
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  $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("$Config{installarchlib}/.packlist");
66
67 # Install the main pod pages.
68 runpod2man('pod', $opts{man1dir}, $opts{man1ext});
69
70 # Install the pods for library modules.
71 runpod2man('lib', $opts{man3dir}, $opts{man3ext});
72
73 # Install the pods embedded in the installed scripts
74 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'c2ph');
75 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'h2ph');
76 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'h2xs');
77 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perlcc');
78 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perldoc');
79 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'perlbug');
80 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'pl2pm');
81 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'splain');
82 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'dprofpp');
83 runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 's2p');
84 runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 'a2p.pod');
85 runpod2man('x2p', $opts{man1dir}, $opts{man1ext}, 'find2perl');
86 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2man');
87 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2html');
88 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2text');
89 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'pod2usage');
90 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'podchecker');
91 runpod2man('pod', $opts{man1dir}, $opts{man1ext}, 'podselect');
92
93 # It would probably be better to have this page linked
94 # to the c2ph man page.  Or, this one could say ".so man1/c2ph.1",
95 # but then it would have to pay attention to $opts{man1dir} and $opts{man1ext}.
96 runpod2man('utils', $opts{man1dir}, $opts{man1ext}, 'pstruct'); 
97
98 runpod2man('lib/ExtUtils', $opts{man1dir}, $opts{man1ext}, 'xsubpp');
99
100 sub runpod2man {
101     # $script is script name if we are installing a manpage embedded 
102     # in a script, undef otherwise
103     my($poddir, $mandir, $manext, $script) = @_;
104
105     my($downdir); # can't just use .. when installing xsubpp manpage
106
107     $downdir = $poddir;
108     $downdir =~ s:[^/]+:..:g;
109     my($builddir) = Cwd::getcwd();
110
111     if ($mandir eq ' ' or $mandir eq '') {
112         warn "Skipping installation of ",
113             ($script ? "$poddir/$script man page" : "$poddir man pages"), ".\n";
114         return;
115     }
116
117     print "chdir $poddir\n" if $opts{verbose};
118     chdir $poddir || die "Unable to cd to $poddir directory!\n$!\n";
119
120     # We insist on using the current version of pod2man in case there
121     # are enhancements or changes from previous installed versions.
122     # The error message doesn't include the '..' because the user
123     # won't be aware that we've chdir to $poddir.
124     -r  "$downdir/pod/pod2man" || die "Executable pod/pod2man not found.\n";
125
126     # We want to be sure to use the current perl.  We can't rely on
127     # the installed perl because it might not be actually installed
128     # yet. (The user may have set the $install* Configure variables 
129     # to point to some temporary home, from which the executable gets
130     # installed by occult means.)
131     my $pod2man = "$downdir/perl -I $downdir/lib $downdir/pod/pod2man --section=$manext --official";
132
133     mkpath($mandir, $opts{verbose}, 0777) unless $opts{notify};  # In File::Path
134     # Make a list of all the .pm and .pod files in the directory.  We will
135     # always run pod2man from the lib directory and feed it the full pathname
136     # of the pod.  This might be useful for pod2man someday.
137     if ($script) {
138         @modpods = ($script);
139     }
140     else {
141         @modpods = ();
142         File::Find::find(\&lsmodpods, '.');
143     }
144     foreach my $mod (@modpods) {
145         my $manpage = $mod;
146         my $tmp;
147         # Skip .pm files that have corresponding .pod files, and Functions.pm.
148         next if (($tmp = $mod) =~ s/\.pm$/.pod/ && -f $tmp);
149         next if ($mod eq 'Pod/Functions.pm');   #### Used only by pod itself
150
151         # Convert name from  File/Basename.pm to File::Basename.3 format,
152         # if necessary.
153         $manpage =~ s#\.p(m|od)$##;
154         if ($^O eq 'os2' || $^O eq 'amigaos' || $^O eq 'uwin' || $^O eq 'cygwin') {
155           $manpage =~ s#/#.#g;
156         }
157         else {
158           $manpage =~ s#/#::#g;
159         }
160         $tmp = "${mandir}/${manpage}.tmp";
161         $manpage = "${mandir}/${manpage}.${manext}";
162         if (&cmd("$pod2man $mod > $tmp") == 0 && !$opts{notify} && -s $tmp) {
163             if (rename($tmp, $manpage)) {
164                 $packlist->{$manpage} = { type => 'file' };
165                 next;
166             }
167         }
168         unless ($opts{notify}) {
169             unlink($tmp);
170         }
171     }
172     chdir "$builddir" || die "Unable to cd back to $builddir directory!\n$!\n";
173     print "  chdir $builddir\n" if $opts{verbose};
174 }
175
176 sub lsmodpods {
177     my $dir  = $File::Find::dir;
178     my $name = $File::Find::name;
179     if (-f $_) {
180         $name =~ s#^\./##;
181         push(@modpods, $name) if ($name =~ /\.p(m|od)$/);
182     }
183 }
184
185 $packlist->write() unless $opts{notify};
186 print "  Installation complete\n" if $opts{verbose};
187
188 exit 0;
189     
190
191 ###############################################################################
192 # Utility subroutines from installperl
193
194 sub cmd {
195     my ($cmd) = @_;
196     print "  $cmd\n" if $opts{verbose};
197     unless ($opts{notify}) {
198         if ($Config{d_fork}) {
199             fork ? wait : exec $cmd;  # Allow user to ^C out of command.
200         }
201         else {
202             system $cmd;
203         }
204         warn "Command failed!!\n" if $?;
205     }
206     return $? != 0;
207 }
208
209 sub unlink {
210     my(@names) = @_;
211     my $cnt = 0;
212
213     foreach my $name (@names) {
214         next unless -e $name;
215         chmod 0777, $name if $^O eq 'os2';
216         print "  unlink $name\n" if $opts{verbose};
217         ( CORE::unlink($name) and ++$cnt 
218             or warn "Couldn't unlink $name: $!\n" ) unless $opts{notify};
219     }
220     return $cnt;
221 }
222
223 sub link {
224     my($from,$to) = @_;
225     my($success) = 0;
226
227     print $opts{verbose} ? "  ln $from $to\n" : "  $to\n" unless $opts{silent};
228     eval {
229         CORE::link($from, $to)
230             ? $success++
231             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
232               ? die "AFS"  # okay inside eval {}
233               : warn "Couldn't link $from to $to: $!\n"
234           unless $opts{notify};
235     };
236     if ($@) {
237         File::Copy::copy($from, $to)
238             ? $success++
239             : warn "Couldn't copy $from to $to: $!\n"
240           unless $opts{notify};
241     }
242     $success;
243 }
244
245 sub rename {
246     my($from,$to) = @_;
247     if (-f $to and not unlink($to)) {
248         my($i);
249         for ($i = 1; $i < 50; $i++) {
250             last if CORE::rename($to, "$to.$i");
251         }
252         warn("Cannot rename to `$to.$i': $!"), return 0 
253             if $i >= 50;        # Give up!
254     }
255     link($from,$to) || return 0;
256     unlink($from);
257 }
258
259 sub chmod {
260     my($mode,$name) = @_;
261
262     printf "  chmod %o %s\n", $mode, $name if $opts{verbose};
263     CORE::chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
264         unless $opts{notify};
265 }
266
267 sub samepath {
268     my($p1, $p2) = @_;
269     my($dev1, $ino1, $dev2, $ino2);
270
271     if ($p1 ne $p2) {
272         ($dev1, $ino1) = stat($p1);
273         ($dev2, $ino2) = stat($p2);
274         ($dev1 == $dev2 && $ino1 == $ino2);
275     }
276     else {
277         1;
278     }
279 }