Use fork if available.
[p5sagit/p5-mst-13.2.git] / installperl
1 #!./perl
2 BEGIN { @INC=('./lib', '../lib') }
3 use File::Find;
4 use Config;
5
6 $mainperldir = "/usr/bin";
7
8 while (@ARGV) {
9     $nonono = 1 if $ARGV[0] eq '-n';
10     $versiononly = 1 if $ARGV[0] eq '-v';
11     shift;
12 }
13
14 umask 022;
15
16 @scripts = ('cppstdin', 'c2ph', 'h2ph', 'h2xs', 'pstruct', 
17             'x2p/s2p', 'x2p/find2perl',
18             'perldoc',
19             'pod/pod2man', 'pod/pod2html', 'pod/pod2latex' );
20
21 # pod documentation now handled by separate installman script.
22 # These two are archaic leftovers.
23 @manpages = ('x2p/a2p.man', 'x2p/s2p.man');
24
25 @pods = (<pod/*.pod>);
26
27 $ver = $];
28 $release = substr($ver,0,3);   # Not used presently.
29 $patchlevel = substr($ver,3,2);
30 die "Patchlevel of perl ($patchlevel)",
31     "and patchlevel of config.sh ($Config{'PATCHLEVEL'}) don't match\n"
32         if $patchlevel != $Config{'PATCHLEVEL'};
33
34 # Fetch some frequently-used items from %Config
35 $installbin = $Config{installbin};
36 $installscript = $Config{installscript};
37 $installprivlib = $Config{installprivlib};
38 $installarchlib = $Config{installarchlib};
39 $installsitelib = $Config{installsitelib};
40 $installsitearch = $Config{installsitearch};
41 $installman1dir = $Config{installman1dir};
42 $man1ext = $Config{man1ext};
43 # Did we build libperl as a shared library?
44 $d_shrplib = $Config{d_shrplib};
45 $shrpdir = $Config{shrpdir};
46 # Shared library and dynamic loading suffixes.
47 $so = $Config{so};
48 $dlext = $Config{dlext};
49
50 $d_dosuid = $Config{d_dosuid};
51 $binexp = $Config{binexp};
52 $osname = $Config{osname};
53
54 # Do some quick sanity checks.
55
56 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
57
58    $installbin          || die "No installbin directory in config.sh\n";
59 -d $installbin          || makedir($installbin);
60 -d $installbin          || die "$installbin is not a directory\n";
61 -w $installbin          || die "$installbin is not writable by you\n"
62         unless $installbin =~ m#^/afs/# || $nonono;
63
64 -x 'perl'               || die "perl isn't executable!\n";
65 -x 'suidperl'           || die "suidperl isn't executable!\n" if $d_dosuid;
66
67 -x 't/TEST'             || warn "WARNING: You've never run 'make test'!!!",
68         "  (Installing anyway.)\n";
69
70 if ($d_shrplib) {
71     if (!<libperl*.$so*>) {
72         warn "WARNING: Can't find libperl*.$so* to install into $shrpdir.",
73             "  (Installing other things anyway.)\n";
74     } else {
75         &makedir($shrpdir);
76         -w $shrpdir     || die "$shrpdir is not writable by you\n";
77         &cmd("cp libperl*.$so* $shrpdir");
78     }
79 }
80
81 # First we install the version-numbered executables.
82
83 &safe_unlink("$installbin/perl$ver");
84 &cmd("cp perl $installbin/perl$ver");
85
86 &safe_unlink("$installbin/sperl$ver");
87 if ($d_dosuid) {
88     &cmd("cp suidperl $installbin/sperl$ver");
89     &chmod(04711, "$installbin/sperl$ver");
90 }
91
92 exit 0 if $versiononly;
93
94 # Make links to ordinary names if installbin directory isn't current directory.
95
96 if (! &samepath($installbin, '.')) {
97     &safe_unlink("$installbin/perl", "$installbin/suidperl");
98     &link("$installbin/perl$ver", "$installbin/perl");
99     &link("$installbin/sperl$ver", "$installbin/suidperl") if $d_dosuid;
100 }
101
102 if (! &samepath($installbin, 'x2p')) {
103     &safe_unlink("$installbin/a2p");
104     &cmd("cp x2p/a2p $installbin/a2p");
105     &chmod(0755, "$installbin/a2p");
106 }
107
108 # Install scripts.
109
110 &makedir($installscript);
111
112 for (@scripts) {
113     if (-f $_) {   # cppstdin might not exist on this system.
114         &cmd("cp $_ $installscript");
115         s#.*/##; &chmod(0755, "$installscript/$_");
116     }
117 }
118
119 # Install pod pages.  Where? I guess in $installprivlib/pod.
120 &makedir("${installprivlib}/pod");
121 foreach $file (@pods) {
122     # $file is a name like  pod/perl.pod
123     cp_if_diff($file, "${installprivlib}/${file}");
124 }
125
126 # Install old man pages.
127
128 if ($installman1dir ne '') {
129     &makedir($installman1dir);
130
131     if (! &samepath($installman1dir, '.')) {
132         for (@manpages) {
133             ($new = $_) =~ s/man$/$man1ext/;
134             $new =~ s#.*/##;
135             print STDERR "  Installing $installman1dir/$new\n";
136             next if $nonono;
137             open(MI,$_) || warn "Can't open $_: $!\n";
138             open(MO,">$installman1dir/$new") || 
139                     warn "Can't install $installman1dir/$new: $!\n";
140             print MO ".ds RP Release $release Patchlevel $patchlevel\n";
141             while (<MI>) {
142                 print MO;
143             }
144             close MI;
145             close MO;
146         }
147     }
148 }
149
150 # Install library files.
151
152 $do_installarchlib = $do_installprivlib = 0;
153     
154 &makedir($installprivlib);
155 &makedir($installarchlib);
156 &makedir($installsitelib) if ($installsitelib);
157 &makedir($installsitearch) if ($installsitearch);
158
159 if (chdir "lib") {
160     $do_installarchlib = ! &samepath($installarchlib, '.');
161     $do_installprivlib = ! &samepath($installprivlib, '.');
162
163     if ($do_installarchlib || $do_installprivlib) {
164         find(\&installlib, '.');
165     }
166     chdir ".." || die "Can't cd back to source directory: $!\n";
167 }
168 else {
169     warn "Can't cd to lib to install lib files: $!\n";
170 }
171
172 # Install header files and libraries.
173 makedir("$installarchlib/CORE");
174 foreach $file (<*.h libperl*.*>) {
175     cp_if_diff($file,"$installarchlib/CORE/$file");
176 }
177 # AIX needs perl.exp installed as well.
178 cp_if_diff("perl.exp" ,"$installarchlib/CORE/perl.exp") if ($osname eq 'aix');
179
180 # If they have built sperl.o...
181 cp_if_diff("sperl.o" ,"$installarchlib/CORE/sperl.o") if (-f 'sperl.o');
182
183
184 # Offer to install perl in a "standard" location
185
186 $mainperl_is_instperl = 0;
187
188 if (-w $mainperldir && ! &samepath($mainperldir, $installbin) && !$nonono) {
189     # First make sure $mainperldir/perl is not already the same as
190     # the perl we just installed
191     if (-x "$mainperldir/perl") {
192         # Try to be clever about mainperl being a symbolic link
193         # to binexp/perl if binexp and installbin are different.
194         $mainperl_is_instperl =
195             &samepath("$mainperldir/perl", "$installbin/perl") ||
196              (($binexp ne $installbin) &&
197               (-l "$mainperldir/perl") &&
198               ((readlink "$mainperldir/perl") eq "$binexp/perl"));
199     }
200     if ((! $mainperl_is_instperl) &&
201         (&yn("Many scripts expect perl to be installed as " .
202              "$mainperldir/perl.\n" . 
203              "Do you wish to have $mainperldir/perl be the same as\n" .
204              "$binexp/perl? [y] ")))
205     {   
206         unlink("$mainperldir/perl");
207         eval 'link("$installbin/perl", "$mainperldir/perl")' ||
208         eval 'symlink("$binexp/perl", "$mainperldir/perl")' ||
209         &cmd("cp $installbin/perl $mainperldir");
210         $mainperl_is_instperl = 1;
211     }
212 }
213
214 # Check to make sure there aren't other perls around in installer's
215 # path.  This is probably UNIX-specific.  Check all absolute directories
216 # in the path except for where public executables are supposed to live.
217 # Also skip $mainperl if the user opted to have it be a link to the
218 # installed perl.
219
220 @path = split(/:/, $ENV{"PATH"});
221 @otherperls = ();
222 for (@path) {
223     next unless m,^/,;
224     next if ($_ eq $binexp);
225     # Use &samepath here because some systems have other dirs linked
226     # to $mainperldir (like SunOS)
227     next if ($mainperl_is_instperl && &samepath($_, $mainperldir));
228     push(@otherperls, "$_/perl") if (-x "$_/perl" && ! -d "$_/perl");
229 }
230 if (@otherperls) {
231     print STDERR "\nWarning: perl appears in your path in the following " .
232         "locations beyond where\nwe just installed it:\n";
233     for (@otherperls) {
234         print STDERR "    ", $_, "\n";
235     }
236     print STDERR "\n";
237 }
238
239 print STDERR "  Installation complete\n";
240
241 exit 0;
242
243 ###############################################################################
244
245 sub yn {
246     local($prompt) = @_;
247     local($answer);
248     local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
249     print STDERR $prompt;
250     chop($answer = <STDIN>);
251     $answer = $default if $answer =~ m/^\s*$/;
252     ($answer =~ m/^[yY]/);
253 }
254
255 sub unlink {
256     local(@names) = @_;
257
258     foreach $name (@names) {
259         next unless -e $name;
260         print STDERR "  unlink $name\n";
261         unlink($name) || warn "Couldn't unlink $name: $!\n" unless $nonono;
262     }
263 }
264
265 sub safe_unlink {
266     local(@names) = @_;
267
268     foreach $name (@names) {
269         next unless -e $name;
270         print STDERR "  unlink $name\n";
271         next if $nonono;
272         next if unlink($name);
273         warn "Couldn't unlink $name: $!\n";
274         if ($! =~ /busy/i) {
275             print STDERR "  mv $name $name.old\n";
276             &rename($name, "$name.old") || warn "Couldn't rename $name: $!\n";
277         }
278     }
279 }
280
281 sub cmd {
282     local($cmd) = @_;
283     print STDERR "  $cmd\n";
284     unless ($nonono) {
285         system $cmd;
286         warn "Command failed!!!\n" if $?;
287     }
288 }
289
290 sub rename {
291     local($from,$to) = @_;
292     unless (unlink($to)) {
293         my($i);
294         for ($i = 1; $i < 50; $i++) {
295             last if rename($to, "$to.$i");
296         }
297         return 0 if $i >= 50;   # Give up!
298     }
299     link($from,$to) || return 0;
300     unlink($from);
301 }
302
303 sub link {
304     local($from,$to) = @_;
305
306     print STDERR "  ln $from $to\n";
307     link($from,$to) || warn "Couldn't link $from to $to: $!\n" unless $nonono;
308 }
309
310 sub chmod {
311     local($mode,$name) = @_;
312
313     printf STDERR "  chmod %o %s\n", $mode, $name;
314     chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
315         unless $nonono;
316 }
317
318 sub makedir {
319     local($dir) = @_;
320     unless (-d $dir) {
321         local($shortdir) = $dir;
322
323         $shortdir =~ s#(.*)/.*#$1#;
324         &makedir($shortdir);
325
326         print STDERR "  mkdir $dir\n";
327         mkdir($dir, 0777) || warn "Couldn't create $dir: $!\n" unless $nonono;
328     }
329 }
330
331 sub samepath {
332     local($p1, $p2) = @_;
333     local($dev1, $ino1, $dev2, $ino2);
334
335     if ($p1 ne $p2) {
336         ($dev1, $ino1) = stat($p1);
337         ($dev2, $ino2) = stat($p2);
338         ($dev1 == $dev2 && $ino1 == $ino2);
339     }
340     else {
341         1;
342     }
343 }
344
345 sub installlib {
346     my $dir = $File::Find::dir;
347     $dir =~ s#^\.(?![^/])/?##;
348
349     my $name = $_;
350     
351     # ignore patch backups and the .exists files.
352     return if $name =~ m{\.orig$|~$|^\.exists};
353
354     $name = "$dir/$name" if $dir ne '';
355
356     my $installlib = $installprivlib;
357     if ((substr($dir, 0, 4) eq 'auto') || ($name eq 'Config.pm')) {
358         $installlib = $installarchlib;
359         return unless $do_installarchlib;
360     } else {
361         return unless $do_installprivlib;
362     }
363
364     if (-f $_) {
365         if (/\.al$/ || /\.ix$/) {
366             $installlib = $installprivlib;
367             #We're installing *.al and *.ix files into $installprivlib,
368             #but we have to delete old *.al and *.ix files from the 5.000
369             #distribution:
370             #This might not work because $archname might have changed.
371             &unlink("$installarchlib/$name");
372         }
373         system "cmp", "-s", $_, "$installlib/$name";
374         if ($?) {
375             &unlink("$installlib/$name");
376             &makedir("$installlib/$dir");
377             cp_if_diff("$_", "$installlib/$dir/$_");
378             # HP-UX (at least) needs to maintain execute permissions
379             # on dynamically-loaded libraries.
380             if ($name =~ /\.(so|$dlext)$/o) {
381                 &chmod(0555, "$installlib/$name");
382             }
383             else {
384                 &chmod(0444, "$installlib/$name");
385             }
386         }
387     } elsif (-d $_) {
388         &makedir("$installlib/$name");
389     }
390 }
391
392 # Copy $from to $to, only if $from is different than $to.
393 # Also preserve modification times for .a libraries.
394 # On some systems, if you do
395 #   ranlib libperl.a
396 #   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
397 # and then try to link against the installed libperl.a, you might
398 # get an error message to the effect that the symbol table is older
399 # than the library.
400 sub cp_if_diff {
401     my($from,$to)=@_;
402     -f $from || die "$0: $from not found";
403     system "cmp", "-s", $from, $to;
404     if ($?) {
405         my ($atime, $mtime);
406         unlink($to);   # In case we don't have write permissions.
407         cmd("cp $from $to");
408         # Restore timestamps if it's a .a library.
409         if ($to =~ /\.a$/) {
410             ($atime, $mtime) = (stat $from)[8,9];
411             utime $atime, $mtime, $to;
412         }
413     }
414 }