This is my patch patch.1n for perl5.001.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Liblist.pm
1 package ExtUtils::Liblist;
2
3 # Broken out of MakeMaker from version 4.11
4
5 use Config;
6 use Cwd;
7 # --- Determine libraries to use and how to use them ---
8
9 sub ext {
10     my($potential_libs, $Verbose) = @_;
11     return ("", "", "") unless $potential_libs;
12     print STDOUT "Potential libraries are '$potential_libs':\n" if $Verbose;
13
14     my($so)   = $Config{'so'};
15     my($libs) = $Config{'libs'};
16
17     # compute $extralibs, $bsloadlibs and $ldloadlibs from
18     # $potential_libs
19     # this is a rewrite of Andy Dougherty's extliblist in perl
20     # its home is in <distribution>/ext/util
21
22     my(@searchpath); # from "-L/path" entries in $potential_libs
23     my(@libpath) = split " ", $Config{'libpth'};
24     my(@ldloadlibs, @bsloadlibs, @extralibs);
25     my($fullname, $thislib, $thispth, @fullname);
26     my($pwd) = fastcwd(); # from Cwd.pm
27     my($found) = 0;
28
29     foreach $thislib (split ' ', $potential_libs){
30
31         # Handle possible linker path arguments.
32         if ($thislib =~ s/^(-[LR])//){  # save path flag type
33             my($ptype) = $1;
34             unless (-d $thislib){
35                 print STDOUT "$ptype$thislib ignored, directory does not exist\n"
36                         if $Verbose;
37                 next;
38             }
39             if ($thislib !~ m|^/|) {
40               print STDOUT "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
41               $thislib = "$pwd/$thislib";
42             }
43             push(@searchpath, $thislib);
44             push(@extralibs,  "$ptype$thislib");
45             push(@ldloadlibs, "$ptype$thislib");
46             next;
47         }
48
49         # Handle possible library arguments.
50         unless ($thislib =~ s/^-l//){
51           print STDOUT "Unrecognized argument in LIBS ignored: '$thislib'\n";
52           next;
53         }
54
55         my($found_lib)=0;
56         foreach $thispth (@searchpath, @libpath){
57
58                 # Try to find the full name of the library.  We need this to
59                 # determine whether it's a dynamically-loadable library or not.
60                 # This tends to be subject to various os-specific quirks.
61                 # For gcc-2.6.2 on linux (March 1995), DLD can not load
62                 # .sa libraries, with the exception of libm.sa, so we
63                 # deliberately skip them.
64             if (@fullname = lsdir($thispth,"^lib$thislib\.$so\.[0-9]+")){
65                 # Take care that libfoo.so.10 wins against libfoo.so.9.
66                 # Compare two libraries to find the most recent version
67                 # number.  E.g.  if you have libfoo.so.9.0.7 and
68                 # libfoo.so.10.1, first convert all digits into two
69                 # decimal places.  Then we'll add ".00" to the shorter
70                 # strings so that we're comparing strings of equal length
71                 # Thus we'll compare libfoo.so.09.07.00 with
72                 # libfoo.so.10.01.00.  Some libraries might have letters
73                 # in the version.  We don't know what they mean, but will
74                 # try to skip them gracefully -- we'll set any letter to
75                 # '0'.  Finally, sort in reverse so we can take the
76                 # first element.
77
78                 #TODO: iterate through the directory instead of sorting
79
80                 $fullname = "$thispth/" .
81                 (sort { my($ma) = $a;
82                         my($mb) = $b;
83                         $ma =~ tr/A-Za-z/0/s;
84                         $ma =~ s/\b(\d)\b/0$1/g;
85                         $mb =~ tr/A-Za-z/0/s;
86                         $mb =~ s/\b(\d)\b/0$1/g;
87                         while (length($ma) < length($mb)) { $ma .= ".00"; }
88                         while (length($mb) < length($ma)) { $mb .= ".00"; }
89                         # Comparison deliberately backwards
90                         $mb cmp $ma;} @fullname)[0];
91             } elsif (-f ($fullname="$thispth/lib$thislib.$so")
92                  && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
93             } elsif (-f ($fullname="$thispth/lib${thislib}_s.a")
94                  && ($thislib .= "_s") ){ # we must explicitly use _s version
95             } elsif (-f ($fullname="$thispth/lib$thislib.a")){
96             } elsif (-f ($fullname="$thispth/Slib$thislib.a")){
97             } else {
98                 print STDOUT "$thislib not found in $thispth\n" if $Verbose;
99                 next;
100             }
101             print STDOUT "'-l$thislib' found at $fullname\n" if $Verbose;
102             $found++;
103             $found_lib++;
104
105             # Now update library lists
106
107             # what do we know about this library...
108             my $is_dyna = ($fullname !~ /\.a$/);
109             my $in_perl = ($libs =~ /\B-l${thislib}\b/s);
110
111             # Do not add it into the list if it is already linked in
112             # with the main perl executable.
113             # We have to special-case the NeXT, because all the math 
114             # is also in libsys_s
115             unless ($in_perl || 
116                     ($Config{'osname'} eq 'next' && $thislib eq 'm') ){
117                 push(@extralibs, "-l$thislib");
118             }
119
120             # We might be able to load this archive file dynamically
121             if ( $Config{'dlsrc'} =~ /dl_next|dl_dld/){
122                 # We push -l$thislib instead of $fullname because
123                 # it avoids hardwiring a fixed path into the .bs file.
124                 # Mkbootstrap will automatically add dl_findfile() to
125                 # the .bs file if it sees a name in the -l format.
126                 # USE THIS, when dl_findfile() is fixed: 
127                 # push(@bsloadlibs, "-l$thislib");
128                 # OLD USE WAS while checking results against old_extliblist
129                 push(@bsloadlibs, "$fullname");
130             } else {
131                 if ($is_dyna){
132                     # For SunOS4, do not add in this shared library if
133                     # it is already linked in the main perl executable
134                     push(@ldloadlibs, "-l$thislib")
135                         unless ($in_perl and $Config{'osname'} eq 'sunos');
136                 } else {
137                     push(@ldloadlibs, "-l$thislib");
138                 }
139             }
140             last;       # found one here so don't bother looking further
141         }
142         print STDOUT "Warning (non-fatal): No library found for -l$thislib\n"
143             unless $found_lib>0;
144     }
145     return ('','','') unless $found;
146     ("@extralibs", "@bsloadlibs", "@ldloadlibs");
147 }
148
149 sub lsdir { #yes, duplicate code seems less hassle than having an
150             #extra file with only lsdir
151     my($dir, $regex) = @_;
152     local(*DIR, @ls);
153     opendir(DIR, $dir || ".") or return ();
154     @ls = readdir(DIR);
155     closedir(DIR);
156     @ls = grep(/$regex/, @ls) if $regex;
157     @ls;
158 }
159
160 1;