d17eb874913f5782e6561945c342392ce3a85f61
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Liblist / Kid.pm
1 package ExtUtils::Liblist::Kid;
2
3 # XXX Splitting this out into its own .pm is a temporary solution.
4
5 # This kid package is to be used by MakeMaker.  It will not work if
6 # $self is not a Makemaker.
7
8 use 5.00503;
9 # Broken out of MakeMaker from version 4.11
10
11 use vars qw($VERSION);
12 $VERSION = 1.29;
13
14 use Config;
15 use Cwd 'cwd';
16 use File::Basename;
17 use File::Spec;
18
19 sub ext {
20   if   ($^O eq 'VMS')     { return &_vms_ext;      }
21   elsif($^O eq 'MSWin32') { return &_win32_ext;    }
22   else                    { return &_unix_os2_ext; }
23 }
24
25 sub _unix_os2_ext {
26     my($self,$potential_libs, $verbose, $give_libs) = @_;
27     $verbose ||= 0;
28
29     if ($^O =~ 'os2' and $Config{perllibs}) { 
30         # Dynamic libraries are not transitive, so we may need including
31         # the libraries linked against perl.dll again.
32
33         $potential_libs .= " " if $potential_libs;
34         $potential_libs .= $Config{perllibs};
35     }
36     return ("", "", "", "", ($give_libs ? [] : ())) unless $potential_libs;
37     warn "Potential libraries are '$potential_libs':\n" if $verbose;
38
39     my($so)   = $Config{so};
40     my($libs) = defined $Config{perllibs} ? $Config{perllibs} : $Config{libs};
41     my $Config_libext = $Config{lib_ext} || ".a";
42
43
44     # compute $extralibs, $bsloadlibs and $ldloadlibs from
45     # $potential_libs
46     # this is a rewrite of Andy Dougherty's extliblist in perl
47
48     my(@searchpath); # from "-L/path" entries in $potential_libs
49     my(@libpath) = split " ", $Config{'libpth'};
50     my(@ldloadlibs, @bsloadlibs, @extralibs, @ld_run_path, %ld_run_path_seen);
51     my(@libs, %libs_seen);
52     my($fullname, $thislib, $thispth, @fullname);
53     my($pwd) = cwd(); # from Cwd.pm
54     my($found) = 0;
55
56     foreach $thislib (split ' ', $potential_libs){
57
58         # Handle possible linker path arguments.
59         if ($thislib =~ s/^(-[LR]|-Wl,-R)//){   # save path flag type
60             my($ptype) = $1;
61             unless (-d $thislib){
62                 warn "$ptype$thislib ignored, directory does not exist\n"
63                         if $verbose;
64                 next;
65             }
66             my($rtype) = $ptype;
67             if (($ptype eq '-R') or ($ptype eq '-Wl,-R')) {
68                 if ($Config{'lddlflags'} =~ /-Wl,-R/) {
69                     $rtype = '-Wl,-R';
70                 } elsif ($Config{'lddlflags'} =~ /-R/) {
71                     $rtype = '-R';
72                 }
73             }
74             unless (File::Spec->file_name_is_absolute($thislib)) {
75               warn "Warning: $ptype$thislib changed to $ptype$pwd/$thislib\n";
76               $thislib = $self->catdir($pwd,$thislib);
77             }
78             push(@searchpath, $thislib);
79             push(@extralibs,  "$ptype$thislib");
80             push(@ldloadlibs, "$rtype$thislib");
81             next;
82         }
83
84         # Handle possible library arguments.
85         unless ($thislib =~ s/^-l//){
86           warn "Unrecognized argument in LIBS ignored: '$thislib'\n";
87           next;
88         }
89
90         my($found_lib)=0;
91         foreach $thispth (@searchpath, @libpath){
92
93                 # Try to find the full name of the library.  We need this to
94                 # determine whether it's a dynamically-loadable library or not.
95                 # This tends to be subject to various os-specific quirks.
96                 # For gcc-2.6.2 on linux (March 1995), DLD can not load
97                 # .sa libraries, with the exception of libm.sa, so we
98                 # deliberately skip them.
99             if (@fullname =
100                     $self->lsdir($thispth,"^\Qlib$thislib.$so.\E[0-9]+")){
101                 # Take care that libfoo.so.10 wins against libfoo.so.9.
102                 # Compare two libraries to find the most recent version
103                 # number.  E.g.  if you have libfoo.so.9.0.7 and
104                 # libfoo.so.10.1, first convert all digits into two
105                 # decimal places.  Then we'll add ".00" to the shorter
106                 # strings so that we're comparing strings of equal length
107                 # Thus we'll compare libfoo.so.09.07.00 with
108                 # libfoo.so.10.01.00.  Some libraries might have letters
109                 # in the version.  We don't know what they mean, but will
110                 # try to skip them gracefully -- we'll set any letter to
111                 # '0'.  Finally, sort in reverse so we can take the
112                 # first element.
113
114                 #TODO: iterate through the directory instead of sorting
115
116                 $fullname = "$thispth/" .
117                 (sort { my($ma) = $a;
118                         my($mb) = $b;
119                         $ma =~ tr/A-Za-z/0/s;
120                         $ma =~ s/\b(\d)\b/0$1/g;
121                         $mb =~ tr/A-Za-z/0/s;
122                         $mb =~ s/\b(\d)\b/0$1/g;
123                         while (length($ma) < length($mb)) { $ma .= ".00"; }
124                         while (length($mb) < length($ma)) { $mb .= ".00"; }
125                         # Comparison deliberately backwards
126                         $mb cmp $ma;} @fullname)[0];
127             } elsif (-f ($fullname="$thispth/lib$thislib.$so")
128                  && (($Config{'dlsrc'} ne "dl_dld.xs") || ($thislib eq "m"))){
129             } elsif (-f ($fullname="$thispth/lib${thislib}_s$Config_libext")
130                  && (! $Config{'archname'} =~ /RM\d\d\d-svr4/)
131                  && ($thislib .= "_s") ){ # we must explicitly use _s version
132             } elsif (-f ($fullname="$thispth/lib$thislib$Config_libext")){
133             } elsif (-f ($fullname="$thispth/$thislib$Config_libext")){
134             } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
135             } elsif ($^O eq 'dgux'
136                  && -l ($fullname="$thispth/lib$thislib$Config_libext")
137                  && readlink($fullname) =~ /^elink:/s) {
138                  # Some of DG's libraries look like misconnected symbolic
139                  # links, but development tools can follow them.  (They
140                  # look like this:
141                  #
142                  #    libm.a -> elink:${SDE_PATH:-/usr}/sde/\
143                  #    ${TARGET_BINARY_INTERFACE:-m88kdgux}/usr/lib/libm.a
144                  #
145                  # , the compilation tools expand the environment variables.)
146             } else {
147                 warn "$thislib not found in $thispth\n" if $verbose;
148                 next;
149             }
150             warn "'-l$thislib' found at $fullname\n" if $verbose;
151             my($fullnamedir) = dirname($fullname);
152             push @ld_run_path, $fullnamedir 
153               unless $ld_run_path_seen{$fullnamedir}++;
154             push @libs, $fullname unless $libs_seen{$fullname}++;
155             $found++;
156             $found_lib++;
157
158             # Now update library lists
159
160             # what do we know about this library...
161             my $is_dyna = ($fullname !~ /\Q$Config_libext\E\z/);
162             my $in_perl = ($libs =~ /\B-l\Q${thislib}\E\b/s);
163
164             # Do not add it into the list if it is already linked in
165             # with the main perl executable.
166             # We have to special-case the NeXT, because math and ndbm 
167             # are both in libsys_s
168             unless ($in_perl || 
169                 ($Config{'osname'} eq 'next' &&
170                     ($thislib eq 'm' || $thislib eq 'ndbm')) ){
171                 push(@extralibs, "-l$thislib");
172             }
173
174             # We might be able to load this archive file dynamically
175             if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
176             ||   ($Config{'dlsrc'} =~ /dl_dld/) )
177             {
178                 # We push -l$thislib instead of $fullname because
179                 # it avoids hardwiring a fixed path into the .bs file.
180                 # Mkbootstrap will automatically add dl_findfile() to
181                 # the .bs file if it sees a name in the -l format.
182                 # USE THIS, when dl_findfile() is fixed: 
183                 # push(@bsloadlibs, "-l$thislib");
184                 # OLD USE WAS while checking results against old_extliblist
185                 push(@bsloadlibs, "$fullname");
186             } else {
187                 if ($is_dyna){
188                     # For SunOS4, do not add in this shared library if
189                     # it is already linked in the main perl executable
190                     push(@ldloadlibs, "-l$thislib")
191                         unless ($in_perl and $^O eq 'sunos');
192                 } else {
193                     push(@ldloadlibs, "-l$thislib");
194                 }
195             }
196             last;       # found one here so don't bother looking further
197         }
198         warn "Note (probably harmless): "
199                      ."No library found for -l$thislib\n"
200             unless $found_lib>0;
201     }
202
203     unless( $found ) {
204         return ('','','','', ($give_libs ? \@libs : ()));
205     }
206     else {
207         return ("@extralibs", "@bsloadlibs", "@ldloadlibs",
208                 join(":",@ld_run_path), ($give_libs ? \@libs : ()));
209     }
210 }
211
212 sub _win32_ext {
213
214     require Text::ParseWords;
215
216     my($self, $potential_libs, $verbose, $give_libs) = @_;
217     $verbose ||= 0;
218
219     # If user did not supply a list, we punt.
220     # (caller should probably use the list in $Config{libs})
221     return ("", "", "", "", ($give_libs ? [] : ())) unless $potential_libs;
222
223     my $cc              = $Config{cc};
224     my $VC              = 1 if $cc =~ /^cl/i;
225     my $BC              = 1 if $cc =~ /^bcc/i;
226     my $GC              = 1 if $cc =~ /^gcc/i;
227     my $so              = $Config{'so'};
228     my $libs            = $Config{'perllibs'};
229     my $libpth          = $Config{'libpth'};
230     my $libext          = $Config{'lib_ext'} || ".lib";
231     my(@libs, %libs_seen);
232
233     if ($libs and $potential_libs !~ /:nodefault/i) { 
234         # If Config.pm defines a set of default libs, we always
235         # tack them on to the user-supplied list, unless the user
236         # specified :nodefault
237
238         $potential_libs .= " " if $potential_libs;
239         $potential_libs .= $libs;
240     }
241     warn "Potential libraries are '$potential_libs':\n" if $verbose;
242
243     # normalize to forward slashes
244     $libpth =~ s,\\,/,g;
245     $potential_libs =~ s,\\,/,g;
246
247     # compute $extralibs from $potential_libs
248
249     my @searchpath;                 # from "-L/path" in $potential_libs
250     my @libpath         = Text::ParseWords::quotewords('\s+', 0, $libpth);
251     my @extralibs;
252     my $pwd             = cwd();    # from Cwd.pm
253     my $lib             = '';
254     my $found           = 0;
255     my $search          = 1;
256     my($fullname, $thislib, $thispth);
257
258     # add "$Config{installarchlib}/CORE" to default search path
259     push @libpath, "$Config{installarchlib}/CORE";
260
261     if ($VC and exists $ENV{LIB} and $ENV{LIB}) {
262         push @libpath, split /;/, $ENV{LIB};
263     }
264
265     foreach (Text::ParseWords::quotewords('\s+', 0, $potential_libs)){
266
267         $thislib = $_;
268
269         # see if entry is a flag
270         if (/^:\w+$/) {
271             $search     = 0 if lc eq ':nosearch';
272             $search     = 1 if lc eq ':search';
273             warn "Ignoring unknown flag '$thislib'\n"
274                 if $verbose and !/^:(no)?(search|default)$/i;
275             next;
276         }
277
278         # if searching is disabled, do compiler-specific translations
279         unless ($search) {
280             s/^-l(.+)$/$1.lib/ unless $GC;
281             s/^-L/-libpath:/ if $VC;
282             push(@extralibs, $_);
283             $found++;
284             next;
285         }
286
287         # handle possible linker path arguments
288         if (s/^-L// and not -d) {
289             warn "$thislib ignored, directory does not exist\n"
290                 if $verbose;
291             next;
292         }
293         elsif (-d) {
294             unless (File::Spec->file_name_is_absolute($_)) {
295               warn "Warning: '$thislib' changed to '-L$pwd/$_'\n";
296               $_ = $self->catdir($pwd,$_);
297             }
298             push(@searchpath, $_);
299             next;
300         }
301
302         # handle possible library arguments
303         if (s/^-l// and $GC and !/^lib/i) {
304             $_ = "lib$_";
305         }
306         $_ .= $libext if !/\Q$libext\E$/i;
307
308         my $secondpass = 0;
309     LOOKAGAIN:
310
311         # look for the file itself
312         if (-f) {
313             warn "'$thislib' found as '$_'\n" if $verbose;
314             $found++;
315             push(@extralibs, $_);
316             next;
317         }
318
319         my $found_lib = 0;
320         foreach $thispth (@searchpath, @libpath){
321             unless (-f ($fullname="$thispth\\$_")) {
322                 warn "'$thislib' not found as '$fullname'\n" if $verbose;
323                 next;
324             }
325             warn "'$thislib' found as '$fullname'\n" if $verbose;
326             $found++;
327             $found_lib++;
328             push(@extralibs, $fullname);
329             push @libs, $fullname unless $libs_seen{$fullname}++;
330             last;
331         }
332
333         # do another pass with (or without) leading 'lib' if they used -l
334         if (!$found_lib and $thislib =~ /^-l/ and !$secondpass++) {
335             if ($GC) {
336                 goto LOOKAGAIN if s/^lib//i;
337             }
338             elsif (!/^lib/i) {
339                 $_ = "lib$_";
340                 goto LOOKAGAIN;
341             }
342         }
343
344         # give up
345         warn "Note (probably harmless): "
346                      ."No library found for $thislib\n"
347             unless $found_lib>0;
348
349     }
350
351     return ('','','','', ($give_libs ? \@libs : ())) unless $found;
352
353     # make sure paths with spaces are properly quoted
354     @extralibs = map { (/\s/ && !/^".*"$/) ? qq["$_"] : $_ } @extralibs;
355     @libs = map { (/\s/ && !/^".*"$/) ? qq["$_"] : $_ } @libs;
356     $lib = join(' ',@extralibs);
357
358     # normalize back to backward slashes (to help braindead tools)
359     # XXX this may break equally braindead GNU tools that don't understand
360     # backslashes, either.  Seems like one can't win here.  Cursed be CP/M.
361     $lib =~ s,/,\\,g;
362
363     warn "Result: $lib\n" if $verbose;
364     wantarray ? ($lib, '', $lib, '', ($give_libs ? \@libs : ())) : $lib;
365 }
366
367
368 sub _vms_ext {
369   my($self, $potential_libs,$verbose,$give_libs) = @_;
370   $verbose ||= 0;
371
372   my(@crtls,$crtlstr);
373   my($dbgqual) = $self->{OPTIMIZE} || $Config{'optimize'} ||
374                  $self->{CCFLAGS}   || $Config{'ccflags'};
375   @crtls = ( ($dbgqual =~ m-/Debug-i ? $Config{'dbgprefix'} : '')
376               . 'PerlShr/Share' );
377   push(@crtls, grep { not /\(/ } split /\s+/, $Config{'perllibs'});
378   push(@crtls, grep { not /\(/ } split /\s+/, $Config{'libc'});
379   # In general, we pass through the basic libraries from %Config unchanged.
380   # The one exception is that if we're building in the Perl source tree, and
381   # a library spec could be resolved via a logical name, we go to some trouble
382   # to insure that the copy in the local tree is used, rather than one to
383   # which a system-wide logical may point.
384   if ($self->{PERL_SRC}) {
385     my($lib,$locspec,$type);
386     foreach $lib (@crtls) { 
387       if (($locspec,$type) = $lib =~ m-^([\w$\-]+)(/\w+)?- and $locspec =~ /perl/i) {
388         if    (lc $type eq '/share')   { $locspec .= $Config{'exe_ext'}; }
389         elsif (lc $type eq '/library') { $locspec .= $Config{'lib_ext'}; }
390         else                           { $locspec .= $Config{'obj_ext'}; }
391         $locspec = $self->catfile($self->{PERL_SRC},$locspec);
392         $lib = "$locspec$type" if -e $locspec;
393       }
394     }
395   }
396   $crtlstr = @crtls ? join(' ',@crtls) : '';
397
398   unless ($potential_libs) {
399     warn "Result:\n\tEXTRALIBS: \n\tLDLOADLIBS: $crtlstr\n" if $verbose;
400     return ('', '', $crtlstr, '', ($give_libs ? [] : ()));
401   }
402
403   my(@dirs,@libs,$dir,$lib,%found,@fndlibs,$ldlib);
404   my $cwd = cwd();
405   my($so,$lib_ext,$obj_ext) = @Config{'so','lib_ext','obj_ext'};
406   # List of common Unix library names and their VMS equivalents
407   # (VMS equivalent of '' indicates that the library is automatically
408   # searched by the linker, and should be skipped here.)
409   my(@flibs, %libs_seen);
410   my %libmap = ( 'm' => '', 'f77' => '', 'F77' => '', 'V77' => '', 'c' => '',
411                  'malloc' => '', 'crypt' => '', 'resolv' => '', 'c_s' => '',
412                  'socket' => '', 'X11' => 'DECW$XLIBSHR',
413                  'Xt' => 'DECW$XTSHR', 'Xm' => 'DECW$XMLIBSHR',
414                  'Xmu' => 'DECW$XMULIBSHR');
415   if ($Config{'vms_cc_type'} ne 'decc') { $libmap{'curses'} = 'VAXCCURSE'; }
416
417   warn "Potential libraries are '$potential_libs'\n" if $verbose;
418
419   # First, sort out directories and library names in the input
420   foreach $lib (split ' ',$potential_libs) {
421     push(@dirs,$1),   next if $lib =~ /^-L(.*)/;
422     push(@dirs,$lib), next if $lib =~ /[:>\]]$/;
423     push(@dirs,$lib), next if -d $lib;
424     push(@libs,$1),   next if $lib =~ /^-l(.*)/;
425     push(@libs,$lib);
426   }
427   push(@dirs,split(' ',$Config{'libpth'}));
428
429   # Now make sure we've got VMS-syntax absolute directory specs
430   # (We don't, however, check whether someone's hidden a relative
431   # path in a logical name.)
432   foreach $dir (@dirs) {
433     unless (-d $dir) {
434       warn "Skipping nonexistent Directory $dir\n" if $verbose > 1;
435       $dir = '';
436       next;
437     }
438     warn "Resolving directory $dir\n" if $verbose;
439     if (File::Spec->file_name_is_absolute($dir)) { 
440         $dir = $self->fixpath($dir,1); 
441     }
442     else { 
443         $dir = $self->catdir($cwd,$dir); 
444     }
445   }
446   @dirs = grep { length($_) } @dirs;
447   unshift(@dirs,''); # Check each $lib without additions first
448
449   LIB: foreach $lib (@libs) {
450     if (exists $libmap{$lib}) {
451       next unless length $libmap{$lib};
452       $lib = $libmap{$lib};
453     }
454
455     my(@variants,$variant,$name,$test,$cand);
456     my($ctype) = '';
457
458     # If we don't have a file type, consider it a possibly abbreviated name and
459     # check for common variants.  We try these first to grab libraries before
460     # a like-named executable image (e.g. -lperl resolves to perlshr.exe
461     # before perl.exe).
462     if ($lib !~ /\.[^:>\]]*$/) {
463       push(@variants,"${lib}shr","${lib}rtl","${lib}lib");
464       push(@variants,"lib$lib") if $lib !~ /[:>\]]/;
465     }
466     push(@variants,$lib);
467     warn "Looking for $lib\n" if $verbose;
468     foreach $variant (@variants) {
469       foreach $dir (@dirs) {
470         my($type);
471
472         $name = "$dir$variant";
473         warn "\tChecking $name\n" if $verbose > 2;
474         if (-f ($test = VMS::Filespec::rmsexpand($name))) {
475           # It's got its own suffix, so we'll have to figure out the type
476           if    ($test =~ /(?:$so|exe)$/i)      { $type = 'SHR'; }
477           elsif ($test =~ /(?:$lib_ext|olb)$/i) { $type = 'OLB'; }
478           elsif ($test =~ /(?:$obj_ext|obj)$/i) {
479             warn "Note (probably harmless): "
480                          ."Plain object file $test found in library list\n";
481             $type = 'OBJ';
482           }
483           else {
484             warn "Note (probably harmless): "
485                          ."Unknown library type for $test; assuming shared\n";
486             $type = 'SHR';
487           }
488         }
489         elsif (-f ($test = VMS::Filespec::rmsexpand($name,$so))      or
490                -f ($test = VMS::Filespec::rmsexpand($name,'.exe')))     {
491           $type = 'SHR';
492           $name = $test unless $test =~ /exe;?\d*$/i;
493         }
494         elsif (not length($ctype) and  # If we've got a lib already, don't bother
495                ( -f ($test = VMS::Filespec::rmsexpand($name,$lib_ext)) or
496                  -f ($test = VMS::Filespec::rmsexpand($name,'.olb'))))  {
497           $type = 'OLB';
498           $name = $test unless $test =~ /olb;?\d*$/i;
499         }
500         elsif (not length($ctype) and  # If we've got a lib already, don't bother
501                ( -f ($test = VMS::Filespec::rmsexpand($name,$obj_ext)) or
502                  -f ($test = VMS::Filespec::rmsexpand($name,'.obj'))))  {
503           warn "Note (probably harmless): "
504                        ."Plain object file $test found in library list\n";
505           $type = 'OBJ';
506           $name = $test unless $test =~ /obj;?\d*$/i;
507         }
508         if (defined $type) {
509           $ctype = $type; $cand = $name;
510           last if $ctype eq 'SHR';
511         }
512       }
513       if ($ctype) { 
514         # This has to precede any other CRTLs, so just make it first
515         if ($cand eq 'VAXCCURSE') { unshift @{$found{$ctype}}, $cand; }  
516         else                      { push    @{$found{$ctype}}, $cand; }
517         warn "\tFound as $cand (really $test), type $ctype\n" if $verbose > 1;
518         push @flibs, $name unless $libs_seen{$fullname}++;
519         next LIB;
520       }
521     }
522     warn "Note (probably harmless): "
523                  ."No library found for $lib\n";
524   }
525
526   push @fndlibs, @{$found{OBJ}}                      if exists $found{OBJ};
527   push @fndlibs, map { "$_/Library" } @{$found{OLB}} if exists $found{OLB};
528   push @fndlibs, map { "$_/Share"   } @{$found{SHR}} if exists $found{SHR};
529   $lib = join(' ',@fndlibs);
530
531   $ldlib = $crtlstr ? "$lib $crtlstr" : $lib;
532   warn "Result:\n\tEXTRALIBS: $lib\n\tLDLOADLIBS: $ldlib\n" if $verbose;
533   wantarray ? ($lib, '', $ldlib, '', ($give_libs ? \@flibs : ())) : $lib;
534 }
535
536 1;