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