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