93b9947d941b8005d2213783ef566c070a0adc76
[p5sagit/p5-mst-13.2.git] / installperl
1 #!./perl
2
3 BEGIN {
4     require 5.004;
5     chdir '..' if !-d 'lib' and -d '..\lib';
6     @INC = 'lib';
7     $ENV{PERL5LIB} = 'lib';
8 }
9
10 use strict;
11 use vars qw($Is_VMS $Is_W32 $Is_OS2 $nonono $versiononly $depth);
12
13 BEGIN {
14     $Is_VMS = $^O eq 'VMS';
15     $Is_W32 = $^O eq 'MSWin32';
16     $Is_OS2 = $^O eq 'os2';
17     if ($Is_VMS) { eval 'use VMS::Filespec;' }
18 }
19
20 my $scr_ext = ($Is_VMS ? '.Com' : $Is_W32 ? '.bat' : '');
21
22 use File::Find;
23 use File::Compare;
24 use File::Copy ();
25 use File::Path ();
26 use ExtUtils::Packlist;
27 use Config;
28 use subs qw(unlink link chmod);
29 use vars qw($packlist);
30
31 # override the ones in the rest of the script
32 sub mkpath {
33     File::Path::mkpath(@_) unless $nonono;
34 }
35
36 my $mainperldir = "/usr/bin";
37 my $exe_ext = $Config{exe_ext};
38
39 # Allow ``make install PERLNAME=something_besides_perl'':
40 my $perl = defined($ENV{PERLNAME}) ? $ENV{PERLNAME} : 'perl';
41
42 while (@ARGV) {
43     $nonono = 1 if $ARGV[0] eq '-n';
44     $versiononly = 1 if $ARGV[0] eq '-v';
45     shift;
46 }
47
48 umask 022 unless $Is_VMS;
49
50 my @scripts = qw(utils/c2ph utils/h2ph utils/h2xs utils/perlbug utils/perldoc
51                 utils/pl2pm utils/splain utils/perlcc
52                 x2p/s2p x2p/find2perl 
53                 pod/pod2man pod/pod2html pod/pod2latex pod/pod2text
54                 pod/pod2usage pod/podchecker pod/podselect);
55
56 if ($scr_ext) { @scripts = map { "$_$scr_ext" } @scripts; }
57
58 my @pods = (<pod/*.pod>);
59
60 # Specify here any .pm files that are actually architecture-dependent.
61 # (Those included with XS extensions under ext/ are automatically
62 # added later.)
63 # Now that the default privlib has the full perl version number included,
64 # we no longer have to play the trick of sticking version-specific .pm 
65 # files under the archlib directory.
66 my %archpms = (
67     Config => 1, 
68 );
69
70 if ($^O eq 'dos') {
71     push(@scripts,'djgpp/fixpmain');
72     $archpms{config} = $archpms{filehand} = 1;
73 }
74
75 if ((-e "testcompile") && (defined($ENV{'COMPILE'})))
76 {
77         push(@scripts, map("$_.exe", @scripts));
78 }
79
80 find(sub {
81         if ("$File::Find::dir/$_" =~ m{^ext/[^/]+/(.*)\.pm$}) {
82             (my $pm = $1) =~ s{^lib/}{};
83             $archpms{$pm} = 1;
84         }
85     }, 'ext');
86
87 my $ver = $];
88 my $release = substr($ver,0,3);   # Not used presently.
89 my $patchlevel = substr($ver,3,2);
90 die "Patchlevel of perl ($patchlevel)",
91     "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
92         if $patchlevel != $Config{'PERL_VERSION'};
93
94 # Fetch some frequently-used items from %Config
95 my $installbin = $Config{installbin};
96 my $installscript = $Config{installscript};
97 my $installprivlib = $Config{installprivlib};
98 my $installarchlib = $Config{installarchlib};
99 my $installsitelib = $Config{installsitelib};
100 my $installsitearch = $Config{installsitearch};
101 my $installman1dir = $Config{installman1dir};
102 my $man1ext = $Config{man1ext};
103 my $libperl = $Config{libperl};
104 # Shared library and dynamic loading suffixes.
105 my $so = $Config{so};
106 my $dlext = $Config{dlext};
107
108 my $d_dosuid = $Config{d_dosuid};
109 my $binexp = $Config{binexp};
110
111 if ($Is_VMS) {  # Hang in there until File::Spec hits the big time
112     foreach ( \$installbin,     \$installscript,  \$installprivlib,
113               \$installarchlib, \$installsitelib, \$installsitearch,
114               \$installman1dir ) {
115       $$_ = unixify($$_);  $$_ =~ s:/$::;
116     }
117 }
118
119 # Do some quick sanity checks.
120
121 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
122
123    $installbin          || die "No installbin directory in config.sh\n";
124 -d $installbin          || mkpath($installbin, 1, 0777);
125 -d $installbin          || $nonono || die "$installbin is not a directory\n";
126 -w $installbin          || $nonono || die "$installbin is not writable by you\n"
127         unless $installbin =~ m#^/afs/# || $nonono;
128
129 -x 'perl' . $exe_ext    || die "perl isn't executable!\n";
130 -x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
131
132 -x 't/TEST'             || $Is_W32
133                         || warn "WARNING: You've never run 'make test'!!!",
134                                 "  (Installing anyway.)\n";
135
136 if ($Is_W32) {
137
138 my $perldll = 'perl.' . $dlext;
139 $perldll = 'perlcore.' . $dlext if $Config{'ccflags'} =~ /PERL_OBJECT/i;
140
141 -f $perldll || die "No perl DLL built\n";
142
143 # Install the DLL
144
145 safe_unlink("$installbin/$perldll");
146 copy("$perldll", "$installbin/$perldll");
147 chmod(0755, "$installbin/$perldll");
148 }
149
150 # This will be used to store the packlist
151 my $packlist = ExtUtils::Packlist->new("$installarchlib/.packlist");
152
153 # First we install the version-numbered executables.
154
155 if ($Is_VMS) {
156     safe_unlink("$installbin/$perl$exe_ext");
157     copy("perl$exe_ext", "$installbin/$perl$exe_ext");
158     chmod(0755, "$installbin/$perl$exe_ext");
159     safe_unlink("$installbin/${perl}shr$exe_ext");
160     copy("perlshr$exe_ext", "$installbin/${perl}shr$exe_ext");
161     chmod(0755, "$installbin/${perl}shr$exe_ext");
162 }
163 elsif ($^O eq 'mpeix') {
164     # MPE lacks hard links and requires that executables with special
165     # capabilities reside in the MPE namespace.
166     safe_unlink("$installbin/perl$ver$exe_ext", $Config{perlpath});
167     # Install the primary executable into the MPE namespace as perlpath.
168     copy("perl$exe_ext", $Config{perlpath});
169     chmod(0755, $Config{perlpath});
170     # Create a backup copy with the version number.
171     link($Config{perlpath}, "$installbin/perl$ver$exe_ext");
172 }
173 elsif ($^O ne 'dos') {
174     safe_unlink("$installbin/$perl$ver$exe_ext");
175     copy("perl$exe_ext", "$installbin/$perl$ver$exe_ext");
176     strip("$installbin/perl$ver$exe_ext") if $^O =~ /^(rhapsody)$;
177     chmod(0755, "$installbin/$perl$ver$exe_ext");
178 }
179 else {
180     safe_unlink("$installbin/$perl.exe");
181     copy("perl.exe", "$installbin/$perl.exe");
182 }
183
184 safe_unlink("$installbin/s$perl$ver$exe_ext");
185 if ($d_dosuid) {
186     copy("suidperl$exe_ext", "$installbin/s$perl$ver$exe_ext");
187     chmod(04711, "$installbin/s$perl$ver$exe_ext");
188 }
189
190 # Install library files.
191
192 my ($do_installarchlib, $do_installprivlib) = (0, 0);
193     
194 mkpath($installprivlib, 1, 0777);
195 mkpath($installarchlib, 1, 0777);
196 mkpath($installsitelib, 1, 0777) if ($installsitelib);
197 mkpath($installsitearch, 1, 0777) if ($installsitearch);
198
199 if (chdir "lib") {
200     $do_installarchlib = ! samepath($installarchlib, '.');
201     $do_installprivlib = ! samepath($installprivlib, '.');
202     $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$]/);
203
204     if ($do_installarchlib || $do_installprivlib) {
205         find(\&installlib, '.');
206     }
207     chdir ".." || die "Can't cd back to source directory: $!\n";
208 }
209 else {
210     warn "Can't cd to lib to install lib files: $!\n";
211 }
212
213 # Install header files and libraries.
214 mkpath("$installarchlib/CORE", 1, 0777);
215 my @corefiles;
216 if ($Is_VMS) {  # We did core file selection during build
217     my $coredir = "lib/$Config{'arch'}/$]";
218     $coredir =~ tr/./_/;
219     @corefiles = <$coredir/*.*>;
220 }
221 else {
222     @corefiles = <*.h libperl*.*>;
223     # AIX needs perl.exp installed as well.
224     push(@corefiles,'perl.exp') if $^O eq 'aix';
225     if ($^O eq 'mpeix') {
226         # MPE needs mpeixish.h installed as well.
227         mkpath("$installarchlib/CORE/mpeix", 1, 0777);
228         push(@corefiles,'mpeix/mpeixish.h');
229     }
230     # If they have built sperl.o...
231     push(@corefiles,'sperl.o') if -f 'sperl.o';
232 }
233 foreach my $file (@corefiles) {
234     # HP-UX (at least) needs to maintain execute permissions
235     # on dynamically-loadable libraries. So we do it for all.
236     if (copy_if_diff($file,"$installarchlib/CORE/$file")) {
237         if ($file =~ /\.(so|\Q$dlext\E)$/) {
238             chmod(0555, "$installarchlib/CORE/$file");
239             strip("-S", "$installarchlib/CORE/$file") if $^O =~ /^(rhapsody)$;
240         } else {
241             chmod(0444, "$installarchlib/CORE/$file");
242         }
243     }
244 }
245
246 # Install main perl executables
247 # Make links to ordinary names if installbin directory isn't current directory.
248
249 if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS) {
250     safe_unlink("$installbin/$perl$exe_ext", "$installbin/suid$perl$exe_ext");
251     if ($^O eq 'mpeix') {
252         # MPE doesn't support hard links, so use a symlink.
253         # We don't want another cloned copy.
254         symlink($Config{perlpath}, "$installbin/perl$exe_ext");
255     } else {
256         link("$installbin/$perl$ver$exe_ext", "$installbin/$perl$exe_ext");
257     }
258     link("$installbin/s$perl$ver$exe_ext", "$installbin/suid$perl$exe_ext") 
259       if $d_dosuid;
260 }
261
262 # Offer to install perl in a "standard" location
263
264 my $mainperl_is_instperl = 0;
265
266 if ($Config{installusrbinperl} eq 'define' &&
267     !$versiononly && !$nonono && !$Is_W32 && !$Is_VMS && -t STDIN && -t STDERR
268         && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
269     my($usrbinperl)     = "$mainperldir/$perl$exe_ext";
270     my($instperl)       = "$installbin/$perl$exe_ext";
271     my($expinstperl)    = "$binexp/$perl$exe_ext";
272
273     # First make sure $usrbinperl is not already the same as the perl we
274     # just installed.
275     if (-x $usrbinperl) {
276         # Try to be clever about mainperl being a symbolic link
277         # to binexp/perl if binexp and installbin are different.
278         $mainperl_is_instperl =
279             samepath($usrbinperl, $instperl) ||
280             samepath($usrbinperl, $expinstperl) ||
281              (($binexp ne $installbin) &&
282               (-l $usrbinperl) &&
283               ((readlink $usrbinperl) eq $expinstperl));
284     }
285     if ((! $mainperl_is_instperl) &&
286         (yn("Many scripts expect perl to be installed as $usrbinperl.\n" . 
287              "Do you wish to have $usrbinperl be the same as\n" .
288              "$expinstperl? [y] ")))
289     {
290         unlink($usrbinperl);
291         ( $Config{'d_link'} eq 'define' &&
292           eval { CORE::link $instperl, $usrbinperl } )  ||
293         eval { symlink $expinstperl, $usrbinperl }      ||
294         copy($instperl, $usrbinperl);
295
296         $mainperl_is_instperl = 1;
297     }
298 }
299
300 # Make links to ordinary names if installbin directory isn't current directory.
301
302 if (!$versiononly && ! samepath($installbin, 'x2p')) {
303     safe_unlink("$installbin/a2p$exe_ext");
304     copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
305     chmod(0755, "$installbin/a2p$exe_ext");
306 }
307
308 # cppstdin is just a script, but it is architecture-dependent, so
309 # it can't safely be shared.  Place it in $installbin.
310 # Note that Configure doesn't build cppstin if it isn't needed, so
311 # we skip this if cppstdin doesn't exist.
312 if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
313     safe_unlink("$installbin/cppstdin");
314     copy("cppstdin", "$installbin/cppstdin");
315     chmod(0755, "$installbin/cppstdin");
316 }
317
318 # Install scripts.
319
320 mkpath($installscript, 1, 0777);
321
322 if (! $versiononly) {
323     for (@scripts) {
324         (my $base = $_) =~ s#.*/##;
325         copy($_, "$installscript/$base");
326         chmod(0755, "$installscript/$base");
327     }
328 }
329
330 # pstruct should be a link to c2ph
331
332 if (! $versiononly) {
333     safe_unlink("$installscript/pstruct$scr_ext");
334     if ($^O eq 'dos' or $Is_VMS) {
335         copy("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext"); 
336     } else {
337         link("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext");
338     }
339 }
340
341 # Install pod pages.  Where? I guess in $installprivlib/pod.
342
343 unless ( $versiononly && !($installprivlib =~ m/\Q$]/)) {
344     mkpath("${installprivlib}/pod", 1, 0777);
345
346     # If Perl 5.003's perldiag.pod is there, rename it.
347     if (open POD, "${installprivlib}/pod/perldiag.pod") {
348         read POD, $_, 4000;
349         close POD;
350         # Some of Perl 5.003's diagnostic messages ended with periods.
351         if (/^=.*\.$/m) {
352             my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
353                                "${installprivlib}/pod/perldiag-5.003.pod");
354             print STDERR "  rename $from $to";
355             rename($from, $to)
356                 or warn "Couldn't rename $from to $to: $!\n"
357                 unless $nonono;
358         }
359     }
360
361     foreach my $file (@pods) {
362         # $file is a name like  pod/perl.pod
363         copy_if_diff($file, "${installprivlib}/${file}");
364     }
365
366 }
367
368 # Check to make sure there aren't other perls around in installer's
369 # path.  This is probably UNIX-specific.  Check all absolute directories
370 # in the path except for where public executables are supposed to live.
371 # Also skip $mainperl if the user opted to have it be a link to the
372 # installed perl.
373
374 if (!$versiononly) {
375         my ($path, @path);
376     my $dirsep = ($Is_OS2 || $Is_W32) ? ';' : ':' ;
377     ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
378     @path = split(/$dirsep/, $path);
379     if ($Is_VMS) {
380         my $i = 0;
381         while (exists $ENV{'DCL$PATH' . $i}) {
382             my $dir = unixpath($ENV{'DCL$PATH' . $i});  $dir =~ s-/$--;
383             push(@path,$dir);
384         }
385     }
386     my @otherperls;
387     for (@path) {
388         next unless m,^/,;
389         # Use &samepath here because some systems have other dirs linked
390         # to $mainperldir (like SunOS)
391         next if samepath($_, $binexp);
392         next if ($mainperl_is_instperl && samepath($_, $mainperldir));
393         push(@otherperls, "$_/$perl$exe_ext")
394             if (-x "$_/$perl$exe_ext" && ! -d "$_/$perl$exe_ext");
395     }
396     if (@otherperls) {
397         print STDERR "\nWarning: $perl appears in your path in the following " .
398             "locations beyond where\nwe just installed it:\n";
399         for (@otherperls) {
400             print STDERR "    ", $_, "\n";
401         }
402         print STDERR "\n";
403     }
404
405 }
406
407 $packlist->write() unless $nonono;
408 print STDERR "  Installation complete\n";
409
410 exit 0;
411
412 ###############################################################################
413
414 sub yn {
415     my($prompt) = @_;
416     my($answer);
417     my($default) = $prompt =~ m/\[([yn])\]\s*$/i;
418     print STDERR $prompt;
419     chop($answer = <STDIN>);
420     $answer = $default if $answer =~ m/^\s*$/;
421     ($answer =~ m/^[yY]/);
422 }
423
424 sub unlink {
425     my(@names) = @_;
426     my($cnt) = 0;
427
428     return scalar(@names) if $Is_VMS;
429
430     foreach my $name (@names) {
431         next unless -e $name;
432         chmod 0777, $name if ($Is_OS2 || $Is_W32);
433         print STDERR "  unlink $name\n";
434         ( CORE::unlink($name) and ++$cnt 
435           or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
436     }
437     return $cnt;
438 }
439
440 sub safe_unlink {
441     return if $nonono or $Is_VMS;
442     my @names = @_;
443     foreach my $name (@names) {
444         next unless -e $name;
445         chmod 0777, $name if ($Is_OS2 || $Is_W32);
446         print STDERR "  unlink $name\n";
447         next if CORE::unlink($name);
448         warn "Couldn't unlink $name: $!\n";
449         if ($! =~ /busy/i) {
450             print STDERR "  mv $name $name.old\n";
451             safe_rename($name, "$name.old")
452                 or warn "Couldn't rename $name: $!\n";
453         }
454     }
455 }
456
457 sub safe_rename {
458     my($from,$to) = @_;
459     if (-f $to and not unlink($to)) {
460         my($i);
461         for ($i = 1; $i < 50; $i++) {
462             last if rename($to, "$to.$i");
463         }
464         warn("Cannot rename to `$to.$i': $!"), return 0 
465            if $i >= 50; # Give up!
466     }
467     link($from,$to) || return 0;
468     unlink($from);
469 }
470
471 sub link {
472     my($from,$to) = @_;
473     my($success) = 0;
474
475     print STDERR "  ln $from $to\n";
476     eval {
477         CORE::link($from, $to)
478             ? $success++
479             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
480               ? die "AFS"  # okay inside eval {}
481               : warn "Couldn't link $from to $to: $!\n"
482           unless $nonono;
483         $packlist->{$to} = { from => $from, type => 'link' };
484     };
485     if ($@) {
486         print STDERR "  creating new version of $to\n" if $Is_VMS and -e $to;
487         File::Copy::copy($from, $to)
488             ? $success++
489             : warn "Couldn't copy $from to $to: $!\n"
490           unless $nonono;
491         $packlist->{$to} = { type => 'file' };
492     }
493     $success;
494 }
495
496 sub chmod {
497     my($mode,$name) = @_;
498
499     return if ($^O eq 'dos');
500     printf STDERR "  chmod %o %s\n", $mode, $name;
501     CORE::chmod($mode,$name)
502         || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
503       unless $nonono;
504 }
505
506 sub copy {
507     my($from,$to) = @_;
508
509     print STDERR "  cp $from $to\n";
510     print STDERR "  creating new version of $to\n" if $Is_VMS and -e $to;
511     File::Copy::copy($from, $to)
512         || warn "Couldn't copy $from to $to: $!\n"
513       unless $nonono;
514     $packlist->{$to} = { type => 'file' };
515 }
516
517 sub samepath {
518     my($p1, $p2) = @_;
519
520     return (lc($p1) eq lc($p2)) if $Is_W32;
521
522     if ($p1 ne $p2) {
523         my($dev1, $ino1, $dev2, $ino2);
524         ($dev1, $ino1) = stat($p1);
525         ($dev2, $ino2) = stat($p2);
526         ($dev1 == $dev2 && $ino1 == $ino2);
527     }
528     else {
529         1;
530     }
531 }
532
533 sub installlib {
534     my $dir = $File::Find::dir;
535     $dir =~ s#^\.(?![^/])/?##;
536     local($depth) = $dir ? "lib/$dir" : "lib";
537
538     my $name = $_;
539
540     if ($name eq 'CVS' && -d $name) {
541         $File::Find::prune = 1;
542         return;
543     }
544     
545     # ignore patch backups and the .exists files.
546     return if $name =~ m{\.orig$|~$|^\.exists};
547
548     $name = "$dir/$name" if $dir ne '';
549
550     my $installlib = $installprivlib;
551     if ($dir =~ /^auto/ ||
552           ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
553           ($name =~ /^(.*)\.(?:h|lib)$/i && $Is_W32)
554        ) {
555         $installlib = $installarchlib;
556         return unless $do_installarchlib;
557     } else {
558         return unless $do_installprivlib;
559     }
560
561     if (-f $_) {
562         if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
563             $installlib = $installprivlib;
564             #We're installing *.al and *.ix files into $installprivlib,
565             #but we have to delete old *.al and *.ix files from the 5.000
566             #distribution:
567             #This might not work because $archname might have changed.
568             unlink("$installarchlib/$name");
569         }
570         $packlist->{"$installlib/$name"} = { type => 'file' };
571         if (compare($_, "$installlib/$name") || $nonono) {
572             unlink("$installlib/$name");
573             mkpath("$installlib/$dir", 1, 0777);
574             # HP-UX (at least) needs to maintain execute permissions
575             # on dynamically-loaded libraries.
576             copy_if_diff($_, "$installlib/$name")
577                 and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
578                            "$installlib/$name");
579         }
580     }
581 }
582
583 # Copy $from to $to, only if $from is different than $to.
584 # Also preserve modification times for .a libraries.
585 # On some systems, if you do
586 #   ranlib libperl.a
587 #   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
588 # and then try to link against the installed libperl.a, you might
589 # get an error message to the effect that the symbol table is older
590 # than the library.
591 # Return true if copying occurred.
592
593 sub copy_if_diff {
594     my($from,$to)=@_;
595     return 1 if (($^O eq 'VMS') && (-d $from));
596     -f $from || die "$0: $from not found";
597     $packlist->{$to} = { type => 'file' };
598     if (compare($from, $to) || $nonono) {
599         safe_unlink($to);   # In case we don't have write permissions.
600         if ($nonono) {
601             $from = $depth . "/" . $from if $depth;
602         }
603         copy($from, $to);
604         # Restore timestamps if it's a .a library or for OS/2.
605         if (!$nonono && ($Is_OS2 || $to =~ /\.a$/)) {
606             my ($atime, $mtime) = (stat $from)[8,9];
607             utime $atime, $mtime, $to;
608         }
609         1;
610     }
611 }
612
613 sub strip
614 {
615     my(@args) = @_;
616
617     my @opts;
618     while (@args && $args[0] =~ /^(-\w+)$/) {
619         push @opts, shift @args;
620     }
621
622     foreach my $file (@args) {
623         if (-f $file) {
624             print STDERR "  strip $file\n";
625             system("strip", @opts, $file);
626         } else {
627             print STDERR "# file '$file' skipped\n";
628         }
629     }
630 }
631