This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Okay, here's your official unofficial closure leak patch
[perl5.git] / installperl
1 #!./perl
2 BEGIN { @INC=('./lib', '../lib') }
3 use File::Find;
4
5 $mainperldir = "/usr/bin";
6
7 while (@ARGV) {
8     $nonono = 1 if $ARGV[0] eq '-n';
9     $versiononly = 1 if $ARGV[0] eq '-v';
10     shift;
11 }
12
13 umask 022;
14
15 @scripts = ('cppstdin', 'c2ph', 'h2xs', 'pstruct', 'x2p/s2p', 'x2p/find2perl');
16 @manpages = (<pod/*.man>, 'x2p/a2p.man', 'x2p/s2p.man');
17
18 # Read in the config file.
19
20 open(CONFIG, "config.sh") || die "You haven't run Configure yet!\n";
21 while (<CONFIG>) {
22     if (s/^(\w+=)/\$$1/) {
23         $accum =~ s/'undef'/undef/g;
24         eval $accum;
25         $accum = '';
26     }
27     $accum .= $_;
28 }
29 close CONFIG;
30
31 $ver = $];
32 $release = substr($ver,0,3);
33 $patchlevel = substr($ver,3,2);
34 die "Patchlevel of perl ($patchlevel)",
35     "and patchlevel of config.sh ($PATCHLEVEL) don't match\n"
36         if $patchlevel != $PATCHLEVEL;
37
38 # Do some quick sanity checks.
39
40 if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
41
42    $installbin          || die "No installbin directory in config.sh\n";
43 -d $installbin          || die "$installbin is not a directory\n";
44 -w $installbin          || die "$installbin is not writable by you\n"
45         unless $installbin =~ m#^/afs/# || $nonono;
46
47 -x 'perl'               || die "perl isn't executable!\n";
48 -x 'suidperl'           || die "suidperl isn't executable!\n" if $d_dosuid;
49
50 -x 't/TEST'             || warn "WARNING: You've never run 'make test'!!!",
51         "  (Installing anyway.)\n";
52
53 if ($d_shrplib) {
54     if (!<libperl*.$so*>) {
55         warn "WARNING: Can't find libperl*.$so* to install into $shrpdir.",
56             "  (Installing other things anyway.)\n";
57     } else {
58         &makedir($shrpdir);
59         -w $shrpdir     || die "$shrpdir is not writable by you\n";
60         &cmd("cp libperl*.$so* $shrpdir");
61     }
62 }
63
64 # First we install the version-numbered executables.
65
66 &unlink("$installbin/perl$ver");
67 &cmd("cp perl $installbin/perl$ver");
68
69 &unlink("$installbin/sperl$ver");
70 if ($d_dosuid) {
71     &cmd("cp suidperl $installbin/sperl$ver");
72     &chmod(04711, "$installbin/sperl$ver");
73 }
74
75 exit 0 if $versiononly;
76
77 # Make links to ordinary names if installbin directory isn't current directory.
78
79 if (! &samepath($installbin, '.')) {
80     &unlink("$installbin/perl", "$installbin/suidperl");
81     &link("$installbin/perl$ver", "$installbin/perl");
82     &link("$installbin/sperl$ver", "$installbin/suidperl") if $d_dosuid;
83 }
84
85 if (! &samepath($installbin, 'x2p')) {
86     &unlink("$installbin/a2p");
87     &cmd("cp x2p/a2p $installbin/a2p");
88     &chmod(0755, "$installbin/a2p");
89 }
90
91 # Install scripts.
92
93 &makedir($installscript);
94
95 for (@scripts) {
96     if (-f $_) {   # cppstdin might not exist on this system.
97         &cmd("cp $_ $installscript");
98         s#.*/##; &chmod(0755, "$installscript/$_");
99     }
100 }
101
102 # Install man pages.
103
104 if ($installmansrc ne '') {
105     &makedir($installmansrc);
106
107     if (! &samepath($installmansrc, '.')) {
108         for (@manpages) {
109             ($new = $_) =~ s/man$/$manext/;
110             $new =~ s#.*/##;
111             print STDERR "  Installing $installmansrc/$new\n";
112             next if $nonono;
113             open(MI,$_) || warn "Can't open $_: $!\n";
114             open(MO,">$installmansrc/$new") || warn "Can't install $installmansrc/$new: $!\n";
115             print MO ".ds RP Release $release Patchlevel $patchlevel\n";
116             while (<MI>) {
117                 print MO;
118             }
119             close MI;
120             close MO;
121         }
122     }
123 }
124
125 # Install library files.
126
127 $do_installarchlib = $do_installprivlib = 0;
128     
129 &makedir($installprivlib);
130 &makedir($installarchlib);
131 if (chdir "lib") {
132     $do_installarchlib = ! &samepath($installarchlib, '.');
133     $do_installprivlib = ! &samepath($installprivlib, '.');
134
135     if ($do_installarchlib || $do_installprivlib) {
136         find(\&installlib, '.');
137     }
138     chdir ".." || die "Can't cd back to source directory: $!\n";
139 }
140 else {
141     warn "Can't cd to lib to install lib files: $!\n";
142 }
143
144 # Install header files and libraries.
145 makedir("$installarchlib/CORE");
146 foreach $file (<*.h libperl*.*>) {
147     cp_if_diff($file,"$installarchlib/CORE/$file");
148     if ($file =~ /\.a$/ && $osname eq 'next') { 
149        #on NeXTs we have to rerun ranlib after copying libraries
150        &cmd("$ranlib $installarchlib/CORE/$file");
151     }
152 }
153 # AIX needs perl.exp installed as well.
154 cp_if_diff("perl.exp" ,"$installarchlib/CORE/perl.exp") if ($osname eq 'aix');
155
156 # If they have built sperl.o...
157 cp_if_diff("sperl.o" ,"$installarchlib/CORE/sperl.o") if (-f 'sperl.o');
158
159
160 # Offer to install perl in a "standard" location
161
162 $mainperl_is_instperl = 0;
163
164 if (-w $mainperldir && ! &samepath($mainperldir, $installbin) && !$nonono) {
165     # First make sure $mainperldir/perl is not already the same as
166     # the perl we just installed
167     if (-x "$mainperldir/perl") {
168         # Try to be clever about mainperl being a symbolic link
169         # to binexp/perl if binexp and installbin are different.
170         $mainperl_is_instperl =
171             &samepath("$mainperldir/perl", "$installbin/perl") ||
172              (($binexp ne $installbin) &&
173               (-l "$mainperldir/perl") &&
174               ((readlink "$mainperldir/perl") eq "$binexp/perl"));
175     }
176     if ((! $mainperl_is_instperl) &&
177         (&yn("Many scripts expect perl to be installed as " .
178              "$mainperldir/perl.\n" . 
179              "Do you wish to have $mainperldir/perl be the same as\n" .
180              "$binexp/perl? [y] ")))
181     {   
182         unlink("$mainperldir/perl");
183         eval 'link("$installbin/perl", "$mainperldir/perl")' ||
184         eval 'symlink("$binexp/perl", "$mainperldir/perl")' ||
185         &cmd("cp $installbin/perl $mainperldir");
186         $mainperl_is_instperl = 1;
187     }
188 }
189
190 # Check to make sure there aren't other perls around in installer's
191 # path.  This is probably UNIX-specific.  Check all absolute directories
192 # in the path except for where public executables are supposed to live.
193 # Also skip $mainperl if the user opted to have it be a link to the
194 # installed perl.
195
196 @path = split(/:/, $ENV{"PATH"});
197 @otherperls = ();
198 for (@path) {
199     next unless m,^/,;
200     next if ($_ eq $binexp);
201     # Use &samepath here because some systems have other dirs linked
202     # to $mainperldir (like SunOS)
203     next if ($mainperl_is_instperl && &samepath($_, $mainperldir));
204     push(@otherperls, "$_/perl") if (-x "$_/perl" && ! -d "$_/perl");
205 }
206 if (@otherperls) {
207     print STDERR "\nWarning: perl appears in your path in the following " .
208         "locations beyond where\nwe just installed it:\n";
209     for (@otherperls) {
210         print STDERR "    ", $_, "\n";
211     }
212     print STDERR "\n";
213 }
214
215 print STDERR "  Installation complete\n";
216
217 exit 0;
218
219 ###############################################################################
220
221 sub yn {
222     local($prompt) = @_;
223     local($answer);
224     local($default) = $prompt =~ m/\[([yn])\]\s*$/i;
225     print STDERR $prompt;
226     chop($answer = <STDIN>);
227     $answer = $default if $answer =~ m/^\s*$/;
228     ($answer =~ m/^[yY]/);
229 }
230
231 sub unlink {
232     local(@names) = @_;
233
234     foreach $name (@names) {
235         next unless -e $name;
236         print STDERR "  unlink $name\n";
237         unlink($name) || warn "Couldn't unlink $name: $!\n" unless $nonono;
238     }
239 }
240
241 sub cmd {
242     local($cmd) = @_;
243     print STDERR "  $cmd\n";
244     unless ($nonono) {
245         system $cmd;
246         warn "Command failed!!!\n" if $?;
247     }
248 }
249
250 sub link {
251     local($from,$to) = @_;
252
253     print STDERR "  ln $from $to\n";
254     link($from,$to) || warn "Couldn't link $from to $to: $!\n" unless $nonono;
255 }
256
257 sub chmod {
258     local($mode,$name) = @_;
259
260     printf STDERR "  chmod %o %s\n", $mode, $name;
261     chmod($mode,$name) || warn sprintf("Couldn't chmod %o %s: $!\n",$mode,$name)
262         unless $nonono;
263 }
264
265 sub makedir {
266     local($dir) = @_;
267     unless (-d $dir) {
268         local($shortdir) = $dir;
269
270         $shortdir =~ s#(.*)/.*#$1#;
271         &makedir($shortdir);
272
273         print STDERR "  mkdir $dir\n";
274         mkdir($dir, 0777) || warn "Couldn't create $dir: $!\n" unless $nonono;
275     }
276 }
277
278 sub samepath {
279     local($p1, $p2) = @_;
280     local($dev1, $ino1, $dev2, $ino2);
281
282     if ($p1 ne $p2) {
283         ($dev1, $ino1) = stat($p1);
284         ($dev2, $ino2) = stat($p2);
285         ($dev1 == $dev2 && $ino1 == $ino2);
286     }
287     else {
288         1;
289     }
290 }
291
292 sub installlib {
293     my $dir = $File::Find::dir;
294     $dir =~ s#^\.(?![^/])/?##;
295
296     my $name = $_;
297     $name = "$dir/$name" if $dir ne '';
298
299     my $installlib = $installprivlib;
300     if ((substr($dir, 0, 4) eq 'auto') || ($name eq 'Config.pm')) {
301         $installlib = $installarchlib;
302         return unless $do_installarchlib;
303     } else {
304         return unless $do_installprivlib;
305     }
306
307     if (-f $_) {
308         if (/\.al$/ || /\.ix$/) {
309             $installlib = $installprivlib;
310             #We're installing *.al and *.ix files into $installprivlib,
311             #but we have to delete old *.al and *.ix files from the 5.000
312             #distribution:
313             #This might not work because $archname might have changed.
314             &unlink("$installarchlib/$name");
315         }
316         system "cmp", "-s", $_, "$installlib/$name";
317         if ($?) {
318             &unlink("$installlib/$name");
319             &makedir("$installlib/$dir");
320             &cmd("cp $_ $installlib/$dir");
321             if (/\.a$/ && $osname eq 'next') { 
322                 #on NeXTs we have to rerun ranlib after copying libraries
323                 &cmd("$ranlib $installlib/$dir/$_");
324             }
325             # HP-UX (at least) needs to maintain execute permissions
326             # on dynamically-loaded libraries.
327             if ($name =~ /\.(so|$dlext)$/o) {
328                 &chmod(0555, "$installlib/$name");
329             }
330             else {
331                 &chmod(0444, "$installlib/$name");
332             }
333         }
334     } elsif (-d $_) {
335         &makedir("$installlib/$name");
336     }
337 }
338
339 sub cp_if_diff {
340     my($from,$to)=@_;
341     -f $from || die "$0: $from not found";
342     system "cmp", "-s", $from, $to;
343     if ($?) {
344         cmd("cp $from $to");
345     }
346 }