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