source: trunk/third/perl/installperl @ 14554

Revision 14554, 18.5 KB checked in by ghudson, 24 years ago (diff)
Merge local change.
Line 
1#!./perl
2
3BEGIN {
4    require 5.004;
5    chdir '..' if !-d 'lib' and -d '..\lib';
6    @INC = 'lib';
7    $ENV{PERL5LIB} = 'lib';
8}
9
10use strict;
11use vars qw($Is_VMS $Is_W32 $Is_OS2 $Is_Cygwin $nonono $dostrip $versiononly $depth);
12
13BEGIN {
14    $Is_VMS = $^O eq 'VMS';
15    $Is_W32 = $^O eq 'MSWin32';
16    $Is_OS2 = $^O eq 'os2';
17    $Is_Cygwin = $^O eq 'cygwin';
18    if ($Is_VMS) { eval 'use VMS::Filespec;' }
19}
20
21my $scr_ext = ($Is_VMS ? '.Com' : $Is_W32 ? '.bat' : '');
22
23use File::Find;
24use File::Compare;
25use File::Copy ();
26use File::Path ();
27use ExtUtils::Packlist;
28use Config;
29use subs qw(unlink link chmod);
30use vars qw($packlist);
31
32# override the ones in the rest of the script
33sub mkpath {
34    File::Path::mkpath(@_) unless $nonono;
35}
36
37my $mainperldir = "/usr/bin";
38my $exe_ext = $Config{exe_ext};
39
40# Allow ``make install PERLNAME=something_besides_perl'':
41my $perl = defined($ENV{PERLNAME}) ? $ENV{PERLNAME} : 'perl';
42
43# This is the base used for versioned names, like "perl5.6.0".
44# It's separate because a common use of $PERLNAME is to install
45# perl as "perl5", if that's used as base for versioned files you
46# get "perl55.6.0".
47my $perl_verbase = defined($ENV{PERLNAME_VERBASE})
48                    ? $ENV{PERLNAME_VERBASE}
49                    : $perl;
50
51while (@ARGV) {
52    $nonono = 1 if $ARGV[0] eq '-n';
53    $dostrip = 1 if $ARGV[0] eq '-s';
54    $versiononly = 1 if $ARGV[0] eq '-v';
55    shift;
56}
57
58my @scripts = qw(utils/c2ph utils/h2ph utils/h2xs utils/perlbug utils/perldoc
59                utils/pl2pm utils/splain utils/perlcc utils/dprofpp
60                x2p/s2p x2p/find2perl
61                pod/pod2man pod/pod2html pod/pod2latex pod/pod2text
62                pod/pod2usage pod/podchecker pod/podselect);
63
64if ($scr_ext) { @scripts = map { "$_$scr_ext" } @scripts; }
65
66my @pods = (<pod/*.pod>);
67
68# Specify here any .pm files that are actually architecture-dependent.
69# (Those included with XS extensions under ext/ are automatically
70# added later.)
71# Now that the default privlib has the full perl version number included,
72# we no longer have to play the trick of sticking version-specific .pm
73# files under the archlib directory.
74my %archpms = (
75    Config => 1,
76);
77
78if ($^O eq 'dos') {
79    push(@scripts,'djgpp/fixpmain');
80    $archpms{config} = $archpms{filehand} = 1;
81}
82
83if ((-e "testcompile") && (defined($ENV{'COMPILE'})))
84{
85        push(@scripts, map("$_.exe", @scripts));
86}
87
88find(sub {
89        if ("$File::Find::dir/$_" =~ m{^ext\b(.*)/([^/]+)\.pm$}) {
90            my($path, $modname) = ($1,$2);
91
92            # strip trailing component first
93            $path =~ s{/[^/]*$}{};
94
95            # strip optional "/lib";
96            $path =~ s{/lib\b}{};
97
98            # strip any leading /
99            $path =~ s{^/}{};
100
101            # reconstitute canonical module name
102            $modname = "$path/$modname" if length $path;
103
104            # remember it
105            $archpms{$modname} = 1;
106        }
107    }, 'ext');
108
109# print "[$_]\n" for sort keys %archpms;
110
111my $ver = $Config{version};
112my $release = substr($],0,3);   # Not used presently.
113my $patchlevel = substr($],3,2);
114die "Patchlevel of perl ($patchlevel)",
115    "and patchlevel of config.sh ($Config{'PERL_VERSION'}) don't match\n"
116        if $patchlevel != $Config{'PERL_VERSION'};
117
118# Fetch some frequently-used items from %Config
119my $installbin = $ENV{"DESTDIR"} . $Config{installbin};
120my $installscript = $ENV{"DESTDIR"} . $Config{installscript};
121my $installprivlib = $ENV{"DESTDIR"} . $Config{installprivlib};
122my $installarchlib = $ENV{"DESTDIR"} . $Config{installarchlib};
123my $installsitelib = $ENV{"DESTDIR"} . $Config{installsitelib};
124my $installsitearch = $ENV{"DESTDIR"} . $Config{installsitearch};
125my $installman1dir = $ENV{"DESTDIR"} . $Config{installman1dir};
126my $man1ext = $Config{man1ext};
127my $libperl = $Config{libperl};
128# Shared library and dynamic loading suffixes.
129my $so = $Config{so};
130my $dlext = $Config{dlext};
131my $dlsrc = $Config{dlsrc};
132
133my $d_dosuid = $Config{d_dosuid};
134my $binexp = $Config{binexp};
135
136if ($Is_VMS) {  # Hang in there until File::Spec hits the big time
137    foreach ( \$installbin,     \$installscript,  \$installprivlib,
138              \$installarchlib, \$installsitelib, \$installsitearch,
139              \$installman1dir ) {
140      $$_ = unixify($$_);  $$_ =~ s:/$::;
141    }
142}
143
144# Do some quick sanity checks.
145
146if ($d_dosuid && $>) { die "You must run as root to install suidperl\n"; }
147
148   $installbin          || die "No installbin directory in config.sh\n";
149-d $installbin          || mkpath($installbin, 1, 0777);
150-d $installbin          || $nonono || die "$installbin is not a directory\n";
151-w $installbin          || $nonono || die "$installbin is not writable by you\n"
152        unless $installbin =~ m#^/afs/# || $nonono;
153
154-x 'perl' . $exe_ext    || die "perl isn't executable!\n";
155-x 'suidperl' . $exe_ext|| die "suidperl isn't executable!\n" if $d_dosuid;
156
157-x 't/TEST'             || $Is_W32
158                        || warn "WARNING: You've never run 'make test'!!!",
159                                "  (Installing anyway.)\n";
160
161if ($Is_W32 or $Is_Cygwin) {
162  my $perldll;
163
164if ($Is_Cygwin) {
165  $perldll = $libperl;
166  $perldll =~ s/(\..*)?$/.$dlext/;
167  if ($Config{useshrplib} eq 'true') {
168    # install ld2 and perlld as well
169    foreach ('ld2', 'perlld') {
170      safe_unlink("$installbin/$_");
171      copy("$_", "$installbin/$_");
172      chmod(0755, "$installbin/$_");
173    };
174  };
175} else {
176  $perldll = 'perl56.' . $dlext;
177}
178
179   if ($dlsrc ne "dl_none.xs") {
180      -f $perldll || die "No perl DLL built\n";
181   }
182# Install the DLL
183
184   safe_unlink("$installbin/$perldll");
185   copy("$perldll", "$installbin/$perldll");
186   chmod(0755, "$installbin/$perldll");
187   
188} # if ($Is_W32 or $Is_Cygwin)
189
190# This will be used to store the packlist
191my $packlist = ExtUtils::Packlist->new("$installarchlib/.packlist");
192
193# First we install the version-numbered executables.
194
195if ($Is_VMS) {
196    safe_unlink("$installbin/$perl$exe_ext");
197    copy("perl$exe_ext", "$installbin/$perl$exe_ext");
198    chmod(0755, "$installbin/$perl$exe_ext");
199    safe_unlink("$installbin/${perl}shr$exe_ext");
200    copy("perlshr$exe_ext", "$installbin/${perl}shr$exe_ext");
201    chmod(0755, "$installbin/${perl}shr$exe_ext");
202}
203elsif ($^O eq 'mpeix') {
204    # MPE lacks hard links and requires that executables with special
205    # capabilities reside in the MPE namespace.
206    safe_unlink("$installbin/perl$ver$exe_ext", $Config{perlpath});
207    # Install the primary executable into the MPE namespace as perlpath.
208    copy("perl$exe_ext", $Config{perlpath});
209    chmod(0755, $Config{perlpath});
210    # Create a backup copy with the version number.
211    link($Config{perlpath}, "$installbin/perl$ver$exe_ext");
212}
213elsif ($^O ne 'dos') {
214    safe_unlink("$installbin/$perl_verbase$ver$exe_ext");
215    copy("perl$exe_ext", "$installbin/$perl_verbase$ver$exe_ext");
216    strip("$installbin/$perl_verbase$ver$exe_ext");
217    chmod(0755, "$installbin/$perl_verbase$ver$exe_ext");
218}
219else {
220    safe_unlink("$installbin/$perl.exe");
221    copy("perl.exe", "$installbin/$perl.exe");
222}
223
224safe_unlink("$installbin/s$perl_verbase$ver$exe_ext");
225if ($d_dosuid) {
226    copy("suidperl$exe_ext", "$installbin/s$perl_verbase$ver$exe_ext");
227    chmod(04711, "$installbin/s$perl_verbase$ver$exe_ext");
228}
229
230# Install library files.
231
232my ($do_installarchlib, $do_installprivlib) = (0, 0);
233   
234mkpath($installprivlib, 1, 0777);
235mkpath($installarchlib, 1, 0777);
236mkpath($installsitelib, 1, 0777) if ($installsitelib);
237mkpath($installsitearch, 1, 0777) if ($installsitearch);
238
239if (chdir "lib") {
240    $do_installarchlib = ! samepath($installarchlib, '.');
241    $do_installprivlib = ! samepath($installprivlib, '.');
242    $do_installprivlib = 0 if $versiononly && !($installprivlib =~ m/\Q$ver/);
243
244    if ($do_installarchlib || $do_installprivlib) {
245        find(\&installlib, '.');
246    }
247    chdir ".." || die "Can't cd back to source directory: $!\n";
248}
249else {
250    warn "Can't cd to lib to install lib files: $!\n";
251}
252
253# Install header files and libraries.
254mkpath("$installarchlib/CORE", 1, 0777);
255my @corefiles;
256if ($Is_VMS) {  # We did core file selection during build
257    my $coredir = "lib/$Config{'arch'}/$ver";
258    $coredir =~ tr/./_/;
259    @corefiles = map { s|^$coredir/||i; } <$coredir/*.*>;
260}
261else {
262    # [als] hard-coded 'libperl' name... not good!
263    @corefiles = <*.h libperl*.*>;
264
265    # AIX needs perl.exp installed as well.
266    push(@corefiles,'perl.exp') if $^O eq 'aix';
267    if ($^O eq 'mpeix') {
268        # MPE needs mpeixish.h installed as well.
269        mkpath("$installarchlib/CORE/mpeix", 1, 0777);
270        push(@corefiles,'mpeix/mpeixish.h');
271    }
272    # If they have built sperl.o...
273    push(@corefiles,'sperl.o') if -f 'sperl.o';
274}
275foreach my $file (@corefiles) {
276    # HP-UX (at least) needs to maintain execute permissions
277    # on dynamically-loadable libraries. So we do it for all.
278    if (copy_if_diff($file,"$installarchlib/CORE/$file")) {
279        if ($file =~ /\.(\Q$so\E|\Q$dlext\E)$/) {
280            chmod(0555, "$installarchlib/CORE/$file");
281            strip("-S", "$installarchlib/CORE/$file") if $^O =~ /^(rhapsody|darwin)$/;
282        } else {
283            chmod(0444, "$installarchlib/CORE/$file");
284        }
285    }
286}
287
288# Install main perl executables
289# Make links to ordinary names if installbin directory isn't current directory.
290
291if (! $versiononly && ! samepath($installbin, '.') && ($^O ne 'dos') && ! $Is_VMS) {
292    safe_unlink("$installbin/$perl$exe_ext", "$installbin/suid$perl$exe_ext");
293    if ($^O eq 'mpeix') {
294        # MPE doesn't support hard links, so use a symlink.
295        # We don't want another cloned copy.
296        symlink($Config{perlpath}, "$installbin/perl$exe_ext");
297    } else {
298        link("$installbin/$perl_verbase$ver$exe_ext",
299                "$installbin/$perl$exe_ext");
300    }
301    link("$installbin/s$perl_verbase$ver$exe_ext",
302            "$installbin/suid$perl$exe_ext")
303      if $d_dosuid;
304}
305
306my $mainperl_is_instperl = 0;
307
308# Make links to ordinary names if installbin directory isn't current directory.
309
310if (!$versiononly && ! samepath($installbin, 'x2p')) {
311    safe_unlink("$installbin/a2p$exe_ext");
312    copy("x2p/a2p$exe_ext", "$installbin/a2p$exe_ext");
313    chmod(0755, "$installbin/a2p$exe_ext");
314}
315
316# cppstdin is just a script, but it is architecture-dependent, so
317# it can't safely be shared.  Place it in $installbin.
318# Note that Configure doesn't build cppstin if it isn't needed, so
319# we skip this if cppstdin doesn't exist.
320if (! $versiononly && (-f 'cppstdin') && (! samepath($installbin, '.'))) {
321    safe_unlink("$installbin/cppstdin");
322    copy("cppstdin", "$installbin/cppstdin");
323    chmod(0755, "$installbin/cppstdin");
324}
325
326# Install scripts.
327
328mkpath($installscript, 1, 0777);
329
330if (! $versiononly) {
331    for (@scripts) {
332        (my $base = $_) =~ s#.*/##;
333        copy($_, "$installscript/$base");
334        chmod(0755, "$installscript/$base");
335    }
336}
337
338# pstruct should be a link to c2ph
339
340if (! $versiononly) {
341    safe_unlink("$installscript/pstruct$scr_ext");
342    if ($^O eq 'dos' or $Is_VMS or $^O eq 'transit') {
343        copy("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext");
344    } else {
345        link("$installscript/c2ph$scr_ext", "$installscript/pstruct$scr_ext");
346    }
347}
348
349# Install pod pages.  Where? I guess in $installprivlib/pod
350# ($installprivlib/pods for cygwin).
351
352my $pod = $Is_Cygwin ? 'pods' : 'pod';
353unless ( $versiononly && !($installprivlib =~ m/\Q$ver/)) {
354    mkpath("${installprivlib}/$pod", 1, 0777);
355
356    # If Perl 5.003's perldiag.pod is there, rename it.
357    if (open POD, "${installprivlib}/$pod/perldiag.pod") {
358        read POD, $_, 4000;
359        close POD;
360        # Some of Perl 5.003's diagnostic messages ended with periods.
361        if (/^=.*\.$/m) {
362            my ($from, $to) = ("${installprivlib}/$pod/perldiag.pod",
363                               "${installprivlib}/$pod/perldiag-5.003.pod");
364            print "  rename $from $to";
365            rename($from, $to)
366                or warn "Couldn't rename $from to $to: $!\n"
367                unless $nonono;
368        }
369    }
370
371    for (@pods) {
372        # $_ is a name like  pod/perl.pod
373        (my $base = $_) =~ s#.*/##;
374        copy_if_diff($_, "${installprivlib}/$pod/${base}");
375    }
376
377}
378
379# Check to make sure there aren't other perls around in installer's
380# path.  This is probably UNIX-specific.  Check all absolute directories
381# in the path except for where public executables are supposed to live.
382# Also skip $mainperl if the user opted to have it be a link to the
383# installed perl.
384
385if (!$versiononly) {
386    my ($path, @path);
387    my $dirsep = ($Is_OS2 || $Is_W32) ? ';' : ':' ;
388    ($path = $ENV{"PATH"}) =~ s:\\:/:g ;
389    @path = split(/$dirsep/, $path);
390    if ($Is_VMS) {
391        my $i = 0;
392        while (exists $ENV{'DCL$PATH' . $i}) {
393            my $dir = unixpath($ENV{'DCL$PATH' . $i});  $dir =~ s-/$--;
394            push(@path,$dir);
395        }
396    }
397    my @otherperls;
398    my %otherperls;
399    for (@path) {
400        next unless m,^/,;
401        # Use &samepath here because some systems have other dirs linked
402        # to $mainperldir (like SunOS)
403        next if samepath($_, $binexp);
404        next if ($mainperl_is_instperl && samepath($_, $mainperldir));
405        my $otherperl = "$_/$perl$exe_ext";
406        next if $otherperls{$otherperl}++;
407        push(@otherperls, $otherperl)
408            if (-x $otherperl && ! -d $otherperl);
409    }
410    if (@otherperls) {
411        print STDERR "\nWarning: $perl appears in your path in the following " .
412            "locations beyond where\nwe just installed it:\n";
413        for (@otherperls) {
414            print STDERR "    ", $_, "\n";
415        }
416        print STDERR "\n";
417    }
418
419}
420
421$packlist->write() unless $nonono;
422print "  Installation complete\n";
423
424exit 0;
425
426###############################################################################
427
428sub yn {
429    my($prompt) = @_;
430    my($answer);
431    my($default) = $prompt =~ m/\[([yn])\]\s*$/i;
432    print $prompt;
433    chop($answer = <STDIN>);
434    $answer = $default if $answer =~ m/^\s*$/;
435    ($answer =~ m/^[yY]/);
436}
437
438sub unlink {
439    my(@names) = @_;
440    my($cnt) = 0;
441
442    return scalar(@names) if $Is_VMS;
443
444    foreach my $name (@names) {
445        next unless -e $name;
446        chmod 0777, $name if ($Is_OS2 || $Is_W32 || $Is_Cygwin);
447        print "  unlink $name\n";
448        ( CORE::unlink($name) and ++$cnt
449          or warn "Couldn't unlink $name: $!\n" ) unless $nonono;
450    }
451    return $cnt;
452}
453
454sub safe_unlink {
455    return if $nonono or $Is_VMS;
456    my @names = @_;
457    foreach my $name (@names) {
458        next unless -e $name;
459        chmod 0777, $name if ($Is_OS2 || $Is_W32);
460        print "  unlink $name\n";
461        next if CORE::unlink($name);
462        warn "Couldn't unlink $name: $!\n";
463        if ($! =~ /busy/i) {
464            print "  mv $name $name.old\n";
465            safe_rename($name, "$name.old")
466                or warn "Couldn't rename $name: $!\n";
467        }
468    }
469}
470
471sub safe_rename {
472    my($from,$to) = @_;
473    if (-f $to and not unlink($to)) {
474        my($i);
475        for ($i = 1; $i < 50; $i++) {
476            last if rename($to, "$to.$i");
477        }
478        warn("Cannot rename to `$to.$i': $!"), return 0
479           if $i >= 50; # Give up!
480    }
481    link($from,$to) || return 0;
482    unlink($from);
483}
484
485sub link {
486    my($from,$to) = @_;
487    my($success) = 0;
488
489    print "  ln $from $to\n";
490    eval {
491        CORE::link($from, $to)
492            ? $success++
493            : ($from =~ m#^/afs/# || $to =~ m#^/afs/#)
494              ? die "AFS"  # okay inside eval {}
495              : die "Couldn't link $from to $to: $!\n"
496          unless $nonono;
497        $packlist->{$to} = { from => $from, type => 'link' };
498    };
499    if ($@) {
500        warn $@;
501        print "  cp $from $to\n";
502        print "  creating new version of $to\n" if $Is_VMS and -e $to;
503        File::Copy::copy($from, $to)
504            ? $success++
505            : warn "Couldn't copy $from to $to: $!\n"
506          unless $nonono;
507        $packlist->{$to} = { type => 'file' };
508    }
509    $success;
510}
511
512sub chmod {
513    my($mode,$name) = @_;
514
515    return if ($^O eq 'dos');
516    printf "  chmod %o %s\n", $mode, $name;
517    CORE::chmod($mode,$name)
518        || warn sprintf("Couldn't chmod %o %s: $!\n", $mode, $name)
519      unless $nonono;
520}
521
522sub copy {
523    my($from,$to) = @_;
524
525    print "  cp $from $to\n";
526    print "  creating new version of $to\n" if $Is_VMS and -e $to;
527    File::Copy::copy($from, $to)
528        || warn "Couldn't copy $from to $to: $!\n"
529      unless $nonono;
530    $packlist->{$to} = { type => 'file' };
531}
532
533sub samepath {
534    my($p1, $p2) = @_;
535
536    return (lc($p1) eq lc($p2)) if $Is_W32;
537
538    if ($p1 ne $p2) {
539        my($dev1, $ino1, $dev2, $ino2);
540        ($dev1, $ino1) = stat($p1);
541        ($dev2, $ino2) = stat($p2);
542        ($dev1 == $dev2 && $ino1 == $ino2);
543    }
544    else {
545        1;
546    }
547}
548
549sub installlib {
550    my $dir = $File::Find::dir;
551    $dir =~ s#^\.(?![^/])/?##;
552    local($depth) = $dir ? "lib/$dir" : "lib";
553
554    my $name = $_;
555
556    # Ignore RCS and CVS directories.
557    if (($name eq 'CVS' or $name eq 'RCS') and -d $name) {
558        $File::Find::prune = 1;
559        return;
560    }
561   
562    # ignore patch backups, RCS files, emacs backup & temp files and the
563    # .exists files.
564    return if $name =~ m{\.orig$|~$|^#.+#$|,v$|^\.exists};
565
566    $name = "$dir/$name" if $dir ne '';
567
568    my $installlib = $installprivlib;
569    if ($dir =~ /^auto/ ||
570          ($name =~ /^(.*)\.(?:pm|pod)$/ && $archpms{$1}) ||
571          ($name =~ /^(.*)\.(?:h|lib)$/i && $Is_W32)
572       ) {
573        $installlib = $installarchlib;
574        return unless $do_installarchlib;
575    } else {
576        return unless $do_installprivlib;
577    }
578
579    if (-f $_) {
580        if (/\.(?:al|ix)$/ && !($dir =~ m[^auto/(.*)$] && $archpms{$1})) {
581            $installlib = $installprivlib;
582            #We're installing *.al and *.ix files into $installprivlib,
583            #but we have to delete old *.al and *.ix files from the 5.000
584            #distribution:
585            #This might not work because $archname might have changed.
586            unlink("$installarchlib/$name");
587        }
588        $packlist->{"$installlib/$name"} = { type => 'file' };
589        if (compare($_, "$installlib/$name") || $nonono) {
590            unlink("$installlib/$name");
591            mkpath("$installlib/$dir", 1, 0777);
592            # HP-UX (at least) needs to maintain execute permissions
593            # on dynamically-loaded libraries.
594            copy_if_diff($_, "$installlib/$name")
595                and chmod($name =~ /\.(so|$dlext)$/o ? 0555 : 0444,
596                           "$installlib/$name");
597        }
598    }
599}
600
601# Copy $from to $to, only if $from is different than $to.
602# Also preserve modification times for .a libraries.
603# On some systems, if you do
604#   ranlib libperl.a
605#   cp libperl.a /usr/local/lib/perl5/archlib/CORE/libperl.a
606# and then try to link against the installed libperl.a, you might
607# get an error message to the effect that the symbol table is older
608# than the library.
609# Return true if copying occurred.
610
611sub copy_if_diff {
612    my($from,$to)=@_;
613    return 1 if (($^O eq 'VMS') && (-d $from));
614    -f $from || warn "$0: $from not found";
615    $packlist->{$to} = { type => 'file' };
616    if (compare($from, $to) || $nonono) {
617        safe_unlink($to);   # In case we don't have write permissions.
618        if ($nonono) {
619            $from = $depth . "/" . $from if $depth;
620        }
621        copy($from, $to);
622        # Restore timestamps if it's a .a library or for OS/2.
623        if (!$nonono && ($Is_OS2 || $to =~ /\.a$/)) {
624            my ($atime, $mtime) = (stat $from)[8,9];
625            utime $atime, $mtime, $to;
626        }
627        1;
628    }
629}
630
631sub strip
632{
633    my(@args) = @_;
634
635    return unless $dostrip;
636
637    my @opts;
638    while (@args && $args[0] =~ /^(-\w+)$/) {
639        push @opts, shift @args;
640    }
641
642    foreach my $file (@args) {
643        if (-f $file) {
644            print "  strip $file\n";
645            system("strip", @opts, $file);
646        } else {
647            print "# file '$file' skipped\n";
648        }
649    }
650}
651
Note: See TracBrowser for help on using the repository browser.