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