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