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