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