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