[asperl] add a part of AS patch#14, backout incomplete variable
[p5sagit/p5-mst-13.2.git] / installperl
CommitLineData
c623bd54 1#!./perl
68dc0745 2
3BEGIN {
c90c0ff4 4 require 5.004;
d07c2202 5 chdir '..' if !-d 'lib' and -d '..\lib';
68dc0745 6 @INC = 'lib';
7 $ENV{PERL5LIB} = 'lib';
8}
9
a0d0e21e 10use File::Find;
5f05dabc 11use File::Compare;
68dc0745 12use File::Copy ();
0ecb046b 13use File::Path ();
4633a7c4 14use Config;
91a06757 15use subs qw(unlink link chmod);
c623bd54 16
0ecb046b 17# override the ones in the rest of the script
68dc0745 18sub mkpath {
19 File::Path::mkpath(@_) unless $nonono;
0ecb046b 20}
21
bee1dbe2 22$mainperldir = "/usr/bin";
39add537 23$exe_ext = $Config{exe_ext};
bee1dbe2 24
c623bd54 25while (@ARGV) {
26 $nonono = 1 if $ARGV[0] eq '-n';
27 $versiononly = 1 if $ARGV[0] eq '-v';
28 shift;
29}
30
45d8adaa 31umask 022;
32
1fef88e7 33@scripts = qw( utils/c2ph utils/h2ph utils/h2xs
5f05dabc 34 utils/perlbug utils/perldoc utils/pl2pm utils/splain
dd6decf0 35 x2p/s2p x2p/find2perl
36 pod/pod2man pod/pod2html pod/pod2latex pod/pod2text);
16d20bd9 37
16d20bd9 38@pods = (<pod/*.pod>);
c623bd54 39
1dd15ed4 40%archpms = (Config => 1, FileHandle => 1, overload => 1);
39e571d4 41
42if ($^O eq 'dos') {
43 push(@scripts,'djgpp/fixpmain');
44 $archpms{config} = $archpms{filehand} = 1;
45}
46
1dd15ed4 47find(sub {
48 if ("$File::Find::dir/$_" =~ m{^ext/[^/]+/(.*)\.pm$}) {
49 (my $pm = $1) =~ s{^lib/}{};
50 $archpms{$pm} = 1;
51 }
52 }, 'ext');
53
748a9306 54$ver = $];
4633a7c4 55$release = substr($ver,0,3); # Not used presently.
bee1dbe2 56$patchlevel = substr($ver,3,2);
748a9306 57die "Patchlevel of perl ($patchlevel)",
4633a7c4 58 "and patchlevel of config.sh ($Config{'PATCHLEVEL'}) don't match\n"
59 if $patchlevel != $Config{'PATCHLEVEL'};
60
61# Fetch some frequently-used items from %Config
62$installbin = $Config{installbin};
63$installscript = $Config{installscript};
64$installprivlib = $Config{installprivlib};
65$installarchlib = $Config{installarchlib};
66$installsitelib = $Config{installsitelib};
67$installsitearch = $Config{installsitearch};
68$installman1dir = $Config{installman1dir};
69$man1ext = $Config{man1ext};
15792f64 70$libperl = $Config{libperl};
4633a7c4 71# Shared library and dynamic loading suffixes.
72$so = $Config{so};
73$dlext = $Config{dlext};
74
75$d_dosuid = $Config{d_dosuid};
76$binexp = $Config{binexp};
c623bd54 77
78# Do some quick sanity checks.
79
80if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
81
fe14fcc3 82 $installbin || die "No installbin directory in config.sh\n";
fcbdbaa8 83-d $installbin || mkpath($installbin, 1, 0777);
0ecb046b 84-d $installbin || $nonono || die "$installbin is not a directory\n";
85-w $installbin || $nonono || die "$installbin is not writable by you\n"
3edbfbe5 86 unless $installbin =~ m#^/afs/# || $nonono;
c623bd54 87
39add537 88-x 'perl' . $exe_ext || die "perl isn't executable!\n";
89-x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
c623bd54 90
d07c2202 91-x 't/TEST' || $^O eq 'MSWin32'
92 || warn "WARNING: You've never run 'make test'!!!",
93 " (Installing anyway.)\n";
c623bd54 94
eef1cf2a 95if ($^O eq 'MSWin32') {
96
f7a92b15 97$perldll = 'perl.' . $dlext;
98$perldll = 'perlcore.' . $dlext if $Config{'ccflags'} =~ /PERL_OBJECT/i;
99
100-f $perldll || die "No perl DLL built\n";
eef1cf2a 101
102# Install the DLL
103
f7a92b15 104safe_unlink("$installbin/$perldll");
105copy("$perldll", "$installbin/$perldll");
106chmod(0755, "$installbin/$perldll");
eef1cf2a 107}
108
c623bd54 109# First we install the version-numbered executables.
110
39e571d4 111if ($^O ne 'dos') {
112 safe_unlink("$installbin/perl$ver$exe_ext");
113 copy("perl$exe_ext", "$installbin/perl$ver$exe_ext");
114 chmod(0755, "$installbin/perl$ver$exe_ext");
115} else {
116 safe_unlink("$installbin/perl.exe");
117 copy("perl.exe", "$installbin/perl.exe");
118}
c623bd54 119
68dc0745 120safe_unlink("$installbin/sperl$ver$exe_ext");
c623bd54 121if ($d_dosuid) {
68dc0745 122 copy("suidperl$exe_ext", "$installbin/sperl$ver$exe_ext");
123 chmod(04711, "$installbin/sperl$ver$exe_ext");
c623bd54 124}
125
45d8adaa 126# Install library files.
127
a0d0e21e 128$do_installarchlib = $do_installprivlib = 0;
129
fcbdbaa8 130mkpath($installprivlib, 1, 0777);
131mkpath($installarchlib, 1, 0777);
132mkpath($installsitelib, 1, 0777) if ($installsitelib);
133mkpath($installsitearch, 1, 0777) if ($installsitearch);
4633a7c4 134
45d8adaa 135if (chdir "lib") {
68dc0745 136 $do_installarchlib = ! samepath($installarchlib, '.');
137 $do_installprivlib = ! samepath($installprivlib, '.');
b971f6e4 138 $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$]/);
45d8adaa 139
a0d0e21e 140 if ($do_installarchlib || $do_installprivlib) {
141 find(\&installlib, '.');
45d8adaa 142 }
143 chdir ".." || die "Can't cd back to source directory: $!\n";
144}
145else {
146 warn "Can't cd to lib to install lib files: $!\n";
147}
148
1aef975c 149# Install header files and libraries.
fcbdbaa8 150mkpath("$installarchlib/CORE", 1, 0777);
340f689c 151@corefiles = <*.h libperl*.*>;
1aef975c 152# AIX needs perl.exp installed as well.
340f689c 153push(@corefiles,'perl.exp') if $^O eq 'aix';
fed7345c 154# If they have built sperl.o...
340f689c 155push(@corefiles,'sperl.o') if -f 'sperl.o';
156foreach $file (@corefiles) {
aa3cf465 157 # HP-UX (at least) needs to maintain execute permissions
efc5ad56 158 # on dynamically-loadable libraries. So we do it for all.
68dc0745 159 copy_if_diff($file,"$installarchlib/CORE/$file")
efc5ad56 160 and chmod($file =~ /\.(so|\Q$dlext\E)$/ ? 0555 : 0444,
aa3cf465 161 "$installarchlib/CORE/$file");
340f689c 162}
3edbfbe5 163
156620fa 164# Install main perl executables
165# Make links to ordinary names if installbin directory isn't current directory.
166
167if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos')) {
168 safe_unlink("$installbin/perl$exe_ext", "$installbin/suidperl$exe_ext");
169 link("$installbin/perl$ver$exe_ext", "$installbin/perl$exe_ext");
170 link("$installbin/sperl$ver$exe_ext", "$installbin/suidperl$exe_ext")
171 if $d_dosuid;
172}
173
a0d0e21e 174# Offer to install perl in a "standard" location
175
a0d0e21e 176$mainperl_is_instperl = 0;
177
d07c2202 178if (!$versiononly && !$nonono && $^O ne 'MSWin32' && -t STDIN && -t STDERR
68dc0745 179 && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
55497cff 180 local($usrbinperl) = "$mainperldir/perl$exe_ext";
181 local($instperl) = "$installbin/perl$exe_ext";
182 local($expinstperl) = "$binexp/perl$exe_ext";
183
184 # First make sure $usrbinperl is not already the same as the perl we
185 # just installed.
186 if (-x $usrbinperl) {
a0d0e21e 187 # Try to be clever about mainperl being a symbolic link
188 # to binexp/perl if binexp and installbin are different.
189 $mainperl_is_instperl =
68dc0745 190 samepath($usrbinperl, $instperl) ||
191 samepath($usrbinperl, $expinstperl) ||
a0d0e21e 192 (($binexp ne $installbin) &&
55497cff 193 (-l $usrbinperl) &&
194 ((readlink $usrbinperl) eq $expinstperl));
a0d0e21e 195 }
196 if ((! $mainperl_is_instperl) &&
68dc0745 197 (yn("Many scripts expect perl to be installed as $usrbinperl.\n" .
55497cff 198 "Do you wish to have $usrbinperl be the same as\n" .
199 "$expinstperl? [y] ")))
a0d0e21e 200 {
55497cff 201 unlink($usrbinperl);
202 eval { CORE::link $instperl, $usrbinperl } ||
203 eval { symlink $expinstperl, $usrbinperl } ||
68dc0745 204 copy($instperl, $usrbinperl);
a0d0e21e 205 $mainperl_is_instperl = 1;
206 }
207}
208
b971f6e4 209# Make links to ordinary names if installbin directory isn't current directory.
210
68dc0745 211if (!$versiononly && ! samepath($installbin, 'x2p')) {
212 safe_unlink("$installbin/a2p$exe_ext");
213 copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
214 chmod(0755, "$installbin/a2p$exe_ext");
b971f6e4 215}
216
217# cppstdin is just a script, but it is architecture-dependent, so
218# it can't safely be shared. Place it in $installbin.
219# Note that Configure doesn't build cppstin if it isn't needed, so
220# we skip this if cppstdin doesn't exist.
68dc0745 221if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
222 safe_unlink("$installbin/cppstdin");
223 copy("cppstdin", "$installbin/cppstdin");
224 chmod(0755, "$installbin/cppstdin");
b971f6e4 225}
226
227# Install scripts.
228
229mkpath($installscript, 1, 0777);
230
231if (! $versiononly) {
232 for (@scripts) {
68dc0745 233 (my $base = $_) =~ s#.*/##;
234 copy($_, "$installscript/$base");
235 chmod(0755, "$installscript/$base");
b971f6e4 236 }
237}
238
239# pstruct should be a link to c2ph
240
241if (! $versiononly) {
68dc0745 242 safe_unlink("$installscript/pstruct");
39e571d4 243 if ($^O eq 'dos') {
244 copy("$installscript/c2ph","$installscript/pstruct");
245 } else {
246 link("$installscript/c2ph","$installscript/pstruct");
247 }
b971f6e4 248}
249
250# Install pod pages. Where? I guess in $installprivlib/pod.
251
68dc0745 252if (! $versiononly || !($installprivlib =~ m/\Q$]/)) {
b971f6e4 253 mkpath("${installprivlib}/pod", 1, 0777);
91a06757 254
255 # If Perl 5.003's perldiag.pod is there, rename it.
256 if (open POD, "${installprivlib}/pod/perldiag.pod") {
257 read POD, $_, 4000;
258 close POD;
259 # Some of Perl 5.003's diagnostic messages ended with periods.
260 if (/^=.*\.$/m) {
261 my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
262 "${installprivlib}/pod/perldiag-5.003.pod");
263 print STDERR " rename $from $to";
264 rename($from, $to)
265 or warn "Couldn't rename $from to $to: $!\n"
266 unless $nonono;
267 }
268 }
269
b971f6e4 270 foreach $file (@pods) {
271 # $file is a name like pod/perl.pod
68dc0745 272 copy_if_diff($file, "${installprivlib}/${file}");
b971f6e4 273 }
91a06757 274
275 # Link perldiag.pod into archlib
276 my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
277 "${installarchlib}/pod/perldiag.pod");
278 if (compare($from, $to) || $nonono) {
279 mkpath("${installarchlib}/pod", 1, 0777);
a3b6010a 280 unlink($to);
39e571d4 281 link($from, $to) if ($^O ne 'dos');
91a06757 282 }
b971f6e4 283}
284
a0d0e21e 285# Check to make sure there aren't other perls around in installer's
286# path. This is probably UNIX-specific. Check all absolute directories
287# in the path except for where public executables are supposed to live.
288# Also skip $mainperl if the user opted to have it be a link to the
289# installed perl.
290
b971f6e4 291if (!$versiononly) {
292
eef1cf2a 293 $dirsep = ($^O eq 'os2' || $^O eq 'MSWin32') ? ';' : ':' ;
b971f6e4 294 ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
295 @path = split(/$dirsep/, $path);
296 @otherperls = ();
297 for (@path) {
298 next unless m,^/,;
299 # Use &samepath here because some systems have other dirs linked
300 # to $mainperldir (like SunOS)
68dc0745 301 next if samepath($_, $binexp);
302 next if ($mainperl_is_instperl && samepath($_, $mainperldir));
b971f6e4 303 push(@otherperls, "$_/perl$exe_ext")
304 if (-x "$_/perl$exe_ext" && ! -d "$_/perl$exe_ext");
305 }
306 if (@otherperls) {
307 print STDERR "\nWarning: perl appears in your path in the following " .
308 "locations beyond where\nwe just installed it:\n";
309 for (@otherperls) {
310 print STDERR " ", $_, "\n";
311 }
312 print STDERR "\n";
a0d0e21e 313 }
b971f6e4 314
a0d0e21e 315}
316
c623bd54 317print STDERR " Installation complete\n";
318
319exit 0;
320
321###############################################################################
322
a0d0e21e 323sub yn {
324 local($prompt) = @_;
325 local($answer);
326 local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
327 print STDERR $prompt;
328 chop($answer = <STDIN>);
329 $answer = $default if $answer =~ m/^\s*$/;
330 ($answer =~ m/^[yY]/);
331}
332
c623bd54 333sub unlink {
334 local(@names) = @_;
39add537 335 my($cnt) = 0;
c623bd54 336
337 foreach $name (@names) {
338 next unless -e $name;
eef1cf2a 339 chmod 0777, $name if ($^O eq 'os2' || $^O eq 'MSWin32');
c623bd54 340 print STDERR " unlink $name\n";
39add537 341 ( CORE::unlink($name) and ++$cnt
342 or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
c623bd54 343 }
39add537 344 return $cnt;
c623bd54 345}
346
e50aee73 347sub safe_unlink {
68dc0745 348 return if $nonono;
349 local @names = @_;
e50aee73 350 foreach $name (@names) {
351 next unless -e $name;
eef1cf2a 352 chmod 0777, $name if ($^O eq 'os2' || $^O eq 'MSWin32');
39add537 353 print STDERR " unlink $name\n";
354 next if CORE::unlink($name);
e50aee73 355 warn "Couldn't unlink $name: $!\n";
356 if ($! =~ /busy/i) {
357 print STDERR " mv $name $name.old\n";
68dc0745 358 safe_rename($name, "$name.old")
359 or warn "Couldn't rename $name: $!\n";
e50aee73 360 }
361 }
362}
363
68dc0745 364sub safe_rename {
e50aee73 365 local($from,$to) = @_;
39add537 366 if (-f $to and not unlink($to)) {
e50aee73 367 my($i);
368 for ($i = 1; $i < 50; $i++) {
91a06757 369 last if rename($to, "$to.$i");
e50aee73 370 }
39add537 371 warn("Cannot rename to `$to.$i': $!"), return 0
372 if $i >= 50; # Give up!
e50aee73 373 }
374 link($from,$to) || return 0;
375 unlink($from);
376}
377
c623bd54 378sub link {
15792f64 379 my($from,$to) = @_;
380 my($success) = 0;
c623bd54 381
382 print STDERR " ln $from $to\n";
39add537 383 eval {
68dc0745 384 CORE::link($from, $to)
385 ? $success++
7bac28a0 386 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
387 ? die "AFS" # okay inside eval {}
388 : warn "Couldn't link $from to $to: $!\n"
68dc0745 389 unless $nonono;
39add537 390 };
391 if ($@) {
68dc0745 392 File::Copy::copy($from, $to)
393 ? $success++
394 : warn "Couldn't copy $from to $to: $!\n"
395 unless $nonono;
39add537 396 }
15792f64 397 $success;
c623bd54 398}
399
400sub chmod {
401 local($mode,$name) = @_;
402
39e571d4 403 return if ($^O eq 'dos');
c623bd54 404 printf STDERR " chmod %o %s\n", $mode, $name;
68dc0745 405 CORE::chmod($mode,$name)
406 || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
407 unless $nonono;
408}
409
410sub copy {
411 my($from,$to) = @_;
412
413 print STDERR " cp $from $to\n";
414 File::Copy::copy($from, $to)
415 || warn "Couldn't copy $from to $to: $!\n"
416 unless $nonono;
c623bd54 417}
418
a0d0e21e 419sub samepath {
420 local($p1, $p2) = @_;
eef1cf2a 421
422 return (lc($p1) eq lc($p2)) if ($^O eq 'MSWin32');
a0d0e21e 423
75f92628 424 if ($p1 ne $p2) {
eef1cf2a 425 local($dev1, $ino1, $dev2, $ino2);
a0d0e21e 426 ($dev1, $ino1) = stat($p1);
427 ($dev2, $ino2) = stat($p2);
428 ($dev1 == $dev2 && $ino1 == $ino2);
429 }
430 else {
431 1;
432 }
433}
434
435sub installlib {
436 my $dir = $File::Find::dir;
437 $dir =~ s#^\.(?![^/])/?##;
0ecb046b 438 local($depth) = $dir ? "lib/$dir" : "lib";
a0d0e21e 439
440 my $name = $_;
efc5ad56 441
442 if ($name eq 'CVS' && -d $name) {
443 $File::Find::prune = 1;
444 return;
445 }
e50aee73 446
447 # ignore patch backups and the .exists files.
448 return if $name =~ m{\.orig$|~$|^\.exists};
449
a0d0e21e 450 $name = "$dir/$name" if $dir ne '';
451
452 my $installlib = $installprivlib;
1dd15ed4 453 if ($dir =~ /^auto/ ||
eef1cf2a 454 ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
455 ($name =~ /^(.*)\.(?:h|lib)$/i && $^O eq 'MSWin32')
456 ) {
a0d0e21e 457 $installlib = $installarchlib;
458 return unless $do_installarchlib;
459 } else {
460 return unless $do_installprivlib;
461 }
462
a0d0e21e 463 if (-f $_) {
5cc330c8 464 if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
3edbfbe5 465 $installlib = $installprivlib;
466 #We're installing *.al and *.ix files into $installprivlib,
467 #but we have to delete old *.al and *.ix files from the 5.000
468 #distribution:
75f92628 469 #This might not work because $archname might have changed.
68dc0745 470 unlink("$installarchlib/$name");
3edbfbe5 471 }
5f05dabc 472 if (compare($_, "$installlib/$name") || $nonono) {
68dc0745 473 unlink("$installlib/$name");
fcbdbaa8 474 mkpath("$installlib/$dir", 1, 0777);
75f92628 475 # HP-UX (at least) needs to maintain execute permissions
476 # on dynamically-loaded libraries.
68dc0745 477 copy_if_diff($_, "$installlib/$name")
478 and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
aa3cf465 479 "$installlib/$name");
a0d0e21e 480 }
481 } elsif (-d $_) {
fcbdbaa8 482 mkpath("$installlib/$name", 1, 0777);
a0d0e21e 483 }
484}
3edbfbe5 485
16d20bd9 486# Copy $from to $to, only if $from is different than $to.
487# Also preserve modification times for .a libraries.
488# On some systems, if you do
489# ranlib libperl.a
490# cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
491# and then try to link against the installed libperl.a, you might
492# get an error message to the effect that the symbol table is older
493# than the library.
aa3cf465 494# Return true if copying occurred.
68dc0745 495
496sub copy_if_diff {
3edbfbe5 497 my($from,$to)=@_;
498 -f $from || die "$0: $from not found";
5f05dabc 499 if (compare($from, $to) || $nonono) {
68dc0745 500 safe_unlink($to); # In case we don't have write permissions.
0ecb046b 501 if ($nonono) {
502 $from = $depth . "/" . $from if $depth;
503 }
68dc0745 504 copy($from, $to);
505 # Restore timestamps if it's a .a library or for OS/2.
506 if (!$nonono && ($^O eq 'os2' || $to =~ /\.a$/)) {
507 my ($atime, $mtime) = (stat $from)[8,9];
16d20bd9 508 utime $atime, $mtime, $to;
509 }
aa3cf465 510 1;
3edbfbe5 511 }
512}