blib/lib/Alien/Build/Plugin/Probe/CBuilder.pm

filter: Statementstmt 78.3% 83 / 106 Branchbran 46.1% 12 / 26 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 70.6% 113 / 160 CC 20 SCAR 18.4
CC
20
Cov
69%
init
12.3
__ANON__
8.0
__ANON__
7.5
CRAP 6.3
« blib/lib/Alien/Build/Plugin/Test/Mock.pm ↑ Summary corpus/lib/Alien/Build/Plugin/Fetch/Corpus.pm »
1 package Alien::Build::Plugin::Probe::CBuilder;
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 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Alien::Build::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Capture::Tiny qw( capture_merged capture );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Alien::Util qw( version_cmp );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Probe for system libraries by guessing with ExtUtils::CBuilder
13 4 our $VERSION = '2.88'; # VERSION
14  
15  
16 4 ▶has options => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18  
19 4 has cflags => '';
20  
21  
22 4 has libs => '';
23  
24  
25 4 has program => 'int main(int argc, char *argv[]) { return 0; }';
26  
27  
28 4 has version => undef;
29  
30  
31 4 has 'atleast_version' => undef;
32  
33  
34 4 has aliens => [];
35  
36  
37 4 has lang => 'C';
38  
39 sub init
40 5 ▶{
Subroutine init: called CC=8 SCAR=25.1
Pod documented
41 5 my($self, $meta) = @_;
42  
43 5 $meta->add_requires('configure' => 'ExtUtils::CBuilder' => 0 );
44  
45 5 ▶ if(@{ $self->aliens })
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->aliens;}) 0 5 5
46 {
47 0 ▶ die "You can't specify both 'aliens' and either 'cflags' or 'libs' for the Probe::CBuilder plugin" if $self->cflags || $self->libs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->cflags or $self->libs 0 0 0
Condition0%0/3
$self->cflags or $self->libs
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cflags or $self->libs
$self->cflags $self->libs
Truth table0%0/3
A or B
A= $self->cflagsB= $self->libs
ABresult
1X1
011
000
48  
49 0 $meta->add_requires('configure' => $_ => 0 ) for @{ $self->aliens };
50 0 $meta->add_requires('Alien::Build::Plugin::Probe::CBuilder' => '0.53');
51  
52 0 my $cflags = '';
53 0 my $libs = '';
54 0 foreach my $alien (@{ $self->aliens })
55 {
56 0 my $pm = "$alien.pm";
57 0 $pm =~ s/::/\//g;
58 0 require $pm;
59 0 $cflags .= $alien->cflags . ' ';
60 0 $libs .= $alien->libs . ' ';
61 }
62 0 $self->cflags($cflags);
63 0 $self->libs($libs);
64 }
65  
66 5 my @cpp;
67  
68 5 ▶ if($self->lang ne 'C')
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lang ne "C") 0 5 5
69 {
70 0 $meta->add_requires('Alien::Build::Plugin::Probe::CBuilder' => '0.53');
71 0 ▶ @cpp = ('C++' => 1) if $self->lang eq 'C++';
Branch: 1 branch
BranchTrueFalseTotal
if $self->lang eq "C++" 0 0 0
72 }
73  
74 $meta->register_hook(
75 4 ▶ probe => sub {
Subroutine __ANON__: called CC=7 SCAR=20.1
76 4 my($build) = @_;
77  
78 4 $build->hook_prop->{probe_class} = __PACKAGE__;
79 4 $build->hook_prop->{probe_instance_id} = $self->instance_id;
80  
81 4 local $CWD = File::Temp::tempdir( CLEANUP => 1, DIR => $CWD );
82  
83 4 open my $fh, '>', 'mytest.c';
84 4 print $fh $self->program;
85 4 close $fh;
86  
87 4 $build->log("trying: cflags=@{[ $self->cflags ]} libs=@{[ $self->libs ]}");
88  
89 4 my $cb = ExtUtils::CBuilder->new(%{ $self->options });
90  
91 4 ▶ my($out1, $obj) = capture_merged { eval {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 4 $cb->compile(
93 source => 'mytest.c',
94 extra_compiler_flags => $self->cflags,
95 @cpp,
96 );
97 4 } };
98  
99 4 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 3 4
100 {
101 1 $build->log("compile failed: $error");
102 1 $build->log("compile failed: $out1");
103 1 die $error;
104 }
105  
106 3 ▶ my($out2, $exe) = capture_merged { eval {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 3 $cb->link_executable(
108 objects => [$obj],
109 extra_linker_flags => $self->libs,
110 );
111 3 } };
112  
113 3 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 0 3 3
114 {
115 0 $build->log("link failed: $error");
116 0 $build->log("link failed: $out2");
117 0 die $error;
118 }
119  
120 3 ▶ my($out, $err, $ret) = capture { system($^O eq 'MSWin32' ? $exe : "./$exe") };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 3 3
121 3 ▶ die "execute failed" if $ret;
Branch: 1 branch
BranchTrueFalseTotal
if $ret 0 3 3
122  
123 3 my $cflags = $self->cflags;
124 3 my $libs = $self->libs;
125  
126 3 $cflags =~ s{\s*$}{ };
127 3 $libs =~ s{\s*$}{ };
128  
129 3 $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id} = {
130 cflags => $cflags,
131 libs => $libs,
132 };
133  
134 3 ▶ if(defined $self->version)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->version) 1 2 3
135 {
136 1 my($version) = $out =~ $self->version;
137 1 ▶ if (defined $self->atleast_version)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->atleast_version) 0 1 1
138 {
139 0 ▶ if(version_cmp ($version, $self->atleast_version) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (version_cmp($version, $self->atleast_version) < 0) 0 0 0
140 {
141 0 die "CBuilder probe found version $version, but at least @{[ $self->atleast_version ]} is required.";
142 }
143 }
144 1 $build->hook_prop->{version} = $version;
145 1 $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id}->{version} = $version;
146 }
147  
148 3 'system';
149 }
150 5 );
151  
152 $meta->register_hook(
153 2 ▶ gather_system => sub {
Subroutine __ANON__: called CC=6 SCAR=20.9
154 2 my($build) = @_;
155  
156 return if $build->hook_prop->{name} eq 'gather_system'
157 2 ▶ && ($build->install_prop->{system_probe_instance_id} || '') ne $self->instance_id;
Branch: 1 branch
BranchTrueFalseTotal
if $build->hook_prop->{"name"} eq "gather_system" and ($build->install_prop->{"system_probe_instance_id"} || "") ne $self->instance_id 0 2 2
Condition40%2/5
$build->hook_prop->{"name"} eq "gather_system" and ($build->install_prop->{"system_probe_instance_id"} || "") ne $self->instance_id
!ll&&!rl&&r
020
$build->install_prop->{'system_probe_instance_id'} || ''
l!l
20
MC/DC0%0/2
$build->hook_prop->{"name"} eq "gather_system" and ($build->install_prop->{"system_probe_instance_id"} || "") ne $self->instance_id
$build->hook_prop->{"name"} eq "gather_system" $build->install_prop->{'system_probe_instance_id'}
Truth table33%1/3
A and B
A= $build->hook_prop->{"name"} eq "gather_system"B= $build->install_prop->{'system_probe_instance_id'}
ABresult
0X0
100
111
158  
159 2 ▶ if(my $p = $build->install_prop->{plugin_probe_cbuilder_gather}->{$self->instance_id})
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $build->install_prop->{"plugin_probe_cbuilder_gather"}{$self->instance_id}) 2 0 2
160 {
161 2 $build->runtime_prop->{$_} = $p->{$_} for keys %$p;
162 }
163 },
164 5 );
165 }
166  
167 4 1;
168  
169 __END__
« blib/lib/Alien/Build/Plugin/Test/Mock.pm ↑ Summary corpus/lib/Alien/Build/Plugin/Fetch/Corpus.pm »