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