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