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