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