blib/lib/Alien/Base/Wrapper.pm

filter: Statementstmt 90.1% 193 / 214 Branchbran 70.7% 75 / 106 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 95.8% 23 / 24 Podpod 100% 8 / 8 totaltotal 83.6% 311 / 372 CC 61 SCAR 23.1
CC
61
Cov
83%
_myexec
20.0
checklib_args2
18.4
new
18.0
CRAP 10.1
« blib/lib/Alien/Util.pm ↑ Summary blib/lib/Alien/Build/Plugin/Fetch/LWP.pm »
1 package Alien::Base::Wrapper;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Text::ParseWords qw( shellwords );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # NOTE: Although this module is now distributed with Alien-Build,
10 # it should have NO non-perl-core dependencies for all Perls
11 # 5.6.0-5.30.1 (as of this writing, and any Perl more recent).
12 # You should be able to extract this module from the rest of
13 # Alien-Build and use it by itself. (There is a dzil plugin
14 # for this [AlienBase::Wrapper::Bundle]
15  
16 # ABSTRACT: Compiler and linker wrapper for Alien
17 2 our $VERSION = '2.88'; # VERSION
18  
19  
20 sub _version_parts
21 16 ▶{
Subroutine _version_parts: called CC=4 SCAR=14.0
22 16 my($version) = @_;
23 16 ▶ $version = '0' unless defined $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version 0 16 16
24  
25 # dotted-decimal style, e.g. 1.2.3 or v1.2.3: compare each part
26 # separately, as integers.
27 16 ▶ if($version =~ /^v?[0-9]+(\.[0-9]+){2,}$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^v?[0-9]+(\.[0-9]+){2,}$/) 3 13 16
28 {
29 3 $version =~ s/^v//;
30 3 return [ split /\./, $version ];
31 }
32  
33 # plain decimal style, e.g. 1.00 or 6.52: compare numerically as
34 # a single number, so that (say) 6.6 is newer than 6.52.
35 13 my($number) = $version =~ /^([0-9]+(?:\.[0-9]+)?)/;
36 13 ▶ $number = 0 unless defined $number;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number 0 13 13
37 13 return [ $number ];
38 }
39  
40 sub _version_cmp
41 8 ▶{
Subroutine _version_cmp: called CC=6 SCAR=18.2
42 8 my($v1, $v2) = @_;
43 8 my $p1 = _version_parts($v1);
44 8 my $p2 = _version_parts($v2);
45  
46 8 ▶ for my $i (0 .. (@$p1 > @$p2 ? $#$p1 : $#$p2))
Branch: 1 branch
BranchTrueFalseTotal
@$p1 > @$p2 ? : 0 8 8
47 {
48 10 ▶ my $n1 = defined $p1->[$i] ? $p1->[$i] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $p1->[$i] ? : 9 1 10
49 10 ▶ my $n2 = defined $p2->[$i] ? $p2->[$i] : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $p2->[$i] ? : 10 0 10
50 10 my $cmp = $n1 <=> $n2;
51 10 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 8 2 10
52 }
53  
54 0 return 0;
55 }
56  
57 # returns whichever of the two version numbers is newer (either may
58 # be undef, in which case the other, possibly also undef, is returned).
59 sub _version_max
60 24 ▶{
Subroutine _version_max: called CC=4 SCAR=13.9
61 24 my($v1, $v2) = @_;
62 24 ▶ return $v2 unless defined $v1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v1 15 9 24
63 9 ▶ return $v1 unless defined $v2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v2 1 8 9
64 8 ▶ return _version_cmp($v1, $v2) >= 0 ? $v1 : $v2;
Branch: 1 branch
BranchTrueFalseTotal
_version_cmp($v1, $v2) >= 0 ? : 3 5 8
65 }
66  
67 sub _join
68 68 ▶{
Subroutine _join: called CC=1 SCAR=0.0
69 join ' ',
70 map {
71 68 my $x = $_;
72 106 $x =~ s/(\s)/\\$1/g;
73 106 $x;
74 } @_;
75 }
76  
77 sub new
78 16 ▶{
Subroutine new: called CC=18 SCAR=28.9
Pod documented
79 16 my($class, @aliens) = @_;
80  
81 16 my $export = 1;
82 16 my $writemakefile = 0;
83  
84 16 my @cflags_I;
85 my @cflags_other;
86 16 my @ldflags_L;
87 16 my @ldflags_l;
88 16 my @ldflags_other;
89 16 my %requires = (
90 'ExtUtils::MakeMaker' => '6.52',
91 'Alien::Base::Wrapper' => '1.97',
92 );
93  
94 16 foreach my $alien (@aliens)
95 {
96 13 ▶ if($alien eq '!export')
Branch: 1 branch
BranchTrueFalseTotal
if ($alien eq "!export") 1 12 13
97 {
98 1 $export = 0;
99 1 next;
100 }
101 12 ▶ if($alien eq 'WriteMakefile')
Branch: 1 branch
BranchTrueFalseTotal
if ($alien eq "WriteMakefile") 1 11 12
102 {
103 1 $writemakefile = 1;
104 1 next;
105 }
106 11 my $version = 0;
107 11 ▶ if($alien =~ s/=(.*)$//)
Branch: 1 branch
BranchTrueFalseTotal
if ($alien =~ s/=(.*)$//) 3 8 11
108 {
109 3 $version = $1;
110 }
111 11 ▶ $alien = "Alien::$alien" unless $alien =~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $alien =~ /::/ 1 10 11
112 11 $requires{$alien} = $version;
113 11 my $alien_pm = $alien . '.pm';
114 11 $alien_pm =~ s/::/\//g;
115 11 ▶ require $alien_pm unless eval { $alien->can('cflags') } && eval { $alien->can('libs') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $alien->can("cflags") } } and eval { do { $alien->can("libs") } } 0 11 11
Condition33%1/3
eval { do { $alien->can("cflags") } } and eval { do { $alien->can("libs") } }
!ll&&!rl&&r
0011
MC/DC0%0/2
eval { do { $alien->can("cflags") } } and eval { do { $alien->can("libs") } }
eval { do { $alien->can("cflags") } } eval { do { $alien->can("libs") } }
Truth table33%1/3
A and B
A= eval { do { $alien->can("cflags") } }B= eval { do { $alien->can("libs") } }
ABresult
0X0
100
111
116 11 my $cflags;
117 my $libs;
118 11 ▶ if($alien->install_type eq 'share' && $alien->can('cflags_static'))
Branch: 1 branch
BranchTrueFalseTotal
if ($alien->install_type eq 'share' and $alien->can('cflags_static')) { } 1 10 11
Condition100%3/3
$alien->install_type eq "share" and $alien->can("cflags_static")
!ll&&!rl&&r
461
MC/DC100%2/2
$alien->install_type eq "share" and $alien->can("cflags_static")
$alien->install_type eq "share" $alien->can("cflags_static")
Truth table100%3/3
A and B
A= $alien->install_type eq "share"B= $alien->can("cflags_static")
ABresult
0X0
100
111
119 {
120 1 $cflags = $alien->cflags_static;
121 1 $libs = $alien->libs_static;
122 }
123 else
124 {
125 10 $cflags = $alien->cflags;
126 10 $libs = $alien->libs;
127 }
128  
129 11 ▶ $cflags = '' unless defined $cflags;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cflags 2 9 11
130 11 ▶ $libs = '' unless defined $libs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $libs 2 9 11
131  
132 11 push @cflags_I, grep /^-I/, shellwords $cflags;
133 11 push @cflags_other, grep !/^-I/, shellwords $cflags;
134  
135 11 push @ldflags_L, grep /^-L/, shellwords $libs;
136 11 push @ldflags_l, grep /^-l/, shellwords $libs;
137 11 push @ldflags_other, grep !/^-[Ll]/, shellwords $libs;
138 }
139  
140 16 my @cflags_define = grep /^-D/, @cflags_other;
141 16 my @cflags_other2 = grep !/^-D/, @cflags_other;
142  
143 16 my @mm;
144  
145 16 ▶ push @mm, INC => _join @cflags_I if @cflags_I;
Branch: 1 branch
BranchTrueFalseTotal
if @cflags_I 6 10 16
146 16 ▶ push @mm, CCFLAGS => _join(@cflags_other2) . " $Config{ccflags}" if @cflags_other2;
Branch: 1 branch
BranchTrueFalseTotal
if @cflags_other2 0 16 16
147 16 ▶ push @mm, DEFINE => _join(@cflags_define) if @cflags_define;
Branch: 1 branch
BranchTrueFalseTotal
if @cflags_define 6 10 16
148  
149 # TODO: handle spaces in -L paths
150 16 push @mm, LIBS => ["@ldflags_L @ldflags_l"];
151 16 my @ldflags = (@ldflags_L, @ldflags_other);
152 16 ▶ push @mm, LDDLFLAGS => _join(@ldflags) . " $Config{lddlflags}" if @ldflags;
Branch: 1 branch
BranchTrueFalseTotal
if @ldflags 6 10 16
153 16 ▶ push @mm, LDFLAGS => _join(@ldflags) . " $Config{ldflags}" if @ldflags;
Branch: 1 branch
BranchTrueFalseTotal
if @ldflags 6 10 16
154  
155 16 my @mb;
156  
157 16 push @mb, extra_compiler_flags => _join(@cflags_I, @cflags_other);
158 16 push @mb, extra_linker_flags => _join(@ldflags_l);
159  
160 16 ▶ if(@ldflags)
Branch: 1 branch
BranchTrueFalseTotal
if (@ldflags) 6 10 16
161 {
162 6 push @mb, config => {
163 lddlflags => _join(@ldflags) . " $Config{lddlflags}",
164 ldflags => _join(@ldflags) . " $Config{ldflags}",
165 },
166 }
167  
168 bless {
169 16 cflags_I => \@cflags_I,
170 cflags_other => \@cflags_other,
171 ldflags_L => \@ldflags_L,
172 ldflags_l => \@ldflags_l,
173 ldflags_other => \@ldflags_other,
174 mm => \@mm,
175 mb => \@mb,
176 _export => $export,
177 _writemakefile => $writemakefile,
178 requires => \%requires,
179 }, $class;
180 }
181  
182 2 my $default_abw = __PACKAGE__->new;
183  
184 # for testing only
185 4 ▶sub _reset { __PACKAGE__->new }
Subroutine _reset: called CC=1 SCAR=0.0
186  
187  
188 sub _myexec
189 0 ▶{
Subroutine _myexec: not called CC=4 SCAR=30.0
190 0 my @command = @_;
191 0 ▶ if($^O eq 'MSWin32')
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
192 {
193 # To handle weird quoting on MSWin32
194 # this logic needs to be improved.
195 0 my $command = "@command";
196 0 $command =~ s{"}{\\"}g;
197 0 system $command;
198  
199 0 ▶ if($? == -1 )
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 0 0 0
elsif ($? & 127) { } 0 0 0
200 {
201 0 die "failed to execute: $!\n";
202 }
203 elsif($? & 127)
204 {
205 0 die "child died with signal @{[ $? & 128 ]}";
206 }
207 else
208 {
209 0 exit($? >> 8);
210 }
211 }
212 else
213 {
214 0 exec @command;
215 }
216 }
217  
218 sub cc
219 4 ▶{
Subroutine cc: called CC=2 SCAR=7.0
Pod documented
220 my @command = (
221 shellwords($Config{cc}),
222 4 @{ $default_abw->{cflags_I} },
223 4 @{ $default_abw->{cflags_other} },
224 @ARGV,
225 );
226 4 ▶ print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"ALIEN_BASE_WRAPPER_QUIET"} 0 4 4
227 4 _myexec @command;
228 }
229  
230  
231 sub ld
232 4 ▶{
Subroutine ld: called CC=2 SCAR=7.0
Pod documented
233 my @command = (
234 shellwords($Config{ld}),
235 4 @{ $default_abw->{ldflags_L} },
236 4 @{ $default_abw->{ldflags_other} },
237 @ARGV,
238 4 @{ $default_abw->{ldflags_l} },
239 );
240 4 ▶ print "@command\n" unless $ENV{ALIEN_BASE_WRAPPER_QUIET};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"ALIEN_BASE_WRAPPER_QUIET"} 0 4 4
241 4 _myexec @command;
242 }
243  
244  
245 sub mm_args
246 1 ▶{
Subroutine mm_args: called CC=2 SCAR=7.1
Pod documented
247 1 ▶ my $self = ref $_[0] ? shift : $default_abw;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
248 1 @{ $self->{mm} };
249 }
250  
251  
252 sub mm_args2
253 4 ▶{
Subroutine mm_args2: called CC=6 SCAR=18.0
Pod documented
254 4 my $self = shift;
255 4 ▶ $self = $default_abw unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 2 4
256 4 my %args = @_;
257  
258 4 my @mm = @{ $self->{mm} };
259  
260 4 ▶ while(@mm)
Branch: 1 branch
BranchTrueFalseTotal
if (@mm) 20 4 24
261 {
262 20 my $key = shift @mm;
263 20 my $value = shift @mm;
264 20 ▶ if(defined $args{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$key}) { } 3 17 20
265 {
266 3 ▶ if($args{$key} eq 'LIBS')
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$key} eq 'LIBS') { } 0 3 3
267 {
268 0 require Carp;
269 # Todo: support this maybe?
270 0 Carp::croak("please do not specify your own LIBS key with mm_args2");
271 }
272 else
273 {
274 3 $args{$key} = join ' ', $value, $args{$key};
275 }
276 }
277 else
278 {
279 17 $args{$key} = $value;
280 }
281 }
282  
283 4 foreach my $module (keys %{ $self->{requires} })
284 {
285 $args{CONFIGURE_REQUIRES}->{$module} = _version_max(
286 $args{CONFIGURE_REQUIRES}->{$module},
287 16 $self->{requires}->{$module},
288 );
289 }
290  
291 4 %args;
292 }
293  
294  
295 sub checklib_args2
296 3 ▶{
Subroutine checklib_args2: called CC=15 SCAR=29.1
Pod documented
297 3 my $self = shift;
298 3 ▶ $self = $default_abw unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 3 0 3
299 3 my %args = @_;
300  
301 3 ▶ if(defined $args{LIBS} || defined $args{INC})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"LIBS"} or defined $args{"INC"}) 2 1 3
Condition100%3/3
defined $args{"LIBS"} or defined $args{"INC"}
l!l&&r!l&&!r
111
MC/DC100%2/2
defined $args{"LIBS"} or defined $args{"INC"}
defined $args{"LIBS"} defined $args{"INC"}
Truth table100%3/3
A or B
A= defined $args{"LIBS"}B= defined $args{"INC"}
ABresult
1X1
011
000
302 {
303 2 require Carp;
304 2 Carp::croak("please do not specify your own LIBS or INC key with checklib_args2");
305 }
306  
307 1 ▶ if(@{ $self->{cflags_I} })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"cflags_I"};}) 1 0 1
308 {
309 0 ▶ my @old = defined $args{incpath} ? (ref $args{incpath} ? @{ $args{incpath} } : ($args{incpath})) : ();
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'incpath'} ? : 1 0 1
ref $args{'incpath'} ? : 0 1 1
310 1 my @new = map { (my $x = $_) =~ s/^-I//; $x } @{ $self->{cflags_I} };
311 1 $args{incpath} = [ @new, @old ];
312 }
313  
314 1 ▶ if(@{ $self->{cflags_other} })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"cflags_other"};}) 1 0 1
315 {
316 1 ▶ $args{ccflags} = join ' ', @{ $self->{cflags_other} }, (defined $args{ccflags} ? $args{ccflags} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'ccflags'} ? : 0 1 1
317 }
318  
319 1 ▶ if(@{ $self->{ldflags_L} })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"ldflags_L"};}) 1 0 1
320 {
321 0 ▶ my @old = defined $args{libpath} ? (ref $args{libpath} ? @{ $args{libpath} } : ($args{libpath})) : ();
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'libpath'} ? : 1 0 1
ref $args{'libpath'} ? : 0 1 1
322 1 my @new = map { (my $x = $_) =~ s/^-L//; $x } @{ $self->{ldflags_L} };
323 1 $args{libpath} = [ @new, @old ];
324 }
325  
326 0 ▶ if(@{ $self->{ldflags_l} } || @{ $self->{ldflags_other} })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"ldflags_l"};} or @{$self->{"ldflags_other"};}) 1 0 1
Condition33%1/3
@{$self->{"ldflags_l"};} or @{$self->{"ldflags_other"};}
l!l&&r!l&&!r
100
MC/DC0%0/2
@{$self->{"ldflags_l"};} or @{$self->{"ldflags_other"};}
@{$self->{"ldflags_l"};} @{$self->{"ldflags_other"};}
Truth table33%1/3
A or B
A= @{$self->{"ldflags_l"};}B= @{$self->{"ldflags_other"};}
ABresult
1X1
011
000
327 {
328 1 ▶ $args{ldflags} = join ' ', @{ $self->{ldflags_l} }, @{ $self->{ldflags_other} }, (defined $args{ldflags} ? $args{ldflags} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'ldflags'} ? : 0 1 1
329 }
330  
331 1 %args;
332 }
333  
334  
335 sub mb_args
336 1 ▶{
Subroutine mb_args: called CC=2 SCAR=7.1
Pod documented
337 1 ▶ my $self = ref $_[0] ? shift : $default_abw;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
338 1 @{ $self->{mb} };
339 }
340  
341 sub import
342 6 ▶{
Subroutine import: called CC=3 SCAR=11.0
343 6 shift;
344 6 my $abw = $default_abw = __PACKAGE__->new(@_);
345 6 ▶ if($abw->_export)
Branch: 1 branch
BranchTrueFalseTotal
if ($abw->_export) 6 0 6
346 {
347 6 my $caller = caller;
348 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
349 6 *{"${caller}::cc"} = \&cc;
350 6 *{"${caller}::ld"} = \&ld;
351 }
352 6 ▶ if($abw->_writemakefile)
Branch: 1 branch
BranchTrueFalseTotal
if ($abw->_writemakefile) 1 5 6
353 {
354 1 my $caller = caller;
355 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
356 1 *{"${caller}::WriteMakefile"} = \&WriteMakefile;
357 }
358 }
359  
360  
361 sub WriteMakefile
362 2 ▶{
Subroutine WriteMakefile: called CC=5 SCAR=16.8
Pod documented
363 2 my %args = @_;
364  
365 2 require ExtUtils::MakeMaker;
366 2 ExtUtils::MakeMaker->VERSION('6.52');
367  
368 2 my @aliens;
369  
370 2 ▶ if(my $reqs = delete $args{alien_requires})
Branch: 1 branch
BranchTrueFalseTotal
if (my $reqs = delete $args{'alien_requires'}) { } 2 0 2
371 {
372 2 ▶ if(ref $reqs eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $reqs eq 'HASH') { } 1 1 2
elsif (ref $reqs eq 'ARRAY') { } 1 0 1
373 {
374 @aliens = map {
375 1 my $module = $_;
376 2 my $version = $reqs->{$module};
377 2 ▶ $version ? "$module=$version" : "$module";
Branch: 1 branch
BranchTrueFalseTotal
$version ? : 1 1 2
378 } sort keys %$reqs;
379 }
380 elsif(ref $reqs eq 'ARRAY')
381 {
382 1 @aliens = @$reqs;
383 }
384 else
385 {
386 0 require Carp;
387 0 Carp::croak("aliens_require must be either a hash or array reference");
388 }
389 }
390 else
391 {
392 0 require Carp;
393 0 Carp::croak("You are using Alien::Base::Wrapper::WriteMakefile, but didn't specify any alien requirements");
394 }
395  
396 2 ExtUtils::MakeMaker::WriteMakefile(
397 Alien::Base::Wrapper->new(@aliens)->mm_args2(%args),
398 );
399 }
400  
401 8 ▶sub _export { shift->{_export} }
Subroutine _export: called CC=1 SCAR=0.0
402 6 ▶sub _writemakefile { shift->{_writemakefile} }
Subroutine _writemakefile: called CC=1 SCAR=0.0
403  
404 2 1;
405  
406 __END__
« blib/lib/Alien/Util.pm ↑ Summary blib/lib/Alien/Build/Plugin/Fetch/LWP.pm »