home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / ExtUtils / Liblist.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  16.2 KB  |  499 lines

  1. package ExtUtils::Liblist;
  2. use vars qw($VERSION);
  3.  
  4. $VERSION = substr q$Revision: 1.25 $, 10;
  5.  
  6. use Config;
  7. use Cwd 'cwd';
  8. use File::Basename;
  9.  
  10. sub ext {
  11.   if   ($^O eq 'VMS')     { return &_vms_ext;      }
  12.   elsif($^O eq 'MSWin32') { return &_win32_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.  
  20.     $potential_libs .= " " if $potential_libs;
  21.     $potential_libs .= $Config{libs};
  22.     }
  23.     return ("", "", "", "") unless $potential_libs;
  24.     print STDOUT "Potential libraries are '$potential_libs':\n" if $verbose;
  25.  
  26.     my($so)   = $Config{'so'};
  27.     my($libs) = $Config{'libs'};
  28.     my $Config_libext = $Config{lib_ext} || ".a";
  29.  
  30.  
  31.  
  32.     my(@searchpath); # from "-L/path" entries in $potential_libs
  33.     my(@libpath) = split " ", $Config{'libpth'};
  34.     my(@ldloadlibs, @bsloadlibs, @extralibs, @ld_run_path, %ld_run_path_seen);
  35.     my($fullname, $thislib, $thispth, @fullname);
  36.     my($pwd) = cwd(); # from Cwd.pm
  37.     my($found) = 0;
  38.  
  39.     foreach $thislib (split ' ', $potential_libs){
  40.  
  41.     if ($thislib =~ s/^(-[LR])//){    # save path flag type
  42.         my($ptype) = $1;
  43.         unless (-d $thislib){
  44.         print STDOUT "$ptype$thislib ignored, directory does not exist\n"
  45.             if $verbose;
  46.         next;
  47.         }
  48.         unless ($self->file_name_is_absolute($thislib)) {
  49.           print STDOUT "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
  50.           $thislib = $self->catdir($pwd,$thislib);
  51.         }
  52.         push(@searchpath, $thislib);
  53.         push(@extralibs,  "$ptype$thislib");
  54.         push(@ldloadlibs, "$ptype$thislib");
  55.         next;
  56.     }
  57.  
  58.     unless ($thislib =~ s/^-l//){
  59.       print STDOUT "Unrecognized argument in LIBS ignored: '$thislib'\n";
  60.       next;
  61.     }
  62.  
  63.     my($found_lib)=0;
  64.     foreach $thispth (@searchpath, @libpath){
  65.  
  66.         if (@fullname =
  67.             $self->lsdir($thispth,"^\Qlib$thislib.$so.\E[0-9]+")){
  68.  
  69.  
  70.         $fullname = "$thispth/" .
  71.         (sort { my($ma) = $a;
  72.             my($mb) = $b;
  73.             $ma =~ tr/A-Za-z/0/s;
  74.             $ma =~ s/\b(\d)\b/0$1/g;
  75.             $mb =~ tr/A-Za-z/0/s;
  76.             $mb =~ s/\b(\d)\b/0$1/g;
  77.             while (length($ma) < length($mb)) { $ma .= ".00"; }
  78.             while (length($mb) < length($ma)) { $mb .= ".00"; }
  79.             $mb cmp $ma;} @fullname)[0];
  80.         } elsif (-f ($fullname="$thispth/lib$thislib.$so")
  81.          && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
  82.         } elsif (-f ($fullname="$thispth/lib${thislib}_s$Config_libext")
  83.          && ($thislib .= "_s") ){ # we must explicitly use _s version
  84.         } elsif (-f ($fullname="$thispth/lib$thislib$Config_libext")){
  85.         } elsif (-f ($fullname="$thispth/$thislib$Config_libext")){
  86.         } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
  87.         } elsif ($^O eq 'dgux'
  88.          && -l ($fullname="$thispth/lib$thislib$Config_libext")
  89.          && readlink($fullname) =~ /^elink:/) {
  90.         } else {
  91.         print STDOUT "$thislib not found in $thispth\n" if $verbose;
  92.         next;
  93.         }
  94.         print STDOUT "'-l$thislib' found at $fullname\n" if $verbose;
  95.         my($fullnamedir) = dirname($fullname);
  96.         push @ld_run_path, $fullnamedir unless $ld_run_path_seen{$fullnamedir}++;
  97.         $found++;
  98.         $found_lib++;
  99.  
  100.  
  101.         my $is_dyna = ($fullname !~ /\Q$Config_libext\E$/);
  102.         my $in_perl = ($libs =~ /\B-l\Q$ {thislib}\E\b/s);
  103.  
  104.         unless ($in_perl || 
  105.         ($Config{'osname'} eq 'next' &&
  106.             ($thislib eq 'm' || $thislib eq 'ndbm')) ){
  107.         push(@extralibs, "-l$thislib");
  108.         }
  109.  
  110.         if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
  111.         ||   ($Config{'dlsrc'} =~ /dl_dld/) )
  112.         {
  113.         push(@bsloadlibs, "$fullname");
  114.         } else {
  115.         if ($is_dyna){
  116.             push(@ldloadlibs, "-l$thislib")
  117.             unless ($in_perl and $^O eq 'sunos');
  118.         } else {
  119.             push(@ldloadlibs, "-l$thislib");
  120.         }
  121.         }
  122.         last;    # found one here so don't bother looking further
  123.     }
  124.     print STDOUT "Note (probably harmless): "
  125.              ."No library found for -l$thislib\n"
  126.         unless $found_lib>0;
  127.     }
  128.     return ('','','','') unless $found;
  129.     ("@extralibs", "@bsloadlibs", "@ldloadlibs",join(":",@ld_run_path));
  130. }
  131.  
  132. sub _win32_ext {
  133.     my($self, $potential_libs, $verbose) = @_;
  134.  
  135.     return ("", "", "", "") unless $potential_libs;
  136.  
  137.     my($so)   = $Config{'so'};
  138.     my($libs) = $Config{'libs'};
  139.     my($libpth) = $Config{'libpth'};
  140.     my($libext) = $Config{'lib_ext'} || ".lib";
  141.  
  142.     if ($libs and $potential_libs !~ /:nodefault/i) { 
  143.  
  144.     $potential_libs .= " " if $potential_libs;
  145.     $potential_libs .= $libs;
  146.     }
  147.     print STDOUT "Potential libraries are '$potential_libs':\n" if $verbose;
  148.  
  149.  
  150.     my(@searchpath); # from "-L/path" entries in $potential_libs
  151.     my(@libpath) = split " ", $libpth;
  152.     my(@extralibs);
  153.     my($fullname, $thislib, $thispth);
  154.     my($pwd) = cwd(); # from Cwd.pm
  155.     my($lib) = '';
  156.     my($found) = 0;
  157.  
  158.     foreach $thislib (split ' ', $potential_libs){
  159.  
  160.     if ($thislib =~ s/^-L// and not -d $thislib) {
  161.         print STDOUT "-L$thislib ignored, directory does not exist\n"
  162.         if $verbose;
  163.         next;
  164.     }
  165.     elsif (-d $thislib) {
  166.         unless ($self->file_name_is_absolute($thislib)) {
  167.           print STDOUT "Warning: -L$thislib changed to -L$pwd/$thislib\n";
  168.           $thislib = $self->catdir($pwd,$thislib);
  169.         }
  170.         push(@searchpath, $thislib);
  171.         next;
  172.     }
  173.  
  174.     $thislib =~ s/^-l//;
  175.     $thislib .= $libext if $thislib !~ /\Q$libext\E$/i;
  176.  
  177.     my($found_lib)=0;
  178.     foreach $thispth (@searchpath, @libpath){
  179.         unless (-f ($fullname="$thispth\\$thislib")) {
  180.         print STDOUT "$thislib not found in $thispth\n" if $verbose;
  181.         next;
  182.         }
  183.         print STDOUT "'$thislib' found at $fullname\n" if $verbose;
  184.         $found++;
  185.         $found_lib++;
  186.         push(@extralibs, $fullname);
  187.         last;
  188.     }
  189.     print STDOUT "Note (probably harmless): "
  190.              ."No library found for '$thislib'\n"
  191.         unless $found_lib>0;
  192.     }
  193.     return ('','','','') unless $found;
  194.     $lib = join(' ',@extralibs);
  195.     print "Result: $lib\n" if $verbose;
  196.     wantarray ? ($lib, '', $lib, '') : $lib;
  197. }
  198.  
  199.  
  200. sub _vms_ext {
  201.   my($self, $potential_libs,$verbose) = @_;
  202.   return ('', '', '', '') unless $potential_libs;
  203.  
  204.   my(@dirs,@libs,$dir,$lib,%sh,%olb,%obj);
  205.   my $cwd = cwd();
  206.   my($so,$lib_ext,$obj_ext) = @Config{'so','lib_ext','obj_ext'};
  207.   my %libmap = ( 'm' => '', 'f77' => '', 'F77' => '', 'V77' => '', 'c' => '',
  208.                  'malloc' => '', 'crypt' => '', 'resolv' => '', 'c_s' => '',
  209.                  'socket' => '', 'X11' => 'DECW$XLIBSHR',
  210.                  'Xt' => 'DECW$XTSHR', 'Xm' => 'DECW$XMLIBSHR',
  211.                  'Xmu' => 'DECW$XMULIBSHR');
  212.   if ($Config{'vms_cc_type'} ne 'decc') { $libmap{'curses'} = 'VAXCCURSE'; }
  213.  
  214.   print STDOUT "Potential libraries are '$potential_libs'\n" if $verbose;
  215.  
  216.   foreach $lib (split ' ',$potential_libs) {
  217.     push(@dirs,$1),   next if $lib =~ /^-L(.*)/;
  218.     push(@dirs,$lib), next if $lib =~ /[:>\]]$/;
  219.     push(@dirs,$lib), next if -d $lib;
  220.     push(@libs,$1),   next if $lib =~ /^-l(.*)/;
  221.     push(@libs,$lib);
  222.   }
  223.   push(@dirs,split(' ',$Config{'libpth'}));
  224.  
  225.   foreach $dir (@dirs) {
  226.     unless (-d $dir) {
  227.       print STDOUT "Skipping nonexistent Directory $dir\n" if $verbose > 1;
  228.       $dir = '';
  229.       next;
  230.     }
  231.     print STDOUT "Resolving directory $dir\n" if $verbose;
  232.     if ($self->file_name_is_absolute($dir)) { $dir = $self->fixpath($dir,1); }
  233.     else                                    { $dir = $self->catdir($cwd,$dir); }
  234.   }
  235.   @dirs = grep { length($_) } @dirs;
  236.   unshift(@dirs,''); # Check each $lib without additions first
  237.  
  238.   LIB: foreach $lib (@libs) {
  239.     if (exists $libmap{$lib}) {
  240.       next unless length $libmap{$lib};
  241.       $lib = $libmap{$lib};
  242.     }
  243.  
  244.     my(@variants,$variant,$name,$test,$cand);
  245.     my($ctype) = '';
  246.  
  247.     if ($lib !~ /\.[^:>\]]*$/) {
  248.       push(@variants,"${lib}shr","${lib}rtl","${lib}lib");
  249.       push(@variants,"lib$lib") if $lib !~ /[:>\]]/;
  250.     }
  251.     push(@variants,$lib);
  252.     print STDOUT "Looking for $lib\n" if $verbose;
  253.     foreach $variant (@variants) {
  254.       foreach $dir (@dirs) {
  255.         my($type);
  256.  
  257.         $name = "$dir$variant";
  258.         print "\tChecking $name\n" if $verbose > 2;
  259.         if (-f ($test = VMS::Filespec::rmsexpand($name))) {
  260.           if    ($test =~ /(?:$so|exe)$/i)      { $type = 'sh'; }
  261.           elsif ($test =~ /(?:$lib_ext|olb)$/i) { $type = 'olb'; }
  262.           elsif ($test =~ /(?:$obj_ext|obj)$/i) {
  263.             print STDOUT "Note (probably harmless): "
  264.              ."Plain object file $test found in library list\n";
  265.             $type = 'obj';
  266.           }
  267.           else {
  268.             print STDOUT "Note (probably harmless): "
  269.              ."Unknown library type for $test; assuming shared\n";
  270.             $type = 'sh';
  271.           }
  272.         }
  273.         elsif (-f ($test = VMS::Filespec::rmsexpand($name,$so))      or
  274.                -f ($test = VMS::Filespec::rmsexpand($name,'.exe')))     {
  275.           $type = 'sh';
  276.           $name = $test unless $test =~ /exe;?\d*$/i;
  277.         }
  278.         elsif (not length($ctype) and  # If we've got a lib already, don't bother
  279.                ( -f ($test = VMS::Filespec::rmsexpand($name,$lib_ext)) or
  280.                  -f ($test = VMS::Filespec::rmsexpand($name,'.olb'))))  {
  281.           $type = 'olb';
  282.           $name = $test unless $test =~ /olb;?\d*$/i;
  283.         }
  284.         elsif (not length($ctype) and  # If we've got a lib already, don't bother
  285.                ( -f ($test = VMS::Filespec::rmsexpand($name,$obj_ext)) or
  286.                  -f ($test = VMS::Filespec::rmsexpand($name,'.obj'))))  {
  287.           print STDOUT "Note (probably harmless): "
  288.                ."Plain object file $test found in library list\n";
  289.           $type = 'obj';
  290.           $name = $test unless $test =~ /obj;?\d*$/i;
  291.         }
  292.         if (defined $type) {
  293.           $ctype = $type; $cand = $name;
  294.           last if $ctype eq 'sh';
  295.         }
  296.       }
  297.       if ($ctype) { 
  298.         eval '$' . $ctype . "{'$cand'}++";
  299.         die "Error recording library: $@" if $@;
  300.         print STDOUT "\tFound as $cand (really $test), type $ctype\n" if $verbose > 1;
  301.         next LIB;
  302.       }
  303.     }
  304.     print STDOUT "Note (probably harmless): "
  305.          ."No library found for $lib\n";
  306.   }
  307.  
  308.   @libs = sort keys %obj;
  309.   if ($olb{VAXCCURSE}) {
  310.     push(@libs,"$olb{VAXCCURSE}/Library");
  311.     delete $olb{VAXCCURSE};
  312.   }
  313.   push(@libs, map { "$_/Library" } sort keys %olb);
  314.   push(@libs, map { "$_/Share"   } sort keys %sh);
  315.   $lib = join(' ',@libs);
  316.   print "Result: $lib\n" if $verbose;
  317.   wantarray ? ($lib, '', $lib, '') : $lib;
  318. }
  319.  
  320. 1;
  321.  
  322. __END__
  323.  
  324. =head1 NAME
  325.  
  326. ExtUtils::Liblist - determine libraries to use and how to use them
  327.  
  328. =head1 SYNOPSIS
  329.  
  330. C<require ExtUtils::Liblist;>
  331.  
  332. C<ExtUtils::Liblist::ext($self, $potential_libs, $verbose);>
  333.  
  334. =head1 DESCRIPTION
  335.  
  336. This utility takes a list of libraries in the form C<-llib1 -llib2
  337. -llib3> and prints out lines suitable for inclusion in an extension
  338. Makefile.  Extra library paths may be included with the form
  339. C<-L/another/path> this will affect the searches for all subsequent
  340. libraries.
  341.  
  342. It returns an array of four scalar values: EXTRALIBS, BSLOADLIBS,
  343. LDLOADLIBS, and LD_RUN_PATH.  Some of these don't mean anything
  344. on VMS and Win32.  See the details about those platform specifics
  345. below.
  346.  
  347. Dependent libraries can be linked in one of three ways:
  348.  
  349. =over 2
  350.  
  351. =item * For static extensions
  352.  
  353. by the ld command when the perl binary is linked with the extension
  354. library. See EXTRALIBS below.
  355.  
  356. =item * For dynamic extensions
  357.  
  358. by the ld command when the shared object is built/linked. See
  359. LDLOADLIBS below.
  360.  
  361. =item * For dynamic extensions
  362.  
  363. by the DynaLoader when the shared object is loaded. See BSLOADLIBS
  364. below.
  365.  
  366. =back
  367.  
  368. =head2 EXTRALIBS
  369.  
  370. List of libraries that need to be linked with when linking a perl
  371. binary which includes this extension Only those libraries that
  372. actually exist are included.  These are written to a file and used
  373. when linking perl.
  374.  
  375. =head2 LDLOADLIBS and LD_RUN_PATH
  376.  
  377. List of those libraries which can or must be linked into the shared
  378. library when created using ld. These may be static or dynamic
  379. libraries.  LD_RUN_PATH is a colon separated list of the directories
  380. in LDLOADLIBS. It is passed as an environment variable to the process
  381. that links the shared library.
  382.  
  383. =head2 BSLOADLIBS
  384.  
  385. List of those libraries that are needed but can be linked in
  386. dynamically at run time on this platform.  SunOS/Solaris does not need
  387. this because ld records the information (from LDLOADLIBS) into the
  388. object file.  This list is used to create a .bs (bootstrap) file.
  389.  
  390. =head1 PORTABILITY
  391.  
  392. This module deals with a lot of system dependencies and has quite a
  393. few architecture specific B<if>s in the code.
  394.  
  395. =head2 VMS implementation
  396.  
  397. The version of ext() which is executed under VMS differs from the
  398. Unix-OS/2 version in several respects:
  399.  
  400. =over 2
  401.  
  402. =item *
  403.  
  404. Input library and path specifications are accepted with or without the
  405. C<-l> and C<-L> prefices used by Unix linkers.  If neither prefix is
  406. present, a token is considered a directory to search if it is in fact
  407. a directory, and a library to search for otherwise.  Authors who wish
  408. their extensions to be portable to Unix or OS/2 should use the Unix
  409. prefixes, since the Unix-OS/2 version of ext() requires them.
  410.  
  411. =item *
  412.  
  413. Wherever possible, shareable images are preferred to object libraries,
  414. and object libraries to plain object files.  In accordance with VMS
  415. naming conventions, ext() looks for files named I<lib>shr and I<lib>rtl;
  416. it also looks for I<lib>lib and libI<lib> to accomodate Unix conventions
  417. used in some ported software.
  418.  
  419. =item *
  420.  
  421. For each library that is found, an appropriate directive for a linker options
  422. file is generated.  The return values are space-separated strings of
  423. these directives, rather than elements used on the linker command line.
  424.  
  425. =item *
  426.  
  427. LDLOADLIBS and EXTRALIBS are always identical under VMS, and BSLOADLIBS
  428. and LD_RIN_PATH are always empty.
  429.  
  430. =back
  431.  
  432. In addition, an attempt is made to recognize several common Unix library
  433. names, and filter them out or convert them to their VMS equivalents, as
  434. appropriate.
  435.  
  436. In general, the VMS version of ext() should properly handle input from
  437. extensions originally designed for a Unix or VMS environment.  If you
  438. encounter problems, or discover cases where the search could be improved,
  439. please let us know.
  440.  
  441. =head2 Win32 implementation
  442.  
  443. The version of ext() which is executed under Win32 differs from the
  444. Unix-OS/2 version in several respects:
  445.  
  446. =over 2
  447.  
  448. =item *
  449.  
  450. Input library and path specifications are accepted with or without the
  451. C<-l> and C<-L> prefices used by Unix linkers.  C<-lfoo> specifies the
  452. library C<foo.lib> and C<-Ls:ome\dir> specifies a directory to look for
  453. the libraries that follow.  If neither prefix is present, a token is
  454. considered a directory to search if it is in fact a directory, and a
  455. library to search for otherwise.  The C<$Config{lib_ext}> suffix will
  456. be appended to any entries that are not directories and don't already
  457. have the suffix.  Authors who wish their extensions to be portable to
  458. Unix or OS/2 should use the Unix prefixes, since the Unix-OS/2 version
  459. of ext() requires them.
  460.  
  461. =item *
  462.  
  463. Entries cannot be plain object files, as many Win32 compilers will
  464. not handle object files in the place of libraries.
  465.  
  466. =item *
  467.  
  468. If C<$potential_libs> is empty, the return value will be empty.
  469. Otherwise, the libraries specified by C<$Config{libs}> (see Config.pm)
  470. will be appended to the list of C<$potential_libs>.  The libraries
  471. will be searched for in the directories specified in C<$potential_libs>
  472. as well as in C<$Config{libpth}>. For each library that is found,  a
  473. space-separated list of fully qualified library pathnames is generated.
  474. You may specify an entry that matches C</:nodefault/i> in
  475. C<$potential_libs> to disable the appending of default libraries
  476. found in C<$Config{libs}> (this should be only needed very rarely).
  477.  
  478. =item *
  479.  
  480. The libraries specified may be a mixture of static libraries and
  481. import libraries (to link with DLLs).  Since both kinds are used
  482. pretty transparently on the win32 platform, we do not attempt to
  483. distinguish between them.
  484.  
  485. =item *
  486.  
  487. LDLOADLIBS and EXTRALIBS are always identical under Win32, and BSLOADLIBS
  488. and LD_RUN_PATH are always empty (this may change in future).
  489.  
  490. =back
  491.  
  492.  
  493. =head1 SEE ALSO
  494.  
  495. L<ExtUtils::MakeMaker>
  496.  
  497. =cut
  498.  
  499.