This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to MakeMaker 6.43_01
[perl5.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.006;
9 # Broken out of MakeMaker from version 4.11
10
11 use strict;
12 our $VERSION = 6.43_01;
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, @fullname);
53     my($pwd) = cwd(); # from Cwd.pm
54     my($found) = 0;
55
56     foreach my $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 my $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/lib$thislib.dll$Config_libext")){
135             } elsif (-f ($fullname="$thispth/Slib$thislib$Config_libext")){
136             } elsif ($^O eq 'dgux'
137                  && -l ($fullname="$thispth/lib$thislib$Config_libext")
138                  && readlink($fullname) =~ /^elink:/s) {
139                  # Some of DG's libraries look like misconnected symbolic
140                  # links, but development tools can follow them.  (They
141                  # look like this:
142                  #
143                  #    libm.a -> elink:${SDE_PATH:-/usr}/sde/\
144                  #    ${TARGET_BINARY_INTERFACE:-m88kdgux}/usr/lib/libm.a
145                  #
146                  # , the compilation tools expand the environment variables.)
147             } else {
148                 warn "$thislib not found in $thispth\n" if $verbose;
149                 next;
150             }
151             warn "'-l$thislib' found at $fullname\n" if $verbose;
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             # include the path to the lib once in the dynamic linker path
163             # but only if it is a dynamic lib and not in Perl itself
164             my($fullnamedir) = dirname($fullname);
165             push @ld_run_path, $fullnamedir
166                  if $is_dyna && !$in_perl &&
167                     !$ld_run_path_seen{$fullnamedir}++;
168
169             # Do not add it into the list if it is already linked in
170             # with the main perl executable.
171             # We have to special-case the NeXT, because math and ndbm 
172             # are both in libsys_s
173             unless ($in_perl || 
174                 ($Config{'osname'} eq 'next' &&
175                     ($thislib eq 'm' || $thislib eq 'ndbm')) ){
176                 push(@extralibs, "-l$thislib");
177             }
178
179             # We might be able to load this archive file dynamically
180             if ( ($Config{'dlsrc'} =~ /dl_next/ && $Config{'osvers'} lt '4_0')
181             ||   ($Config{'dlsrc'} =~ /dl_dld/) )
182             {
183                 # We push -l$thislib instead of $fullname because
184                 # it avoids hardwiring a fixed path into the .bs file.
185                 # Mkbootstrap will automatically add dl_findfile() to
186                 # the .bs file if it sees a name in the -l format.
187                 # USE THIS, when dl_findfile() is fixed: 
188                 # push(@bsloadlibs, "-l$thislib");
189                 # OLD USE WAS while checking results against old_extliblist
190                 push(@bsloadlibs, "$fullname");
191             } else {
192                 if ($is_dyna){
193                     # For SunOS4, do not add in this shared library if
194                     # it is already linked in the main perl executable
195                     push(@ldloadlibs, "-l$thislib")
196                         unless ($in_perl and $^O eq 'sunos');
197                 } else {
198                     push(@ldloadlibs, "-l$thislib");
199                 }
200             }
201             last;       # found one here so don't bother looking further
202         }
203         warn "Note (probably harmless): "
204                      ."No library found for -l$thislib\n"
205             unless $found_lib>0;
206     }
207
208     unless( $found ) {
209         return ('','','','', ($give_libs ? \@libs : ()));
210     }
211     else {
212         return ("@extralibs", "@bsloadlibs", "@ldloadlibs",
213                 join(":",@ld_run_path), ($give_libs ? \@libs : ()));
214     }
215 }
216
217 sub _win32_ext {
218
219     require Text::ParseWords;
220
221     my($self, $potential_libs, $verbose, $give_libs) = @_;
222     $verbose ||= 0;
223
224     # If user did not supply a list, we punt.
225     # (caller should probably use the list in $Config{libs})
226     return ("", "", "", "", ($give_libs ? [] : ())) unless $potential_libs;
227
228     my $cc              = $Config{cc};
229     my $VC              = $cc =~ /^cl/i;
230     my $BC              = $cc =~ /^bcc/i;
231     my $GC              = $cc =~ /^gcc/i;
232     my $so              = $Config{'so'};
233     my $libs            = $Config{'perllibs'};
234     my $libpth          = $Config{'libpth'};
235     my $libext          = $Config{'lib_ext'} || ".lib";
236     my(@libs, %libs_seen);
237
238     if ($libs and $potential_libs !~ /:nodefault/i) { 
239         # If Config.pm defines a set of default libs, we always
240         # tack them on to the user-supplied list, unless the user
241         # specified :nodefault
242
243         $potential_libs .= " " if $potential_libs;
244         $potential_libs .= $libs;
245     }
246     warn "Potential libraries are '$potential_libs':\n" if $verbose;
247
248     # normalize to forward slashes
249     $libpth =~ s,\\,/,g;
250     $potential_libs =~ s,\\,/,g;
251
252     # compute $extralibs from $potential_libs
253
254     my @searchpath;                 # from "-L/path" in $potential_libs
255     my @libpath         = Text::ParseWords::quotewords('\s+', 0, $libpth);
256     my @extralibs;
257     my $pwd             = cwd();    # from Cwd.pm
258     my $lib             = '';
259     my $found           = 0;
260     my $search          = 1;
261     my($fullname);
262
263     # add "$Config{installarchlib}/CORE" to default search path
264     push @libpath, "$Config{installarchlib}/CORE";
265
266     if ($VC and exists $ENV{LIB} and $ENV{LIB}) {
267         push @libpath, split /;/, $ENV{LIB};
268     }
269
270     foreach (Text::ParseWords::quotewords('\s+', 0, $potential_libs)){
271
272         my $thislib = $_;
273
274         # see if entry is a flag
275         if (/^:\w+$/) {
276             $search     = 0 if lc eq ':nosearch';
277             $search     = 1 if lc eq ':search';
278             warn "Ignoring unknown flag '$thislib'\n"
279                 if $verbose and !/^:(no)?(search|default)$/i;
280             next;
281         }
282
283         # if searching is disabled, do compiler-specific translations
284         unless ($search) {
285             s/^-l(.+)$/$1.lib/ unless $GC;
286             s/^-L/-libpath:/ if $VC;
287             push(@extralibs, $_);
288             $found++;
289             next;
290         }
291
292         # handle possible linker path arguments
293         if (s/^-L// and not -d) {
294             warn "$thislib ignored, directory does not exist\n"
295                 if $verbose;
296             next;
297         }
298         elsif (-d) {
299             unless (File::Spec->file_name_is_absolute($_)) {
300               warn "Warning: '$thislib' changed to '-L$pwd/$_'\n";
301               $_ = $self->catdir($pwd,$_);
302             }
303             push(@searchpath, $_);
304             next;
305         }
306
307         # handle possible library arguments
308         if (s/^-l// and $GC and !/^lib/i) {
309             $_ = "lib$_";
310         }
311         $_ .= $libext if !/\Q$libext\E$/i;
312
313         my $secondpass = 0;
314     LOOKAGAIN:
315
316         # look for the file itself
317         if (-f) {
318             warn "'$thislib' found as '$_'\n" if $verbose;
319             $found++;
320             push(@extralibs, $_);
321             next;
322         }
323
324         my $found_lib = 0;
325         foreach my $thispth (@searchpath, @libpath){
326             unless (-f ($fullname="$thispth\\$_")) {
327                 warn "'$thislib' not found as '$fullname'\n" if $verbose;
328                 next;
329             }
330             warn "'$thislib' found as '$fullname'\n" if $verbose;
331             $found++;
332             $found_lib++;
333             push(@extralibs, $fullname);
334             push @libs, $fullname unless $libs_seen{$fullname}++;
335             last;
336         }
337
338         # do another pass with (or without) leading 'lib' if they used -l
339         if (!$found_lib and $thislib =~ /^-l/ and !$secondpass++) {
340             if ($GC) {
341                 goto LOOKAGAIN if s/^lib//i;
342             }
343             elsif (!/^lib/i) {
344                 $_ = "lib$_";
345                 goto LOOKAGAIN;
346             }
347         }
348
349         # give up
350         warn "Note (probably harmless): "
351                      ."No library found for $thislib\n"
352             unless $found_lib>0;
353
354     }
355
356     return ('','','','', ($give_libs ? \@libs : ())) unless $found;
357
358     # make sure paths with spaces are properly quoted
359     @extralibs = map { (/\s/ && !/^".*"$/) ? qq["$_"] : $_ } @extralibs;
360     @libs = map { (/\s/ && !/^".*"$/) ? qq["$_"] : $_ } @libs;
361     $lib = join(' ',@extralibs);
362
363     # normalize back to backward slashes (to help braindead tools)
364     # XXX this may break equally braindead GNU tools that don't understand
365     # backslashes, either.  Seems like one can't win here.  Cursed be CP/M.
366     $lib =~ s,/,\\,g;
367
368     warn "Result: $lib\n" if $verbose;
369     wantarray ? ($lib, '', $lib, '', ($give_libs ? \@libs : ())) : $lib;
370 }
371
372
373 sub _vms_ext {
374   my($self, $potential_libs, $verbose, $give_libs) = @_;
375   $verbose ||= 0;
376
377   my(@crtls,$crtlstr);
378   @crtls = ( ($Config{'ldflags'} =~ m-/Debug-i ? $Config{'dbgprefix'} : '')
379               . 'PerlShr/Share' );
380   push(@crtls, grep { not /\(/ } split /\s+/, $Config{'perllibs'});
381   push(@crtls, grep { not /\(/ } split /\s+/, $Config{'libc'});
382   # In general, we pass through the basic libraries from %Config unchanged.
383   # The one exception is that if we're building in the Perl source tree, and
384   # a library spec could be resolved via a logical name, we go to some trouble
385   # to insure that the copy in the local tree is used, rather than one to
386   # which a system-wide logical may point.
387   if ($self->{PERL_SRC}) {
388     my($locspec,$type);
389     foreach my $lib (@crtls) { 
390       if (($locspec,$type) = $lib =~ m{^([\w\$-]+)(/\w+)?} and $locspec =~ /perl/i) {
391         if    (lc $type eq '/share')   { $locspec .= $Config{'exe_ext'}; }
392         elsif (lc $type eq '/library') { $locspec .= $Config{'lib_ext'}; }
393         else                           { $locspec .= $Config{'obj_ext'}; }
394         $locspec = $self->catfile($self->{PERL_SRC},$locspec);
395         $lib = "$locspec$type" if -e $locspec;
396       }
397     }
398   }
399   $crtlstr = @crtls ? join(' ',@crtls) : '';
400
401   unless ($potential_libs) {
402     warn "Result:\n\tEXTRALIBS: \n\tLDLOADLIBS: $crtlstr\n" if $verbose;
403     return ('', '', $crtlstr, '', ($give_libs ? [] : ()));
404   }
405
406   my(%found,@fndlibs,$ldlib);
407   my $cwd = cwd();
408   my($so,$lib_ext,$obj_ext) = @Config{'so','lib_ext','obj_ext'};
409   # List of common Unix library names and their VMS equivalents
410   # (VMS equivalent of '' indicates that the library is automatically
411   # searched by the linker, and should be skipped here.)
412   my(@flibs, %libs_seen);
413   my %libmap = ( 'm' => '', 'f77' => '', 'F77' => '', 'V77' => '', 'c' => '',
414                  'malloc' => '', 'crypt' => '', 'resolv' => '', 'c_s' => '',
415                  'socket' => '', 'X11' => 'DECW$XLIBSHR',
416                  'Xt' => 'DECW$XTSHR', 'Xm' => 'DECW$XMLIBSHR',
417                  'Xmu' => 'DECW$XMULIBSHR');
418   if ($Config{'vms_cc_type'} ne 'decc') { $libmap{'curses'} = 'VAXCCURSE'; }
419
420   warn "Potential libraries are '$potential_libs'\n" if $verbose;
421
422   # First, sort out directories and library names in the input
423   my(@dirs, @libs);
424   foreach my $lib (split ' ',$potential_libs) {
425     push(@dirs,$1),   next if $lib =~ /^-L(.*)/;
426     push(@dirs,$lib), next if $lib =~ /[:>\]]$/;
427     push(@dirs,$lib), next if -d $lib;
428     push(@libs,$1),   next if $lib =~ /^-l(.*)/;
429     push(@libs,$lib);
430   }
431   push(@dirs,split(' ',$Config{'libpth'}));
432
433   # Now make sure we've got VMS-syntax absolute directory specs
434   # (We don't, however, check whether someone's hidden a relative
435   # path in a logical name.)
436   foreach my $dir (@dirs) {
437     unless (-d $dir) {
438       warn "Skipping nonexistent Directory $dir\n" if $verbose > 1;
439       $dir = '';
440       next;
441     }
442     warn "Resolving directory $dir\n" if $verbose;
443     if (File::Spec->file_name_is_absolute($dir)) { 
444         $dir = $self->fixpath($dir,1); 
445     }
446     else { 
447         $dir = $self->catdir($cwd,$dir); 
448     }
449   }
450   @dirs = grep { length($_) } @dirs;
451   unshift(@dirs,''); # Check each $lib without additions first
452
453   LIB: foreach my $lib (@libs) {
454     if (exists $libmap{$lib}) {
455       next unless length $libmap{$lib};
456       $lib = $libmap{$lib};
457     }
458
459     my(@variants,$cand);
460     my($ctype) = '';
461
462     # If we don't have a file type, consider it a possibly abbreviated name and
463     # check for common variants.  We try these first to grab libraries before
464     # a like-named executable image (e.g. -lperl resolves to perlshr.exe
465     # before perl.exe).
466     if ($lib !~ /\.[^:>\]]*$/) {
467       push(@variants,"${lib}shr","${lib}rtl","${lib}lib");
468       push(@variants,"lib$lib") if $lib !~ /[:>\]]/;
469     }
470     push(@variants,$lib);
471     warn "Looking for $lib\n" if $verbose;
472     foreach my $variant (@variants) {
473       my($fullname, $name);
474
475       foreach my $dir (@dirs) {
476         my($type);
477
478         $name = "$dir$variant";
479         warn "\tChecking $name\n" if $verbose > 2;
480         $fullname = VMS::Filespec::rmsexpand($name);
481         if (defined $fullname and -f $fullname) {
482           # It's got its own suffix, so we'll have to figure out the type
483           if    ($fullname =~ /(?:$so|exe)$/i)      { $type = 'SHR'; }
484           elsif ($fullname =~ /(?:$lib_ext|olb)$/i) { $type = 'OLB'; }
485           elsif ($fullname =~ /(?:$obj_ext|obj)$/i) {
486             warn "Note (probably harmless): "
487                 ."Plain object file $fullname found in library list\n";
488             $type = 'OBJ';
489           }
490           else {
491             warn "Note (probably harmless): "
492                 ."Unknown library type for $fullname; assuming shared\n";
493             $type = 'SHR';
494           }
495         }
496         elsif (-f ($fullname = VMS::Filespec::rmsexpand($name,$so))      or
497                -f ($fullname = VMS::Filespec::rmsexpand($name,'.exe')))     {
498           $type = 'SHR';
499           $name = $fullname unless $fullname =~ /exe;?\d*$/i;
500         }
501         elsif (not length($ctype) and  # If we've got a lib already, 
502                                        # don't bother
503                ( -f ($fullname = VMS::Filespec::rmsexpand($name,$lib_ext)) or
504                  -f ($fullname = VMS::Filespec::rmsexpand($name,'.olb'))))  {
505           $type = 'OLB';
506           $name = $fullname unless $fullname =~ /olb;?\d*$/i;
507         }
508         elsif (not length($ctype) and  # If we've got a lib already, 
509                                        # don't bother
510                ( -f ($fullname = VMS::Filespec::rmsexpand($name,$obj_ext)) or
511                  -f ($fullname = VMS::Filespec::rmsexpand($name,'.obj'))))  {
512           warn "Note (probably harmless): "
513                        ."Plain object file $fullname found in library list\n";
514           $type = 'OBJ';
515           $name = $fullname unless $fullname =~ /obj;?\d*$/i;
516         }
517         if (defined $type) {
518           $ctype = $type; $cand = $name;
519           last if $ctype eq 'SHR';
520         }
521       }
522       if ($ctype) { 
523         # This has to precede any other CRTLs, so just make it first
524         if ($cand eq 'VAXCCURSE') { unshift @{$found{$ctype}}, $cand; }  
525         else                      { push    @{$found{$ctype}}, $cand; }
526         warn "\tFound as $cand (really $fullname), type $ctype\n" 
527           if $verbose > 1;
528         push @flibs, $name unless $libs_seen{$fullname}++;
529         next LIB;
530       }
531     }
532     warn "Note (probably harmless): "
533                  ."No library found for $lib\n";
534   }
535
536   push @fndlibs, @{$found{OBJ}}                      if exists $found{OBJ};
537   push @fndlibs, map { "$_/Library" } @{$found{OLB}} if exists $found{OLB};
538   push @fndlibs, map { "$_/Share"   } @{$found{SHR}} if exists $found{SHR};
539   my $lib = join(' ',@fndlibs);
540
541   $ldlib = $crtlstr ? "$lib $crtlstr" : $lib;
542   warn "Result:\n\tEXTRALIBS: $lib\n\tLDLOADLIBS: $ldlib\n" if $verbose;
543   wantarray ? ($lib, '', $ldlib, '', ($give_libs ? \@flibs : ())) : $lib;
544 }
545
546 1;