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