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