fc7ccb540e4e48ea7e9904e613523c6f80d3207d
[p5sagit/p5-mst-13.2.git] / installperl
1 #!./perl
2
3 BEGIN {
4     require 5.004;
5     chdir '..' if !-d 'lib' and -d '..\lib';
6     @INC = 'lib';
7     $ENV{PERL5LIB} = 'lib';
8 }
9
10 use strict;
11 my ($Is_VMS, $Is_W32, $Is_OS2, $Is_Cygwin, $Is_Darwin,
12     $nonono, $dostrip, $versiononly, $silent, $verbose,
13     $otherperls, $archname,$Is_NetWare, $nwinstall);
14 use vars qw /$depth/;
15
16 BEGIN {
17     $Is_VMS = $^O eq 'VMS';
18     $Is_W32 = $^O eq 'MSWin32';
19     $Is_OS2 = $^O eq 'os2';
20     $Is_Cygwin = $^O eq 'cygwin';
21     $Is_Darwin = $^O eq 'darwin';
22     if ($Is_VMS) { eval 'use VMS::Filespec;' }
23 }
24
25 my $scr_ext = ($Is_VMS ? '.Com' : $Is_W32 ? '.bat' : '');
26
27 use File::Find;
28 use File::Compare;
29 use File::Copy ();
30 use File::Path ();
31 use ExtUtils::Packlist;
32 use Config;
33 use subs qw(unlink link chmod);
34
35 if ($Config{d_umask}) {
36     umask(022); # umasks like 077 aren't that useful for installations
37 }
38
39 $Is_NetWare = $Config{osname} eq 'NetWare';
40 if ($Is_NetWare) {
41         $Is_W32 = 0;
42         $scr_ext = '.pl';
43 }
44
45 # override the ones in the rest of the script
46 sub mkpath {
47     File::Path::mkpath(@_) unless $nonono;
48 }
49
50 my $mainperldir = "/usr/bin";
51 my $exe_ext = $Config{exe_ext};
52
53 # Allow ``make install PERLNAME=something_besides_perl'':
54 my $perl = defined($ENV{PERLNAME}) ? $ENV{PERLNAME} : 'perl';
55
56 # This is the base used for versioned names, like "perl5.6.0".
57 # It's separate because a common use of $PERLNAME is to install
58 # perl as "perl5", if that's used as base for versioned files you
59 # get "perl55.6.0".
60 my $perl_verbase = defined($ENV{PERLNAME_VERBASE})
61                     ? $ENV{PERLNAME_VERBASE}
62                     : $perl;
63
64 $otherperls = 1;
65 while (@ARGV) {
66     $nonono = 1 if $ARGV[0] eq '-n';
67     $dostrip = 1 if $ARGV[0] eq '-s';
68     $versiononly = 1 if $ARGV[0] eq '-v';
69     $versiononly = 0 if $ARGV[0] eq '+v';
70     $silent = 1 if $ARGV[0] eq '-S';
71     $otherperls = 0 if $ARGV[0] eq '-o';
72     $verbose = 1 if $ARGV[0] eq '-V' || $ARGV [0] eq '-n';
73     $archname = 1 if $ARGV[0] eq '-A';
74         $nwinstall = 1 if $ARGV[0] eq '-netware';
75     if ($ARGV[0] eq '-?' or $ARGV[0] =~ /^-?-h/) {
76       print <<"EOT";
77 Usage $0: [switches]
78   -n        Don't actually run any commands; just print them.
79   -s        Run strip on installed binaries.
80   -v        Only install perl as a binary with the version number in the name.
81             (Override whatever config.sh says)
82   +v        Install perl as "perl" and as a binary with the version number in
83             the name.  (Override whatever config.sh says)
84   -S        Silent mode.
85   -o        Skip checking for other copies of perl in your PATH.
86   -V        Verbose mode.
87   -A        Also install perl with the architecture's name in the perl binary's
88             name.
89   -netware  Install correctly on a Netware server.
90 EOT
91       exit;
92     }
93     shift;
94 }
95
96 $versiononly = 1 if $Config{versiononly} && !defined $versiononly;
97 my (@scripts, @tolink);
98 open SCRIPTS, "utils.lst" or die "Can't open utils.lst: $!";
99 while (<SCRIPTS>) {
100     next if /^#/;
101     next if /#\s*pod\s*=/; # Binary programs need separate treatment
102     chomp;
103     if (/(\S*)\s*#\s*link\s*=\s*(\S*)/) {
104         push @scripts, $1;
105         push @tolink, [$1, $2];
106     } else {
107         push @scripts, $_;
108     }
109 }
110 close SCRIPTS;
111
112 if ($scr_ext) { @scripts = map { "$_$scr_ext" } @scripts; }
113
114 my @pods = (<pod/*.pod>);
115
116 # Specify here any .pm files that are actually architecture-dependent.
117 # (Those included with XS extensions under ext/ are automatically
118 # added later.)
119 # Now that the default privlib has the full perl version number included,
120 # we no longer have to play the trick of sticking version-specific .pm
121 # files under the archlib directory.
122 my %archpms = (
123     Config => 1,
124     lib => 1,
125     Cwd => 1,
126 );
127
128 if ($^O eq 'dos') {
129     push(@scripts,'djgpp/fixpmain');
130     $archpms{config} = $archpms{filehand} = 1;
131 }
132
133 if ((-e "testcompile") && (defined($ENV{'COMPILE'})))
134 {
135         push(@scripts, map("$_.exe", @scripts));
136 }
137
138 find(sub {
139         if ("$File::Find::dir/$_" =~ m{^ext\b(.*)/([^/]+)\.pm$}) {
140             my($path, $modname) = ($1,$2);
141
142             # strip trailing component first
143             $path =~ s{/[^/]*$}{};
144
145             # strip optional "/lib";
146             $path =~ s{/lib\b}{};
147
148             # strip any leading /
149             $path =~ s{^/}{};
150
151             # reconstitute canonical module name
152             $modname = "$path/$modname" if length $path;
153
154             # remember it
155             $archpms{$modname} = 1;
156         }
157     }, 'ext');
158
159 # print "[$_]\n" for sort keys %archpms;
160
161 my $ver = $Config{version};
162 my $release = substr($],0,3);   # Not used currently.
163 my $patchlevel = substr($],3,2);
164 die "Patchlevel of perl ($patchlevel)",
165     "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
166         if $patchlevel != $Config{'PERL_VERSION'};
167
168 # Fetch some frequently-used items from %Config
169 my $installbin = $Config{installbin};
170 my $installscript = $Config{installscript};
171 my $installprivlib = $Config{installprivlib};
172 my $installarchlib = $Config{installarchlib};
173 my $installsitelib = $Config{installsitelib};
174 my $installsitearch = $Config{installsitearch};
175 my $installman1dir = $Config{installman1dir};
176 my $man1ext = $Config{man1ext};
177 my $libperl = $Config{libperl};
178 # Shared library and dynamic loading suffixes.
179 my $so = $Config{so};
180 my $dlext = $Config{dlext};
181 my $dlsrc = $Config{dlsrc};
182 if ($^O eq 'os390') {
183     my $pwd;
184     chomp($pwd=`pwd`);
185     my $archlibexp = $Config{archlibexp};
186     my $usedl = $Config{usedl};
187     if ($usedl eq 'define') {
188         `./$^X -pibak -e 's{$pwd\/libperl.x}{$archlibexp/CORE/libperl.x}' lib/Config.pm`;
189     }
190 }
191
192 if ($nwinstall) {
193         # This is required only if we are installing on a NetWare server
194         $installscript = $Config{installnwscripts};
195         $installprivlib = $Config{installnwlib};
196         $installarchlib = $Config{installnwlib};
197         $installsitelib = $Config{installnwlib};
198 }
199
200 my $d_dosuid = $Config{d_dosuid};
201 my $binexp = $Config{binexp};
202
203 if ($Is_VMS) {  # Hang in there until File::Spec hits the big time
204     foreach ( \$installbin,     \$installscript,  \$installprivlib,
205               \$installarchlib, \$installsitelib, \$installsitearch,
206               \$installman1dir ) {
207       $$_ = unixify($$_);  $$_ =~ s:/$::;
208     }
209 }
210
211 # Do some quick sanity checks.
212
213 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
214
215    $installbin          || die "No installbin directory in config.sh\n";
216 -d $installbin          || mkpath($installbin, $verbose, 0777);
217 -d $installbin          || $nonono || die "$installbin is not a directory\n";
218 -w $installbin          || $nonono || die "$installbin is not writable by you\n"
219         unless $installbin =~ m#^/afs/# || $nonono;
220
221 if (!$Is_NetWare) {
222 -x 'perl' . $exe_ext    || die "perl isn't executable!\n";
223 -x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
224
225 -f 't/rantests'         || $Is_W32
226                         || warn "WARNING: You've never run 'make test' or",
227                                 " some tests failed! (Installing anyway.)\n";
228 } #if (!$Is_NetWare)
229
230 if (($Is_W32 and ! $Is_NetWare)  or $Is_Cygwin) {
231   my $perldll;
232
233   if ($Is_Cygwin) {
234     $perldll = $libperl;
235     $perldll =~ s/(\..*)?$/.$dlext/;
236     if ($Config{useshrplib} eq 'true') {
237       # install ld2 and perlld as well
238       foreach ('ld2', 'perlld') {
239         safe_unlink("$installbin/$_");
240         copy("$_", "$installbin/$_");
241         chmod(0755, "$installbin/$_");
242       };
243     };
244   } else {
245     $perldll = 'perl57.' . $dlext;
246   }
247
248   if ($dlsrc ne "dl_none.xs") {
249     -f $perldll || die "No perl DLL built\n";
250   }
251   # Install the DLL
252
253   safe_unlink("$installbin/$perldll");
254   copy("$perldll", "$installbin/$perldll");
255   chmod(0755, "$installbin/$perldll");
256
257 } # if (($Is_W32 and ! $Is_NetWare) or $Is_Cygwin)
258
259 # This will be used to store the packlist
260 my $packlist = ExtUtils::Packlist->new("$installarchlib/.packlist");
261
262 # First we install the version-numbered executables.
263
264 if ($Is_VMS) {
265     safe_unlink("$installbin/$perl$exe_ext");
266     copy("perl$exe_ext", "$installbin/$perl$exe_ext");
267     chmod(0755, "$installbin/$perl$exe_ext");
268     safe_unlink("$installbin/${perl}shr$exe_ext");
269     copy("perlshr$exe_ext", "$installbin/${perl}shr$exe_ext");
270     chmod(0755, "$installbin/${perl}shr$exe_ext");
271 }
272 elsif ($^O eq 'mpeix') {
273     # MPE lacks hard links and requires that executables with special
274     # capabilities reside in the MPE namespace.
275     safe_unlink("$installbin/perl$ver$exe_ext", $Config{perlpath});
276     # Install the primary executable into the MPE namespace as perlpath.
277     copy("perl$exe_ext", $Config{perlpath});
278     chmod(0755, $Config{perlpath});
279     # Create a backup copy with the version number.
280     link($Config{perlpath}, "$installbin/perl$ver$exe_ext");
281 }
282 elsif ($^O ne 'dos') {
283         if (!$Is_NetWare) {
284                 safe_unlink("$installbin/$perl_verbase$ver$exe_ext");
285                 copy("perl$exe_ext", "$installbin/$perl_verbase$ver$exe_ext");
286                 strip("$installbin/$perl_verbase$ver$exe_ext");
287                 chmod(0755, "$installbin/$perl_verbase$ver$exe_ext");
288         }
289         else {
290                 # If installing onto a NetWare server
291                 if ($nwinstall) {
292                         # Copy perl.nlm, echo.nlm, type.nlm, a2p.nlm & cgi2perl.nlm
293                         mkpath($Config{installnwsystem}, 1, 0777);
294                         copy("netware\\".$ENV{'MAKE_TYPE'}."\\perl.nlm", $Config{installnwsystem});
295                         copy("netware\\testnlm\\echo\\echo.nlm", $Config{installnwsystem});
296                         copy("netware\\testnlm\\type\\type.nlm", $Config{installnwsystem});
297                         copy("x2p\\a2p.nlm", $Config{installnwsystem});
298                         chmod(0755, "$Config{installnwsystem}\\perl.nlm");
299                         mkpath($Config{installnwlcgi}, 1, 0777);
300                         copy("lib\\auto\\cgi2perl\\cgi2perl.nlm", $Config{installnwlcgi});
301                 }
302         } #if (!$Is_NetWare)
303 }
304 else {
305     safe_unlink("$installbin/$perl.exe");
306     copy("perl.exe", "$installbin/$perl.exe");
307 }
308
309 safe_unlink("$installbin/s$perl_verbase$ver$exe_ext");
310 if ($d_dosuid) {
311     copy("suidperl$exe_ext", "$installbin/s$perl_verbase$ver$exe_ext");
312     chmod(04711, "$installbin/s$perl_verbase$ver$exe_ext");
313 }
314
315 # Install library files.
316
317 my ($do_installarchlib, $do_installprivlib) = (0, 0);
318
319 mkpath($installprivlib, $verbose, 0777);
320 mkpath($installarchlib, $verbose, 0777);
321 mkpath($installsitelib, $verbose, 0777) if ($installsitelib);
322 mkpath($installsitearch, $verbose, 0777) if ($installsitearch);
323
324 if (chdir "lib") {
325     $do_installarchlib = ! samepath($installarchlib, '.');
326     $do_installprivlib = ! samepath($installprivlib, '.');
327     $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$ver/);
328
329     if ($do_installarchlib || $do_installprivlib) {
330         find(\&installlib, '.');
331     }
332     chdir ".." || die "Can't cd back to source directory: $!\n";
333 }
334 else {
335     warn "Can't cd to lib to install lib files: $!\n";
336 }
337
338 # Install header files and libraries.
339 mkpath("$installarchlib/CORE", $verbose, 0777);
340 my @corefiles;
341 if ($Is_VMS) {  # We did core file selection during build
342     my $coredir = "lib/$Config{archname}/$ver/CORE";
343     $coredir =~ tr/./_/;
344     map { s|^$coredir/||i; } @corefiles = <$coredir/*.*>;
345 }
346 else {
347     # [als] hard-coded 'libperl' name... not good!
348     @corefiles = <*.h libperl*.*>;
349
350     # AIX needs perl.exp installed as well.
351     push(@corefiles,'perl.exp') if $^O eq 'aix';
352     if ($^O eq 'mpeix') {
353         # MPE needs mpeixish.h installed as well.
354         mkpath("$installarchlib/CORE/mpeix", $verbose, 0777);
355         push(@corefiles,'mpeix/mpeixish.h');
356     }
357     # If they have built sperl.o...
358     push(@corefiles,'sperl.o') if -f 'sperl.o';
359 }
360 foreach my $file (@corefiles) {
361     # HP-UX (at least) needs to maintain execute permissions
362     # on dynamically-loadable libraries. So we do it for all.
363     if (copy_if_diff($file,"$installarchlib/CORE/$file")) {
364         if ($file =~ /\.(\Q$so\E|\Q$dlext\E)$/) {
365             strip("-S", "$installarchlib/CORE/$file") if $^O =~ /^(rhapsody|darwin)$/;
366            chmod(0555, "$installarchlib/CORE/$file");
367         } else {
368             chmod(0444, "$installarchlib/CORE/$file");
369         }
370     }
371 }
372
373 # Install main perl executables
374 # Make links to ordinary names if installbin directory isn't current directory.
375
376 if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS && ! $Is_NetWare) {
377     safe_unlink("$installbin/$perl$exe_ext", "$installbin/suid$perl$exe_ext");
378     if ($^O eq 'mpeix') {
379         # MPE doesn't support hard links, so use a symlink.
380         # We don't want another cloned copy.
381         symlink($Config{perlpath}, "$installbin/perl$exe_ext");
382     } else {
383         link("$installbin/$perl_verbase$ver$exe_ext",
384                 "$installbin/$perl$exe_ext");
385     }
386     link("$installbin/s$perl_verbase$ver$exe_ext",
387             "$installbin/suid$perl$exe_ext")
388       if $d_dosuid;
389 }
390
391 # For development purposes it can be very useful to have multiple perls
392 # build for different "architectures" (eg threading or not) simultaneously.
393 if ($archname && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS) {
394     my $archperl = "$perl_verbase$ver-$Config{archname}$exe_ext";
395     safe_unlink("$installbin/$archperl");
396     if ($^O eq 'mpeix') {
397         # MPE doesn't support hard links, so use a symlink.
398         # We don't want another cloned copy.
399         symlink($Config{perlpath}, "$installbin/$archperl");
400     } else {
401         link("$installbin/$perl_verbase$ver$exe_ext",
402                 "$installbin/$archperl");
403     }
404 }
405
406 # Offer to install perl in a "standard" location
407
408 my $mainperl_is_instperl = 0;
409
410 if ($Config{installusrbinperl} && $Config{installusrbinperl} eq 'define' &&
411     !$versiononly && !$nonono && !$Is_W32 && !$Is_NetWare && !$Is_VMS && -t STDIN && -t STDERR
412         && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
413     my($usrbinperl)     = "$mainperldir/$perl$exe_ext";
414     my($instperl)       = "$installbin/$perl$exe_ext";
415     my($expinstperl)    = "$binexp/$perl$exe_ext";
416
417     # First make sure $usrbinperl is not already the same as the perl we
418     # just installed.
419     if (-x $usrbinperl) {
420         # Try to be clever about mainperl being a symbolic link
421         # to binexp/perl if binexp and installbin are different.
422         $mainperl_is_instperl =
423             samepath($usrbinperl, $instperl) ||
424             samepath($usrbinperl, $expinstperl) ||
425              (($binexp ne $installbin) &&
426               (-l $usrbinperl) &&
427               ((readlink $usrbinperl) eq $expinstperl));
428     }
429     if (! $mainperl_is_instperl) {
430         unlink($usrbinperl);
431         ( $Config{'d_link'} eq 'define' &&
432           eval { CORE::link $instperl, $usrbinperl } )  ||
433         eval { symlink $expinstperl, $usrbinperl }      ||
434         copy($instperl, $usrbinperl);
435
436         $mainperl_is_instperl = 1;
437     }
438 }
439
440 # Make links to ordinary names if installbin directory isn't current directory.
441 if (!$Is_NetWare) {
442         if (!$versiononly && ! samepath($installbin, 'x2p')) {
443                 safe_unlink("$installbin/a2p$exe_ext");
444                 copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
445                strip("$installbin/a2p$exe_ext");
446                 chmod(0755, "$installbin/a2p$exe_ext");
447         }
448 }
449
450 # cppstdin is just a script, but it is architecture-dependent, so
451 # it can't safely be shared.  Place it in $installbin.
452 # Note that Configure doesn't build cppstin if it isn't needed, so
453 # we skip this if cppstdin doesn't exist.
454 if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
455     safe_unlink("$installbin/cppstdin");
456     copy("cppstdin", "$installbin/cppstdin");
457     chmod(0755, "$installbin/cppstdin");
458 }
459
460 sub script_alias {
461     my ($installscript, $orig, $alias, $scr_ext) = @_;
462
463     safe_unlink("$installscript/$alias$scr_ext");
464     if ($^O eq 'dos' or $Is_VMS or $^O eq 'transit') {
465         copy("$installscript/$orig$scr_ext",
466              "$installscript/$alias$scr_ext");
467     } else {
468         link("$installscript/$orig$scr_ext",
469              "$installscript/$alias$scr_ext");
470     }
471 }
472
473 # Install scripts.
474 mkpath($installscript, $verbose, 0777);
475 if ($versiononly) {
476     for (@scripts) {
477         (my $base = $_) =~ s#.*/##;
478         $base .= $ver;
479         copy($_,    "$installscript/$base");
480         chmod(0755, "$installscript/$base");
481     }
482
483     for (@tolink) {
484         my ($from, $to) = map { "$_$ver" } @$_;
485         (my $frbase = $from) =~ s#.*/##;
486         (my $tobase = $to) =~ s#.*/##;
487         script_alias($installscript, $frbase, $tobase, $scr_ext);
488     }
489 } else {
490     for (@scripts) {
491         (my $base = $_) =~ s#.*/##;
492         copy($_, "$installscript/$base");
493         chmod(0755, "$installscript/$base");
494     }
495
496     for (@tolink) {
497         my ($from, $to) = @$_;
498         (my $frbase = $from) =~ s#.*/##;
499         (my $tobase = $to) =~ s#.*/##;
500         script_alias($installscript, $frbase, $tobase, $scr_ext);
501     }
502 }
503
504 # Install pod pages.  Where? I guess in $installprivlib/pod
505 # ($installprivlib/pods for cygwin).
506
507 my $pod = ($Is_Cygwin || $Is_Darwin) ? 'pods' : 'pod';
508 if ( !$versiononly || ($installprivlib =~ m/\Q$ver/)) {
509     mkpath("${installprivlib}/$pod", $verbose, 0777);
510
511     # If Perl 5.003's perldiag.pod is there, rename it.
512     if (open POD, "${installprivlib}/$pod/perldiag.pod") {
513         read POD, $_, 4000;
514         close POD;
515         # Some of Perl 5.003's diagnostic messages ended with periods.
516         if (/^=.*\.$/m) {
517             my ($from, $to) = ("${installprivlib}/$pod/perldiag.pod",
518                                "${installprivlib}/$pod/perldiag-5.003.pod");
519             print "  rename $from $to";
520             rename($from, $to)
521                 or warn "Couldn't rename $from to $to: $!\n"
522                 unless $nonono;
523         }
524     }
525
526     for (@pods) {
527         # $_ is a name like  pod/perl.pod
528         (my $base = $_) =~ s#.*/##;
529         copy_if_diff($_, "${installprivlib}/$pod/${base}");
530     }
531
532 }
533
534 # Check to make sure there aren't other perls around in installer's
535 # path.  This is probably UNIX-specific.  Check all absolute directories
536 # in the path except for where public executables are supposed to live.
537 # Also skip $mainperl if the user opted to have it be a link to the
538 # installed perl.
539
540 if (!$versiononly && $otherperls) {
541     my ($path, @path);
542     my $dirsep = ($Is_OS2 || $Is_W32 || $Is_NetWare) ? ';' : ':' ;
543     ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
544     @path = split(/$dirsep/, $path);
545     if ($Is_VMS) {
546         my $i = 0;
547         while (exists $ENV{'DCL$PATH' . $i}) {
548             my $dir = unixpath($ENV{'DCL$PATH' . $i});  $dir =~ s-/$--;
549             push(@path,$dir);
550         }
551     }
552     my @otherperls;
553     my %otherperls;
554     for (@path) {
555         next unless m,^/,;
556         # Use &samepath here because some systems have other dirs linked
557         # to $mainperldir (like SunOS)
558         next if samepath($_, $binexp);
559         next if ($mainperl_is_instperl && samepath($_, $mainperldir));
560         my $otherperl = "$_/$perl$exe_ext";
561         next if $otherperls{$otherperl}++;
562         push(@otherperls, $otherperl)
563             if (-x $otherperl && ! -d $otherperl);
564     }
565     if (@otherperls) {
566         warn "\nWarning: $perl appears in your path in the following " .
567             "locations beyond where\nwe just installed it:\n";
568         for (@otherperls) {
569             warn "    ", $_, "\n";
570         }
571         warn "\n";
572     }
573
574 }
575
576 $packlist->write() unless $nonono;
577 print "  Installation complete\n" if $verbose;
578
579 exit 0;
580
581 ###############################################################################
582
583 sub yn {
584     my($prompt) = @_;
585     my($answer);
586     my($default) = $prompt =~ m/\[([yn])\]\s*$/i;
587     print STDERR $prompt;
588     chop($answer = <STDIN>);
589     $answer = $default if $answer =~ m/^\s*$/;
590     ($answer =~ m/^[yY]/);
591 }
592
593 sub unlink {
594     my(@names) = @_;
595     my($cnt) = 0;
596
597     return scalar(@names) if $Is_VMS;
598
599     foreach my $name (@names) {
600         next unless -e $name;
601         chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin || $Is_NetWare);
602         print "  unlink $name\n" if $verbose;
603         ( CORE::unlink($name) and ++$cnt
604           or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
605     }
606     return $cnt;
607 }
608
609 sub safe_unlink {
610     return if $nonono or $Is_VMS;
611     my @names = @_;
612     foreach my $name (@names) {
613         next unless -e $name;
614         chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_NetWare);
615         print "  unlink $name\n" if $verbose;
616         next if CORE::unlink($name);
617         warn "Couldn't unlink $name: $!\n";
618         if ($! =~ /busy/i) {
619             print "  mv $name $name.old\n" if $verbose;
620             safe_rename($name, "$name.old")
621                 or warn "Couldn't rename $name: $!\n";
622         }
623     }
624 }
625
626 sub safe_rename {
627     my($from,$to) = @_;
628     if (-f $to and not unlink($to)) {
629         my($i);
630         for ($i = 1; $i < 50; $i++) {
631             last if rename($to, "$to.$i");
632         }
633         warn("Cannot rename to `$to.$i': $!"), return 0
634            if $i >= 50; # Give up!
635     }
636     link($from,$to) || return 0;
637     unlink($from);
638 }
639
640 sub link {
641     my($from,$to) = @_;
642     my($success) = 0;
643
644     print $verbose ? "  ln $from $to\n" : "  $to\n" unless $silent;
645     eval {
646         CORE::link($from, $to)
647             ? $success++
648             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
649               ? die "AFS"  # okay inside eval {}
650               : die "Couldn't link $from to $to: $!\n"
651           unless $nonono;
652         $packlist->{$to} = { from => $from, type => 'link' };
653     };
654     if ($@) {
655         warn $@;
656         print $verbose ? "  cp $from $to\n" : "  $to\n" unless $silent;
657         print "  creating new version of $to\n"
658                  if $Is_VMS and -e $to and !$silent;
659         File::Copy::copy($from, $to)
660             ? $success++
661             : warn "Couldn't copy $from to $to: $!\n"
662           unless $nonono;
663         $packlist->{$to} = { type => 'file' };
664     }
665     $success;
666 }
667
668 sub chmod {
669     my($mode,$name) = @_;
670
671     return if ($^O eq 'dos');
672     printf "  chmod %o %s\n", $mode, $name if $verbose;
673     CORE::chmod($mode,$name)
674         || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
675       unless $nonono;
676 }
677
678 sub copy {
679     my($from,$to) = @_;
680
681     print $verbose ? "  cp $from $to\n" : "  $to\n" unless $silent;
682     print "  creating new version of $to\n" if $Is_VMS and -e $to and !$silent;
683     File::Copy::copy($from, $to)
684         || warn "Couldn't copy $from to $to: $!\n"
685       unless $nonono;
686     $packlist->{$to} = { type => 'file' };
687 }
688
689 sub samepath {
690     my($p1, $p2) = @_;
691
692     return (lc($p1) eq lc($p2)) if ($Is_W32 || $Is_NetWare);
693
694     if ($p1 ne $p2) {
695         my($dev1, $ino1, $dev2, $ino2);
696         ($dev1, $ino1) = stat($p1);
697         ($dev2, $ino2) = stat($p2);
698         ($dev1 == $dev2 && $ino1 == $ino2);
699     }
700     else {
701         1;
702     }
703 }
704
705 sub installlib {
706     my $dir = $File::Find::dir;
707     $dir =~ s#^\.(?![^/])/?##;
708     local($depth) = $dir ? "lib/$dir" : "lib";
709
710     my $name = $_;
711
712     # Ignore RCS and CVS directories.
713     if (($name eq 'CVS' or $name eq 'RCS') and -d $name) {
714         $File::Find::prune = 1;
715         return;
716     }
717
718     # ignore patch backups, RCS files, emacs backup & temp files and the
719     # .exists files, .PL files, and .t files.
720     return if $name =~ m{\.orig$|~$|^#.+#$|,v$|^\.exists|\.PL$|\.t$};
721
722     $name = "$dir/$name" if $dir ne '';
723
724     my $installlib = $installprivlib;
725     if ($dir =~ /^auto/ ||
726           ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
727           ($name =~ /^(.*)\.(?:h|lib)$/i && ($Is_W32 || $Is_NetWare))
728        ) {
729         $installlib = $installarchlib;
730         return unless $do_installarchlib;
731     } else {
732         return unless $do_installprivlib;
733     }
734
735     if (-f $_) {
736         if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
737             $installlib = $installprivlib;
738             #We're installing *.al and *.ix files into $installprivlib,
739             #but we have to delete old *.al and *.ix files from the 5.000
740             #distribution:
741             #This might not work because $archname might have changed.
742             unlink("$installarchlib/$name");
743         }
744         $packlist->{"$installlib/$name"} = { type => 'file' };
745         if (compare($_, "$installlib/$name") || $nonono) {
746             unlink("$installlib/$name");
747             mkpath("$installlib/$dir", $verbose, 0777);
748             # HP-UX (at least) needs to maintain execute permissions
749             # on dynamically-loaded libraries.
750            if ($Is_NetWare && !$nwinstall) {
751                # Don't copy .nlp,.nlm files, doesn't make sense on Windows and also
752                # if copied will give problems when building new extensions.
753                # Has to be copied if we are installing on a NetWare server and hence
754                # the check !$nwinstall
755                if (!(/\.(?:nlp|nlm|bs)$/)) {
756                    copy_if_diff($_, "$installlib/$name")
757                         and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
758                                  "$installlib/$name");
759                }
760            } else {
761                if (copy_if_diff($_, "$installlib/$name")) {
762                    if ($name =~ /\.(so|$dlext)$/o) {
763                        strip("-S", "$installlib/$name") if $^O =~ /^(rhapsody|darwin)$/;
764                        chmod(0555, "$installlib/$name");
765                    } else {
766                        strip("-S", "$installlib/$name")
767                            if ($name =~ /\.a$/o and $^O =~ /^(rhapsody|darwin)$/);
768                        chmod(0444, "$installlib/$name");
769                    }
770                }
771            } #if ($Is_NetWare)
772         }
773     }
774 }
775
776 # Copy $from to $to, only if $from is different than $to.
777 # Also preserve modification times for .a libraries.
778 # On some systems, if you do
779 #   ranlib libperl.a
780 #   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
781 # and then try to link against the installed libperl.a, you might
782 # get an error message to the effect that the symbol table is older
783 # than the library.
784 # Return true if copying occurred.
785
786 sub copy_if_diff {
787     my($from,$to)=@_;
788     return 1 if (($^O eq 'VMS') && (-d $from));
789     -f $from || warn "$0: $from not found";
790     $packlist->{$to} = { type => 'file' };
791     if (compare($from, $to) || $nonono) {
792         safe_unlink($to);   # In case we don't have write permissions.
793         if ($nonono) {
794             $from = $depth . "/" . $from if $depth;
795         }
796         copy($from, $to);
797         # Restore timestamps if it's a .a library or for OS/2.
798         if (!$nonono && ($Is_OS2 || $to =~ /\.a$/)) {
799             my ($atime, $mtime) = (stat $from)[8,9];
800             utime $atime, $mtime, $to;
801         }
802         1;
803     }
804 }
805
806 sub strip
807 {
808     my(@args) = @_;
809
810     return unless $dostrip;
811
812     my @opts;
813     while (@args && $args[0] =~ /^(-\w+)$/) {
814         push @opts, shift @args;
815     }
816
817     foreach my $file (@args) {
818         if (-f $file) {
819            if ($verbose) {
820                print "  strip " . join(' ', @opts);
821                print " " if (@opts);
822                print "$file\n";
823            }
824             system("strip", @opts, $file);
825         } else {
826             print "# file '$file' skipped\n" if $verbose;
827         }
828     }
829 }