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