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