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