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