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