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