This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline
[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';
a29d2910
A
8}
9
10use strict;
5f675712 11my ($Is_VMS, $Is_W32, $Is_OS2, $Is_Cygwin, $nonono, $dostrip,
94b11c62 12 $versiononly, $silent, $verbose, $otherperls);
5f675712 13use vars qw /$depth/;
a29d2910
A
14
15BEGIN {
17f28c40 16 $Is_VMS = $^O eq 'VMS';
4a71ed0c
GS
17 $Is_W32 = $^O eq 'MSWin32';
18 $Is_OS2 = $^O eq 'os2';
146174a9 19 $Is_Cygwin = $^O eq 'cygwin';
17f28c40 20 if ($Is_VMS) { eval 'use VMS::Filespec;' }
68dc0745
PP
21}
22
c2cd2081 23my $scr_ext = ($Is_VMS ? '.Com' : $Is_W32 ? '.bat' : '');
4a71ed0c 24
a0d0e21e 25use File::Find;
5f05dabc 26use File::Compare;
68dc0745 27use File::Copy ();
0ecb046b 28use File::Path ();
354f3b56 29use ExtUtils::Packlist;
4633a7c4 30use Config;
91a06757 31use subs qw(unlink link chmod);
c623bd54 32
0ecb046b 33# override the ones in the rest of the script
68dc0745
PP
34sub mkpath {
35 File::Path::mkpath(@_) unless $nonono;
0ecb046b 36}
37
c2cd2081
KS
38my $mainperldir = "/usr/bin";
39my $exe_ext = $Config{exe_ext};
bee1dbe2 40
6ee623d5 41# Allow ``make install PERLNAME=something_besides_perl'':
c2cd2081 42my $perl = defined($ENV{PERLNAME}) ? $ENV{PERLNAME} : 'perl';
6ee623d5 43
146174a9 44# This is the base used for versioned names, like "perl5.6.0".
beb13193
RS
45# It's separate because a common use of $PERLNAME is to install
46# perl as "perl5", if that's used as base for versioned files you
146174a9 47# get "perl55.6.0".
beb13193
RS
48my $perl_verbase = defined($ENV{PERLNAME_VERBASE})
49 ? $ENV{PERLNAME_VERBASE}
50 : $perl;
51
5f675712 52$otherperls = 1;
c623bd54
LW
53while (@ARGV) {
54 $nonono = 1 if $ARGV[0] eq '-n';
f556e5b9 55 $dostrip = 1 if $ARGV[0] eq '-s';
c623bd54 56 $versiononly = 1 if $ARGV[0] eq '-v';
5f675712
A
57 $silent = 1 if $ARGV[0] eq '-S';
58 $otherperls = 0 if $ARGV[0] eq '-o';
4ad019ef 59 $verbose = 1 if $ARGV[0] eq '-V' || $ARGV [0] eq '-n';
c623bd54
LW
60 shift;
61}
62
d56c5707
JH
63$versiononly = 1 if $Config{versiononly};
64
c2cd2081 65my @scripts = qw(utils/c2ph utils/h2ph utils/h2xs utils/perlbug utils/perldoc
95667ae4 66 utils/pl2pm utils/splain utils/perlcc utils/dprofpp
6ee623d5 67 x2p/s2p x2p/find2perl
360aca43
GS
68 pod/pod2man pod/pod2html pod/pod2latex pod/pod2text
69 pod/pod2usage pod/podchecker pod/podselect);
16d20bd9 70
4a71ed0c 71if ($scr_ext) { @scripts = map { "$_$scr_ext" } @scripts; }
17f28c40 72
c2cd2081 73my @pods = (<pod/*.pod>);
c623bd54 74
e8ea6127
AD
75# Specify here any .pm files that are actually architecture-dependent.
76# (Those included with XS extensions under ext/ are automatically
77# added later.)
78# Now that the default privlib has the full perl version number included,
79# we no longer have to play the trick of sticking version-specific .pm
80# files under the archlib directory.
c2cd2081 81my %archpms = (
e8ea6127 82 Config => 1,
38b8243a 83);
39e571d4
ML
84
85if ($^O eq 'dos') {
86 push(@scripts,'djgpp/fixpmain');
87 $archpms{config} = $archpms{filehand} = 1;
88}
89
6ee623d5
GS
90if ((-e "testcompile") && (defined($ENV{'COMPILE'})))
91{
92 push(@scripts, map("$_.exe", @scripts));
93}
94
1dd15ed4 95find(sub {
68c887af
GS
96 if ("$File::Find::dir/$_" =~ m{^ext\b(.*)/([^/]+)\.pm$}) {
97 my($path, $modname) = ($1,$2);
98
99 # strip trailing component first
100 $path =~ s{/[^/]*$}{};
101
102 # strip optional "/lib";
103 $path =~ s{/lib\b}{};
104
105 # strip any leading /
106 $path =~ s{^/}{};
107
108 # reconstitute canonical module name
109 $modname = "$path/$modname" if length $path;
110
111 # remember it
112 $archpms{$modname} = 1;
1dd15ed4
CS
113 }
114 }, 'ext');
115
68c887af
GS
116# print "[$_]\n" for sort keys %archpms;
117
146174a9 118my $ver = $Config{version};
03284eb6
JH
119my $release = substr($],0,3); # Not used presently.
120my $patchlevel = substr($],3,2);
748a9306 121die "Patchlevel of perl ($patchlevel)",
cceca5ed
GS
122 "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
123 if $patchlevel != $Config{'PERL_VERSION'};
4633a7c4
LW
124
125# Fetch some frequently-used items from %Config
c2cd2081
KS
126my $installbin = $Config{installbin};
127my $installscript = $Config{installscript};
128my $installprivlib = $Config{installprivlib};
129my $installarchlib = $Config{installarchlib};
130my $installsitelib = $Config{installsitelib};
131my $installsitearch = $Config{installsitearch};
132my $installman1dir = $Config{installman1dir};
133my $man1ext = $Config{man1ext};
134my $libperl = $Config{libperl};
4633a7c4 135# Shared library and dynamic loading suffixes.
c2cd2081
KS
136my $so = $Config{so};
137my $dlext = $Config{dlext};
146174a9 138my $dlsrc = $Config{dlsrc};
4633a7c4 139
c2cd2081
KS
140my $d_dosuid = $Config{d_dosuid};
141my $binexp = $Config{binexp};
c623bd54 142
17f28c40
CB
143if ($Is_VMS) { # Hang in there until File::Spec hits the big time
144 foreach ( \$installbin, \$installscript, \$installprivlib,
145 \$installarchlib, \$installsitelib, \$installsitearch,
146 \$installman1dir ) {
147 $$_ = unixify($$_); $$_ =~ s:/$::;
148 }
149}
150
c623bd54
LW
151# Do some quick sanity checks.
152
153if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
154
fe14fcc3 155 $installbin || die "No installbin directory in config.sh\n";
7e7af249 156-d $installbin || mkpath($installbin, $verbose, 0777);
0ecb046b 157-d $installbin || $nonono || die "$installbin is not a directory\n";
158-w $installbin || $nonono || die "$installbin is not writable by you\n"
3edbfbe5 159 unless $installbin =~ m#^/afs/# || $nonono;
c623bd54 160
39add537
PP
161-x 'perl' . $exe_ext || die "perl isn't executable!\n";
162-x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
c623bd54 163
9fba8746 164-f 't/rantests' || $Is_W32
360eb788
NIS
165 || warn "WARNING: You've never run 'make test' or",
166 " some tests failed! (Installing anyway.)\n";
c623bd54 167
8736538c
AS
168if ($Is_W32 or $Is_Cygwin) {
169 my $perldll;
170
5f675712
A
171 if ($Is_Cygwin) {
172 $perldll = $libperl;
173 $perldll =~ s/(\..*)?$/.$dlext/;
174 if ($Config{useshrplib} eq 'true') {
175 # install ld2 and perlld as well
176 foreach ('ld2', 'perlld') {
177 safe_unlink("$installbin/$_");
178 copy("$_", "$installbin/$_");
179 chmod(0755, "$installbin/$_");
180 };
8736538c 181 };
5f675712
A
182 } else {
183 $perldll = 'perl56.' . $dlext;
184 }
185
186 if ($dlsrc ne "dl_none.xs") {
187 -f $perldll || die "No perl DLL built\n";
188 }
189 # Install the DLL
190
191 safe_unlink("$installbin/$perldll");
192 copy("$perldll", "$installbin/$perldll");
193 chmod(0755, "$installbin/$perldll");
146174a9
CB
194
195} # if ($Is_W32 or $Is_Cygwin)
eef1cf2a 196
354f3b56 197# This will be used to store the packlist
c2cd2081 198my $packlist = ExtUtils::Packlist->new("$installarchlib/.packlist");
354f3b56 199
c623bd54
LW
200# First we install the version-numbered executables.
201
17f28c40 202if ($Is_VMS) {
6ee623d5
GS
203 safe_unlink("$installbin/$perl$exe_ext");
204 copy("perl$exe_ext", "$installbin/$perl$exe_ext");
205 chmod(0755, "$installbin/$perl$exe_ext");
206 safe_unlink("$installbin/${perl}shr$exe_ext");
207 copy("perlshr$exe_ext", "$installbin/${perl}shr$exe_ext");
208 chmod(0755, "$installbin/${perl}shr$exe_ext");
17f28c40 209}
1d84e8df
JH
210elsif ($^O eq 'mpeix') {
211 # MPE lacks hard links and requires that executables with special
212 # capabilities reside in the MPE namespace.
213 safe_unlink("$installbin/perl$ver$exe_ext", $Config{perlpath});
214 # Install the primary executable into the MPE namespace as perlpath.
215 copy("perl$exe_ext", $Config{perlpath});
216 chmod(0755, $Config{perlpath});
217 # Create a backup copy with the version number.
218 link($Config{perlpath}, "$installbin/perl$ver$exe_ext");
219}
3f5ee302 220elsif ($^O ne 'dos') {
beb13193
RS
221 safe_unlink("$installbin/$perl_verbase$ver$exe_ext");
222 copy("perl$exe_ext", "$installbin/$perl_verbase$ver$exe_ext");
f556e5b9 223 strip("$installbin/$perl_verbase$ver$exe_ext");
beb13193 224 chmod(0755, "$installbin/$perl_verbase$ver$exe_ext");
3f5ee302 225}
1d84e8df 226else {
6ee623d5
GS
227 safe_unlink("$installbin/$perl.exe");
228 copy("perl.exe", "$installbin/$perl.exe");
39e571d4 229}
c623bd54 230
beb13193 231safe_unlink("$installbin/s$perl_verbase$ver$exe_ext");
c623bd54 232if ($d_dosuid) {
beb13193
RS
233 copy("suidperl$exe_ext", "$installbin/s$perl_verbase$ver$exe_ext");
234 chmod(04711, "$installbin/s$perl_verbase$ver$exe_ext");
c623bd54
LW
235}
236
45d8adaa
LW
237# Install library files.
238
c2cd2081 239my ($do_installarchlib, $do_installprivlib) = (0, 0);
a0d0e21e 240
7e7af249
A
241mkpath($installprivlib, $verbose, 0777);
242mkpath($installarchlib, $verbose, 0777);
243mkpath($installsitelib, $verbose, 0777) if ($installsitelib);
244mkpath($installsitearch, $verbose, 0777) if ($installsitearch);
4633a7c4 245
45d8adaa 246if (chdir "lib") {
68dc0745
PP
247 $do_installarchlib = ! samepath($installarchlib, '.');
248 $do_installprivlib = ! samepath($installprivlib, '.');
146174a9 249 $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$ver/);
45d8adaa 250
a0d0e21e
LW
251 if ($do_installarchlib || $do_installprivlib) {
252 find(\&installlib, '.');
45d8adaa
LW
253 }
254 chdir ".." || die "Can't cd back to source directory: $!\n";
255}
256else {
257 warn "Can't cd to lib to install lib files: $!\n";
258}
259
1aef975c 260# Install header files and libraries.
7e7af249 261mkpath("$installarchlib/CORE", $verbose, 0777);
c2cd2081 262my @corefiles;
17f28c40 263if ($Is_VMS) { # We did core file selection during build
0185066f 264 my $coredir = "lib/$Config{archname}/$ver/CORE";
17f28c40 265 $coredir =~ tr/./_/;
0185066f 266 map { s|^$coredir/||i; } @corefiles = <$coredir/*.*>;
17f28c40
CB
267}
268else {
8736538c 269 # [als] hard-coded 'libperl' name... not good!
17f28c40 270 @corefiles = <*.h libperl*.*>;
8736538c 271
17f28c40
CB
272 # AIX needs perl.exp installed as well.
273 push(@corefiles,'perl.exp') if $^O eq 'aix';
9d7f10f2
MB
274 if ($^O eq 'mpeix') {
275 # MPE needs mpeixish.h installed as well.
7e7af249 276 mkpath("$installarchlib/CORE/mpeix", $verbose, 0777);
9d7f10f2
MB
277 push(@corefiles,'mpeix/mpeixish.h');
278 }
17f28c40
CB
279 # If they have built sperl.o...
280 push(@corefiles,'sperl.o') if -f 'sperl.o';
281}
c2cd2081 282foreach my $file (@corefiles) {
aa3cf465 283 # HP-UX (at least) needs to maintain execute permissions
efc5ad56 284 # on dynamically-loadable libraries. So we do it for all.
8f1f23e8 285 if (copy_if_diff($file,"$installarchlib/CORE/$file")) {
f556e5b9 286 if ($file =~ /\.(\Q$so\E|\Q$dlext\E)$/) {
8f1f23e8 287 chmod(0555, "$installarchlib/CORE/$file");
f556e5b9 288 strip("-S", "$installarchlib/CORE/$file") if $^O =~ /^(rhapsody|darwin)$/;
8f1f23e8
W
289 } else {
290 chmod(0444, "$installarchlib/CORE/$file");
291 }
292 }
340f689c 293}
3edbfbe5 294
156620fa
NA
295# Install main perl executables
296# Make links to ordinary names if installbin directory isn't current directory.
297
17f28c40 298if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS) {
6ee623d5 299 safe_unlink("$installbin/$perl$exe_ext", "$installbin/suid$perl$exe_ext");
1d84e8df
JH
300 if ($^O eq 'mpeix') {
301 # MPE doesn't support hard links, so use a symlink.
302 # We don't want another cloned copy.
303 symlink($Config{perlpath}, "$installbin/perl$exe_ext");
304 } else {
beb13193
RS
305 link("$installbin/$perl_verbase$ver$exe_ext",
306 "$installbin/$perl$exe_ext");
1d84e8df 307 }
beb13193
RS
308 link("$installbin/s$perl_verbase$ver$exe_ext",
309 "$installbin/suid$perl$exe_ext")
156620fa
NA
310 if $d_dosuid;
311}
312
a0d0e21e
LW
313# Offer to install perl in a "standard" location
314
c2cd2081 315my $mainperl_is_instperl = 0;
a0d0e21e 316
a3ed4895 317if ($Config{installusrbinperl} && $Config{installusrbinperl} eq 'define' &&
472a4973 318 !$versiononly && !$nonono && !$Is_W32 && !$Is_VMS && -t STDIN && -t STDERR
68dc0745 319 && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
c2cd2081
KS
320 my($usrbinperl) = "$mainperldir/$perl$exe_ext";
321 my($instperl) = "$installbin/$perl$exe_ext";
322 my($expinstperl) = "$binexp/$perl$exe_ext";
55497cff
PP
323
324 # First make sure $usrbinperl is not already the same as the perl we
325 # just installed.
326 if (-x $usrbinperl) {
a0d0e21e
LW
327 # Try to be clever about mainperl being a symbolic link
328 # to binexp/perl if binexp and installbin are different.
329 $mainperl_is_instperl =
68dc0745
PP
330 samepath($usrbinperl, $instperl) ||
331 samepath($usrbinperl, $expinstperl) ||
a0d0e21e 332 (($binexp ne $installbin) &&
55497cff
PP
333 (-l $usrbinperl) &&
334 ((readlink $usrbinperl) eq $expinstperl));
a0d0e21e
LW
335 }
336 if ((! $mainperl_is_instperl) &&
68dc0745 337 (yn("Many scripts expect perl to be installed as $usrbinperl.\n" .
55497cff
PP
338 "Do you wish to have $usrbinperl be the same as\n" .
339 "$expinstperl? [y] ")))
1d84e8df 340 {
55497cff 341 unlink($usrbinperl);
1d84e8df
JH
342 ( $Config{'d_link'} eq 'define' &&
343 eval { CORE::link $instperl, $usrbinperl } ) ||
344 eval { symlink $expinstperl, $usrbinperl } ||
345 copy($instperl, $usrbinperl);
346
a0d0e21e
LW
347 $mainperl_is_instperl = 1;
348 }
349}
350
b971f6e4
PP
351# Make links to ordinary names if installbin directory isn't current directory.
352
68dc0745
PP
353if (!$versiononly && ! samepath($installbin, 'x2p')) {
354 safe_unlink("$installbin/a2p$exe_ext");
355 copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
356 chmod(0755, "$installbin/a2p$exe_ext");
b971f6e4
PP
357}
358
359# cppstdin is just a script, but it is architecture-dependent, so
360# it can't safely be shared. Place it in $installbin.
361# Note that Configure doesn't build cppstin if it isn't needed, so
362# we skip this if cppstdin doesn't exist.
68dc0745
PP
363if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
364 safe_unlink("$installbin/cppstdin");
365 copy("cppstdin", "$installbin/cppstdin");
366 chmod(0755, "$installbin/cppstdin");
b971f6e4
PP
367}
368
d56c5707
JH
369if (! $versiononly) {
370 # Install scripts.
b971f6e4 371
d56c5707 372 mkpath($installscript, $verbose, 0777);
b971f6e4 373
22e77942
JH
374 for (@scripts) {
375 (my $base = $_) =~ s#.*/##;
376 copy($_, "$installscript/$base");
377 chmod(0755, "$installscript/$base");
b971f6e4 378 }
22e77942 379
d56c5707 380 # pstruct should be a link to c2ph
22e77942
JH
381 safe_unlink("$installscript/pstruct$scr_ext");
382 if ($^O eq 'dos' or $Is_VMS or $^O eq 'transit') {
d56c5707
JH
383 copy("$installscript/c2ph$scr_ext",
384 "$installscript/pstruct$scr_ext");
22e77942 385 } else {
d56c5707
JH
386 link("$installscript/c2ph$scr_ext",
387 "$installscript/pstruct$scr_ext");
39e571d4 388 }
b971f6e4
PP
389}
390
146174a9
CB
391# Install pod pages. Where? I guess in $installprivlib/pod
392# ($installprivlib/pods for cygwin).
b971f6e4 393
146174a9 394my $pod = $Is_Cygwin ? 'pods' : 'pod';
d56c5707 395if ( !$versiononly || ($installprivlib =~ m/\Q$ver/)) {
7e7af249 396 mkpath("${installprivlib}/$pod", $verbose, 0777);
91a06757
CS
397
398 # If Perl 5.003's perldiag.pod is there, rename it.
146174a9 399 if (open POD, "${installprivlib}/$pod/perldiag.pod") {
91a06757
CS
400 read POD, $_, 4000;
401 close POD;
402 # Some of Perl 5.003's diagnostic messages ended with periods.
403 if (/^=.*\.$/m) {
146174a9
CB
404 my ($from, $to) = ("${installprivlib}/$pod/perldiag.pod",
405 "${installprivlib}/$pod/perldiag-5.003.pod");
f311e742 406 print " rename $from $to";
91a06757
CS
407 rename($from, $to)
408 or warn "Couldn't rename $from to $to: $!\n"
409 unless $nonono;
410 }
411 }
412
146174a9
CB
413 for (@pods) {
414 # $_ is a name like pod/perl.pod
415 (my $base = $_) =~ s#.*/##;
416 copy_if_diff($_, "${installprivlib}/$pod/${base}");
b971f6e4 417 }
91a06757 418
b971f6e4
PP
419}
420
a0d0e21e
LW
421# Check to make sure there aren't other perls around in installer's
422# path. This is probably UNIX-specific. Check all absolute directories
423# in the path except for where public executables are supposed to live.
424# Also skip $mainperl if the user opted to have it be a link to the
425# installed perl.
426
5f675712 427if (!$versiononly && $otherperls) {
146174a9 428 my ($path, @path);
c2cd2081 429 my $dirsep = ($Is_OS2 || $Is_W32) ? ';' : ':' ;
b971f6e4
PP
430 ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
431 @path = split(/$dirsep/, $path);
17f28c40
CB
432 if ($Is_VMS) {
433 my $i = 0;
434 while (exists $ENV{'DCL$PATH' . $i}) {
c2cd2081 435 my $dir = unixpath($ENV{'DCL$PATH' . $i}); $dir =~ s-/$--;
17f28c40
CB
436 push(@path,$dir);
437 }
438 }
c2cd2081 439 my @otherperls;
160a7e37 440 my %otherperls;
b971f6e4
PP
441 for (@path) {
442 next unless m,^/,;
443 # Use &samepath here because some systems have other dirs linked
444 # to $mainperldir (like SunOS)
68dc0745
PP
445 next if samepath($_, $binexp);
446 next if ($mainperl_is_instperl && samepath($_, $mainperldir));
160a7e37
HS
447 my $otherperl = "$_/$perl$exe_ext";
448 next if $otherperls{$otherperl}++;
449 push(@otherperls, $otherperl)
450 if (-x $otherperl && ! -d $otherperl);
b971f6e4
PP
451 }
452 if (@otherperls) {
5f675712 453 warn "\nWarning: $perl appears in your path in the following " .
b971f6e4
PP
454 "locations beyond where\nwe just installed it:\n";
455 for (@otherperls) {
5f675712 456 warn " ", $_, "\n";
b971f6e4 457 }
5f675712 458 warn "\n";
a0d0e21e 459 }
b971f6e4 460
a0d0e21e
LW
461}
462
3f9aafa0 463$packlist->write() unless $nonono;
94b11c62 464print " Installation complete\n" if $verbose;
c623bd54
LW
465
466exit 0;
467
468###############################################################################
469
a0d0e21e 470sub yn {
c2cd2081
KS
471 my($prompt) = @_;
472 my($answer);
473 my($default) = $prompt =~ m/\[([yn])\]\s*$/i;
5f675712 474 warn $prompt;
a0d0e21e
LW
475 chop($answer = <STDIN>);
476 $answer = $default if $answer =~ m/^\s*$/;
477 ($answer =~ m/^[yY]/);
478}
479
c623bd54 480sub unlink {
c2cd2081 481 my(@names) = @_;
39add537 482 my($cnt) = 0;
c623bd54 483
17f28c40
CB
484 return scalar(@names) if $Is_VMS;
485
c2cd2081 486 foreach my $name (@names) {
c623bd54 487 next unless -e $name;
8736538c 488 chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin);
94b11c62 489 print " unlink $name\n" if $verbose;
39add537
PP
490 ( CORE::unlink($name) and ++$cnt
491 or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
c623bd54 492 }
39add537 493 return $cnt;
c623bd54
LW
494}
495
e50aee73 496sub safe_unlink {
17f28c40 497 return if $nonono or $Is_VMS;
c2cd2081
KS
498 my @names = @_;
499 foreach my $name (@names) {
e50aee73 500 next unless -e $name;
4a71ed0c 501 chmod 0777, $name if ($Is_OS2 || $Is_W32);
94b11c62 502 print " unlink $name\n" if $verbose;
39add537 503 next if CORE::unlink($name);
e50aee73
AD
504 warn "Couldn't unlink $name: $!\n";
505 if ($! =~ /busy/i) {
94b11c62 506 print " mv $name $name.old\n" if $verbose;
68dc0745
PP
507 safe_rename($name, "$name.old")
508 or warn "Couldn't rename $name: $!\n";
e50aee73
AD
509 }
510 }
511}
512
68dc0745 513sub safe_rename {
c2cd2081 514 my($from,$to) = @_;
55a105fd 515 if (-f $to and not unlink($to)) {
e50aee73
AD
516 my($i);
517 for ($i = 1; $i < 50; $i++) {
91a06757 518 last if rename($to, "$to.$i");
e50aee73 519 }
39add537
PP
520 warn("Cannot rename to `$to.$i': $!"), return 0
521 if $i >= 50; # Give up!
e50aee73
AD
522 }
523 link($from,$to) || return 0;
55a105fd 524 unlink($from);
e50aee73
AD
525}
526
c623bd54 527sub link {
15792f64
PP
528 my($from,$to) = @_;
529 my($success) = 0;
c623bd54 530
94b11c62 531 print $verbose ? " ln $from $to\n" : " $to\n" unless $silent;
39add537 532 eval {
68dc0745
PP
533 CORE::link($from, $to)
534 ? $success++
7bac28a0
PP
535 : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
536 ? die "AFS" # okay inside eval {}
146174a9 537 : die "Couldn't link $from to $to: $!\n"
68dc0745 538 unless $nonono;
354f3b56 539 $packlist->{$to} = { from => $from, type => 'link' };
39add537
PP
540 };
541 if ($@) {
146174a9 542 warn $@;
94b11c62 543 print $verbose ? " cp $from $to\n" : " $to\n" unless $silent;
5f675712
A
544 print " creating new version of $to\n"
545 if $Is_VMS and -e $to and !$silent;
68dc0745
PP
546 File::Copy::copy($from, $to)
547 ? $success++
548 : warn "Couldn't copy $from to $to: $!\n"
549 unless $nonono;
354f3b56 550 $packlist->{$to} = { type => 'file' };
39add537 551 }
15792f64 552 $success;
c623bd54
LW
553}
554
555sub chmod {
c2cd2081 556 my($mode,$name) = @_;
c623bd54 557
39e571d4 558 return if ($^O eq 'dos');
94b11c62 559 printf " chmod %o %s\n", $mode, $name if $verbose;
68dc0745
PP
560 CORE::chmod($mode,$name)
561 || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
562 unless $nonono;
563}
564
565sub copy {
566 my($from,$to) = @_;
567
94b11c62 568 print $verbose ? " cp $from $to\n" : " $to\n" unless $silent;
5f675712 569 print " creating new version of $to\n" if $Is_VMS and -e $to and !$silent;
68dc0745
PP
570 File::Copy::copy($from, $to)
571 || warn "Couldn't copy $from to $to: $!\n"
572 unless $nonono;
354f3b56 573 $packlist->{$to} = { type => 'file' };
c623bd54
LW
574}
575
a0d0e21e 576sub samepath {
c2cd2081 577 my($p1, $p2) = @_;
eef1cf2a 578
4a71ed0c 579 return (lc($p1) eq lc($p2)) if $Is_W32;
a0d0e21e 580
75f92628 581 if ($p1 ne $p2) {
c2cd2081 582 my($dev1, $ino1, $dev2, $ino2);
a0d0e21e
LW
583 ($dev1, $ino1) = stat($p1);
584 ($dev2, $ino2) = stat($p2);
585 ($dev1 == $dev2 && $ino1 == $ino2);
586 }
587 else {
588 1;
589 }
590}
591
592sub installlib {
593 my $dir = $File::Find::dir;
594 $dir =~ s#^\.(?![^/])/?##;
0ecb046b 595 local($depth) = $dir ? "lib/$dir" : "lib";
a0d0e21e
LW
596
597 my $name = $_;
efc5ad56 598
146174a9
CB
599 # Ignore RCS and CVS directories.
600 if (($name eq 'CVS' or $name eq 'RCS') and -d $name) {
efc5ad56
TB
601 $File::Find::prune = 1;
602 return;
603 }
e50aee73 604
146174a9
CB
605 # ignore patch backups, RCS files, emacs backup & temp files and the
606 # .exists files.
607 return if $name =~ m{\.orig$|~$|^#.+#$|,v$|^\.exists};
e50aee73 608
a0d0e21e
LW
609 $name = "$dir/$name" if $dir ne '';
610
611 my $installlib = $installprivlib;
1dd15ed4 612 if ($dir =~ /^auto/ ||
eef1cf2a 613 ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
4a71ed0c 614 ($name =~ /^(.*)\.(?:h|lib)$/i && $Is_W32)
eef1cf2a 615 ) {
a0d0e21e
LW
616 $installlib = $installarchlib;
617 return unless $do_installarchlib;
618 } else {
619 return unless $do_installprivlib;
620 }
621
a0d0e21e 622 if (-f $_) {
5cc330c8 623 if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
3edbfbe5
TB
624 $installlib = $installprivlib;
625 #We're installing *.al and *.ix files into $installprivlib,
626 #but we have to delete old *.al and *.ix files from the 5.000
627 #distribution:
75f92628 628 #This might not work because $archname might have changed.
55a105fd 629 unlink("$installarchlib/$name");
3edbfbe5 630 }
354f3b56 631 $packlist->{"$installlib/$name"} = { type => 'file' };
5f05dabc 632 if (compare($_, "$installlib/$name") || $nonono) {
55a105fd 633 unlink("$installlib/$name");
7e7af249 634 mkpath("$installlib/$dir", $verbose, 0777);
75f92628
AD
635 # HP-UX (at least) needs to maintain execute permissions
636 # on dynamically-loaded libraries.
68dc0745 637 copy_if_diff($_, "$installlib/$name")
55a105fd 638 and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
aa3cf465 639 "$installlib/$name");
a0d0e21e 640 }
a0d0e21e
LW
641 }
642}
3edbfbe5 643
16d20bd9
AD
644# Copy $from to $to, only if $from is different than $to.
645# Also preserve modification times for .a libraries.
646# On some systems, if you do
647# ranlib libperl.a
648# cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
649# and then try to link against the installed libperl.a, you might
650# get an error message to the effect that the symbol table is older
651# than the library.
aa3cf465 652# Return true if copying occurred.
68dc0745
PP
653
654sub copy_if_diff {
3edbfbe5 655 my($from,$to)=@_;
8530e788 656 return 1 if (($^O eq 'VMS') && (-d $from));
f0963acb 657 -f $from || warn "$0: $from not found";
354f3b56 658 $packlist->{$to} = { type => 'file' };
5f05dabc 659 if (compare($from, $to) || $nonono) {
68dc0745 660 safe_unlink($to); # In case we don't have write permissions.
0ecb046b 661 if ($nonono) {
662 $from = $depth . "/" . $from if $depth;
663 }
68dc0745
PP
664 copy($from, $to);
665 # Restore timestamps if it's a .a library or for OS/2.
4a71ed0c 666 if (!$nonono && ($Is_OS2 || $to =~ /\.a$/)) {
68dc0745 667 my ($atime, $mtime) = (stat $from)[8,9];
16d20bd9
AD
668 utime $atime, $mtime, $to;
669 }
aa3cf465 670 1;
3edbfbe5
TB
671 }
672}
8f1f23e8
W
673
674sub strip
675{
676 my(@args) = @_;
677
f556e5b9
JH
678 return unless $dostrip;
679
8f1f23e8
W
680 my @opts;
681 while (@args && $args[0] =~ /^(-\w+)$/) {
682 push @opts, shift @args;
683 }
684
685 foreach my $file (@args) {
686 if (-f $file) {
94b11c62 687 print " strip $file\n" if $verbose;
8f1f23e8
W
688 system("strip", @opts, $file);
689 } else {
94b11c62 690 print "# file '$file' skipped\n" if $verbose;
8f1f23e8
W
691 }
692 }
693}
694