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