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