blib/lib/SPVM/Builder/Util.pm

filter: Statementstmt 70.2% 612 / 871 Branchbran 48.0% 149 / 310 Conditioncond 38.9% 30 / 77 MC/DCmcdc 9.3% 4 / 43 Subroutinesub 82.4% 89 / 108 Podpod 0.0% 0 / 84 totaltotal 59.2% 884 / 1493 CC 223 SCAR 22.1
CC
223
Cov
63%
create_make_rule_parallel
26.0
create_make_rule_parallel
26.0
create_make_rule_parallel_libspvm
17.9
CRAP 9.1
« blib/lib/SPVM/Builder/DependencyAnalyzer.pm ↑ Summary blib/lib/SPVM/Global.pm »
1 package SPVM::Builder::Util;
2  
3 # This module is used from Makefile.PL via SPVM::Builder::Util::API
4 # so this class must be wrote as pure perl. Do not contain XS functions.
5  
6 627 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 627 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 627 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 627 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 627 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 627 ▶use Pod::Usage 'pod2usage';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 627 ▶use Getopt::Long 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 627 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 627 ▶use File::Basename 'dirname', 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 627 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 627 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 627 ▶use File::Find 'find';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 627 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 627 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 627 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 627 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 627 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 627 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 627 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 627 ▶use File::Copy 'move';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 627 ▶use SPVM::Builder::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub get_spvm_header_files {
Subroutine get_spvm_header_files: not called CC=2 SCAR=14.8
Pod undocumented
30 0 ▶ my $builder_dir = &get_builder_dir;
Subroutine get_spvm_header_files: not called CC=2 SCAR=14.8
Pod undocumented
31
32 0 my $include_dir = "$builder_dir/include";
33
34 # Get all .h files in the include directory
35 0 ▶ my @spvm_header_files = glob("$include_dir/*.h");
Subroutine BEGIN: called CC=1 SCAR=0.6
36
37 0 ▶ unless (@spvm_header_files) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@spvm_header_files) 0 0 0
unless (@spvm_header_files) 0 0 0
38 0 confess("The SPVM header files are not found in the directory '$include_dir'.");
39 }
40
41 0 return \@spvm_header_files;
42 }
43  
44 0 ▶sub get_spvm_precompile_source_files {
Subroutine get_spvm_precompile_source_files: not called CC=3 SCAR=23.9
Pod undocumented
45 0 ▶ my $builder_dir = &get_builder_dir;
Subroutine get_spvm_precompile_source_files: not called CC=3 SCAR=23.9
Pod undocumented
46
47 0 my $src_dir = "$builder_dir/src";
48
49 # Specify the required source files
50 0 my @filenames = (
51 'spvm_precompile.c',
52 'spvm_string_buffer.c',
53 );
54
55 0 my @spvm_precompile_source_files;
56 0 for my $filename (@filenames) {
57 0 my $file = "$src_dir/$filename";
58 0 ▶ if (-f $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $file) { } 0 0 0
if (-f $file) { } 0 0 0
59 0 push @spvm_precompile_source_files, $file;
60 }
61 else {
62 0 confess("The SPVM precompile source file '$file' is not found.");
63 }
64 }
65
66 0 return \@spvm_precompile_source_files;
67 }
68  
69 81 ▶sub get_spvm_core_source_file_names {
Subroutine get_spvm_core_source_file_names: called CC=1 SCAR=0.0
Pod undocumented
70
71 81 ▶ my @spvm_core_source_file_names = qw(
Subroutine get_spvm_core_source_file_names: not called CC=1 SCAR=0.0
Pod undocumented
72 spvm_allocator.c
73 spvm_allow.c
74 spvm_api_allocator.c
75 spvm_api_arg.c
76 spvm_api_basic_type.c
77 spvm_api.c
78 spvm_api_cfunc.c
79 spvm_api_class_file.c
80 spvm_api_class_var.c
81 spvm_api_compiler.c
82 spvm_api_field.c
83 spvm_api_internal.c
84 spvm_api_method.c
85 spvm_api_mutex.c
86 spvm_api_runtime.c
87 spvm_api_string_buffer.c
88 spvm_api_type.c
89 spvm_array_field_access.c
90 spvm_attribute.c
91 spvm_basic_type.c
92 spvm_block.c
93 spvm_call_method.c
94 spvm_case_info.c
95 spvm_check.c
96 spvm_class_file.c
97 spvm_class_var_access.c
98 spvm_class_var.c
99 spvm_compiler.c
100 spvm_constant.c
101 spvm_dumper.c
102 spvm_field_access.c
103 spvm_field.c
104 spvm_hash.c
105 spvm_interface.c
106 spvm_list.c
107 spvm_method.c
108 spvm_mutex.c
109 spvm_op.c
110 spvm_opcode_builder.c
111 spvm_opcode.c
112 spvm_opcode_list.c
113 spvm_precompile.c
114 spvm_precompile_header_content.c
115 spvm_runtime.c
116 spvm_strerror.c
117 spvm_string_buffer.c
118 spvm_string.c
119 spvm_switch_info.c
120 spvm_toke.c
121 spvm_type.c
122 spvm_use.c
123 spvm_utf8.c
124 spvm_var.c
125 spvm_var_decl.c
126 spvm_version_from.c
127 spvm_vm.c
128 spvm_yacc.c
129 spvm_yacc_util.c
130 );
131  
132 81 return \@spvm_core_source_file_names;
133 }
134  
135 3640 ▶sub slurp_binary {
Subroutine slurp_binary: called CC=2 SCAR=7.1
Pod undocumented
136 3640 ▶ my ($file) = @_;
Subroutine slurp_binary: called CC=2 SCAR=7.1
Pod undocumented
137
138 3640 ▶ open my $fh, '<', $file
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "<", $file or confess("Can't open file '${file}':$!") 3638 2 3640
open my $fh, "<", $file or confess("Can't open file '${file}':$!") 258 3 261
139 or confess("Can't open file '$file':$!");
140
141 0 my $content = do { local $/; <$fh> };
142
143 0 return $content;
144 }
145  
146 203 ▶sub slurp_utf8 {
Subroutine slurp_utf8: called CC=1 SCAR=1.2
Pod undocumented
147 0 ▶ my ($file) = @_;
Subroutine slurp_utf8: not called CC=1 SCAR=1.2
Pod undocumented
148
149 0 my $content = &slurp_binary($file);
150
151 0 $content = decode('UTF-8', $content);
152
153 0 return $content;
154 }
155  
156 203 ▶sub file_contains {
Subroutine file_contains: called CC=2 SCAR=7.5
Pod undocumented
157 0 ▶ my ($file, $string) = @_;
Subroutine file_contains: not called CC=2 SCAR=7.5
Pod undocumented
158
159 0 my $content = &slurp_utf8($file);
160
161 0 my $contains;
162 203 ▶ if (index($content, $string) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($content, $string) != -1) 193 10 203
if (index($content, $string) != -1) 0 0 0
163 193 $contains = 1;
164 }
165
166 203 return $contains;
167 }
168  
169 1413 ▶sub spurt_binary {
Subroutine spurt_binary: called CC=11 SCAR=26.8
Pod undocumented
170 1413 ▶ my ($output_file, $content, $global_lock_fh) = @_;
Subroutine spurt_binary: called CC=11 SCAR=26.8
Pod undocumented
171
172 1413 ▶ if ($global_lock_fh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global_lock_fh) 949 464 1413
if ($global_lock_fh) 333 0 333
173 949 ▶ flock($global_lock_fh, LOCK_EX) or confess "Can't lock global lock file: $!";
Branch: 2 branches
BranchTrueFalseTotal
flock $global_lock_fh, 2 or confess("Can't lock global lock file: $!") 949 0 949
flock $global_lock_fh, 2 or confess("Can't lock global lock file: $!") 333 0 333
174 }
175
176 0 eval {
177 0 my $should_write = 1;
178 0 ▶ if (-f $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $output_file) 73 1340 1413
if (-f $output_file) 0 333 333
179 0 my $old_content = do {
180 0 ▶ open my $read_fh, '<:raw', $output_file or die "Can't open for read '$output_file':$!";
Branch: 2 branches
BranchTrueFalseTotal
open my $read_fh, "<:raw", $output_file or die "Can't open for read '${output_file}':$!" 73 0 73
open my $read_fh, "<:raw", $output_file or die "Can't open for read '${output_file}':$!" 0 0 0
181 0 local $/;
182 73 <$read_fh>;
183 };
184
185 73 ▶ if ($old_content eq $content) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_content eq $content) 27 46 73
if ($old_content eq $content) 0 0 0
186 27 $should_write = 0;
187 }
188 }
189
190 1413 ▶ if ($should_write) {
Branch: 2 branches
BranchTrueFalseTotal
if ($should_write) 1386 27 1413
if ($should_write) 333 0 333
191 1386 my $dir = dirname($output_file);
192 1386 mkpath $dir;
193
194 # Write to a temporary file first
195 1386 my $tmp_file = "$output_file.tmp.$$";
196
197 1386 ▶ open my $output_fh, '>:raw', $tmp_file
Branch: 2 branches
BranchTrueFalseTotal
open my $output_fh, ">:raw", $tmp_file or die "Can't open temporary file '${tmp_file}':$!" 1386 0 1386
open my $output_fh, ">:raw", $tmp_file or die "Can't open temporary file '${tmp_file}':$!" 333 0 333
198 or die "Can't open temporary file '$tmp_file':$!";
199 1386 print $output_fh $content;
200 1386 close $output_fh;
201
202 # Atomically replace the target file
203 1386 ▶ move($tmp_file, $output_file)
Branch: 2 branches
BranchTrueFalseTotal
move($tmp_file, $output_file) or confess("Can't move '${tmp_file}' to '${output_file}':$!") 1386 0 1386
move($tmp_file, $output_file) or confess("Can't move '${tmp_file}' to '${output_file}':$!") 333 0 333
204 or confess("Can't move '$tmp_file' to '$output_file':$!");
205 }
206 };
207  
208 1413 my $error = $@;
209  
210 0 ▶ if ($global_lock_fh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global_lock_fh) 949 464 1413
if ($global_lock_fh) 333 0 333
211 0 flock($global_lock_fh, LOCK_UN);
212 }
213  
214 0 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 1413 1413
if ($error) 0 333 333
215 0 confess $error;
216 }
217 }
218  
219 2 ▶sub unindent {
Subroutine unindent: called CC=3 SCAR=15.8
Pod undocumented
220 0 ▶ my $str = shift;
Subroutine unindent: not called CC=3 SCAR=15.8
Pod undocumented
221 0 ▶ my $min = min map { m/^([ \t]*)/; length $1 || () } split "\n", $str;
Condition33%2/6
length $1 || ()
l!l&&r!l&&!r
4620
length $1 || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
length $1 || ()
length $1 ()
MC/DC0%0/2
length $1 || ()
length $1 ()
Truth table66%2/3
A || B
A= length $1B= ()
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= length $1B= ()
ABresult
1X1
011
000
222 0 ▶ $str =~ s/^[ \t]{0,$min}//gm if $min;
Branch: 2 branches
BranchTrueFalseTotal
if $min 2 0 2
if $min 0 0 0
223 0 return $str;
224 }
225  
226 2 ▶sub extract_usage {
Subroutine extract_usage: called CC=2 SCAR=7.9
Pod undocumented
227 0 ▶ my $file = @_ ? "$_[0]" : (caller)[1];
Subroutine extract_usage: not called CC=2 SCAR=7.9
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 2 2
@_ ? : 0 0 0
228
229 0 open my $handle, '>', \my $output;
230
231 0 pod2usage -exitval => 'noexit', -input => $file, -output => $handle, -verbose => 99, -sections => "Usage";
232 2 $output =~ s/^.*\n|\n$//;
233 2 $output =~ s/\n$//;
234  
235 2 return SPVM::Builder::Util::unindent($output);
236 }
237  
238 135 ▶sub getopt {
Subroutine getopt: called CC=3 SCAR=11.2
Pod undocumented
239 135 ▶ my ($array, $opts) = map { ref $_[0] eq 'ARRAY' ? shift : $_ } \@ARGV, [];
Subroutine getopt: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 270 270
ref $_[0] eq 'ARRAY' ? : 0 2 2
240 135 my $save = Getopt::Long::Configure(qw(default no_auto_abbrev no_ignore_case),
241 @$opts);
242 0 my $success = GetOptionsFromArray $array, @_;
243 135 Getopt::Long::Configure($save);
244
245 135 ▶ unless ($success) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($success) 4 131 135
unless ($success) 0 1 1
246 4 Carp::confess("Failed to parse command line options.");
247 }
248 }
249  
250 1572 ▶sub convert_class_file_to_dynamic_lib_file {
Subroutine convert_class_file_to_dynamic_lib_file: called CC=2 SCAR=7.1
Pod undocumented
251 1572 ▶ my ($class_file, $category) = @_;
Subroutine convert_class_file_to_dynamic_lib_file: called CC=2 SCAR=7.1
Pod undocumented
252
253 1572 my $so = $Config{so};
254 1572 $class_file =~ s/\.[^.]+$//;
255 0 my $dynamic_lib_category_file = $class_file;
256 0 ▶ $dynamic_lib_category_file .= $category eq 'native' ? ".$so" : ".$category.$so";
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'native' ? : 751 821 1572
$category eq 'native' ? : 297 582 879
257
258 0 return $dynamic_lib_category_file;
259 }
260  
261 0 ▶sub convert_class_name_to_dynamic_lib_rel_file {
Subroutine convert_class_name_to_dynamic_lib_rel_file: not called CC=2 SCAR=13.7
Pod undocumented
262 0 ▶ my ($class_name, $category) = @_;
Subroutine convert_class_name_to_dynamic_lib_rel_file: not called CC=2 SCAR=13.7
Pod undocumented
263
264 0 my $so = $Config{so};
265 0 my $dynamic_lib_category_rel_file = &convert_class_name_to_rel_file($class_name);
266 0 ▶ $dynamic_lib_category_rel_file .= $category eq 'native' ? ".$so" : ".$category.$so";
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'native' ? : 0 0 0
$category eq 'native' ? : 0 0 0
267
268 0 return $dynamic_lib_category_rel_file;
269 }
270  
271 1506 ▶sub convert_class_name_to_category_rel_file {
Subroutine convert_class_name_to_category_rel_file: called CC=3 SCAR=11.1
Pod undocumented
272 1506 ▶ my ($class_name, $category, $ext) = @_;
Subroutine convert_class_name_to_category_rel_file: called CC=3 SCAR=11.1
Pod undocumented
273
274 1506 my $rel_file_with_ext = "SPVM::$class_name";
275 1506 $rel_file_with_ext =~ s/::/\//g;
276 1506 ▶ $rel_file_with_ext .= $category eq 'native' ? "" : ".$category";
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'native' ? : 857 649 1506
$category eq 'native' ? : 18 395 413
277 0 ▶ if (defined $ext) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext) 1283 223 1506
if (defined $ext) 354 59 413
278 0 $rel_file_with_ext .= ".$ext";
279 }
280
281 0 return $rel_file_with_ext;
282 }
283  
284 0 ▶sub convert_class_name_to_rel_dir {
Subroutine convert_class_name_to_rel_dir: not called CC=1 SCAR=3.5
Pod undocumented
285 0 ▶ my ($class_name) = @_;
Subroutine convert_class_name_to_rel_dir: not called CC=1 SCAR=3.5
Pod undocumented
286
287 0 my $rel_dir;
288 0 my $rel_file = "SPVM::$class_name";
289 0 $rel_file =~ s/::/\//g;
290 0 $rel_dir = dirname $rel_file;
291
292 0 return $rel_dir;
293 }
294  
295 4240 ▶sub convert_class_name_to_rel_file {
Subroutine convert_class_name_to_rel_file: called CC=2 SCAR=7.0
Pod undocumented
296 4240 ▶ my ($class_name, $ext) = @_;
Subroutine convert_class_name_to_rel_file: called CC=2 SCAR=7.0
Pod undocumented
297
298 4240 my $rel_file_with_ext = "SPVM::$class_name";
299 4240 $rel_file_with_ext =~ s/::/\//g;
300
301 4240 ▶ if (defined $ext) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext) 4240 0 4240
if (defined $ext) 1085 0 1085
302 4240 $rel_file_with_ext .= ".$ext";
303 }
304
305 4240 return $rel_file_with_ext;
306 }
307  
308 255 ▶sub get_class_base_dir {
Subroutine get_class_base_dir: called CC=1 SCAR=0.0
Pod undocumented
309 255 ▶ my ($file, $class_name) = @_;
Subroutine get_class_base_dir: called CC=1 SCAR=0.0
Pod undocumented
310
311 255 $file =~ s/\.spvm$//;
312 255 my $class_file = "SPVM::$class_name";
313 255 $class_file =~ s/::/\//g;
314 255 $file =~ s/$class_file$//;
315 255 $file =~ s/[\\\/]$//;
316
317 255 return $file;
318 }
319  
320 0 ▶sub create_make_rule_native {
Subroutine create_make_rule_native: not called CC=1 SCAR=1.2
Pod undocumented
321 0 ▶ my $class_name = shift;
Subroutine create_make_rule_native: called CC=1 SCAR=1.2
Pod undocumented
322
323 0 create_make_rule($class_name, 'native', @_);
324 }
325  
326 0 ▶sub create_make_rule_precompile {
Subroutine create_make_rule_precompile: not called CC=1 SCAR=1.2
Pod undocumented
327 0 ▶ my $class_name = shift;
Subroutine create_make_rule_precompile: called CC=1 SCAR=1.2
Pod undocumented
328
329 0 create_make_rule($class_name, 'precompile', @_);
330 }
331  
332 324320 ▶sub create_make_rule {
Subroutine create_make_rule: called CC=4 SCAR=19.7
Pod undocumented
333 0 ▶ my ($class_name, $category, $options) = @_;
Subroutine create_make_rule: called CC=4 SCAR=19.7
Pod undocumented
334  
335 0 ▶ $options ||= {};
Condition25%1/4
$options ||= {}
l!l
00
$options ||= {}
l!l
20
MC/DC0%0/1
$options ||= {}
$options
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
Truth table50%1/2
A
A= $options
Aresult
11
00
336  
337 0 ▶ if ($category eq 'native') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'native') { } 0 0 0
elsif ($category eq 'precompile') { } 0 0 0
if ($category eq 'native') { } 1 1 2
elsif ($category eq 'precompile') { } 1 0 1
338 0 $options->{native_classes} = [$class_name];
339 }
340 elsif ($category eq 'precompile') {
341 0 $options->{precompile_classes} = [$class_name];
342 }
343  
344 0 return create_make_rule_parallel($options);
345 }
346  
347 10 ▶sub create_make_rule_parallel {
Subroutine create_make_rule_parallel: called CC=20 SCAR=32.6
Pod undocumented
348 10 ▶ my ($options) = @_;
Subroutine create_make_rule_parallel: called CC=20 SCAR=32.6
Pod undocumented
349
350 10 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
100
$options ||= {}
l!l
20
MC/DC0%0/1
$options ||= {}
$options
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
Truth table50%1/2
A
A= $options
Aresult
11
00
351
352 10 my $make_rule = '';
353
354 # Gen target name
355 10 my @target_parts;
356
357 # Target ID includes filenames for unique SHA1
358 10 ▶ if (my $precompile_classes = $options->{precompile_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes = $options->{"precompile_classes"}) 10 0 10
if (my $precompile_classes = $options->{"precompile_classes"}) 1 1 2
359 0 push @target_parts, map { "precompile|$_" } @$precompile_classes;
360 }
361 10 ▶ if (my $precompile_classes_file = $options->{precompile_classes_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes_file = $options->{"precompile_classes_file"}) 0 10 10
if (my $precompile_classes_file = $options->{"precompile_classes_file"}) 0 2 2
362 0 push @target_parts, "precompile_file|$precompile_classes_file";
363 }
364 10 ▶ if (my $native_classes = $options->{native_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes = $options->{"native_classes"}) 10 0 10
if (my $native_classes = $options->{"native_classes"}) 1 1 2
365 0 push @target_parts, map { "native|$_" } @$native_classes;
366 }
367 10 ▶ if (my $native_classes_file = $options->{native_classes_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes_file = $options->{"native_classes_file"}) 0 10 10
if (my $native_classes_file = $options->{"native_classes_file"}) 0 2 2
368 0 push @target_parts, "native_file|$native_classes_file";
369 }
370
371 # Create SHA1 hex digest
372 10 my $target_id = sha1_hex(join("\n", sort @target_parts));
373 10 my $target = "spvm-build-parallel-$target_id";
374
375 # Order-only deps
376 10 ▶ my $dependent_files = $options->{dependent_files} // [];
Condition50%2/4
$options->{"dependent_files"} // []
l!l
010
$options->{"dependent_files"} // []
l!l
02
MC/DC0%0/1
$options->{"dependent_files"} // []
$options->{"dependent_files"}
MC/DC0%0/1
$options->{"dependent_files"} // []
$options->{"dependent_files"}
Truth table50%1/2
A
A= $options->{"dependent_files"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"dependent_files"}
Aresult
11
00
377 10 ▶ my $dependent_files_string = @$dependent_files ? " " . join(' ', @$dependent_files) : "";
Branch: 2 branches
BranchTrueFalseTotal
@$dependent_files ? : 0 10 10
@$dependent_files ? : 0 2 2
378
379 10 ▶ my $order_only_dependent_files = $options->{order_only_dependent_files} // [];
Condition50%2/4
$options->{"order_only_dependent_files"} // []
l!l
100
$options->{"order_only_dependent_files"} // []
l!l
02
MC/DC0%0/1
$options->{"order_only_dependent_files"} // []
$options->{"order_only_dependent_files"}
MC/DC0%0/1
$options->{"order_only_dependent_files"} // []
$options->{"order_only_dependent_files"}
Truth table50%1/2
A
A= $options->{"order_only_dependent_files"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"order_only_dependent_files"}
Aresult
11
00
380 10 ▶ my $order_only_dependent_files_string = @$order_only_dependent_files ? " | " . join(' ', @$order_only_dependent_files) : "";
Branch: 2 branches
BranchTrueFalseTotal
@$order_only_dependent_files ? : 10 0 10
@$order_only_dependent_files ? : 0 2 2
381
382 # Dynamic target
383 10 $make_rule .= "dynamic :: $target\n";
384 10 $make_rule .= "\t\$(NOECHO) \$(NOOP)\n\n";
385
386 # Parallel build rule
387 10 $make_rule .= ".PHONY: $target\n";
388 10 $make_rule .= "$target :$dependent_files_string$order_only_dependent_files_string\n";
389
390 # Collect all build options
391 10 my @build_options;
392
393 10 ▶ my $build_dir = $options->{build_dir} // '.spvm_build';
Condition50%2/4
$options->{"build_dir"} // ".spvm_build"
l!l
010
$options->{"build_dir"} // ".spvm_build"
l!l
02
MC/DC0%0/1
$options->{"build_dir"} // ".spvm_build"
$options->{"build_dir"}
MC/DC0%0/1
$options->{"build_dir"} // ".spvm_build"
$options->{"build_dir"}
Truth table50%1/2
A
A= $options->{"build_dir"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"build_dir"}
Aresult
11
00
394 10 push @build_options, "build_dir => '$build_dir'";
395
396 0 ▶ my $output_dir = $options->{output_dir} // 'blib/lib';
Condition75%3/4
$options->{"output_dir"} // "blib/lib"
l!l
28
$options->{"output_dir"} // "blib/lib"
l!l
02
MC/DC100%1/1
$options->{"output_dir"} // "blib/lib"
$options->{"output_dir"}
MC/DC0%0/1
$options->{"output_dir"} // "blib/lib"
$options->{"output_dir"}
Truth table100%2/2
A
A= $options->{"output_dir"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"output_dir"}
Aresult
11
00
397 10 push @build_options, "output_dir => '$output_dir'";
398
399 0 ▶ if (defined(my $jobs = $options->{jobs})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $jobs = $options->{"jobs"})) 0 10 10
if (defined(my $jobs = $options->{"jobs"})) 0 2 2
400 0 push @build_options, "jobs => $jobs";
401 }
402
403 0 ▶ if (defined(my $config_global_file = $options->{config_global_file})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $config_global_file = $options->{"config_global_file"})) 10 0 10
if (defined(my $config_global_file = $options->{"config_global_file"})) 0 2 2
404 10 push @build_options, "config_global_file => '$config_global_file'";
405 }
406
407 0 ▶ if (defined(my $native_classes_file = $options->{native_classes_file})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $native_classes_file = $options->{"native_classes_file"})) 0 10 10
if (defined(my $native_classes_file = $options->{"native_classes_file"})) 0 2 2
408 0 push @build_options, "native_classes_file => '$native_classes_file'";
409 }
410 10 ▶ if (defined(my $precompile_classes_file = $options->{precompile_classes_file})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $precompile_classes_file = $options->{"precompile_classes_file"})) 0 10 10
if (defined(my $precompile_classes_file = $options->{"precompile_classes_file"})) 0 2 2
411 0 push @build_options, "precompile_classes_file => '$precompile_classes_file'";
412 }
413
414 10 ▶ if (my $native_classes = $options->{native_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes = $options->{"native_classes"}) 10 0 10
if (my $native_classes = $options->{"native_classes"}) 1 1 2
415 10 push @build_options, "native_classes => [" . join(', ', map { "'$_'" } @$native_classes) . "]";
416 }
417 10 ▶ if (my $precompile_classes = $options->{precompile_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes = $options->{"precompile_classes"}) 10 0 10
if (my $precompile_classes = $options->{"precompile_classes"}) 1 1 2
418 10 push @build_options, "precompile_classes => [" . join(', ', map { "'$_'" } @$precompile_classes) . "]";
419 }
420
421 10 my $build_options_hash_str = "{" . join(', ', @build_options) . "}";
422
423 # Build @INC string for -I
424 0 ▶ my $inc = $options->{class_search_dir} // [];
Condition75%3/4
$options->{"class_search_dir"} // []
l!l
28
$options->{"class_search_dir"} // []
l!l
02
MC/DC100%1/1
$options->{"class_search_dir"} // []
$options->{"class_search_dir"}
MC/DC0%0/1
$options->{"class_search_dir"} // []
$options->{"class_search_dir"}
Truth table100%2/2
A
A= $options->{"class_search_dir"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"class_search_dir"}
Aresult
11
00
425 0 my $inc_string = '';
426 10 ▶ if (@$inc) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$inc) 2 8 10
if (@$inc) 0 2 2
427 2 $inc_string = join(' ', map { "-I$_" } @$inc) . ' ';
428 }
429
430 # Build cmd
431 10 $make_rule .= "\t\$(FULLPERLRUN) ${inc_string} -Mblib -MSPVM::Builder::API -e \"SPVM::Builder::API::build_parallel_dynamic_lib_dist($build_options_hash_str)\"\n\n";
432
433 10 return $make_rule;
434 }
435  
436 2133 ▶sub search_config_file {
Subroutine search_config_file: called CC=4 SCAR=13.9
Pod undocumented
437 0 ▶ my ($class_name, $mode) = @_;
Subroutine search_config_file: called CC=4 SCAR=13.9
Pod undocumented
438
439 2133 my $ext = SPVM::Builder::Config::Util::get_config_ext();
440 2133 ▶ if (defined $mode) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $mode) 0 2133 2133
if (defined $mode) 0 38 38
441 0 $ext = "$mode.$ext";
442 }
443
444 2133 my $config_file_base = SPVM::Builder::Util::convert_class_name_to_rel_file($class_name, $ext);
445 2133 my $config_file;
446 2133 for my $inc (@INC) {
447 31795 my $config_file_tmp = "$inc/$config_file_base";
448 31795 ▶ if (my $found_config_file = SPVM::Builder::Config::Util::exists_config_file($config_file_tmp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $found_config_file = SPVM::Builder::Config::Util::exists_config_file($config_file_tmp)) 322 31473 31795
if (my $found_config_file = SPVM::Builder::Config::Util::exists_config_file($config_file_tmp)) 17 252 269
449 322 $config_file = $found_config_file;
450 322 last;
451 }
452 }
453
454 2133 return $config_file;
455 }
456  
457 1594 ▶sub search_spvm_file {
Subroutine search_spvm_file: called CC=3 SCAR=11.0
Pod undocumented
458 1594 ▶ my ($class_name) = @_;
Subroutine search_spvm_file: called CC=3 SCAR=11.0
Pod undocumented
459
460 1594 my $ext = 'spvm';
461
462 1594 my $spvm_file_base = SPVM::Builder::Util::convert_class_name_to_rel_file($class_name, $ext);
463 1594 my $spvm_file;
464 1594 for my $inc (@INC) {
465 6201 my $spvm_file_tmp = "$inc/$spvm_file_base";
466 6201 ▶ if (-f $spvm_file_tmp) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $spvm_file_tmp) 1594 4607 6201
if (-f $spvm_file_tmp) 879 2742 3621
467 1594 $spvm_file = $spvm_file_tmp;
468 1594 last;
469 }
470 }
471
472 1594 return $spvm_file;
473 }
474  
475 4086 ▶sub get_builder_dir {
Subroutine get_builder_dir: called CC=1 SCAR=0.0
Pod undocumented
476 4058 ▶ my $builder_config_dir = $INC{"SPVM/Builder/Config.pm"};
Subroutine get_builder_dir: called CC=1 SCAR=0.0
Pod undocumented
477 4058 my $builder_dir = $builder_config_dir;
478 4058 $builder_dir =~ s/\/Config\.pm$//;
479 4058 return $builder_dir;
480 }
481  
482 1572 ▶sub get_dynamic_lib_file_dist {
Subroutine get_dynamic_lib_file_dist: called CC=1 SCAR=0.0
Pod undocumented
483 1572 ▶ my ($class_file, $category) = @_;
Subroutine get_dynamic_lib_file_dist: called CC=1 SCAR=0.0
Pod undocumented
484  
485 1572 my $dynamic_lib_file = SPVM::Builder::Util::convert_class_file_to_dynamic_lib_file($class_file, $category);
486
487 1572 return $dynamic_lib_file;
488 }
489  
490 2480 ▶sub create_default_config {
Subroutine create_default_config: called CC=1 SCAR=0.0
Pod undocumented
491
492 2190 ▶ my $config = SPVM::Builder::Config->new_c99;
Subroutine create_default_config: called CC=1 SCAR=0.0
Pod undocumented
493
494 2190 return $config;
495 }
496  
497 9591 ▶sub get_normalized_env {
Subroutine get_normalized_env: called CC=3 SCAR=13.4
Pod undocumented
498 0 ▶ my ($name) = @_;
Subroutine get_normalized_env: called CC=3 SCAR=13.4
Pod undocumented
499
500 9301 my $value = $ENV{$name};
501
502 0 ▶ if (defined $value && !length $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and not length $value) 0 9301 9301
if (defined $value and not length $value) 0 672 672
Condition66%4/6
defined $value and not length $value
!ll&&!rl&&r
626230390
defined $value and not length $value
!ll&&!rl&&r
4622100
MC/DC0%0/2
defined $value and not length $value
defined $value not length $value
MC/DC0%0/2
defined $value and not length $value
defined $value not length $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
503 0 $value = undef;
504 }
505
506 0 return $value;
507 }
508  
509 290 ▶sub get_version_string {
Subroutine get_version_string: called CC=4 SCAR=27.7
Pod undocumented
510 0 ▶ my ($spvm_class_file) = @_;
Subroutine get_version_string: not called CC=4 SCAR=27.7
Pod undocumented
511
512 0 ▶ open my $spvm_module_fh, '<', $spvm_class_file or die "Can't open the file '$spvm_class_file': $!";
Branch: 2 branches
BranchTrueFalseTotal
open my $spvm_module_fh, "<", $spvm_class_file or die "Can't open the file '${spvm_class_file}': $!" 0 0 0
open my $spvm_module_fh, "<", $spvm_class_file or die "Can't open the file '${spvm_class_file}': $!" 0 0 0
513 0 local $/;
514 0 my $content = <$spvm_module_fh>;
515 0 my $version_string;
516 0 ▶ if ($content =~ /\bversion\s*"([\d\._]+)"\s*;/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /\bversion\s*"([\d\._]+)"\s*;/) 0 0 0
if ($content =~ /\bversion\s*"([\d\._]+)"\s*;/) 0 0 0
517 0 $version_string = $1;
518 }
519  
520 0 ▶ unless (defined $version_string) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $version_string) 0 0 0
unless (defined $version_string) 0 0 0
521 0 confess("The version string can't be find in $spvm_class_file file");
522 }
523
524 0 return $version_string;
525 }
526  
527 290 ▶sub get_spvm_version_string {
Subroutine get_spvm_version_string: called CC=4 SCAR=20.5
Pod undocumented
528
529 0 ▶ my $builder_dir = &get_builder_dir;
Subroutine get_spvm_version_string: called CC=4 SCAR=20.5
Pod undocumented
530 0 my $spvm_native_header_file = "$builder_dir/include/spvm_native.h";
531
532 0 ▶ open my $spvm_module_fh, '<', $spvm_native_header_file
Branch: 2 branches
BranchTrueFalseTotal
open my $spvm_module_fh, "<", $spvm_native_header_file or die "Can't open the file '${spvm_native_header_file}': $!" 0 0 0
open my $spvm_module_fh, "<", $spvm_native_header_file or die "Can't open the file '${spvm_native_header_file}': $!" 1 0 1
533 or die "Can't open the file '$spvm_native_header_file': $!";
534 0 local $/;
535 0 my $content = <$spvm_module_fh>;
536 0 my $version_string;
537 0 ▶ if ($content =~ /#define\s+SPVM_NATIVE_VERSION_NUMBER\s* ([\d\._]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ /#define\s+SPVM_NATIVE_VERSION_NUMBER\s* ([\d\._]+)/) 0 0 0
if ($content =~ /#define\s+SPVM_NATIVE_VERSION_NUMBER\s* ([\d\._]+)/) 1 0 1
538 0 $version_string = $1;
539 }
540
541 0 ▶ unless (defined $version_string) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $version_string) 0 0 0
unless (defined $version_string) 0 1 1
542 0 confess("The version string can't be found in '$spvm_native_header_file'");
543 }
544
545 0 return $version_string;
546 }
547  
548 923 ▶sub create_dl_func_list {
Subroutine create_dl_func_list: called CC=5 SCAR=16.2
Pod undocumented
549 633 ▶ my ($class_name, $method_names, $options) = @_;
Subroutine create_dl_func_list: called CC=5 SCAR=16.2
Pod undocumented
550
551 633 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
6330
$options ||= {}
l!l
2770
MC/DC0%0/1
$options ||= {}
$options
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
Truth table50%1/2
A
A= $options
Aresult
11
00
552
553 633 ▶ my $category = $options->{category} || '';
Condition50%2/4
$options->{'category'} || ''
l!l
6330
$options->{'category'} || ''
l!l
2770
MC/DC0%0/1
$options->{'category'} || ''
$options->{'category'}
MC/DC0%0/1
$options->{'category'} || ''
$options->{'category'}
Truth table50%1/2
A
A= $options->{'category'}
Aresult
11
00
Truth table50%1/2
A
A= $options->{'category'}
Aresult
11
00
554
555 # dl_func_list
556 # This option is needed Windows DLL file
557 633 my $dl_func_list = [];
558 633 for my $method_name (@$method_names) {
559 3083 my $cfunc_name = SPVM::Builder::Util::create_cfunc_name($class_name, $method_name, $category);
560 3083 push @$dl_func_list, $cfunc_name;
561 }
562
563 # This is bad hack to suppress boot strap function error.
564 633 ▶ unless (@$dl_func_list) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@$dl_func_list) 37 596 633
unless (@$dl_func_list) 15 262 277
565 37 push @$dl_func_list, '';
566 }
567
568 633 return $dl_func_list;
569 }
570  
571 13416 ▶sub create_cfunc_name {
Subroutine create_cfunc_name: called CC=3 SCAR=11.0
Pod undocumented
572 13126 ▶ my ($class_name, $method_name, $category) = @_;
Subroutine create_cfunc_name: called CC=3 SCAR=11.0
Pod undocumented
573
574 13126 my $prefix;
575 13126 ▶ if ($category eq 'native') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'native') { } 6185 6941 13126
elsif ($category eq 'precompile') { } 6941 0 6941
if ($category eq 'native') { } 5582 7336 12918
elsif ($category eq 'precompile') { } 7336 0 7336
576 6185 $prefix = 'SPVM__';
577 }
578 elsif ($category eq 'precompile') {
579 6941 $prefix = 'SPVMPRECOMPILE__'
580 }
581
582 # Precompile Method names
583 13126 my $method_abs_name_under_score = "${class_name}::$method_name";
584 13126 $method_abs_name_under_score =~ s/:/_/g;
585 13126 my $cfunc_name = "$prefix$method_abs_name_under_score";
586
587 13126 return $cfunc_name;
588 }
589  
590 386 ▶sub parse_lib_directive {
Subroutine parse_lib_directive: called CC=2 SCAR=8.0
Pod undocumented
591 0 ▶ my ($source, $bin) = @_;
Subroutine parse_lib_directive: called CC=2 SCAR=8.0
Pod undocumented
592
593 0 my $class_search_dirs = [];
594
595 0 ▶ while ($source =~ /^#lib "([^"]+)"/gm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source =~ /^#lib "([^"]+)"/gm) 3 96 99
if ($source =~ /^#lib "([^"]+)"/gm) 0 1 1
596 3 my $class_search_dir = $1;
597
598 0 $class_search_dir =~ s/\$FindBin::Bin/$bin/g;
599
600 0 unshift @$class_search_dirs, $class_search_dir;
601 }
602
603 0 return $class_search_dirs;
604 }
605  
606 # Create C string literal for MSVC/TCC compatibility
607 627 my $CREATE_C_STRING_LITERAL_HEX_TABLE = [map { sprintf("0x%02x", $_) } 0..255];
608 290 ▶sub create_c_string_literal {
Subroutine create_c_string_literal: called CC=2 SCAR=17.9
Pod undocumented
609 0 ▶ my ($content) = @_;
Subroutine create_c_string_literal: not called CC=2 SCAR=17.9
Pod undocumented
610
611 0 ▶ return 'NULL' unless defined $content;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $content 0 0 0
unless defined $content 0 0 0
612  
613 # write brief comment in English
614 # Use the pre-calculated table to convert bytes to hex values
615 0 my $hex_table = $CREATE_C_STRING_LITERAL_HEX_TABLE;
616 0 my @escaped = map { $hex_table->[$_] } unpack('C*', $content);
617  
618 # write brief comment in English
619 # Return as a C99 compound literal cast to (const char[])
620 # Includes a null terminator at the end
621 0 return '(const char[]){' . join(',', @escaped) . ',0}';
622 }
623  
624 290 ▶sub build_precompile_header_content_c_source {
Subroutine build_precompile_header_content_c_source: called CC=3 SCAR=24.1
Pod undocumented
625 0 ▶ my ($options) = @_;
Subroutine build_precompile_header_content_c_source: not called CC=3 SCAR=24.1
Pod undocumented
626
627 0 my $builder_dir = &get_builder_dir;
628 0 my $include_dir = "$builder_dir/include";
629 0 my $src_dir = "$builder_dir/src";
630
631 0 my $spvm_native_h = "$include_dir/spvm_native.h";
632 0 my $spvm_implement_h = "$include_dir/spvm_implement.h";
633 0 my $output_file = "$src_dir/spvm_precompile_header_content.c";
634
635 # Minimal definitions for standard types and functions
636 0 my $header_content = <<'EOS';
637 /* Minimal definitions for standard types and functions */
638 #define NULL ((void*)0)
639 struct _iobuf;
640 typedef struct _iobuf FILE;
641  
642 typedef signed char int8_t;
643 typedef short int16_t;
644 typedef int int32_t;
645 typedef unsigned char uint8_t;
646 typedef unsigned short uint16_t;
647 typedef unsigned int uint32_t;
648  
649 #if defined(__LP64__)
650 /* LP64 System (Linux, macOS, etc.) */
651 typedef long int64_t;
652 typedef unsigned long uint64_t;
653 typedef long intptr_t;
654 typedef unsigned long size_t;
655 #define PRId64 "ld"
656 #else
657 /* LLP64 System (Windows 64-bit) */
658 typedef long long int64_t;
659 typedef unsigned long long uint64_t;
660 typedef long long intptr_t;
661 typedef unsigned long long size_t;
662 #define PRId64 "lld"
663 #endif
664  
665 #ifndef __builtin_expect
666 # ifdef __has_builtin
667 # if !__has_builtin(__builtin_expect)
668 # define __builtin_expect(x, expected_value) (x)
669 # endif
670 # else
671 # define __builtin_expect(x, expected_value) (x)
672 # endif
673 #endif
674
675  
676 #define PRId8 "d"
677 #define PRId16 "d"
678 #define PRId32 "d"
679 #define INT8_MIN (-128)
680 #define INT8_MAX 127
681 #define INT16_MIN (-32768)
682 #define INT16_MAX 32767
683 #define INT32_MIN (-2147483647 - 1)
684 #define INT32_MAX 2147483647
685  
686 #define INT64_MIN (-9223372036854775807LL - 1)
687 #define INT64_MAX 9223372036854775807LL
688 #define EOF (-1)
689 EOS
690  
691 # Remove the #ifndef SPVM_NATIVE_NO_INCLUDE_HEADERS block
692 290 ▶ my $process_header = sub {
Subroutine __ANON__: called CC=1 SCAR=4.1
693 0 ▶ my ($path) = @_;
Subroutine __ANON__: not called CC=1 SCAR=4.1
694 0 my $content = &slurp_binary($path);
695
696 # Remove the block including #ifndef and #endif
697 0 $content =~ s/^\s*#ifndef\s+SPVM_NATIVE_NO_INCLUDE_HEADERS.*?#endif//msg;
698
699 # Remove the block including #ifndef and #endif for version utilities
700 0 $content =~ s/^\s*#ifndef\s+SPVM_NATIVE_NO_VERSION_UTILITIES.*?#endif//msg;
701
702 0 return $content;
703 0 };
704  
705 0 $header_content .= "\n/* spvm_native.h (headers-stripped) */\n" . $process_header->($spvm_native_h);
706 0 $header_content .= "\n/* spvm_implement.h (headers-stripped) */\n" . $process_header->($spvm_implement_h);
707  
708 0 my $c_string_literal = &create_c_string_literal($header_content);
709  
710 0 my $final_c_source = "/* This file is auto-generated by SPVM::Builder::Util::build_precompile_header_content_c_source */\n";
711 0 $final_c_source .= "static const char* spvm_precompile_header_content = \n$c_string_literal;\n\n";
712 0 $final_c_source .= "const char* get_precompile_header_content() {\n";
713 0 $final_c_source .= " return spvm_precompile_header_content;\n";
714 0 $final_c_source .= "}\n";
715  
716 # Check if the content is completely identical
717 0 ▶ my $old_content = -f $output_file ? &slurp_binary($output_file) : "";
Branch: 2 branches
BranchTrueFalseTotal
-f $output_file ? : 0 0 0
-f $output_file ? : 0 0 0
718
719 0 ▶ if ($old_content ne $final_c_source) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_content ne $final_c_source) 0 0 0
if ($old_content ne $final_c_source) 0 0 0
720 0 &spurt_binary($output_file, $final_c_source);
721 0 warn "Generated \"$output_file\".\n";
722 }
723  
724 0 return $output_file;
725 }
726  
727 783 ▶sub get_cpu_count {
Subroutine get_cpu_count: called CC=6 SCAR=27.3
Pod undocumented
728 493 ▶ my $cpu_count;
Subroutine get_cpu_count: called CC=6 SCAR=27.3
Pod undocumented
729  
730 493 ▶ if (SPVM::Builder::Util::is_windows()) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_windows()) { } 0 493 493
if (is_windows()) { } 0 1582 1582
731 # Windows
732 0 ▶ $cpu_count = $ENV{NUMBER_OF_PROCESSORS} || 1;
Condition0%0/4
$ENV{'NUMBER_OF_PROCESSORS'} || 1
l!l
00
$ENV{'NUMBER_OF_PROCESSORS'} || 1
l!l
00
MC/DC0%0/1
$ENV{'NUMBER_OF_PROCESSORS'} || 1
$ENV{'NUMBER_OF_PROCESSORS'}
MC/DC0%0/1
$ENV{'NUMBER_OF_PROCESSORS'} || 1
$ENV{'NUMBER_OF_PROCESSORS'}
Truth table0%0/2
A
A= $ENV{'NUMBER_OF_PROCESSORS'}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{'NUMBER_OF_PROCESSORS'}
Aresult
11
00
733 }
734 else {
735 # Linux / Unix / macOS
736 # (Comment: Using nproc is the most reliable way on modern Linux)
737 0 my $nproc = `nproc 2>/dev/null`;
738 0 ▶ if ($nproc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nproc) { } 493 0 493
if ($nproc) { } 1582 0 1582
739 0 $cpu_count = int($nproc);
740 }
741 else {
742 # (Comment: Fallback to /proc/cpuinfo or sysctl)
743 0 my $cpuinfo = `grep -c ^processor /proc/cpuinfo 2>/dev/null`;
744 0 ▶ if ($cpuinfo) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cpuinfo) { } 0 0 0
if ($cpuinfo) { } 0 0 0
745 0 $cpu_count = int($cpuinfo);
746 }
747 else {
748 0 my $sysctl = `sysctl -n hw.ncpu 2>/dev/null`;
749 0 ▶ $cpu_count = $sysctl ? int($sysctl) : 1;
Branch: 2 branches
BranchTrueFalseTotal
$sysctl ? : 0 0 0
$sysctl ? : 0 0 0
750 }
751 }
752 }
753  
754 0 return $cpu_count;
755 }
756  
757 44657 ▶sub check_option_names {
Subroutine check_option_names: called CC=5 SCAR=17.5
Pod undocumented
758 44367 ▶ my ($options, $available_option_names) = @_;
Subroutine check_option_names: called CC=5 SCAR=17.5
Pod undocumented
759
760 # Check if options exists
761 44367 ▶ unless ($options) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($options) 0 44367 44367
unless ($options) 0 5690 5690
762 0 return;
763 }
764
765 # Create a hash for fast lookup
766 44367 my $available_option_names_h = {};
767 44367 for my $available_option_name (@$available_option_names) {
768 0 $available_option_names_h->{$available_option_name} = 1;
769 }
770
771 # Validate each option name (Iterate through hash keys)
772 44367 for my $option_name (keys %$options) {
773 # Check if the option name is available
774 132174 ▶ unless ($available_option_names_h->{$option_name}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($available_option_names_h->{$option_name}) 0 132174 132174
unless ($available_option_names_h->{$option_name}) 0 14148 14148
775 0 Carp::confess("'$option_name' option is not available.");
776 }
777 }
778 }
779  
780 4520 ▶sub normalize_path {
Subroutine normalize_path: called CC=3 SCAR=11.7
Pod undocumented
781 4230 ▶ my ($path, $base_dir) = @_;
Subroutine normalize_path: called CC=3 SCAR=11.7
Pod undocumented
782
783 0 ▶ unless (defined $path) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $path) 0 4230 4230
unless (defined $path) 0 311 311
784 0 confess("The path \$path must be defined.");
785 }
786
787 4230 ▶ unless (defined $base_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $base_dir) 0 4230 4230
unless (defined $base_dir) 0 311 311
788 0 confess("The base directory \$base_dir must be defined.");
789 }
790
791 4230 my $abs_path = File::Spec->rel2abs($path);
792 4230 my $abs_base_dir = File::Spec->rel2abs($base_dir);
793
794 4230 my $normalized_path = File::Spec->abs2rel($abs_path, $abs_base_dir);
795 4230 $normalized_path =~ s|\\|/|g;
796
797 0 return $normalized_path;
798 }
799  
800 3360844 ▶sub quote_literal {
Subroutine quote_literal: called CC=6 SCAR=23.5
Pod undocumented
801 0 ▶ my ($string) = @_;
Subroutine quote_literal: called CC=6 SCAR=23.5
Pod undocumented
802  
803 0 ▶ if (SPVM::Builder::Util::is_windows()) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_windows()) { } 0 3360554 3360554
if (is_windows()) { } 0 319016 319016
804 0 ▶ if (length $string && $string !~ /[ \t\n\x0b"|<>%]/) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $string and not $string =~ /[ \t\n\x0b"|<>%]/) 0 0 0
if (length $string and not $string =~ /[ \t\n\x0b"|<>%]/) 0 0 0
Condition0%0/6
length $string and not $string =~ /[ \t\n\x0b"|<>%]/
!ll&&!rl&&r
000
length $string and not $string =~ /[ \t\n\x0b"|<>%]/
!ll&&!rl&&r
000
MC/DC0%0/2
length $string and not $string =~ /[ \t\n\x0b"|<>%]/
length $string not $string =~ /[ \t\n\x0b"|<>%]/
MC/DC0%0/2
length $string and not $string =~ /[ \t\n\x0b"|<>%]/
length $string not $string =~ /[ \t\n\x0b"|<>%]/
Truth table0%0/3
A and not(B)
A= length $stringB= not $string =~ /[ \t\n\x0b"|<>%]/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= length $stringB= not $string =~ /[ \t\n\x0b"|<>%]/
ABresult
0X0
100
111
805 0 return $string;
806 }
807  
808 0 $string =~ s{(\\*)(?="|\z)}{$1$1}g;
809 0 $string =~ s{"}{\\"}g;
810
811 0 my $quoted_string = qq{"$string"};
812
813 0 return $quoted_string;
814 }
815 else {
816 3360554 ▶ if (length $string && $string !~ /[^a-zA-Z0-9,._+@%\/-]/) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]) 3082327 278227 3360554
if (length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]) 295151 23865 319016
Condition66%4/6
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
!ll&&!rl&&r
02782273082327
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
!ll&&!rl&&r
023865295151
MC/DC50%1/2
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
length $string not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
MC/DC50%1/2
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
length $string not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
Truth table66%2/3
A and not(B)
A= length $stringB= not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
ABresult
0X0
100
111
Truth table66%2/3
A and not(B)
A= length $stringB= not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
ABresult
0X0
100
111
817 3082327 return $string;
818 }
819  
820 278227 $string =~ s{'}{'\\''}g;
821
822 278227 my $quoted_string = "'$string'";
823
824 278227 return $quoted_string;
825 }
826 }
827  
828 386 ▶sub setup_spvm_command_environment {
Subroutine setup_spvm_command_environment: called CC=6 SCAR=19.9
Pod undocumented
829 96 ▶ my ($script_name, $source, $blib, $class_search_dirs, $build_dir, $quiet, $no_quiet, $warning, $force) = @_;
Subroutine setup_spvm_command_environment: called CC=6 SCAR=19.9
Pod undocumented
830
831 96 $0 = $script_name;
832 96 FindBin::again();
833
834 0 @INC = @{SPVM::Builder::Util::resolve_spvm_command_inc(\@INC, $source, $blib, $class_search_dirs)};
835
836 0 ▶ if (defined $build_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $build_dir) 10 86 96
if (defined $build_dir) 0 1 1
837 0 $ENV{SPVM_BUILD_DIR} = $build_dir;
838 }
839
840 96 ▶ if (defined $quiet) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $quiet) { } 83 13 96
elsif (defined $no_quiet) { } 0 13 13
if (defined $quiet) { } 0 1 1
elsif (defined $no_quiet) { } 0 1 1
841 0 $ENV{SPVM_FORCE_QUIET} = $quiet;
842 }
843 elsif (defined $no_quiet) {
844 0 $ENV{SPVM_FORCE_QUIET} = !$no_quiet;
845 }
846
847 0 ▶ if (defined $force) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $force) 0 96 96
if (defined $force) 0 1 1
848 0 $ENV{SPVM_FORCE_BUILD} = !!$force;
849 }
850
851 96 ▶ if ($warning) {
Branch: 2 branches
BranchTrueFalseTotal
if ($warning) 0 96 96
if ($warning) 0 1 1
852 0 $^W = 1;
853 }
854 }
855  
856 386 ▶sub resolve_spvm_command_inc {
Subroutine resolve_spvm_command_inc: called CC=2 SCAR=7.3
Pod undocumented
857 96 ▶ my ($current_inc, $source, $blib, $class_search_dirs) = @_;
Subroutine resolve_spvm_command_inc: called CC=2 SCAR=7.3
Pod undocumented
858
859 0 my $class_search_dirs_by_lib_directive = SPVM::Builder::Util::parse_lib_directive($source, $FindBin::Bin);
860
861 # Get blib include directories if $blib is true
862 0 my @blib_class_search_dirs;
863 0 ▶ if ($blib) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blib) 1 95 96
if ($blib) 0 1 1
864 # Temporarily isolate @INC to get paths from the blib module
865 0 local @INC;
866 1 require blib;
867 1 blib->import;
868 1 @blib_class_search_dirs = @INC;
869 }
870
871 # Final include directories
872 96 my @inc = @$current_inc;
873
874 # To match Perl's @INC priority (last-in, first-priority),
875 # we add paths in reverse order of priority using unshift.
876
877 # 3rd priority: include directories from arguments (like -I)
878 96 unshift @inc, @$class_search_dirs;
879
880 # 2nd priority: include directories from blib (like -Mblib)
881 0 unshift @inc, @blib_class_search_dirs;
882
883 # 1st priority: include directories from #lib directives in the source (like use lib)
884 0 unshift @inc, @$class_search_dirs_by_lib_directive;
885
886 0 return \@inc;
887 }
888  
889 3387496 ▶sub is_windows { $^O eq 'MSWin32' }
Subroutine is_windows: called CC=1 SCAR=0.0
Pod undocumented
890  
891  
892 301 ▶sub search_gnu_make_command {
Subroutine search_gnu_make_command: called CC=5 SCAR=16.4
Pod undocumented
893 39 my @commands = ('gmake', 'make');
894  
895 39 for my $command (@commands) {
896 11 my $tempdir_obj = File::Temp->newdir;
897 39 my $tempdir = $tempdir_obj->dirname;
898 11 my $filename = "$tempdir/Makefile";
899  
900 11 ▶ if (open my $fh, '>', $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, ">", $filename) 11 0 11
901 11 print $fh "all:\n\t\@: # do nothing\n";
902 11 print $fh "\$(info gnu-make-confirmed)\n";
903 11 close $fh;
904  
905 591 my $output = `$command -f $filename 2>&1`;
906 74541 my $exit_code = $?;
907  
908 591 ▶ if ($exit_code == 0 && $output =~ /gnu-make-confirmed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0 and $output =~ /gnu-make-confirmed/) 11 0 11
Condition33%1/3
$exit_code == 0 and $output =~ /gnu-make-confirmed/
!ll&&!rl&&r
0011
MC/DC0%0/2
$exit_code == 0 and $output =~ /gnu-make-confirmed/
$exit_code == 0 $output =~ /gnu-make-confirmed/
Truth table33%1/3
A and B
A= $exit_code == 0B= $output =~ /gnu-make-confirmed/
ABresult
0X0
100
111
909 301 return $command;
910 }
911 }
912 }
913  
914 290 return undef;
915 }
916  
917 290 ▶sub build_parallel_libspvm {
Subroutine build_parallel_libspvm: called CC=4 SCAR=14.0
Pod undocumented
918 290 my ($options) = @_;
919
920 # For recursive loading
921 290 require SPVM::Builder;
922
923 290 my @new_option_names = (
924 'build_dir',
925 'output_dir',
926 'jobs',
927 );
928
929 290 my @build_parallel_option_names = (
930 'config_global_file',
931 );
932
933 290 my @all_option_names = (@new_option_names, @build_parallel_option_names);
934 290 SPVM::Builder::Util::check_option_names($options, \@all_option_names);
935
936 290 my %is_new_option = map { $_ => 1 } @new_option_names;
937
938 290 my $new_options = {};
939 290 my $build_parallel_options = {};
940
941 290 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
942 290 for my $key (keys %$options) {
943 290 ▶ if ($is_new_option{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new_option{$key}) { } 0 0 0
944 290 $new_options->{$key} = $options->{$key};
945 }
946 else {
947 290 $build_parallel_options->{$key} = $options->{$key};
948 }
949 }
950 }
951
952 290 my $builder = SPVM::Builder->new(%$new_options);
953 290 my $link_info = $builder->prepare_compile_spvm_core_source_files($build_parallel_options);
954 290 $link_info->config->category('libspvm');
955 290 $link_info->config->class_name('libspvm');
956
957 290 $link_info->dl_func_list(['SPVM_NATIVE_new_env']);
958
959 290 my $diagnostic_message = "[Generate Static Link Library for libpsvm]";
960 290 $link_info->diagnostic_message($diagnostic_message);
961 290 $link_info->config->output_type('static_lib');
962
963 290 my $link_infos = [$link_info];
964
965 290 $builder->build_parallel_with_link_infos($link_infos, $options);
966 }
967  
968 290 ▶sub create_make_rule_parallel_libspvm {
Subroutine create_make_rule_parallel_libspvm: called CC=10 SCAR=28.8
Pod undocumented
969 290 my ($options) = @_;
970  
971 290 ▶ $options ||= {};
Condition0%0/2
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
972  
973 290 my $make_rule = '';
974  
975 # Gen target name
976 290 my $target = "blib/lib/SPVM/Builder/lib/libspvm.a";
977  
978 # Order-only deps
979 290 ▶ my $dependent_files = $options->{dependent_files} // [];
Condition0%0/2
$options->{"dependent_files"} // []
l!l
00
MC/DC0%0/1
$options->{"dependent_files"} // []
$options->{"dependent_files"}
Truth table0%0/2
A
A= $options->{"dependent_files"}
Aresult
11
00
980 290 ▶ my $dependent_files_string = @$dependent_files ? " " . join(' ', @$dependent_files) : "";
Branch: 1 branch
BranchTrueFalseTotal
@$dependent_files ? : 0 0 0
981
982 290 ▶ my $order_only_dependent_files = $options->{order_only_dependent_files} // [];
Condition0%0/2
$options->{"order_only_dependent_files"} // []
l!l
00
MC/DC0%0/1
$options->{"order_only_dependent_files"} // []
$options->{"order_only_dependent_files"}
Truth table0%0/2
A
A= $options->{"order_only_dependent_files"}
Aresult
11
00
983 290 ▶ my $order_only_dependent_files_string = @$order_only_dependent_files ? " | " . join(' ', @$order_only_dependent_files) : "";
Branch: 1 branch
BranchTrueFalseTotal
@$order_only_dependent_files ? : 0 0 0
984  
985 # Dynamic target
986 290 $make_rule .= "dynamic :: $target\n";
987 290 $make_rule .= "\t\$(NOECHO) \$(NOOP)\n\n";
988  
989 # Parallel build rule
990 290 $make_rule .= ".PHONY: $target\n";
991 290 $make_rule .= "$target :$dependent_files_string$order_only_dependent_files_string\n";
992  
993 # Collect all build options
994 290 my @build_options;
995
996 290 ▶ my $build_dir = $options->{build_dir} // '.spvm_build';
Condition0%0/2
$options->{"build_dir"} // ".spvm_build"
l!l
00
MC/DC0%0/1
$options->{"build_dir"} // ".spvm_build"
$options->{"build_dir"}
Truth table0%0/2
A
A= $options->{"build_dir"}
Aresult
11
00
997 290 push @build_options, "build_dir => '$build_dir'";
998
999 290 ▶ my $output_dir = $options->{output_dir} // 'blib/lib';
Condition0%0/2
$options->{"output_dir"} // "blib/lib"
l!l
00
MC/DC0%0/1
$options->{"output_dir"} // "blib/lib"
$options->{"output_dir"}
Truth table0%0/2
A
A= $options->{"output_dir"}
Aresult
11
00
1000 290 push @build_options, "output_dir => '$output_dir'";
1001
1002 290 ▶ if (defined(my $jobs = $options->{jobs})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $jobs = $options->{"jobs"})) 0 0 0
1003 290 push @build_options, "jobs => $jobs";
1004 }
1005
1006 290 ▶ if (defined(my $config_global_file = $options->{config_global_file})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $config_global_file = $options->{"config_global_file"})) 0 0 0
1007 290 push @build_options, "config_global_file => '$config_global_file'";
1008 }
1009
1010 290 my $build_options_hash_str = "{" . join(', ', @build_options) . "}";
1011  
1012 # Build cmd
1013 290 $make_rule .= "\t\$(FULLPERLRUN) -Mblib -MSPVM::Builder::Util -e \"SPVM::Builder::Util::build_parallel_libspvm($build_options_hash_str)\"\n\n";
1014  
1015 290 return $make_rule;
1016 }
1017  
1018 381 ▶sub is_msvc {
Subroutine is_msvc: called CC=3 SCAR=11.2
Pod undocumented
1019 381 my ($cc) = @_;
1020
1021 381 ▶ unless (defined $cc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cc) 0 119 119
1022 290 confess("Compiler \$cc must be defined.");
1023 }
1024
1025 381 my $is_msvc = 0;
1026 381 ▶ if ($cc =~ /cl(\.exe)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc =~ /cl(\.exe)?$/) 0 119 119
1027 290 $is_msvc = 1;
1028 }
1029
1030 381 return $is_msvc;
1031 }
1032  
1033 290 ▶sub is_gcc_compatible {
Subroutine is_gcc_compatible: called CC=2 SCAR=7.4
Pod undocumented
1034 290 my ($cc) = @_;
1035
1036 290 my $is_gcc_compatible = 0;
1037 290 ▶ if ($cc =~ /(?:gcc|clang|g\+\+|clang\+\+)(?:\.exe)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc =~ /(?:gcc|clang|g\+\+|clang\+\+)(?:\.exe)?$/) 0 0 0
1038 290 $is_gcc_compatible = 1;
1039 }
1040
1041 290 return $is_gcc_compatible;
1042 }
1043  
1044  
1045 627 1;
1046  
1047 =head1 Name
1048  
1049 SPVM::Builder::Util - Builder Utilities
1050  
1051 =head1 Description
1052  
1053 The SPVM::Builder::Util class has utility functions for L<SPVM::Builder> class.
1054  
1055 =head1 Copyright & License
1056  
1057 Copyright (c) 2023 Yuki Kimoto
1058  
1059 MIT License
« blib/lib/SPVM/Builder/DependencyAnalyzer.pm ↑ Summary blib/lib/SPVM/Global.pm »