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