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