lib/FFI/Build.pm

filter: Statementstmt 91.3% 190 / 208 Branchbran 74.1% 46 / 62 Conditioncond 65.2% 15 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 32 / 32 Podpod 100% 14 / 14 totaltotal 85.5% 302 / 353 CC 52 SCAR 22.9
CC
52
Cov
86%
new
22.6
build
15.1
source
9.0
CRAP 9.9
« blib/lib/FFI/Build/MM.pm ↑ Summary blib/lib/FFI/Platypus.pm »
1 package FFI::Build;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use FFI::Build::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use FFI::Build::PluginData qw( plugin_data );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use FFI::Build::File::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use File::Glob ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use List::Util 1.45 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Capture::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: Build shared libraries for use with FFI
17 10 our $VERSION = '2.12'; # VERSION
18  
19 # Platypus-Man,
20 # Platypus-Man,
21 # Friendly Neighborhood Platypus-Man
22 # Is He Strong?
23 # Listen Bud
24 # He's got Proportional Strength of a Platypus
25 # Hey Man!
26 # There Goes The Platypus-Man
27  
28  
29 {
30 10 my $plugins = FFI::Build::Plugin->new;
31 # PLUGIN: require
32 # ARGS: NONE
33 10 $plugins->call('build-require');
34 245 ▶ sub _plugins { $plugins ||= FFI::Build::Plugin->new };
Subroutine _plugins: called CC=2 SCAR=9.2
Condition33%1/3
$plugins ||= "FFI::Build::Plugin"->new
l!l&&r!l&&!r
24500
MC/DC0%0/2
$plugins ||= "FFI::Build::Plugin"->new
$plugins "FFI::Build::Plugin"->new
Truth table33%1/3
A ||= B
A= $pluginsB= "FFI::Build::Plugin"->new
ABresult
1X1
011
000
35 }
36  
37 sub import
38 10 ▶{
Subroutine import: called CC=1 SCAR=0.0
39 13 my @caller = caller;
40 # PLUGIN: import
41 # ARGS: @caller, \@args
42 13 _plugins->call('build-import', \@caller, \@_);
43 }
44  
45 sub _native_name
46 40 ▶{
Subroutine _native_name: called CC=1 SCAR=0.0
47 40 my($self, $name) = @_;
48 40 join '', $self->platform->library_prefix, $name, scalar $self->platform->library_suffix;
49 }
50  
51 sub new
52 40 ▶{
Subroutine new: called CC=20 SCAR=31.2
Pod documented
53 40 my($class, $name, %args) = @_;
54  
55 40 ▶ Carp::croak "name is required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 40 40
56  
57 # PLUGIN: new-pre
58 # ARGS: $name, \%args
59 40 _plugins->call('build-new-pre', $name, \%args);
60  
61 40 my $self = bless {
62 source => [],
63 cflags_I => [],
64 cflags => [],
65 libs_L => [],
66 libs => [],
67 alien => [],
68 }, $class;
69  
70 40 ▶ my $platform = $self->{platform} = $args{platform} || FFI::Build::Platform->default;
Condition66%2/3
$args{'platform'} || 'FFI::Build::Platform'->default
l!l&&r!l&&!r
20200
MC/DC0%0/2
$args{'platform'} || 'FFI::Build::Platform'->default
$args{'platform'} 'FFI::Build::Platform'->default
Truth table66%2/3
A || B
A= $args{'platform'}B= 'FFI::Build::Platform'->default
ABresult
1X1
011
000
71 40 ▶ my $file = $self->{file} = $args{file} || FFI::Build::File::Library->new([$args{dir} || '.', $self->_native_name($name)], platform => $self->platform);
Condition60%3/5
$args{'file'} || 'FFI::Build::File::Library'->new([$args{'dir'} || '.', $self->_native_name($name)], 'platform', $self->platform)
l!l&&r!l&&!r
0400
$args{'dir'} || '.'
l!l
355
MC/DC0%0/2
$args{'file'} || 'FFI::Build::File::Library'->new([$args{'dir'} || '.', $self->_native_name($name)], 'platform', $self->platform)
$args{'file'} 'FFI::Build::File::Library'->new([$args{'dir'} || '.', $self->_native_name($name)], 'platform', $self->platform)
MC/DC100%1/1
$args{'dir'} || '.'
$args{'dir'}
Truth table33%1/3
A || B
A= $args{'file'}B= 'FFI::Build::File::Library'->new([$args{'dir'} || '.', $self->_native_name($name)], 'platform', $self->platform)
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{'dir'}
Aresult
11
00
72 40 ▶ my $buildname = $self->{buildname} = $args{buildname} || '_build';
Condition100%2/2
$args{'buildname'} || '_build'
l!l
634
MC/DC100%1/1
$args{'buildname'} || '_build'
$args{'buildname'}
Truth table100%2/2
A
A= $args{'buildname'}
Aresult
11
00
73 40 ▶ my $verbose = $self->{verbose} = $args{verbose} || 0;
Condition100%2/2
$args{'verbose'} || 0
l!l
391
MC/DC100%1/1
$args{'verbose'} || 0
$args{'verbose'}
Truth table100%2/2
A
A= $args{'verbose'}
Aresult
11
00
74 40 ▶ my $export = $self->{export} = $args{export} || [];
Condition100%2/2
$args{'export'} || []
l!l
1426
MC/DC100%1/1
$args{'export'} || []
$args{'export'}
Truth table100%2/2
A
A= $args{'export'}
Aresult
11
00
75  
76 40 ▶ $self->{verbose} = $verbose = 2 if $ENV{V};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"V"} 0 40 40
77  
78 40 ▶ if(defined $args{cflags})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"cflags"}) 4 36 40
79 {
80 4 ▶ my @flags = ref $args{cflags} ? @{ $args{cflags} } : $self->platform->shellwords($args{cflags});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'cflags'} ? : 1 3 4
81 4 push @{ $self->{cflags} }, grep !/^-I/, @flags;
82 4 push @{ $self->{cflags_I} }, grep /^-I/, @flags;
83 }
84  
85 40 ▶ if(defined $args{libs})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"libs"}) 0 40 40
86 {
87 0 ▶ my @flags = ref $args{libs} ? @{ $args{libs} } : $self->platform->shellwords($args{libs});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'libs'} ? : 0 0 0
88 0 push @{ $self->{libs} }, grep !/^-L/, @flags;
89 0 push @{ $self->{libs_L} }, grep /^-L/, @flags;
90 }
91  
92 40 ▶ if(defined $args{alien})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"alien"}) 10 30 40
93 {
94 10 ▶ my @aliens = ref $args{alien} ? @{ $args{alien} } : ($args{alien});
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'alien'} ? : 10 0 10
95 10 foreach my $alien (@aliens)
96 {
97 2 ▶ unless(eval { $alien->can('cflags') && $alien->can('libs') })
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $alien->can("cflags") and $alien->can("libs") } }) 1 1 2
Condition66%2/3
$alien->can('cflags') && $alien->can('libs')
!ll&&!rl&&r
101
MC/DC50%1/2
$alien->can('cflags') && $alien->can('libs')
$alien->can('cflags') $alien->can('libs')
Truth table66%2/3
A && B
A= $alien->can('cflags')B= $alien->can('libs')
ABresult
0X0
100
111
98 {
99 1 my $pm = "$alien.pm";
100 1 $pm =~ s/::/\//g;
101 1 require $pm;
102 }
103 2 push @{ $self->{alien} }, $alien;
104 2 push @{ $self->{cflags} }, grep !/^-I/, $self->platform->shellwords($alien->cflags);
105 2 push @{ $self->{cflags_I} }, grep /^-I/, $self->platform->shellwords($alien->cflags);
106 2 push @{ $self->{libs} }, grep !/^-L/, $self->platform->shellwords($alien->libs);
107 2 push @{ $self->{libs_L} }, grep /^-L/, $self->platform->shellwords($alien->libs);
108 }
109 }
110  
111 40 ▶ $self->source(ref $args{source} ? @{ $args{source} } : ($args{source})) if $args{source};
Branch: 2 branches
BranchTrueFalseTotal
if $args{"source"} 25 15 40
ref $args{'source'} ? : 23 2 25
112  
113 # PLUGIN: new-post
114 # ARGS: $self
115 40 _plugins->call('build-new-post', $self);
116  
117 40 $self;
118 }
119  
120  
121 83 ▶sub buildname { shift->{buildname} }
Subroutine buildname: called CC=1 SCAR=0.0
Pod documented
122 21 ▶sub export { shift->{export} }
Subroutine export: called CC=1 SCAR=0.0
Pod documented
123 133 ▶sub file { shift->{file} }
Subroutine file: called CC=1 SCAR=0.0
Pod documented
124 347 ▶sub platform { shift->{platform} }
Subroutine platform: called CC=1 SCAR=0.0
Pod documented
125 132 ▶sub verbose { shift->{verbose} }
Subroutine verbose: called CC=1 SCAR=0.0
Pod documented
126 67 ▶sub cflags { shift->{cflags} }
Subroutine cflags: called CC=1 SCAR=0.0
Pod documented
127 65 ▶sub cflags_I { shift->{cflags_I} }
Subroutine cflags_I: called CC=1 SCAR=0.0
Pod documented
128 21 ▶sub libs { shift->{libs} }
Subroutine libs: called CC=1 SCAR=0.0
Pod documented
129 21 ▶sub libs_L { shift->{libs_L} }
Subroutine libs_L: called CC=1 SCAR=0.0
Pod documented
130 9 ▶sub alien { shift->{alien} }
Subroutine alien: called CC=1 SCAR=0.0
Pod documented
131  
132 10 my @file_classes;
133 sub _file_classes
134 106 ▶{
Subroutine _file_classes: called CC=6 SCAR=18.0
135 106 ▶ unless(@file_classes)
Branch: 1 branch
BranchTrueFalseTotal
unless (@file_classes) 7 99 106
136 {
137 7 ▶ if(defined $FFI::Build::VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $FFI::Build::VERSION) { } 7 0 7
138 {
139 7 foreach my $inc (@INC)
140 {
141 push @file_classes,
142 68 map { my $f = $_; $f =~ s/\.pm$//; "FFI::Build::File::$f" }
143 grep !/^Base\.pm$/,
144 58 map { File::Basename::basename($_) }
145 File::Glob::bsd_glob(
146 File::Spec->catfile($inc, 'FFI', 'Build', 'File', '*.pm')
147 );
148 }
149 }
150 else
151 {
152 # When building out of git without dzil, $VERSION will not
153 # usually be defined and any file plugins that require a
154 # specific version will break, so we only use core file
155 # classes for that.
156 0 push @file_classes, map { "FFI::Build::File::$_" } qw( C CXX Library Object );
157 }
158  
159 # also anything already loaded, that might not be in the
160 # @INC path (for testing ususally)
161 push @file_classes,
162 7 map { my $f = $_; $f =~ s/::$//; "FFI::Build::File::$f" }
163 grep !/Base::/,
164 grep /::$/,
165 keys %{FFI::Build::File::};
166  
167 7 @file_classes = List::Util::uniq(@file_classes);
168 7 foreach my $class (@file_classes)
169 {
170 30 ▶ next if(eval { $class->can('new') });
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $class->can("new") } } 20 10 30
171 10 my $pm = $class . ".pm";
172 10 $pm =~ s/::/\//g;
173 10 require $pm;
174 }
175 }
176 106 @file_classes;
177 }
178  
179  
180 sub source
181 73 ▶{
Subroutine source: called CC=9 SCAR=22.0
Pod documented
182 73 my($self, @file_spec) = @_;
183  
184 73 foreach my $file_spec (@file_spec)
185 {
186 64 ▶ if(eval { $file_spec->isa('FFI::Build::File::Base') })
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $file_spec->isa("FFI::Build::File::Base") } }) 7 57 64
187 {
188 7 push @{ $self->{source} }, $file_spec;
189 7 next;
190 }
191 57 ▶ if(ref $file_spec eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file_spec eq "ARRAY") 5 52 57
192 {
193 5 my($type, $content, @args) = @$file_spec;
194 5 my $class = "FFI::Build::File::$type";
195 5 ▶ unless($class->can('new'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 1 4 5
196 {
197 1 my $pm = "FFI/Build/File/$type.pm";
198 1 require $pm;
199 }
200 5 push @{ $self->{source} }, $class->new(
201 $content,
202 build => $self,
203 platform => $self->platform,
204 @args
205 );
206 5 next;
207 }
208 52 my @paths = File::Glob::bsd_glob($file_spec);
209 path:
210 52 foreach my $path (@paths)
211 {
212 105 foreach my $class (_file_classes)
213 {
214 107 foreach my $regex ($class->accept_suffix)
215 {
216 107 ▶ if($path =~ $regex)
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$regex/) 105 2 107
217 {
218 105 push @{ $self->{source} }, $class->new($path, platform => $self->platform, build => $self);
219 105 next path;
220 }
221 }
222 }
223 0 Carp::croak("Unknown file type: $path");
224 }
225 }
226  
227 73 @{ $self->{source} };
228 }
229  
230  
231 sub build
232 21 ▶{
Subroutine build: called CC=12 SCAR=27.2
Pod documented
233 21 my($self) = @_;
234  
235 # PLUGIN: build
236 # ARGS: $self
237 21 _plugins->call('build-build', $self);
238  
239 21 my @objects;
240  
241 21 my $ld = $self->platform->ld;
242  
243 21 foreach my $source ($self->source)
244 {
245 # PLUGIN: build-item
246 # ARGS: $self, $source
247 59 _plugins->call('build-build-item', $self, $source);
248  
249 59 ▶ if($source->can('build_all'))
Branch: 1 branch
BranchTrueFalseTotal
if ($source->can("build_all")) 0 59 59
250 {
251 0 my $count = scalar $self->source;
252 0 ▶ if($count == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 0 0 0
253 {
254 0 return $source->build_all($self->file);
255 }
256 else
257 {
258 0 die "@{[ ref $source ]} has build_all method, but there is not exactly one source";
259 }
260 }
261  
262 59 ▶ $ld = $source->ld if $source->ld;
Branch: 1 branch
BranchTrueFalseTotal
if $source->ld 2 57 59
263 59 my $output;
264 59 ▶ while(my $next = $source->build_item)
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = $source->build_item) 59 59 118
265 {
266 59 ▶ $ld = $next->ld if $next->ld;
Branch: 1 branch
BranchTrueFalseTotal
if $next->ld 0 59 59
267 59 $output = $source = $next;
268 }
269 59 push @objects, $output;
270 }
271  
272 21 ▶ my $needs_rebuild = sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
273 21 my(@objects) = @_;
274 21 ▶ return 1 unless -f $self->file->path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->file->path 20 1 21
275 1 my $target_time = [stat $self->file->path]->[9];
276 1 foreach my $object (@objects)
277 {
278 1 my $object_time = [stat "$object"]->[9];
279 1 ▶ return 1 if $object_time > $target_time;
Branch: 1 branch
BranchTrueFalseTotal
if $object_time > $target_time 1 0 1
280 }
281 0 return 0;
282 21 };
283  
284 21 ▶ return $self->file unless $needs_rebuild->(@objects);
Branch: 1 branch
BranchTrueFalseTotal
unless &$needs_rebuild(@objects) 0 21 21
285  
286 21 File::Path::mkpath($self->file->dirname, 0, oct(755));
287  
288 my @cmd = (
289 $ld,
290 $self->libs_L,
291 $self->platform->ldflags,
292 59 (map { "$_" } @objects),
293 $self->libs,
294 21 $self->platform->flag_export(@{ $self->export }),
295 $self->platform->flag_library_output($self->file->path),
296 );
297  
298 # PLUGIN: build-link
299 # ARGS: $self, \@cmd
300 21 _plugins->call('build-build-link', $self, \@cmd);
301  
302 21 ▶ my($out, $exit) = Capture::Tiny::capture_merged(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
303 21 $self->platform->run(@cmd);
304 21 });
305  
306 21 ▶ if($exit || !-f $self->file->path)
Branch: 3 branches
BranchTrueFalseTotal
if ($exit or not -f $self->file->path) { } 0 21 21
elsif ($self->verbose >= 2) { } 12 9 21
elsif ($self->verbose >= 1) { } 9 0 9
Condition33%1/3
$exit or not -f $self->file->path
l!l&&r!l&&!r
0021
MC/DC0%0/2
$exit or not -f $self->file->path
$exit not -f $self->file->path
Truth table33%1/3
A or not(B)
A= $exitB= not -f $self->file->path
ABresult
1X1
011
000
307 {
308 0 print $out;
309 0 die "error building @{[ $self->file->path ]} from @objects";
310 }
311 elsif($self->verbose >= 2)
312 {
313 12 print $out;
314 }
315 elsif($self->verbose >= 1)
316 {
317 9 print "LD @{[ $self->file->path ]}\n";
318 }
319  
320 # PLUGIN: link-postlink
321 # ARGS: $self, \@cmd
322 21 _plugins->call('build-build-postlink', $self);
323  
324 21 $self->file;
325 }
326  
327  
328 sub clean
329 9 ▶{
Subroutine clean: called CC=5 SCAR=16.1
Pod documented
330 9 my($self) = @_;
331 9 my $dll = $self->file->path;
332 9 ▶ if(-f $dll)
Branch: 1 branch
BranchTrueFalseTotal
if (-f $dll) 9 0 9
333 {
334 # PLUGIN: clean
335 # ARGS: $self, $path
336 9 _plugins->call('build-clean', $self, $dll);
337 9 unlink $dll;
338 }
339 9 foreach my $source ($self->source)
340 {
341 17 my $dir = File::Spec->catdir($source->dirname, $self->buildname);
342 17 ▶ if(-d $dir)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 6 11 17
343 {
344 6 foreach my $path (File::Glob::bsd_glob("$dir/*"))
345 {
346 15 _plugins->call('build-clean', $self, $path);
347 15 unlink $path;
348 }
349 6 _plugins->call('build-clean', $self, $dir);
350 6 rmdir $dir;
351 }
352 }
353 }
354  
355 10 1;
356  
357 __END__
« blib/lib/FFI/Build/MM.pm ↑ Summary blib/lib/FFI/Platypus.pm »