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