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