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