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