Builds completely with Mingw32, dynamic loaded extensions
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Liblist.pm
CommitLineData
005c1a0e 1package ExtUtils::Liblist;
8a1da95f 2use vars qw($VERSION);
005c1a0e 3# Broken out of MakeMaker from version 4.11
4
84902520 5$VERSION = substr q$Revision: 1.25 $, 10;
f1387719 6
005c1a0e 7use Config;
8f1aa56b 8use Cwd 'cwd';
4633a7c4 9use File::Basename;
10
005c1a0e 11sub ext {
3e3baf6d 12 if ($^O eq 'VMS') { return &_vms_ext; }
13 elsif($^O eq 'MSWin32') { return &_win32_ext; }
14 else { return &_unix_os2_ext; }
55497cff 15}
16
17sub _unix_os2_ext {
e611f912 18 my($self,$potential_libs, $verbose) = @_;
f1387719 19 if ($^O =~ 'os2' and $Config{libs}) {
4e68a208 20 # Dynamic libraries are not transitive, so we may need including
21 # the libraries linked against perl.dll again.
22
23 $potential_libs .= " " if $potential_libs;
24 $potential_libs .= $Config{libs};
25 }
4633a7c4 26 return ("", "", "", "") unless $potential_libs;
fb73857a 27 warn "Potential libraries are '$potential_libs':\n" if $verbose;
005c1a0e 28
29 my($so) = $Config{'so'};
30 my($libs) = $Config{'libs'};
55497cff 31 my $Config_libext = $Config{lib_ext} || ".a";
32
005c1a0e 33
34 # compute $extralibs, $bsloadlibs and $ldloadlibs from
35 # $potential_libs
36 # this is a rewrite of Andy Dougherty's extliblist in perl
005c1a0e 37
38 my(@searchpath); # from "-L/path" entries in $potential_libs
39 my(@libpath) = split " ", $Config{'libpth'};
4633a7c4 40 my(@ldloadlibs, @bsloadlibs, @extralibs, @ld_run_path, %ld_run_path_seen);
005c1a0e 41 my($fullname, $thislib, $thispth, @fullname);
8f1aa56b 42 my($pwd) = cwd(); # from Cwd.pm
005c1a0e 43 my($found) = 0;
44
45 foreach $thislib (split ' ', $potential_libs){
46
47 # Handle possible linker path arguments.
48 if ($thislib =~ s/^(-[LR])//){ # save path flag type
49 my($ptype) = $1;
50 unless (-d $thislib){
fb73857a 51 warn "$ptype$thislib ignored, directory does not exist\n"
e611f912 52 if $verbose;
005c1a0e 53 next;
54 }
f1387719 55 unless ($self->file_name_is_absolute($thislib)) {
fb73857a 56 warn "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
f1387719 57 $thislib = $self->catdir($pwd,$thislib);
005c1a0e 58 }
59 push(@searchpath, $thislib);
60 push(@extralibs, "$ptype$thislib");
61 push(@ldloadlibs, "$ptype$thislib");
62 next;
63 }
64
65 # Handle possible library arguments.
66 unless ($thislib =~ s/^-l//){
fb73857a 67 warn "Unrecognized argument in LIBS ignored: '$thislib'\n";
005c1a0e 68 next;
69 }
70
71 my($found_lib)=0;
72 foreach $thispth (@searchpath, @libpath){
73
74 # Try to find the full name of the library. We need this to
75 # determine whether it's a dynamically-loadable library or not.
76 # This tends to be subject to various os-specific quirks.
77 # For gcc-2.6.2 on linux (March 1995), DLD can not load
78 # .sa libraries, with the exception of libm.sa, so we
79 # deliberately skip them.
4e1fac24 80 if (@fullname =
81 $self->lsdir($thispth,"^\Qlib$thislib.$so.\E[0-9]+")){
005c1a0e 82 # Take care that libfoo.so.10 wins against libfoo.so.9.
83 # Compare two libraries to find the most recent version
84 # number. E.g. if you have libfoo.so.9.0.7 and
85 # libfoo.so.10.1, first convert all digits into two
86 # decimal places. Then we'll add ".00" to the shorter
87 # strings so that we're comparing strings of equal length
88 # Thus we'll compare libfoo.so.09.07.00 with
89 # libfoo.so.10.01.00. Some libraries might have letters
90 # in the version. We don't know what they mean, but will
91 # try to skip them gracefully -- we'll set any letter to
92 # '0'. Finally, sort in reverse so we can take the
93 # first element.
94
95 #TODO: iterate through the directory instead of sorting
96
97 $fullname = "$thispth/" .
98 (sort { my($ma) = $a;
99 my($mb) = $b;
100 $ma =~ tr/A-Za-z/0/s;
101 $ma =~ s/\b(\d)\b/0$1/g;
102 $mb =~ tr/A-Za-z/0/s;
103 $mb =~ s/\b(\d)\b/0$1/g;
104 while (length($ma) < length($mb)) { $ma .= ".00"; }
105 while (length($mb) < length($ma)) { $mb .= ".00"; }
106 # Comparison deliberately backwards
107 $mb cmp $ma;} @fullname)[0];
108 } elsif (-f ($fullname="$thispth/lib$thislib.$so")
109 && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
4633a7c4 110 } elsif (-f ($fullname="$thispth/lib${thislib}_s$Config_libext")
005c1a0e 111 && ($thislib .= "_s") ){ # we must explicitly use _s version
4633a7c4 112 } elsif (-f ($fullname="$thispth/lib$thislib$Config_libext")){
864a5fa8 113 } elsif (-f ($fullname="$thispth/$thislib$Config_libext")){
4633a7c4 114 } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
8f1aa56b 115 } elsif ($^O eq 'dgux'
c07a80fd 116 && -l ($fullname="$thispth/lib$thislib$Config_libext")
117 && readlink($fullname) =~ /^elink:/) {
118 # Some of DG's libraries look like misconnected symbolic
119 # links, but development tools can follow them. (They
120 # look like this:
121 #
122 # libm.a -> elink:${SDE_PATH:-/usr}/sde/\
123 # ${TARGET_BINARY_INTERFACE:-m88kdgux}/usr/lib/libm.a
124 #
125 # , the compilation tools expand the environment variables.)
005c1a0e 126 } else {
fb73857a 127 warn "$thislib not found in $thispth\n" if $verbose;
005c1a0e 128 next;
129 }
fb73857a 130 warn "'-l$thislib' found at $fullname\n" if $verbose;
4633a7c4 131 my($fullnamedir) = dirname($fullname);
132 push @ld_run_path, $fullnamedir unless $ld_run_path_seen{$fullnamedir}++;
005c1a0e 133 $found++;
134 $found_lib++;
135
136 # Now update library lists
137
138 # what do we know about this library...
4633a7c4 139 my $is_dyna = ($fullname !~ /\Q$Config_libext\E$/);
4e68a208 140 my $in_perl = ($libs =~ /\B-l\Q$ {thislib}\E\b/s);
005c1a0e 141
142 # Do not add it into the list if it is already linked in
143 # with the main perl executable.
974f612f 144 # We have to special-case the NeXT, because math and ndbm
145 # are both in libsys_s
005c1a0e 146 unless ($in_perl ||
974f612f 147 ($Config{'osname'} eq 'next' &&
148 ($thislib eq 'm' || $thislib eq 'ndbm')) ){
005c1a0e 149 push(@extralibs, "-l$thislib");
150 }
151
152 # We might be able to load this archive file dynamically
974f612f 153 if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
154 || ($Config{'dlsrc'} =~ /dl_dld/) )
155 {
005c1a0e 156 # We push -l$thislib instead of $fullname because
157 # it avoids hardwiring a fixed path into the .bs file.
158 # Mkbootstrap will automatically add dl_findfile() to
159 # the .bs file if it sees a name in the -l format.
160 # USE THIS, when dl_findfile() is fixed:
161 # push(@bsloadlibs, "-l$thislib");
162 # OLD USE WAS while checking results against old_extliblist
163 push(@bsloadlibs, "$fullname");
164 } else {
165 if ($is_dyna){
166 # For SunOS4, do not add in this shared library if
167 # it is already linked in the main perl executable
168 push(@ldloadlibs, "-l$thislib")
8f1aa56b 169 unless ($in_perl and $^O eq 'sunos');
005c1a0e 170 } else {
171 push(@ldloadlibs, "-l$thislib");
172 }
173 }
174 last; # found one here so don't bother looking further
175 }
fb73857a 176 warn "Note (probably harmless): "
f3d9a6ba 177 ."No library found for -l$thislib\n"
005c1a0e 178 unless $found_lib>0;
179 }
4633a7c4 180 return ('','','','') unless $found;
181 ("@extralibs", "@bsloadlibs", "@ldloadlibs",join(":",@ld_run_path));
005c1a0e 182}
183
3e3baf6d 184sub _win32_ext {
e611f912 185 my($self, $potential_libs, $verbose) = @_;
3e3baf6d 186
187 # If user did not supply a list, we punt.
188 # (caller should probably use the list in $Config{libs})
189 return ("", "", "", "") unless $potential_libs;
190
191 my($so) = $Config{'so'};
192 my($libs) = $Config{'libs'};
193 my($libpth) = $Config{'libpth'};
194 my($libext) = $Config{'lib_ext'} || ".lib";
195
196 if ($libs and $potential_libs !~ /:nodefault/i) {
197 # If Config.pm defines a set of default libs, we always
198 # tack them on to the user-supplied list, unless the user
199 # specified :nodefault
200
201 $potential_libs .= " " if $potential_libs;
202 $potential_libs .= $libs;
203 }
fb73857a 204 warn "Potential libraries are '$potential_libs':\n" if $verbose;
3e3baf6d 205
206 # compute $extralibs from $potential_libs
207
208 my(@searchpath); # from "-L/path" entries in $potential_libs
209 my(@libpath) = split " ", $libpth;
210 my(@extralibs);
211 my($fullname, $thislib, $thispth);
212 my($pwd) = cwd(); # from Cwd.pm
213 my($lib) = '';
214 my($found) = 0;
215
216 foreach $thislib (split ' ', $potential_libs){
217
218 # Handle possible linker path arguments.
219 if ($thislib =~ s/^-L// and not -d $thislib) {
fb73857a 220 warn "-L$thislib ignored, directory does not exist\n"
e611f912 221 if $verbose;
3e3baf6d 222 next;
223 }
224 elsif (-d $thislib) {
225 unless ($self->file_name_is_absolute($thislib)) {
fb73857a 226 warn "Warning: -L$thislib changed to -L$pwd/$thislib\n";
3e3baf6d 227 $thislib = $self->catdir($pwd,$thislib);
228 }
229 push(@searchpath, $thislib);
230 next;
231 }
232
233 # Handle possible library arguments.
234 $thislib =~ s/^-l//;
235 $thislib .= $libext if $thislib !~ /\Q$libext\E$/i;
236
237 my($found_lib)=0;
238 foreach $thispth (@searchpath, @libpath){
239 unless (-f ($fullname="$thispth\\$thislib")) {
fb73857a 240 warn "$thislib not found in $thispth\n" if $verbose;
3e3baf6d 241 next;
242 }
fb73857a 243 warn "'$thislib' found at $fullname\n" if $verbose;
3e3baf6d 244 $found++;
245 $found_lib++;
246 push(@extralibs, $fullname);
247 last;
248 }
fb73857a 249 warn "Note (probably harmless): "
3e3baf6d 250 ."No library found for '$thislib'\n"
251 unless $found_lib>0;
252 }
253 return ('','','','') unless $found;
254 $lib = join(' ',@extralibs);
fb73857a 255 warn "Result: $lib\n" if $verbose;
3e3baf6d 256 wantarray ? ($lib, '', $lib, '') : $lib;
257}
258
55497cff 259
260sub _vms_ext {
261 my($self, $potential_libs,$verbose) = @_;
262 return ('', '', '', '') unless $potential_libs;
263
264 my(@dirs,@libs,$dir,$lib,%sh,%olb,%obj);
265 my $cwd = cwd();
266 my($so,$lib_ext,$obj_ext) = @Config{'so','lib_ext','obj_ext'};
267 # List of common Unix library names and there VMS equivalents
268 # (VMS equivalent of '' indicates that the library is automatially
269 # searched by the linker, and should be skipped here.)
270 my %libmap = ( 'm' => '', 'f77' => '', 'F77' => '', 'V77' => '', 'c' => '',
271 'malloc' => '', 'crypt' => '', 'resolv' => '', 'c_s' => '',
272 'socket' => '', 'X11' => 'DECW$XLIBSHR',
273 'Xt' => 'DECW$XTSHR', 'Xm' => 'DECW$XMLIBSHR',
274 'Xmu' => 'DECW$XMULIBSHR');
275 if ($Config{'vms_cc_type'} ne 'decc') { $libmap{'curses'} = 'VAXCCURSE'; }
276
fb73857a 277 warn "Potential libraries are '$potential_libs'\n" if $verbose;
55497cff 278
279 # First, sort out directories and library names in the input
280 foreach $lib (split ' ',$potential_libs) {
281 push(@dirs,$1), next if $lib =~ /^-L(.*)/;
282 push(@dirs,$lib), next if $lib =~ /[:>\]]$/;
283 push(@dirs,$lib), next if -d $lib;
284 push(@libs,$1), next if $lib =~ /^-l(.*)/;
285 push(@libs,$lib);
286 }
287 push(@dirs,split(' ',$Config{'libpth'}));
288
289 # Now make sure we've got VMS-syntax absolute directory specs
290 # (We don't, however, check whether someone's hidden a relative
291 # path in a logical name.)
292 foreach $dir (@dirs) {
293 unless (-d $dir) {
fb73857a 294 warn "Skipping nonexistent Directory $dir\n" if $verbose > 1;
55497cff 295 $dir = '';
296 next;
297 }
fb73857a 298 warn "Resolving directory $dir\n" if $verbose;
55497cff 299 if ($self->file_name_is_absolute($dir)) { $dir = $self->fixpath($dir,1); }
300 else { $dir = $self->catdir($cwd,$dir); }
301 }
302 @dirs = grep { length($_) } @dirs;
303 unshift(@dirs,''); # Check each $lib without additions first
304
305 LIB: foreach $lib (@libs) {
306 if (exists $libmap{$lib}) {
307 next unless length $libmap{$lib};
308 $lib = $libmap{$lib};
309 }
310
311 my(@variants,$variant,$name,$test,$cand);
312 my($ctype) = '';
313
314 # If we don't have a file type, consider it a possibly abbreviated name and
315 # check for common variants. We try these first to grab libraries before
316 # a like-named executable image (e.g. -lperl resolves to perlshr.exe
317 # before perl.exe).
318 if ($lib !~ /\.[^:>\]]*$/) {
319 push(@variants,"${lib}shr","${lib}rtl","${lib}lib");
320 push(@variants,"lib$lib") if $lib !~ /[:>\]]/;
321 }
322 push(@variants,$lib);
fb73857a 323 warn "Looking for $lib\n" if $verbose;
55497cff 324 foreach $variant (@variants) {
325 foreach $dir (@dirs) {
326 my($type);
327
328 $name = "$dir$variant";
fb73857a 329 warn "\tChecking $name\n" if $verbose > 2;
55497cff 330 if (-f ($test = VMS::Filespec::rmsexpand($name))) {
331 # It's got its own suffix, so we'll have to figure out the type
332 if ($test =~ /(?:$so|exe)$/i) { $type = 'sh'; }
333 elsif ($test =~ /(?:$lib_ext|olb)$/i) { $type = 'olb'; }
334 elsif ($test =~ /(?:$obj_ext|obj)$/i) {
fb73857a 335 warn "Note (probably harmless): "
f3d9a6ba 336 ."Plain object file $test found in library list\n";
55497cff 337 $type = 'obj';
338 }
339 else {
fb73857a 340 warn "Note (probably harmless): "
f3d9a6ba 341 ."Unknown library type for $test; assuming shared\n";
55497cff 342 $type = 'sh';
343 }
344 }
345 elsif (-f ($test = VMS::Filespec::rmsexpand($name,$so)) or
346 -f ($test = VMS::Filespec::rmsexpand($name,'.exe'))) {
347 $type = 'sh';
348 $name = $test unless $test =~ /exe;?\d*$/i;
349 }
350 elsif (not length($ctype) and # If we've got a lib already, don't bother
351 ( -f ($test = VMS::Filespec::rmsexpand($name,$lib_ext)) or
352 -f ($test = VMS::Filespec::rmsexpand($name,'.olb')))) {
353 $type = 'olb';
354 $name = $test unless $test =~ /olb;?\d*$/i;
355 }
356 elsif (not length($ctype) and # If we've got a lib already, don't bother
357 ( -f ($test = VMS::Filespec::rmsexpand($name,$obj_ext)) or
358 -f ($test = VMS::Filespec::rmsexpand($name,'.obj')))) {
fb73857a 359 warn "Note (probably harmless): "
f3d9a6ba 360 ."Plain object file $test found in library list\n";
55497cff 361 $type = 'obj';
362 $name = $test unless $test =~ /obj;?\d*$/i;
363 }
364 if (defined $type) {
365 $ctype = $type; $cand = $name;
366 last if $ctype eq 'sh';
367 }
368 }
369 if ($ctype) {
370 eval '$' . $ctype . "{'$cand'}++";
371 die "Error recording library: $@" if $@;
fb73857a 372 warn "\tFound as $cand (really $test), type $ctype\n" if $verbose > 1;
55497cff 373 next LIB;
374 }
375 }
fb73857a 376 warn "Note (probably harmless): "
f3d9a6ba 377 ."No library found for $lib\n";
55497cff 378 }
379
380 @libs = sort keys %obj;
381 # This has to precede any other CRTLs, so just make it first
382 if ($olb{VAXCCURSE}) {
383 push(@libs,"$olb{VAXCCURSE}/Library");
384 delete $olb{VAXCCURSE};
385 }
386 push(@libs, map { "$_/Library" } sort keys %olb);
387 push(@libs, map { "$_/Share" } sort keys %sh);
388 $lib = join(' ',@libs);
fb73857a 389 warn "Result: $lib\n" if $verbose;
55497cff 390 wantarray ? ($lib, '', $lib, '') : $lib;
391}
392
005c1a0e 3931;
c2e89b3d 394
864a5fa8 395__END__
cb1a09d0 396
864a5fa8 397=head1 NAME
398
399ExtUtils::Liblist - determine libraries to use and how to use them
400
401=head1 SYNOPSIS
402
403C<require ExtUtils::Liblist;>
404
e611f912 405C<ExtUtils::Liblist::ext($self, $potential_libs, $verbose);>
864a5fa8 406
407=head1 DESCRIPTION
408
409This utility takes a list of libraries in the form C<-llib1 -llib2
410-llib3> and prints out lines suitable for inclusion in an extension
411Makefile. Extra library paths may be included with the form
412C<-L/another/path> this will affect the searches for all subsequent
413libraries.
414
415It returns an array of four scalar values: EXTRALIBS, BSLOADLIBS,
3e3baf6d 416LDLOADLIBS, and LD_RUN_PATH. Some of these don't mean anything
417on VMS and Win32. See the details about those platform specifics
418below.
864a5fa8 419
420Dependent libraries can be linked in one of three ways:
421
422=over 2
423
424=item * For static extensions
425
426by the ld command when the perl binary is linked with the extension
427library. See EXTRALIBS below.
428
429=item * For dynamic extensions
430
431by the ld command when the shared object is built/linked. See
432LDLOADLIBS below.
433
434=item * For dynamic extensions
435
436by the DynaLoader when the shared object is loaded. See BSLOADLIBS
437below.
438
439=back
440
441=head2 EXTRALIBS
442
443List of libraries that need to be linked with when linking a perl
444binary which includes this extension Only those libraries that
445actually exist are included. These are written to a file and used
446when linking perl.
447
448=head2 LDLOADLIBS and LD_RUN_PATH
449
450List of those libraries which can or must be linked into the shared
451library when created using ld. These may be static or dynamic
452libraries. LD_RUN_PATH is a colon separated list of the directories
453in LDLOADLIBS. It is passed as an environment variable to the process
454that links the shared library.
455
456=head2 BSLOADLIBS
457
458List of those libraries that are needed but can be linked in
459dynamically at run time on this platform. SunOS/Solaris does not need
460this because ld records the information (from LDLOADLIBS) into the
461object file. This list is used to create a .bs (bootstrap) file.
462
463=head1 PORTABILITY
464
465This module deals with a lot of system dependencies and has quite a
466few architecture specific B<if>s in the code.
467
55497cff 468=head2 VMS implementation
469
470The version of ext() which is executed under VMS differs from the
471Unix-OS/2 version in several respects:
472
473=over 2
474
475=item *
476
477Input library and path specifications are accepted with or without the
478C<-l> and C<-L> prefices used by Unix linkers. If neither prefix is
479present, a token is considered a directory to search if it is in fact
480a directory, and a library to search for otherwise. Authors who wish
481their extensions to be portable to Unix or OS/2 should use the Unix
482prefixes, since the Unix-OS/2 version of ext() requires them.
483
484=item *
485
486Wherever possible, shareable images are preferred to object libraries,
487and object libraries to plain object files. In accordance with VMS
488naming conventions, ext() looks for files named I<lib>shr and I<lib>rtl;
489it also looks for I<lib>lib and libI<lib> to accomodate Unix conventions
490used in some ported software.
491
492=item *
493
494For each library that is found, an appropriate directive for a linker options
495file is generated. The return values are space-separated strings of
496these directives, rather than elements used on the linker command line.
497
498=item *
499
500LDLOADLIBS and EXTRALIBS are always identical under VMS, and BSLOADLIBS
501and LD_RIN_PATH are always empty.
502
503=back
504
505In addition, an attempt is made to recognize several common Unix library
506names, and filter them out or convert them to their VMS equivalents, as
507appropriate.
508
509In general, the VMS version of ext() should properly handle input from
510extensions originally designed for a Unix or VMS environment. If you
511encounter problems, or discover cases where the search could be improved,
512please let us know.
513
3e3baf6d 514=head2 Win32 implementation
515
516The version of ext() which is executed under Win32 differs from the
517Unix-OS/2 version in several respects:
518
519=over 2
520
521=item *
522
523Input library and path specifications are accepted with or without the
524C<-l> and C<-L> prefices used by Unix linkers. C<-lfoo> specifies the
525library C<foo.lib> and C<-Ls:ome\dir> specifies a directory to look for
526the libraries that follow. If neither prefix is present, a token is
527considered a directory to search if it is in fact a directory, and a
528library to search for otherwise. The C<$Config{lib_ext}> suffix will
529be appended to any entries that are not directories and don't already
530have the suffix. Authors who wish their extensions to be portable to
531Unix or OS/2 should use the Unix prefixes, since the Unix-OS/2 version
532of ext() requires them.
533
534=item *
535
536Entries cannot be plain object files, as many Win32 compilers will
537not handle object files in the place of libraries.
538
539=item *
540
541If C<$potential_libs> is empty, the return value will be empty.
542Otherwise, the libraries specified by C<$Config{libs}> (see Config.pm)
543will be appended to the list of C<$potential_libs>. The libraries
544will be searched for in the directories specified in C<$potential_libs>
545as well as in C<$Config{libpth}>. For each library that is found, a
546space-separated list of fully qualified library pathnames is generated.
547You may specify an entry that matches C</:nodefault/i> in
548C<$potential_libs> to disable the appending of default libraries
549found in C<$Config{libs}> (this should be only needed very rarely).
550
551=item *
552
553The libraries specified may be a mixture of static libraries and
554import libraries (to link with DLLs). Since both kinds are used
555pretty transparently on the win32 platform, we do not attempt to
556distinguish between them.
557
558=item *
559
560LDLOADLIBS and EXTRALIBS are always identical under Win32, and BSLOADLIBS
561and LD_RUN_PATH are always empty (this may change in future).
562
563=back
564
565
864a5fa8 566=head1 SEE ALSO
567
568L<ExtUtils::MakeMaker>
569
570=cut
571