home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / ExtUtils / Liblist.pm < prev    next >
Text File  |  1997-05-19  |  16KB  |  443 lines

  1. package ExtUtils::Liblist;
  2. use vars qw($VERSION);
  3. # Broken out of MakeMaker from version 4.11
  4.  
  5. $VERSION = substr q$Revision: 1.3 $, 10;
  6.  
  7. use Config;
  8. use Cwd 'cwd';
  9. use File::Basename;
  10.  
  11. sub ext {
  12.   if   ($^O eq 'VMS') { return &_vms_ext;      }
  13.   else                { return &_unix_os2_ext; }
  14. }
  15.  
  16. sub _unix_os2_ext {
  17.     my($self,$potential_libs, $Verbose) = @_;
  18.     if ($^O =~ 'os2' and $Config{libs}) { 
  19.     # Dynamic libraries are not transitive, so we may need including
  20.     # the libraries linked against perl.dll again.
  21.  
  22.     $potential_libs .= " " if $potential_libs;
  23.     $potential_libs .= $Config{libs};
  24.     }
  25.     return ("", "", "", "") unless $potential_libs;
  26.     print STDOUT "Potential libraries are '$potential_libs':\n" if $Verbose;
  27.  
  28.     my($so)   = $Config{'so'};
  29.     my($libs) = $Config{'libs'};
  30.     my $Config_libext = $Config{lib_ext} || ".a";
  31.  
  32.  
  33.     # compute $extralibs, $bsloadlibs and $ldloadlibs from
  34.     # $potential_libs
  35.     # this is a rewrite of Andy Dougherty's extliblist in perl
  36.     # its home is in <distribution>/ext/util
  37.  
  38.     my(@searchpath); # from "-L/path" entries in $potential_libs
  39.     my(@libpath) = split " ", $Config{'libpth'};
  40.     my(@ldloadlibs, @bsloadlibs, @extralibs, @ld_run_path, %ld_run_path_seen);
  41.     my($fullname, $thislib, $thispth, @fullname);
  42.     my($pwd) = cwd(); # from Cwd.pm
  43.     my($found) = 0;
  44.  
  45.     foreach $thislib (split ' ', $potential_libs){
  46.  
  47.     # Handle possible linker path arguments.
  48.     if ($thislib =~ s/^(-[LR])//){    # save path flag type
  49.         my($ptype) = $1;
  50.         unless (-d $thislib){
  51.         print STDOUT "$ptype$thislib ignored, directory does not exist\n"
  52.             if $Verbose;
  53.         next;
  54.         }
  55.         unless ($self->file_name_is_absolute($thislib)) {
  56.           print STDOUT "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
  57.           $thislib = $self->catdir($pwd,$thislib);
  58.         }
  59.         push(@searchpath, $thislib);
  60.         push(@extralibs,  "$ptype$thislib");
  61.         push(@ldloadlibs, "$ptype$thislib");
  62.         next;
  63.     }
  64.  
  65.     # Handle possible library arguments.
  66.     unless ($thislib =~ s/^-l//){
  67.       print STDOUT "Unrecognized argument in LIBS ignored: '$thislib'\n";
  68.       next;
  69.     }
  70.  
  71.     my($found_lib)=0;
  72.     foreach $thispth (@searchpath, @libpath){
  73.  
  74.         # Try to find the full name of the library.  We need this to
  75.         # determine whether it's a dynamically-loadable library or not.
  76.         # This tends to be subject to various os-specific quirks.
  77.         # For gcc-2.6.2 on linux (March 1995), DLD can not load
  78.         # .sa libraries, with the exception of libm.sa, so we
  79.         # deliberately skip them.
  80.         if (@fullname =
  81.             $self->lsdir($thispth,"^\Qlib$thislib.$so.\E[0-9]+")){
  82.         # Take care that libfoo.so.10 wins against libfoo.so.9.
  83.         # Compare two libraries to find the most recent version
  84.         # number.  E.g.  if you have libfoo.so.9.0.7 and
  85.         # libfoo.so.10.1, first convert all digits into two
  86.         # decimal places.  Then we'll add ".00" to the shorter
  87.         # strings so that we're comparing strings of equal length
  88.         # Thus we'll compare libfoo.so.09.07.00 with
  89.         # libfoo.so.10.01.00.  Some libraries might have letters
  90.         # in the version.  We don't know what they mean, but will
  91.         # try to skip them gracefully -- we'll set any letter to
  92.         # '0'.  Finally, sort in reverse so we can take the
  93.         # first element.
  94.  
  95.         #TODO: iterate through the directory instead of sorting
  96.  
  97.         $fullname = "$thispth/" .
  98.         (sort { my($ma) = $a;
  99.             my($mb) = $b;
  100.             $ma =~ tr/A-Za-z/0/s;
  101.             $ma =~ s/\b(\d)\b/0$1/g;
  102.             $mb =~ tr/A-Za-z/0/s;
  103.             $mb =~ s/\b(\d)\b/0$1/g;
  104.             while (length($ma) < length($mb)) { $ma .= ".00"; }
  105.             while (length($mb) < length($ma)) { $mb .= ".00"; }
  106.             # Comparison deliberately backwards
  107.             $mb cmp $ma;} @fullname)[0];
  108.         } elsif (-f ($fullname="$thispth/lib$thislib.$so")
  109.          && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
  110.         } elsif (-f ($fullname="$thispth/lib${thislib}_s$Config_libext")
  111.          && ($thislib .= "_s") ){ # we must explicitly use _s version
  112.         } elsif (-f ($fullname="$thispth/lib$thislib$Config_libext")){
  113.         } elsif (-f ($fullname="$thispth/$thislib$Config_libext")){
  114.         } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
  115.         } elsif ($^O eq 'dgux'
  116.          && -l ($fullname="$thispth/lib$thislib$Config_libext")
  117.          && readlink($fullname) =~ /^elink:/) {
  118.          # Some of DG's libraries look like misconnected symbolic
  119.          # links, but development tools can follow them.  (They
  120.          # look like this:
  121.          #
  122.          #    libm.a -> elink:${SDE_PATH:-/usr}/sde/\
  123.          #    ${TARGET_BINARY_INTERFACE:-m88kdgux}/usr/lib/libm.a
  124.          #
  125.          # , the compilation tools expand the environment variables.)
  126.         } else {
  127.         print STDOUT "$thislib not found in $thispth\n" if $Verbose;
  128.         next;
  129.         }
  130.         print STDOUT "'-l$thislib' found at $fullname\n" if $Verbose;
  131.         my($fullnamedir) = dirname($fullname);
  132.         push @ld_run_path, $fullnamedir unless $ld_run_path_seen{$fullnamedir}++;
  133.         $found++;
  134.         $found_lib++;
  135.  
  136.         # Now update library lists
  137.  
  138.         # what do we know about this library...
  139.         my $is_dyna = ($fullname !~ /\Q$Config_libext\E$/);
  140.         my $in_perl = ($libs =~ /\B-l\Q$ {thislib}\E\b/s);
  141.  
  142.         # Do not add it into the list if it is already linked in
  143.         # with the main perl executable.
  144.         # We have to special-case the NeXT, because math and ndbm 
  145.         # are both in libsys_s
  146.         unless ($in_perl || 
  147.         ($Config{'osname'} eq 'next' &&
  148.             ($thislib eq 'm' || $thislib eq 'ndbm')) ){
  149.         push(@extralibs, "-l$thislib");
  150.         }
  151.  
  152.         # We might be able to load this archive file dynamically
  153.         if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
  154.         ||   ($Config{'dlsrc'} =~ /dl_dld/) )
  155.         {
  156.         # We push -l$thislib instead of $fullname because
  157.         # it avoids hardwiring a fixed path into the .bs file.
  158.         # Mkbootstrap will automatically add dl_findfile() to
  159.         # the .bs file if it sees a name in the -l format.
  160.         # USE THIS, when dl_findfile() is fixed: 
  161.         # push(@bsloadlibs, "-l$thislib");
  162.         # OLD USE WAS while checking results against old_extliblist
  163.         push(@bsloadlibs, "$fullname");
  164.         } else {
  165.         if ($is_dyna){
  166.                     # For SunOS4, do not add in this shared library if
  167.                     # it is already linked in the main perl executable
  168.             push(@ldloadlibs, "-l$thislib")
  169.             unless ($in_perl and $^O eq 'sunos');
  170.         } else {
  171.             push(@ldloadlibs, "-l$thislib");
  172.         }
  173.         }
  174.         last;    # found one here so don't bother looking further
  175.     }
  176.     print STDOUT "Note (probably harmless): "
  177.              ."No library found for -l$thislib\n"
  178.         unless $found_lib>0;
  179.     }
  180.     return ('','','','') unless $found;
  181.     ("@extralibs", "@bsloadlibs", "@ldloadlibs",join(":",@ld_run_path));
  182. }
  183.  
  184.  
  185. sub _vms_ext {
  186.   my($self, $potential_libs,$verbose) = @_;
  187.   return ('', '', '', '') unless $potential_libs;
  188.  
  189.   my(@dirs,@libs,$dir,$lib,%sh,%olb,%obj);
  190.   my $cwd = cwd();
  191.   my($so,$lib_ext,$obj_ext) = @Config{'so','lib_ext','obj_ext'};
  192.   # List of common Unix library names and there VMS equivalents
  193.   # (VMS equivalent of '' indicates that the library is automatially
  194.   # searched by the linker, and should be skipped here.)
  195.   my %libmap = ( 'm' => '', 'f77' => '', 'F77' => '', 'V77' => '', 'c' => '',
  196.                  'malloc' => '', 'crypt' => '', 'resolv' => '', 'c_s' => '',
  197.                  'socket' => '', 'X11' => 'DECW$XLIBSHR',
  198.                  'Xt' => 'DECW$XTSHR', 'Xm' => 'DECW$XMLIBSHR',
  199.                  'Xmu' => 'DECW$XMULIBSHR');
  200.   if ($Config{'vms_cc_type'} ne 'decc') { $libmap{'curses'} = 'VAXCCURSE'; }
  201.  
  202.   print STDOUT "Potential libraries are '$potential_libs'\n" if $verbose;
  203.  
  204.   # First, sort out directories and library names in the input
  205.   foreach $lib (split ' ',$potential_libs) {
  206.     push(@dirs,$1),   next if $lib =~ /^-L(.*)/;
  207.     push(@dirs,$lib), next if $lib =~ /[:>\]]$/;
  208.     push(@dirs,$lib), next if -d $lib;
  209.     push(@libs,$1),   next if $lib =~ /^-l(.*)/;
  210.     push(@libs,$lib);
  211.   }
  212.   push(@dirs,split(' ',$Config{'libpth'}));
  213.  
  214.   # Now make sure we've got VMS-syntax absolute directory specs
  215.   # (We don't, however, check whether someone's hidden a relative
  216.   # path in a logical name.)
  217.   foreach $dir (@dirs) {
  218.     unless (-d $dir) {
  219.       print STDOUT "Skipping nonexistent Directory $dir\n" if $verbose > 1;
  220.       $dir = '';
  221.       next;
  222.     }
  223.     print STDOUT "Resolving directory $dir\n" if $verbose;
  224.     if ($self->file_name_is_absolute($dir)) { $dir = $self->fixpath($dir,1); }
  225.     else                                    { $dir = $self->catdir($cwd,$dir); }
  226.   }
  227.   @dirs = grep { length($_) } @dirs;
  228.   unshift(@dirs,''); # Check each $lib without additions first
  229.  
  230.   LIB: foreach $lib (@libs) {
  231.     if (exists $libmap{$lib}) {
  232.       next unless length $libmap{$lib};
  233.       $lib = $libmap{$lib};
  234.     }
  235.  
  236.     my(@variants,$variant,$name,$test,$cand);
  237.     my($ctype) = '';
  238.  
  239.     # If we don't have a file type, consider it a possibly abbreviated name and
  240.     # check for common variants.  We try these first to grab libraries before
  241.     # a like-named executable image (e.g. -lperl resolves to perlshr.exe
  242.     # before perl.exe).
  243.     if ($lib !~ /\.[^:>\]]*$/) {
  244.       push(@variants,"${lib}shr","${lib}rtl","${lib}lib");
  245.       push(@variants,"lib$lib") if $lib !~ /[:>\]]/;
  246.     }
  247.     push(@variants,$lib);
  248.     print STDOUT "Looking for $lib\n" if $verbose;
  249.     foreach $variant (@variants) {
  250.       foreach $dir (@dirs) {
  251.         my($type);
  252.  
  253.         $name = "$dir$variant";
  254.         print "\tChecking $name\n" if $verbose > 2;
  255.         if (-f ($test = VMS::Filespec::rmsexpand($name))) {
  256.           # It's got its own suffix, so we'll have to figure out the type
  257.           if    ($test =~ /(?:$so|exe)$/i)      { $type = 'sh'; }
  258.           elsif ($test =~ /(?:$lib_ext|olb)$/i) { $type = 'olb'; }
  259.           elsif ($test =~ /(?:$obj_ext|obj)$/i) {
  260.             print STDOUT "Note (probably harmless): "
  261.              ."Plain object file $test found in library list\n";
  262.             $type = 'obj';
  263.           }
  264.           else {
  265.             print STDOUT "Note (probably harmless): "
  266.              ."Unknown library type for $test; assuming shared\n";
  267.             $type = 'sh';
  268.           }
  269.         }
  270.         elsif (-f ($test = VMS::Filespec::rmsexpand($name,$so))      or
  271.                -f ($test = VMS::Filespec::rmsexpand($name,'.exe')))     {
  272.           $type = 'sh';
  273.           $name = $test unless $test =~ /exe;?\d*$/i;
  274.         }
  275.         elsif (not length($ctype) and  # If we've got a lib already, don't bother
  276.                ( -f ($test = VMS::Filespec::rmsexpand($name,$lib_ext)) or
  277.                  -f ($test = VMS::Filespec::rmsexpand($name,'.olb'))))  {
  278.           $type = 'olb';
  279.           $name = $test unless $test =~ /olb;?\d*$/i;
  280.         }
  281.         elsif (not length($ctype) and  # If we've got a lib already, don't bother
  282.                ( -f ($test = VMS::Filespec::rmsexpand($name,$obj_ext)) or
  283.                  -f ($test = VMS::Filespec::rmsexpand($name,'.obj'))))  {
  284.           print STDOUT "Note (probably harmless): "
  285.                ."Plain object file $test found in library list\n";
  286.           $type = 'obj';
  287.           $name = $test unless $test =~ /obj;?\d*$/i;
  288.         }
  289.         if (defined $type) {
  290.           $ctype = $type; $cand = $name;
  291.           last if $ctype eq 'sh';
  292.         }
  293.       }
  294.       if ($ctype) { 
  295.         eval '$' . $ctype . "{'$cand'}++";
  296.         die "Error recording library: $@" if $@;
  297.         print STDOUT "\tFound as $cand (really $ctest), type $ctype\n" if $verbose > 1;
  298.         next LIB;
  299.       }
  300.     }
  301.     print STDOUT "Note (probably harmless): "
  302.          ."No library found for $lib\n";
  303.   }
  304.  
  305.   @libs = sort keys %obj;
  306.   # This has to precede any other CRTLs, so just make it first
  307.   if ($olb{VAXCCURSE}) {
  308.     push(@libs,"$olb{VAXCCURSE}/Library");
  309.     delete $olb{VAXCCURSE};
  310.   }
  311.   push(@libs, map { "$_/Library" } sort keys %olb);
  312.   push(@libs, map { "$_/Share"   } sort keys %sh);
  313.   $lib = join(' ',@libs);
  314.   print "Result: $lib\n" if $verbose;
  315.   wantarray ? ($lib, '', $lib, '') : $lib;
  316. }
  317.  
  318. 1;
  319.  
  320. __END__
  321.  
  322. =head1 NAME
  323.  
  324. ExtUtils::Liblist - determine libraries to use and how to use them
  325.  
  326. =head1 SYNOPSIS
  327.  
  328. C<require ExtUtils::Liblist;>
  329.  
  330. C<ExtUtils::Liblist::ext($potential_libs, $Verbose);>
  331.  
  332. =head1 DESCRIPTION
  333.  
  334. This utility takes a list of libraries in the form C<-llib1 -llib2
  335. -llib3> and prints out lines suitable for inclusion in an extension
  336. Makefile.  Extra library paths may be included with the form
  337. C<-L/another/path> this will affect the searches for all subsequent
  338. libraries.
  339.  
  340. It returns an array of four scalar values: EXTRALIBS, BSLOADLIBS,
  341. LDLOADLIBS, and LD_RUN_PATH.
  342.  
  343. Dependent libraries can be linked in one of three ways:
  344.  
  345. =over 2
  346.  
  347. =item * For static extensions
  348.  
  349. by the ld command when the perl binary is linked with the extension
  350. library. See EXTRALIBS below.
  351.  
  352. =item * For dynamic extensions
  353.  
  354. by the ld command when the shared object is built/linked. See
  355. LDLOADLIBS below.
  356.  
  357. =item * For dynamic extensions
  358.  
  359. by the DynaLoader when the shared object is loaded. See BSLOADLIBS
  360. below.
  361.  
  362. =back
  363.  
  364. =head2 EXTRALIBS
  365.  
  366. List of libraries that need to be linked with when linking a perl
  367. binary which includes this extension Only those libraries that
  368. actually exist are included.  These are written to a file and used
  369. when linking perl.
  370.  
  371. =head2 LDLOADLIBS and LD_RUN_PATH
  372.  
  373. List of those libraries which can or must be linked into the shared
  374. library when created using ld. These may be static or dynamic
  375. libraries.  LD_RUN_PATH is a colon separated list of the directories
  376. in LDLOADLIBS. It is passed as an environment variable to the process
  377. that links the shared library.
  378.  
  379. =head2 BSLOADLIBS
  380.  
  381. List of those libraries that are needed but can be linked in
  382. dynamically at run time on this platform.  SunOS/Solaris does not need
  383. this because ld records the information (from LDLOADLIBS) into the
  384. object file.  This list is used to create a .bs (bootstrap) file.
  385.  
  386. =head1 PORTABILITY
  387.  
  388. This module deals with a lot of system dependencies and has quite a
  389. few architecture specific B<if>s in the code.
  390.  
  391. =head2 VMS implementation
  392.  
  393. The version of ext() which is executed under VMS differs from the
  394. Unix-OS/2 version in several respects:
  395.  
  396. =over 2
  397.  
  398. =item *
  399.  
  400. Input library and path specifications are accepted with or without the
  401. C<-l> and C<-L> prefices used by Unix linkers.  If neither prefix is
  402. present, a token is considered a directory to search if it is in fact
  403. a directory, and a library to search for otherwise.  Authors who wish
  404. their extensions to be portable to Unix or OS/2 should use the Unix
  405. prefixes, since the Unix-OS/2 version of ext() requires them.
  406.  
  407. =item *
  408.  
  409. Wherever possible, shareable images are preferred to object libraries,
  410. and object libraries to plain object files.  In accordance with VMS
  411. naming conventions, ext() looks for files named I<lib>shr and I<lib>rtl;
  412. it also looks for I<lib>lib and libI<lib> to accomodate Unix conventions
  413. used in some ported software.
  414.  
  415. =item *
  416.  
  417. For each library that is found, an appropriate directive for a linker options
  418. file is generated.  The return values are space-separated strings of
  419. these directives, rather than elements used on the linker command line.
  420.  
  421. =item *
  422.  
  423. LDLOADLIBS and EXTRALIBS are always identical under VMS, and BSLOADLIBS
  424. and LD_RIN_PATH are always empty.
  425.  
  426. =back
  427.  
  428. In addition, an attempt is made to recognize several common Unix library
  429. names, and filter them out or convert them to their VMS equivalents, as
  430. appropriate.
  431.  
  432. In general, the VMS version of ext() should properly handle input from
  433. extensions originally designed for a Unix or VMS environment.  If you
  434. encounter problems, or discover cases where the search could be improved,
  435. please let us know.
  436.  
  437. =head1 SEE ALSO
  438.  
  439. L<ExtUtils::MakeMaker>
  440.  
  441. =cut
  442.  
  443.