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