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