NeXT-specific support
[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.20 $, 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 math and ndbm 
139             # are both in libsys_s
140             unless ($in_perl || 
141                 ($Config{'osname'} eq 'next' &&
142                     ($thislib eq 'm' || $thislib eq 'ndbm')) ){
143                 push(@extralibs, "-l$thislib");
144             }
145
146             # We might be able to load this archive file dynamically
147             if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
148             ||   ($Config{'dlsrc'} =~ /dl_dld/) )
149             {
150                 # We push -l$thislib instead of $fullname because
151                 # it avoids hardwiring a fixed path into the .bs file.
152                 # Mkbootstrap will automatically add dl_findfile() to
153                 # the .bs file if it sees a name in the -l format.
154                 # USE THIS, when dl_findfile() is fixed: 
155                 # push(@bsloadlibs, "-l$thislib");
156                 # OLD USE WAS while checking results against old_extliblist
157                 push(@bsloadlibs, "$fullname");
158             } else {
159                 if ($is_dyna){
160                     # For SunOS4, do not add in this shared library if
161                     # it is already linked in the main perl executable
162                     push(@ldloadlibs, "-l$thislib")
163                         unless ($in_perl and $^O eq 'sunos');
164                 } else {
165                     push(@ldloadlibs, "-l$thislib");
166                 }
167             }
168             last;       # found one here so don't bother looking further
169         }
170         print STDOUT "Warning (will try anyway): No library found for -l$thislib\n"
171             unless $found_lib>0;
172     }
173     return ('','','','') unless $found;
174     ("@extralibs", "@bsloadlibs", "@ldloadlibs",join(":",@ld_run_path));
175 }
176
177 1;
178
179 __END__
180
181 =head1 NAME
182
183 ExtUtils::Liblist - determine libraries to use and how to use them
184
185 =head1 SYNOPSIS
186
187 C<require ExtUtils::Liblist;>
188
189 C<ExtUtils::Liblist::ext($potential_libs, $Verbose);>
190
191 =head1 DESCRIPTION
192
193 This utility takes a list of libraries in the form C<-llib1 -llib2
194 -llib3> and prints out lines suitable for inclusion in an extension
195 Makefile.  Extra library paths may be included with the form
196 C<-L/another/path> this will affect the searches for all subsequent
197 libraries.
198
199 It returns an array of four scalar values: EXTRALIBS, BSLOADLIBS,
200 LDLOADLIBS, and LD_RUN_PATH.
201
202 Dependent libraries can be linked in one of three ways:
203
204 =over 2
205
206 =item * For static extensions
207
208 by the ld command when the perl binary is linked with the extension
209 library. See EXTRALIBS below.
210
211 =item * For dynamic extensions
212
213 by the ld command when the shared object is built/linked. See
214 LDLOADLIBS below.
215
216 =item * For dynamic extensions
217
218 by the DynaLoader when the shared object is loaded. See BSLOADLIBS
219 below.
220
221 =back
222
223 =head2 EXTRALIBS
224
225 List of libraries that need to be linked with when linking a perl
226 binary which includes this extension Only those libraries that
227 actually exist are included.  These are written to a file and used
228 when linking perl.
229
230 =head2 LDLOADLIBS and LD_RUN_PATH
231
232 List of those libraries which can or must be linked into the shared
233 library when created using ld. These may be static or dynamic
234 libraries.  LD_RUN_PATH is a colon separated list of the directories
235 in LDLOADLIBS. It is passed as an environment variable to the process
236 that links the shared library.
237
238 =head2 BSLOADLIBS
239
240 List of those libraries that are needed but can be linked in
241 dynamically at run time on this platform.  SunOS/Solaris does not need
242 this because ld records the information (from LDLOADLIBS) into the
243 object file.  This list is used to create a .bs (bootstrap) file.
244
245 =head1 PORTABILITY
246
247 This module deals with a lot of system dependencies and has quite a
248 few architecture specific B<if>s in the code.
249
250 =head1 SEE ALSO
251
252 L<ExtUtils::MakeMaker>
253
254 =cut
255
256
257