source: trunk/third/perl/installperl @ 13574

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