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