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