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