Initial devel changes.
[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 $ExtUtils::Liblist::VERSION = substr q$Revision: 1.19 $, 10;
6
7 use Config;
8 use Cwd 'cwd';
9 use File::Basename;
10
11 my $Config_libext = $Config{lib_ext} || ".a";
12
13 sub ext {
14     my($self,$potential_libs, $Verbose) = @_;
15     if ($^O =~ 'os2' and $Config{libs}) { 
16         # Dynamic libraries are not transitive, so we may need including
17         # the libraries linked against perl.dll again.
18
19         $potential_libs .= " " if $potential_libs;
20         $potential_libs .= $Config{libs};
21     }
22     return ("", "", "", "") unless $potential_libs;
23     print STDOUT "Potential libraries are '$potential_libs':\n" if $Verbose;
24
25     my($so)   = $Config{'so'};
26     my($libs) = $Config{'libs'};
27
28     # compute $extralibs, $bsloadlibs and $ldloadlibs from
29     # $potential_libs
30     # this is a rewrite of Andy Dougherty's extliblist in perl
31     # its home is in <distribution>/ext/util
32
33     my(@searchpath); # from "-L/path" entries in $potential_libs
34     my(@libpath) = split " ", $Config{'libpth'};
35     my(@ldloadlibs, @bsloadlibs, @extralibs, @ld_run_path, %ld_run_path_seen);
36     my($fullname, $thislib, $thispth, @fullname);
37     my($pwd) = cwd(); # from Cwd.pm
38     my($found) = 0;
39
40     foreach $thislib (split ' ', $potential_libs){
41
42         # Handle possible linker path arguments.
43         if ($thislib =~ s/^(-[LR])//){  # save path flag type
44             my($ptype) = $1;
45             unless (-d $thislib){
46                 print STDOUT "$ptype$thislib ignored, directory does not exist\n"
47                         if $Verbose;
48                 next;
49             }
50             unless ($self->file_name_is_absolute($thislib)) {
51               print STDOUT "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
52               $thislib = $self->catdir($pwd,$thislib);
53             }
54             push(@searchpath, $thislib);
55             push(@extralibs,  "$ptype$thislib");
56             push(@ldloadlibs, "$ptype$thislib");
57             next;
58         }
59
60         # Handle possible library arguments.
61         unless ($thislib =~ s/^-l//){
62           print STDOUT "Unrecognized argument in LIBS ignored: '$thislib'\n";
63           next;
64         }
65
66         my($found_lib)=0;
67         foreach $thispth (@searchpath, @libpath){
68
69                 # Try to find the full name of the library.  We need this to
70                 # determine whether it's a dynamically-loadable library or not.
71                 # This tends to be subject to various os-specific quirks.
72                 # For gcc-2.6.2 on linux (March 1995), DLD can not load
73                 # .sa libraries, with the exception of libm.sa, so we
74                 # deliberately skip them.
75             if (@fullname = $self->lsdir($thispth,"^lib$thislib\.$so\.[0-9]+")){
76                 # Take care that libfoo.so.10 wins against libfoo.so.9.
77                 # Compare two libraries to find the most recent version
78                 # number.  E.g.  if you have libfoo.so.9.0.7 and
79                 # libfoo.so.10.1, first convert all digits into two
80                 # decimal places.  Then we'll add ".00" to the shorter
81                 # strings so that we're comparing strings of equal length
82                 # Thus we'll compare libfoo.so.09.07.00 with
83                 # libfoo.so.10.01.00.  Some libraries might have letters
84                 # in the version.  We don't know what they mean, but will
85                 # try to skip them gracefully -- we'll set any letter to
86                 # '0'.  Finally, sort in reverse so we can take the
87                 # first element.
88
89                 #TODO: iterate through the directory instead of sorting
90
91                 $fullname = "$thispth/" .
92                 (sort { my($ma) = $a;
93                         my($mb) = $b;
94                         $ma =~ tr/A-Za-z/0/s;
95                         $ma =~ s/\b(\d)\b/0$1/g;
96                         $mb =~ tr/A-Za-z/0/s;
97                         $mb =~ s/\b(\d)\b/0$1/g;
98                         while (length($ma) < length($mb)) { $ma .= ".00"; }
99                         while (length($mb) < length($ma)) { $mb .= ".00"; }
100                         # Comparison deliberately backwards
101                         $mb cmp $ma;} @fullname)[0];
102             } elsif (-f ($fullname="$thispth/lib$thislib.$so")
103                  && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
104             } elsif (-f ($fullname="$thispth/lib${thislib}_s$Config_libext")
105                  && ($thislib .= "_s") ){ # we must explicitly use _s version
106             } elsif (-f ($fullname="$thispth/lib$thislib$Config_libext")){
107             } elsif (-f ($fullname="$thispth/$thislib$Config_libext")){
108             } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
109             } elsif ($^O eq 'dgux'
110                  && -l ($fullname="$thispth/lib$thislib$Config_libext")
111                  && readlink($fullname) =~ /^elink:/) {
112                  # Some of DG's libraries look like misconnected symbolic
113                  # links, but development tools can follow them.  (They
114                  # look like this:
115                  #
116                  #    libm.a -> elink:${SDE_PATH:-/usr}/sde/\
117                  #    ${TARGET_BINARY_INTERFACE:-m88kdgux}/usr/lib/libm.a
118                  #
119                  # , the compilation tools expand the environment variables.)
120             } else {
121                 print STDOUT "$thislib not found in $thispth\n" if $Verbose;
122                 next;
123             }
124             print STDOUT "'-l$thislib' found at $fullname\n" if $Verbose;
125             my($fullnamedir) = dirname($fullname);
126             push @ld_run_path, $fullnamedir unless $ld_run_path_seen{$fullnamedir}++;
127             $found++;
128             $found_lib++;
129
130             # Now update library lists
131
132             # what do we know about this library...
133             my $is_dyna = ($fullname !~ /\Q$Config_libext\E$/);
134             my $in_perl = ($libs =~ /\B-l\Q$ {thislib}\E\b/s);
135
136             # Do not add it into the list if it is already linked in
137             # with the main perl executable.
138             # We have to special-case the NeXT, because all the math 
139             # is also in libsys_s
140             unless ($in_perl || 
141                     ($^O eq 'next' && $thislib eq 'm') ){
142                 push(@extralibs, "-l$thislib");
143             }
144
145             # We might be able to load this archive file dynamically
146             if ( $Config{'dlsrc'} =~ /dl_next|dl_dld/){
147                 # We push -l$thislib instead of $fullname because
148                 # it avoids hardwiring a fixed path into the .bs file.
149                 # Mkbootstrap will automatically add dl_findfile() to
150                 # the .bs file if it sees a name in the -l format.
151                 # USE THIS, when dl_findfile() is fixed: 
152                 # push(@bsloadlibs, "-l$thislib");
153                 # OLD USE WAS while checking results against old_extliblist
154                 push(@bsloadlibs, "$fullname");
155             } else {
156                 if ($is_dyna){
157                     # For SunOS4, do not add in this shared library if
158                     # it is already linked in the main perl executable
159                     push(@ldloadlibs, "-l$thislib")
160                         unless ($in_perl and $^O eq 'sunos');
161                 } else {
162                     push(@ldloadlibs, "-l$thislib");
163                 }
164             }
165             last;       # found one here so don't bother looking further
166         }
167         print STDOUT "Warning (will try anyway): No library found for -l$thislib\n"
168             unless $found_lib>0;
169     }
170     return ('','','','') unless $found;
171     ("@extralibs", "@bsloadlibs", "@ldloadlibs",join(":",@ld_run_path));
172 }
173
174 1;
175
176 __END__
177
178 =head1 NAME
179
180 ExtUtils::Liblist - determine libraries to use and how to use them
181
182 =head1 SYNOPSIS
183
184 C<require ExtUtils::Liblist;>
185
186 C<ExtUtils::Liblist::ext($potential_libs, $Verbose);>
187
188 =head1 DESCRIPTION
189
190 This utility takes a list of libraries in the form C<-llib1 -llib2
191 -llib3> and prints out lines suitable for inclusion in an extension
192 Makefile.  Extra library paths may be included with the form
193 C<-L/another/path> this will affect the searches for all subsequent
194 libraries.
195
196 It returns an array of four scalar values: EXTRALIBS, BSLOADLIBS,
197 LDLOADLIBS, and LD_RUN_PATH.
198
199 Dependent libraries can be linked in one of three ways:
200
201 =over 2
202
203 =item * For static extensions
204
205 by the ld command when the perl binary is linked with the extension
206 library. See EXTRALIBS below.
207
208 =item * For dynamic extensions
209
210 by the ld command when the shared object is built/linked. See
211 LDLOADLIBS below.
212
213 =item * For dynamic extensions
214
215 by the DynaLoader when the shared object is loaded. See BSLOADLIBS
216 below.
217
218 =back
219
220 =head2 EXTRALIBS
221
222 List of libraries that need to be linked with when linking a perl
223 binary which includes this extension Only those libraries that
224 actually exist are included.  These are written to a file and used
225 when linking perl.
226
227 =head2 LDLOADLIBS and LD_RUN_PATH
228
229 List of those libraries which can or must be linked into the shared
230 library when created using ld. These may be static or dynamic
231 libraries.  LD_RUN_PATH is a colon separated list of the directories
232 in LDLOADLIBS. It is passed as an environment variable to the process
233 that links the shared library.
234
235 =head2 BSLOADLIBS
236
237 List of those libraries that are needed but can be linked in
238 dynamically at run time on this platform.  SunOS/Solaris does not need
239 this because ld records the information (from LDLOADLIBS) into the
240 object file.  This list is used to create a .bs (bootstrap) file.
241
242 =head1 PORTABILITY
243
244 This module deals with a lot of system dependencies and has quite a
245 few architecture specific B<if>s in the code.
246
247 =head1 SEE ALSO
248
249 L<ExtUtils::MakeMaker>
250
251 =cut
252
253
254