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