blib/lib/ExtUtils/Depends.pm

filter: Statementstmt 82.0% 169 / 206 Branchbran 39.6% 23 / 58 Conditioncond 36.3% 8 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 83.7% 31 / 37 Podpod 75.0% 15 / 20 totaltotal 69.9% 249 / 356 SCAR 46.4
CC
46
Cov
70%
find_extra_libs
53.3
get_makefile_vars
19.9
load
12.2
CRAP 103.5
↑ Summary
1 package ExtUtils::Depends;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '0.8001';
12  
13 sub import {
14 4 ▶ my $class = shift;
Subroutine import: called CC=3 SCAR=15.4
15 4 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 0 4
16 0 ▶ die "$class version $_[0] is required--this is only version $VERSION"
Branch: 1 branch
BranchTrueFalseTotal
if $ExtUtils::Depends::VERSION < $_[0] 0 0 0
17 if $VERSION < $_[0];
18 }
19  
20 sub new {
21 4 ▶ my ($class, $name, @deps) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 4 my $self = bless {
23 name => $name,
24 deps => {},
25 inc => [],
26 libs => '',
27  
28 pm => {},
29 typemaps => [],
30 xs => [],
31 c => [],
32 }, $class;
33  
34 4 $self->add_deps (@deps);
35  
36 # attempt to load these now, so we'll find out as soon as possible
37 # whether the dependencies are valid. we'll load them again in
38 # get_makefile_vars to catch any added between now and then.
39 4 $self->load_deps;
40  
41 4 return $self;
42 }
43  
44 sub add_deps {
45 5 ▶ my $self = shift;
Subroutine add_deps: called CC=3 SCAR=11.2
Pod documented
46 5 foreach my $d (@_) {
47 $self->{deps}{$d} = undef
48 2 ▶ unless $self->{deps}{$d};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"deps"}{$d} 2 0 2
49 }
50 }
51  
52 sub get_deps {
53 2 ▶ my $self = shift;
Subroutine get_deps: called CC=1 SCAR=0.0
Pod documented
54 2 $self->load_deps; # just in case
55  
56 2 return %{$self->{deps}};
57 }
58  
59 sub set_inc {
60 1 ▶ my $self = shift;
Subroutine set_inc: called CC=1 SCAR=0.0
Pod documented
61 1 push @{ $self->{inc} }, @_;
62 }
63  
64 sub set_libs {
65 1 ▶ my ($self, $newlibs) = @_;
Subroutine set_libs: called CC=1 SCAR=0.0
Pod documented
66 1 $self->{libs} = $newlibs;
67 }
68  
69 sub add_pm {
70 7 ▶ my ($self, %pm) = @_;
Subroutine add_pm: called CC=2 SCAR=6.9
Pod documented
71 7 ▶ while (my ($key, $value) = each %pm) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %pm) 8 7 15
72 8 $self->{pm}{$key} = $value;
73 }
74 }
75  
76 sub _listkey_add_list {
77 3 ▶ my ($self, $key, @list) = @_;
Subroutine _listkey_add_list: called CC=2 SCAR=7.0
78 3 ▶ $self->{$key} = [] unless $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} 0 3 3
79 3 push @{ $self->{$key} }, @list;
80 }
81  
82 1 ▶sub add_xs { shift->_listkey_add_list ('xs', @_) }
Subroutine add_xs: called CC=1 SCAR=0.0
Pod documented
83 1 ▶sub add_c { shift->_listkey_add_list ('c', @_) }
Subroutine add_c: called CC=1 SCAR=0.0
Pod documented
84 sub add_typemaps {
85 1 ▶ my $self = shift;
Subroutine add_typemaps: called CC=1 SCAR=0.0
Pod documented
86 1 $self->_listkey_add_list ('typemaps', @_);
87 1 $self->install (@_);
88 }
89  
90 # no-op, only used for source back-compat
91 0 ▶sub add_headers { carp "add_headers() is a no-op" }
Subroutine add_headers: not called CC=1 SCAR=6.9
Pod documented
92  
93 ####### PRIVATE
94 8 ▶sub basename { (File::Spec->splitdir ($_[0]))[-1] }
Subroutine basename: called CC=1 SCAR=0.0
Pod undocumented
95 # get the name in Makefile syntax.
96 sub installed_filename {
97 6 ▶ my $self = shift;
Subroutine installed_filename: called CC=1 SCAR=0.0
Pod undocumented
98 6 return '$(INST_ARCHLIB)/$(FULLEXT)/Install/'.basename ($_[0]);
99 }
100  
101 sub install {
102 # install things by adding them to the hash of pm files that gets
103 # passed through WriteMakefile's PM key.
104 2 ▶ my $self = shift;
Subroutine install: called CC=2 SCAR=6.9
Pod documented
105 2 foreach my $f (@_) {
106 4 $self->add_pm ($f, $self->installed_filename ($f));
107 }
108 }
109  
110 sub save_config {
111 4 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶ local $Data::Dumper::Terse = 0;
Subroutine save_config: called CC=2 SCAR=6.9
Pod documented
113 2 local $Data::Dumper::Sortkeys = 1;
114 4 ▶ use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
115  
116 2 my ($self, $filename) = @_;
117  
118 2 ▶ my $file = IO::File->new (">".$filename)
Branch: 1 branch
BranchTrueFalseTotal
my $file = "IO::File"->new('>' . $filename) or croak("can't open '${filename}' for writing: $!\n") 0 2 2
119 or croak "can't open '$filename' for writing: $!\n";
120  
121 2 print $file "package $self->{name}\::Install::Files;\n\n";
122 print $file "".Data::Dumper->Dump([{
123 2 inc => join (" ", @{ $self->{inc} }),
124 libs => $self->{libs},
125 2 typemaps => [ map { basename $_ } @{ $self->{typemaps} } ],
126 2 deps => [sort keys %{ $self->{deps} }],
127 }], ['self']);
128 2 print $file <<'EOF';
129  
130 @deps = @{ $self->{deps} };
131 @typemaps = @{ $self->{typemaps} };
132 $libs = $self->{libs};
133 $inc = $self->{inc};
134 EOF
135 # this is ridiculous, but old versions of ExtUtils::Depends take
136 # first $loadedmodule::CORE and then $INC{$file} --- the fallback
137 # includes the Filename.pm, which is not useful. so we must add
138 # this crappy code. we don't worry about portable pathnames,
139 # as the old code didn't either.
140 2 (my $mdir = $self->{name}) =~ s{::}{/}g;
141 2 print $file <<"EOT";
142  
143 \$CORE = undef;
144 foreach (\@INC) {
145 if ( -f \$_ . "/$mdir/Install/Files.pm") {
146 \$CORE = \$_ . "/$mdir/Install/";
147 last;
148 }
149 }
150  
151 sub deps { \@{ \$self->{deps} }; }
152  
153 sub Inline {
154 my (\$class, \$lang) = \@_;
155 +{ map { (uc(\$_) => \$self->{\$_}) } qw(inc libs typemaps) };
156 }
157 EOT
158  
159 2 print $file "\n1;\n";
160  
161 2 close $file;
162  
163 # we need to ensure that the file we just created gets put into
164 # the install dir with everything else.
165 #$self->install ($filename);
166 2 $self->add_pm ($filename, $self->installed_filename ('Files.pm'));
167 }
168  
169 sub load {
170 5 ▶ my $dep = shift;
Subroutine load: called CC=12 SCAR=25.0
Pod documented
171 5 my @pieces = split /::/, $dep;
172 5 my @suffix = qw/ Install Files /;
173 # not File::Spec - see perldoc -f require
174 5 my $relpath = join('/', @pieces, @suffix) . '.pm';
175 5 my $depinstallfiles = join "::", @pieces, @suffix;
176 5 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $relpath } } or die " *** Can't load dependency information for ${dep}:\n $@\n" 0 5 5
177 5 require $relpath
178 } or die " *** Can't load dependency information for $dep:\n $@\n";
179 #print Dumper(\%INC);
180  
181 # effectively $instpath = dirname($INC{$relpath})
182 5 @pieces = File::Spec->splitdir ($INC{$relpath});
183 5 pop @pieces;
184 5 my $instpath = File::Spec->catdir (@pieces);
185  
186 4 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 5 ▶ croak "No dependency information found for $dep"
Branch: 1 branch
BranchTrueFalseTotal
unless $instpath 0 5 5
189 unless $instpath;
190  
191 5 ▶ if (not File::Spec->file_name_is_absolute ($instpath)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($instpath)) 0 5 5
192 0 $instpath = File::Spec->rel2abs ($instpath);
193 }
194  
195 5 my (@typemaps, $inc, $libs, @deps);
196  
197 # this will not exist when loading files from old versions
198 # of ExtUtils::Depends.
199 5 @deps = eval { $depinstallfiles->deps };
200 1 @deps = @{"$depinstallfiles\::deps"}
201 5 ▶ if $@ and exists ${"$depinstallfiles\::"}{deps};
Branch: 1 branch
BranchTrueFalseTotal
if $@ and exists do { "${depinstallfiles}::" }->{"deps"} 1 4 5
Condition66%2/3
$@ and exists do { "${depinstallfiles}::" }->{"deps"}
!ll&&!rl&&r
401
MC/DC50%1/2
$@ and exists do { "${depinstallfiles}::" }->{"deps"}
$@ exists do { "${depinstallfiles}::" }->{"deps"}
Truth table66%2/3
A and B
A= $@B= exists do { "${depinstallfiles}::" }->{"deps"}
ABresult
0X0
100
111
202  
203 5 my $inline = eval { $depinstallfiles->Inline('C') };
204 5 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 4 1 5
205 4 ▶ $inc = $inline->{INC} || '';
Condition100%2/2
$inline->{'INC'} || ''
l!l
22
MC/DC100%1/1
$inline->{'INC'} || ''
$inline->{'INC'}
Truth table100%2/2
A
A= $inline->{'INC'}
Aresult
11
00
206 4 ▶ $libs = $inline->{LIBS} || '';
Condition100%2/2
$inline->{'LIBS'} || ''
l!l
13
MC/DC100%1/1
$inline->{'LIBS'} || ''
$inline->{'LIBS'}
Truth table100%2/2
A
A= $inline->{'LIBS'}
Aresult
11
00
207 4 ▶ @typemaps = @{ $inline->{TYPEMAPS} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$inline->{"TYPEMAPS"} or [] 1 3 4
208 } else {
209 1 ▶ $inc = ${"$depinstallfiles\::inc"} || '';
Condition50%1/2
${"${depinstallfiles}::inc";} || ''
l!l
10
MC/DC0%0/1
${"${depinstallfiles}::inc";} || ''
${"${depinstallfiles}::inc";}
Truth table50%1/2
A
A= ${"${depinstallfiles}::inc";}
Aresult
11
00
210 1 ▶ $libs = ${"$depinstallfiles\::libs"} || '';
Condition50%1/2
${"${depinstallfiles}::libs";} || ''
l!l
01
MC/DC0%0/1
${"${depinstallfiles}::libs";} || ''
${"${depinstallfiles}::libs";}
Truth table50%1/2
A
A= ${"${depinstallfiles}::libs";}
Aresult
11
00
211 1 @typemaps = @{"$depinstallfiles\::typemaps"};
212 }
213 5 @typemaps = map { File::Spec->rel2abs ($_, $instpath) } @typemaps;
214  
215 {
216 5 instpath => $instpath,
217 typemaps => \@typemaps,
218 inc => "-I". _quote_if_space($instpath) ." $inc",
219 libs => $libs,
220 deps => \@deps,
221 }
222 }
223  
224 5 ▶sub _quote_if_space { $_[0] =~ / / ? qq{"$_[0]"} : $_[0] }
Subroutine _quote_if_space: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ / / ? : 0 5 5
225  
226 sub load_deps {
227 8 ▶ my $self = shift;
Subroutine load_deps: called CC=4 SCAR=14.5
Pod documented
228 8 my @load = grep !$self->{deps}{$_}, keys %{ $self->{deps} };
229 8 my %in_load; @in_load{@load} = ();
230 8 foreach my $d (@load) {
231 2 $self->{deps}{$d} = my $dep = load($d);
232 my @new_deps = grep !($self->{deps}{$_} || exists $in_load{$_}),
233 2 ▶ @{ $dep->{deps} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$dep->{"deps"} or [] 0 2 2
Condition0%0/3
$self->{'deps'}{$_} || exists $in_load{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'deps'}{$_} || exists $in_load{$_}
$self->{'deps'}{$_} exists $in_load{$_}
Truth table0%0/3
A || B
A= $self->{'deps'}{$_}B= exists $in_load{$_}
ABresult
1X1
011
000
234 2 push @load, @new_deps;
235 2 @in_load{@new_deps} = ();
236 }
237 }
238  
239 sub uniquify {
240 3 ▶ my %seen;
Subroutine uniquify: called CC=1 SCAR=0.0
Pod undocumented
241 3 grep !$seen{$_}++, @_;
242 }
243  
244 sub get_makefile_vars {
245 1 ▶ my $self = shift;
Subroutine get_makefile_vars: called CC=13 SCAR=29.9
Pod documented
246  
247 # collect and uniquify things from the dependencies.
248 # first, ensure they are completely loaded.
249 1 $self->load_deps;
250  
251 ##my @defbits = map { split } @{ $self->{defines} };
252 1 my @incbits = map { split } @{ $self->{inc} };
253 1 my @libsbits = split /\s+/, $self->{libs};
254 1 my @typemaps = @{ $self->{typemaps} };
255 1 foreach my $d (sort keys %{ $self->{deps} }) {
256 0 my $dep = $self->{deps}{$d};
257 #push @defbits, @{ $dep->{defines} };
258 0 ▶ push @incbits, @{ $dep->{defines} } if $dep->{defines};
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"defines"} 0 0 0
259 0 ▶ push @incbits, split /\s+/, $dep->{inc} if $dep->{inc};
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"inc"} 0 0 0
260 0 ▶ push @libsbits, split /\s+/, $dep->{libs} if $dep->{libs};
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"libs"} 0 0 0
261 0 ▶ push @typemaps, @{ $dep->{typemaps} } if $dep->{typemaps};
Branch: 1 branch
BranchTrueFalseTotal
if $dep->{"typemaps"} 0 0 0
262 }
263  
264 # we have a fair bit of work to do for the xs files...
265 1 my @clean = ();
266 1 my @OBJECT = ();
267 1 my %XS = ();
268 1 foreach my $xs (@{ $self->{xs} }) {
269 2 (my $c = $xs) =~ s/\.xs$/\.c/i;
270 2 (my $o = $xs) =~ s/\.xs$/\$(OBJ_EXT)/i;
271 2 $XS{$xs} = $c;
272 2 push @OBJECT, $o;
273 # according to the MakeMaker manpage, the C files listed in
274 # XS will be added automatically to the list of cleanfiles.
275 2 push @clean, $o;
276 }
277  
278 # we may have C files, as well:
279 1 foreach my $c (@{ $self->{c} }) {
280 2 (my $o = $c) =~ s/\.c$/\$(OBJ_EXT)/i;
281 2 push @OBJECT, $o;
282 2 push @clean, $o;
283 }
284  
285 1 my %vars = (
286 INC => join (' ', uniquify @incbits),
287 LIBS => join (' ', uniquify $self->find_extra_libs, @libsbits),
288 TYPEMAPS => [@typemaps],
289 );
290  
291 1 ▶ $self->build_dll_lib(\%vars) if $^O =~ /MSWin32/;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /MSWin32/ 0 1 1
292  
293 # we don't want to provide these if there is no data in them;
294 # that way, the caller can still get default behavior out of
295 # MakeMaker when INC, LIBS and TYPEMAPS are all that are required.
296 $vars{PM} = $self->{pm}
297 1 ▶ if %{ $self->{pm} };
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->{"pm"};} 1 0 1
298 1 ▶ $vars{clean} = { FILES => join (" ", @clean), }
Branch: 1 branch
BranchTrueFalseTotal
if @clean 1 0 1
299 if @clean;
300 1 ▶ $vars{OBJECT} = join (" ", @OBJECT)
Branch: 1 branch
BranchTrueFalseTotal
if @OBJECT 1 0 1
301 if @OBJECT;
302 1 ▶ $vars{XS} = \%XS
Branch: 1 branch
BranchTrueFalseTotal
if %XS 1 0 1
303 if %XS;
304  
305 1 %vars;
306 }
307  
308 sub build_dll_lib {
309 0 ▶ my ($self, $vars) = @_;
Subroutine build_dll_lib: not called CC=2 SCAR=17.9
Pod undocumented
310 0 ▶ $vars->{macro} ||= {};
Condition0%0/2
$vars->{"macro"} ||= {}
l!l
00
MC/DC0%0/1
$vars->{"macro"} ||= {}
$vars->{"macro"}
Truth table0%0/2
A
A= $vars->{"macro"}
Aresult
11
00
311 0 $vars->{macro}{'INST_DYNAMIC_LIB'} =
312 '$(INST_ARCHAUTODIR)/$(DLBASE)$(LIB_EXT)';
313 }
314  
315 # Search for extra library files to link against on Windows (either native
316 # Windows library # files, or Cygwin library files)
317 # NOTE: not meant to be called publicly, so no POD documentation
318 sub find_extra_libs {
319 1 ▶ my $self = shift;
Subroutine find_extra_libs: called CC=8 SCAR=39.8
Pod undocumented
320  
321 my %mappers = (
322 0 ▶ MSWin32 => sub { $_[0] . '\.(?:lib|a)' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
323 0 ▶ cygwin => sub { $_[0] . '\.dll'},
Subroutine __ANON__: not called CC=1 SCAR=6.9
324 0 ▶ android => sub { $_[0] . '\.' . $Config{dlext} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
325 1 );
326 1 my $mapper = $mappers{$^O};
327 1 ▶ return () unless defined $mapper;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mapper 1 0 1
328  
329 0 my @found_libs = ();
330 0 foreach my $name (keys %{ $self->{deps} }) {
331 0 (my $stem = $name) =~ s/^.*:://;
332 0 ▶ if ( defined &DynaLoader::mod2fname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &DynaLoader::mod2fname) 0 0 0
333 0 my @parts = split /::/, $name;
334 0 $stem = DynaLoader::mod2fname([@parts]);
335 }
336 0 my $lib = $mapper->($stem);
337 0 my $pattern = qr/$lib$/;
338  
339 0 my $matching_dir;
340 my $matching_file;
341 find (sub {
342 0 ▶ if ((not $matching_file) && /$pattern/) {;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (not $matching_file and /$pattern/) 0 0 0
Condition0%0/3
not $matching_file and /$pattern/
!ll&&!rl&&r
000
MC/DC0%0/2
not $matching_file and /$pattern/
not $matching_file /$pattern/
Truth table0%0/3
not(A) and B
A= not $matching_fileB= /$pattern/
ABresult
0X0
100
111
343 0 $matching_dir = $File::Find::dir;
344 0 $matching_file = $File::Find::name;
345 }
346 0 ▶ }, map { -d $_ ? ($_) : () } @INC); # only extant dirs
Branch: 1 branch
BranchTrueFalseTotal
-d $_ ? : 0 0 0
347  
348 0 ▶ if ($matching_file && -f $matching_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matching_file and -f $matching_file) 0 0 0
Condition0%0/3
$matching_file and -f $matching_file
!ll&&!rl&&r
000
MC/DC0%0/2
$matching_file and -f $matching_file
$matching_file -f $matching_file
Truth table0%0/3
A and B
A= $matching_fileB= -f $matching_file
ABresult
0X0
100
111
349 0 push @found_libs,
350 '-L' . _quote_if_space($matching_dir),
351 '-l' . $stem;
352 # Android's linker ignores the RTLD_GLOBAL flag
353 # and loads everything as if under RTLD_LOCAL.
354 # What this means in practice is that modules need
355 # to explicitly link to their dependencies,
356 # because otherwise they won't be able to locate any
357 # functions they define.
358 # We use the -l:foo.so flag to indicate that the
359 # actual library name to look for is foo.so, not
360 # libfoo.so
361 0 ▶ if ( $^O eq 'android' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "android") 0 0 0
362 0 $found_libs[-1] = "-l:$stem.$Config{dlext}";
363 }
364 0 next;
365 }
366 }
367  
368 0 return @found_libs;
369 }
370  
371 __END__
↑ Summary