improved diagnostic on syntax errors at EOL
[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{'PATCHLEVEL'}) don't match\n"
91         if $patchlevel != $Config{'PATCHLEVEL'};
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     chmod(0755, "$installbin/$perl$ver$exe_ext");
176 }
177 else {
178     safe_unlink("$installbin/$perl.exe");
179     copy("perl.exe", "$installbin/$perl.exe");
180 }
181
182 safe_unlink("$installbin/s$perl$ver$exe_ext");
183 if ($d_dosuid) {
184     copy("suidperl$exe_ext", "$installbin/s$perl$ver$exe_ext");
185     chmod(04711, "$installbin/s$perl$ver$exe_ext");
186 }
187
188 # Install library files.
189
190 my ($do_installarchlib, $do_installprivlib) = (0, 0);
191     
192 mkpath($installprivlib, 1, 0777);
193 mkpath($installarchlib, 1, 0777);
194 mkpath($installsitelib, 1, 0777) if ($installsitelib);
195 mkpath($installsitearch, 1, 0777) if ($installsitearch);
196
197 if (chdir "lib") {
198     $do_installarchlib = ! samepath($installarchlib, '.');
199     $do_installprivlib = ! samepath($installprivlib, '.');
200     $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$]/);
201
202     if ($do_installarchlib || $do_installprivlib) {
203         find(\&installlib, '.');
204     }
205     chdir ".." || die "Can't cd back to source directory: $!\n";
206 }
207 else {
208     warn "Can't cd to lib to install lib files: $!\n";
209 }
210
211 # Install header files and libraries.
212 mkpath("$installarchlib/CORE", 1, 0777);
213 my @corefiles;
214 if ($Is_VMS) {  # We did core file selection during build
215     my $coredir = "lib/$Config{'arch'}/$]";
216     $coredir =~ tr/./_/;
217     @corefiles = <$coredir/*.*>;
218 }
219 else {
220     @corefiles = <*.h libperl*.*>;
221     # AIX needs perl.exp installed as well.
222     push(@corefiles,'perl.exp') if $^O eq 'aix';
223     if ($^O eq 'mpeix') {
224         # MPE needs mpeixish.h installed as well.
225         mkpath("$installarchlib/CORE/mpeix", 1, 0777);
226         push(@corefiles,'mpeix/mpeixish.h');
227     }
228     # If they have built sperl.o...
229     push(@corefiles,'sperl.o') if -f 'sperl.o';
230 }
231 foreach my $file (@corefiles) {
232     # HP-UX (at least) needs to maintain execute permissions
233     # on dynamically-loadable libraries. So we do it for all.
234     copy_if_diff($file,"$installarchlib/CORE/$file")
235         and chmod($file =~ /\.(so|\Q$dlext\E)$/ ? 0555 : 0444,
236                    "$installarchlib/CORE/$file");
237 }
238
239 # Install main perl executables
240 # Make links to ordinary names if installbin directory isn't current directory.
241
242 if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS) {
243     safe_unlink("$installbin/$perl$exe_ext", "$installbin/suid$perl$exe_ext");
244     if ($^O eq 'mpeix') {
245         # MPE doesn't support hard links, so use a symlink.
246         # We don't want another cloned copy.
247         symlink($Config{perlpath}, "$installbin/perl$exe_ext");
248     } else {
249         link("$installbin/$perl$ver$exe_ext", "$installbin/$perl$exe_ext");
250     }
251     link("$installbin/s$perl$ver$exe_ext", "$installbin/suid$perl$exe_ext") 
252       if $d_dosuid;
253 }
254
255 # Offer to install perl in a "standard" location
256
257 my $mainperl_is_instperl = 0;
258
259 if (!$versiononly && !$nonono && !$Is_W32 && !$Is_VMS && -t STDIN && -t STDERR
260         && -w $mainperldir && ! samepath($mainperldir, $installbin)) {
261     my($usrbinperl)     = "$mainperldir/$perl$exe_ext";
262     my($instperl)       = "$installbin/$perl$exe_ext";
263     my($expinstperl)    = "$binexp/$perl$exe_ext";
264
265     # First make sure $usrbinperl is not already the same as the perl we
266     # just installed.
267     if (-x $usrbinperl) {
268         # Try to be clever about mainperl being a symbolic link
269         # to binexp/perl if binexp and installbin are different.
270         $mainperl_is_instperl =
271             samepath($usrbinperl, $instperl) ||
272             samepath($usrbinperl, $expinstperl) ||
273              (($binexp ne $installbin) &&
274               (-l $usrbinperl) &&
275               ((readlink $usrbinperl) eq $expinstperl));
276     }
277     if ((! $mainperl_is_instperl) &&
278         (yn("Many scripts expect perl to be installed as $usrbinperl.\n" . 
279              "Do you wish to have $usrbinperl be the same as\n" .
280              "$expinstperl? [y] ")))
281     {
282         unlink($usrbinperl);
283         ( $Config{'d_link'} eq 'define' &&
284           eval { CORE::link $instperl, $usrbinperl } )  ||
285         eval { symlink $expinstperl, $usrbinperl }      ||
286         copy($instperl, $usrbinperl);
287
288         $mainperl_is_instperl = 1;
289     }
290 }
291
292 # Make links to ordinary names if installbin directory isn't current directory.
293
294 if (!$versiononly && ! samepath($installbin, 'x2p')) {
295     safe_unlink("$installbin/a2p$exe_ext");
296     copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
297     chmod(0755, "$installbin/a2p$exe_ext");
298 }
299
300 # cppstdin is just a script, but it is architecture-dependent, so
301 # it can't safely be shared.  Place it in $installbin.
302 # Note that Configure doesn't build cppstin if it isn't needed, so
303 # we skip this if cppstdin doesn't exist.
304 if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
305     safe_unlink("$installbin/cppstdin");
306     copy("cppstdin", "$installbin/cppstdin");
307     chmod(0755, "$installbin/cppstdin");
308 }
309
310 # Install scripts.
311
312 mkpath($installscript, 1, 0777);
313
314 if (! $versiononly) {
315     for (@scripts) {
316         (my $base = $_) =~ s#.*/##;
317         copy($_, "$installscript/$base");
318         chmod(0755, "$installscript/$base");
319     }
320 }
321
322 # pstruct should be a link to c2ph
323
324 if (! $versiononly) {
325     safe_unlink("$installscript/pstruct$scr_ext");
326     if ($^O eq 'dos' or $Is_VMS) {
327         copy("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext"); 
328     } else {
329         link("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext");
330     }
331 }
332
333 # Install pod pages.  Where? I guess in $installprivlib/pod.
334
335 unless ( $versiononly && !($installprivlib =~ m/\Q$]/)) {
336     mkpath("${installprivlib}/pod", 1, 0777);
337
338     # If Perl 5.003's perldiag.pod is there, rename it.
339     if (open POD, "${installprivlib}/pod/perldiag.pod") {
340         read POD, $_, 4000;
341         close POD;
342         # Some of Perl 5.003's diagnostic messages ended with periods.
343         if (/^=.*\.$/m) {
344             my ($from, $to) = ("${installprivlib}/pod/perldiag.pod",
345                                "${installprivlib}/pod/perldiag-5.003.pod");
346             print STDERR "  rename $from $to";
347             rename($from, $to)
348                 or warn "Couldn't rename $from to $to: $!\n"
349                 unless $nonono;
350         }
351     }
352
353     foreach my $file (@pods) {
354         # $file is a name like  pod/perl.pod
355         copy_if_diff($file, "${installprivlib}/${file}");
356     }
357
358 }
359
360 # Check to make sure there aren't other perls around in installer's
361 # path.  This is probably UNIX-specific.  Check all absolute directories
362 # in the path except for where public executables are supposed to live.
363 # Also skip $mainperl if the user opted to have it be a link to the
364 # installed perl.
365
366 if (!$versiononly) {
367         my ($path, @path);
368     my $dirsep = ($Is_OS2 || $Is_W32) ? ';' : ':' ;
369     ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
370     @path = split(/$dirsep/, $path);
371     if ($Is_VMS) {
372         my $i = 0;
373         while (exists $ENV{'DCL$PATH' . $i}) {
374             my $dir = unixpath($ENV{'DCL$PATH' . $i});  $dir =~ s-/$--;
375             push(@path,$dir);
376         }
377     }
378     my @otherperls;
379     for (@path) {
380         next unless m,^/,;
381         # Use &samepath here because some systems have other dirs linked
382         # to $mainperldir (like SunOS)
383         next if samepath($_, $binexp);
384         next if ($mainperl_is_instperl && samepath($_, $mainperldir));
385         push(@otherperls, "$_/$perl$exe_ext")
386             if (-x "$_/$perl$exe_ext" && ! -d "$_/$perl$exe_ext");
387     }
388     if (@otherperls) {
389         print STDERR "\nWarning: $perl appears in your path in the following " .
390             "locations beyond where\nwe just installed it:\n";
391         for (@otherperls) {
392             print STDERR "    ", $_, "\n";
393         }
394         print STDERR "\n";
395     }
396
397 }
398
399 $packlist->write() unless $nonono;
400 print STDERR "  Installation complete\n";
401
402 exit 0;
403
404 ###############################################################################
405
406 sub yn {
407     my($prompt) = @_;
408     my($answer);
409     my($default) = $prompt =~ m/\[([yn])\]\s*$/i;
410     print STDERR $prompt;
411     chop($answer = <STDIN>);
412     $answer = $default if $answer =~ m/^\s*$/;
413     ($answer =~ m/^[yY]/);
414 }
415
416 sub unlink {
417     my(@names) = @_;
418     my($cnt) = 0;
419
420     return scalar(@names) if $Is_VMS;
421
422     foreach my $name (@names) {
423         next unless -e $name;
424         chmod 0777, $name if ($Is_OS2 || $Is_W32);
425         print STDERR "  unlink $name\n";
426         ( CORE::unlink($name) and ++$cnt 
427           or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
428     }
429     return $cnt;
430 }
431
432 sub safe_unlink {
433     return if $nonono or $Is_VMS;
434     my @names = @_;
435     foreach my $name (@names) {
436         next unless -e $name;
437         chmod 0777, $name if ($Is_OS2 || $Is_W32);
438         print STDERR "  unlink $name\n";
439         next if CORE::unlink($name);
440         warn "Couldn't unlink $name: $!\n";
441         if ($! =~ /busy/i) {
442             print STDERR "  mv $name $name.old\n";
443             safe_rename($name, "$name.old")
444                 or warn "Couldn't rename $name: $!\n";
445         }
446     }
447 }
448
449 sub safe_rename {
450     my($from,$to) = @_;
451     if (-f $to and not unlink($to)) {
452         my($i);
453         for ($i = 1; $i < 50; $i++) {
454             last if rename($to, "$to.$i");
455         }
456         warn("Cannot rename to `$to.$i': $!"), return 0 
457            if $i >= 50; # Give up!
458     }
459     link($from,$to) || return 0;
460     unlink($from);
461 }
462
463 sub link {
464     my($from,$to) = @_;
465     my($success) = 0;
466
467     print STDERR "  ln $from $to\n";
468     eval {
469         CORE::link($from, $to)
470             ? $success++
471             : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
472               ? die "AFS"  # okay inside eval {}
473               : warn "Couldn't link $from to $to: $!\n"
474           unless $nonono;
475         $packlist->{$to} = { from => $from, type => 'link' };
476     };
477     if ($@) {
478         print STDERR "  creating new version of $to\n" if $Is_VMS and -e $to;
479         File::Copy::copy($from, $to)
480             ? $success++
481             : warn "Couldn't copy $from to $to: $!\n"
482           unless $nonono;
483         $packlist->{$to} = { type => 'file' };
484     }
485     $success;
486 }
487
488 sub chmod {
489     my($mode,$name) = @_;
490
491     return if ($^O eq 'dos');
492     printf STDERR "  chmod %o %s\n", $mode, $name;
493     CORE::chmod($mode,$name)
494         || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
495       unless $nonono;
496 }
497
498 sub copy {
499     my($from,$to) = @_;
500
501     print STDERR "  cp $from $to\n";
502     print STDERR "  creating new version of $to\n" if $Is_VMS and -e $to;
503     File::Copy::copy($from, $to)
504         || warn "Couldn't copy $from to $to: $!\n"
505       unless $nonono;
506     $packlist->{$to} = { type => 'file' };
507 }
508
509 sub samepath {
510     my($p1, $p2) = @_;
511
512     return (lc($p1) eq lc($p2)) if $Is_W32;
513
514     if ($p1 ne $p2) {
515         my($dev1, $ino1, $dev2, $ino2);
516         ($dev1, $ino1) = stat($p1);
517         ($dev2, $ino2) = stat($p2);
518         ($dev1 == $dev2 && $ino1 == $ino2);
519     }
520     else {
521         1;
522     }
523 }
524
525 sub installlib {
526     my $dir = $File::Find::dir;
527     $dir =~ s#^\.(?![^/])/?##;
528     local($depth) = $dir ? "lib/$dir" : "lib";
529
530     my $name = $_;
531
532     if ($name eq 'CVS' && -d $name) {
533         $File::Find::prune = 1;
534         return;
535     }
536     
537     # ignore patch backups and the .exists files.
538     return if $name =~ m{\.orig$|~$|^\.exists};
539
540     $name = "$dir/$name" if $dir ne '';
541
542     my $installlib = $installprivlib;
543     if ($dir =~ /^auto/ ||
544           ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
545           ($name =~ /^(.*)\.(?:h|lib)$/i && $Is_W32)
546        ) {
547         $installlib = $installarchlib;
548         return unless $do_installarchlib;
549     } else {
550         return unless $do_installprivlib;
551     }
552
553     if (-f $_) {
554         if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
555             $installlib = $installprivlib;
556             #We're installing *.al and *.ix files into $installprivlib,
557             #but we have to delete old *.al and *.ix files from the 5.000
558             #distribution:
559             #This might not work because $archname might have changed.
560             unlink("$installarchlib/$name");
561         }
562         $packlist->{"$installlib/$name"} = { type => 'file' };
563         if (compare($_, "$installlib/$name") || $nonono) {
564             unlink("$installlib/$name");
565             mkpath("$installlib/$dir", 1, 0777);
566             # HP-UX (at least) needs to maintain execute permissions
567             # on dynamically-loaded libraries.
568             copy_if_diff($_, "$installlib/$name")
569                 and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
570                            "$installlib/$name");
571         }
572     }
573 }
574
575 # Copy $from to $to, only if $from is different than $to.
576 # Also preserve modification times for .a libraries.
577 # On some systems, if you do
578 #   ranlib libperl.a
579 #   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
580 # and then try to link against the installed libperl.a, you might
581 # get an error message to the effect that the symbol table is older
582 # than the library.
583 # Return true if copying occurred.
584
585 sub copy_if_diff {
586     my($from,$to)=@_;
587     return 1 if (($^O eq 'VMS') && (-d $from));
588     -f $from || die "$0: $from not found";
589     $packlist->{$to} = { type => 'file' };
590     if (compare($from, $to) || $nonono) {
591         safe_unlink($to);   # In case we don't have write permissions.
592         if ($nonono) {
593             $from = $depth . "/" . $from if $depth;
594         }
595         copy($from, $to);
596         # Restore timestamps if it's a .a library or for OS/2.
597         if (!$nonono && ($Is_OS2 || $to =~ /\.a$/)) {
598             my ($atime, $mtime) = (stat $from)[8,9];
599             utime $atime, $mtime, $to;
600         }
601         1;
602     }
603 }