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