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