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