blib/lib/SPVM/Builder/Util.pm

filter: Statementstmt 65.4% 568 / 868 Branchbran 45.1% 140 / 310 Conditioncond 32.4% 25 / 77 MC/DCmcdc 11.6% 5 / 43 Subroutinesub 82.4% 89 / 108 Podpod 0.0% 0 / 84 totaltotal 55.5% 827 / 1490 CC 223 SCAR 26.0
CC
223
Cov
58%
create_make_rule_parallel
53.7
create_make_rule_parallel
53.7
create_make_rule_parallel_libspvm
33.7
CRAP 13.5
« blib/lib/SPVM/Builder/Config.pm ↑ Summary blib/lib/SPVM/ExchangeAPI.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 1414 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 707 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 707 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 707 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 707 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 707 ▶use Pod::Usage 'pod2usage';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 707 ▶use Getopt::Long 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 707 ▶use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 707 ▶use File::Basename 'dirname', 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 707 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 707 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 707 ▶use File::Find 'find';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 707 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 707 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 707 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 707 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 707 ▶use Fcntl qw(O_RDONLY);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 707 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 729 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 729 ▶use File::Copy 'move';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 729 ▶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=7.9
Pod undocumented
30 84 ▶ my $builder_dir = &get_builder_dir;
Subroutine get_spvm_header_files: called CC=2 SCAR=7.9
Pod undocumented
31
32 84 my $include_dir = "$builder_dir/include";
33
34 # Get all .h files in the include directory
35 84 ▶ my @spvm_header_files = glob("$include_dir/*.h");
Subroutine BEGIN: called CC=1 SCAR=0.0
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=15.7
Pod undocumented
45 0 ▶ my $builder_dir = &get_builder_dir;
Subroutine get_spvm_precompile_source_files: called CC=3 SCAR=15.7
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 122 ▶sub get_spvm_core_source_file_names {
Subroutine get_spvm_core_source_file_names: called CC=1 SCAR=0.0
Pod undocumented
70
71 84 ▶ my @spvm_core_source_file_names = qw(
Subroutine get_spvm_core_source_file_names: 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 84 return \@spvm_core_source_file_names;
133 }
134  
135 2588 ▶sub slurp_binary {
Subroutine slurp_binary: called CC=2 SCAR=6.9
Pod undocumented
136 84 ▶ my ($file) = @_;
Subroutine slurp_binary: called CC=2 SCAR=6.9
Pod undocumented
137
138 84 ▶ open my $fh, '<', $file
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "<", $file or confess("Can't open file '${file}':$!") 216 1 217
open my $fh, "<", $file or confess("Can't open file '${file}':$!") 2567 21 2588
139 or confess("Can't open file '$file':$!");
140
141 84 my $content = do { local $/; <$fh> };
142
143 84 return $content;
144 }
145  
146 203 ▶sub slurp_utf8 {
Subroutine slurp_utf8: called CC=1 SCAR=0.0
Pod undocumented
147 84 ▶ my ($file) = @_;
Subroutine slurp_utf8: called CC=1 SCAR=0.0
Pod undocumented
148
149 84 my $content = &slurp_binary($file);
150
151 84 $content = decode('UTF-8', $content);
152
153 84 return $content;
154 }
155  
156 203 ▶sub file_contains {
Subroutine file_contains: called CC=2 SCAR=7.0
Pod undocumented
157 84 ▶ my ($file, $string) = @_;
Subroutine file_contains: called CC=2 SCAR=7.0
Pod undocumented
158
159 84 my $content = &slurp_utf8($file);
160
161 84 my $contains;
162 84 ▶ if (index($content, $string) >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($content, $string) != -1) 0 0 0
if (index($content, $string) != -1) 193 10 203
163 84 $contains = 1;
164 }
165
166 84 return $contains;
167 }
168  
169 1365 ▶sub spurt_binary {
Subroutine spurt_binary: called CC=11 SCAR=24.4
Pod undocumented
170 84 ▶ my ($output_file, $content, $global_lock_fh) = @_;
Subroutine spurt_binary: called CC=11 SCAR=24.4
Pod undocumented
171
172 84 ▶ if ($global_lock_fh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global_lock_fh) 250 0 250
if ($global_lock_fh) 979 386 1365
173 84 ▶ 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: $!") 250 0 250
flock $global_lock_fh, 2 or confess("Can't lock global lock file: $!") 979 0 979
174 }
175
176 84 eval {
177 84 my $should_write = 1;
178 84 ▶ if (-f $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $output_file) 1 249 250
if (-f $output_file) 93 1272 1365
179 84 my $old_content = do {
180 84 ▶ 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}':$!" 1 0 1
open my $read_fh, "<:raw", $output_file or die "Can't open for read '${output_file}':$!" 93 0 93
181 84 local $/;
182 84 <$read_fh>;
183 };
184
185 84 ▶ if ($old_content eq $content) {
Branch: 2 branches
BranchTrueFalseTotal
if ($old_content eq $content) 1 0 1
if ($old_content eq $content) 89 4 93
186 84 $should_write = 0;
187 }
188 }
189
190 84 ▶ if ($should_write) {
Branch: 2 branches
BranchTrueFalseTotal
if ($should_write) 249 1 250
if ($should_write) 1276 89 1365
191 84 my $dir = dirname($output_file);
192 84 mkpath $dir;
193
194 # Write to a temporary file first
195 84 my $tmp_file = "$output_file.tmp.$$";
196
197 84 ▶ 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}':$!" 249 0 249
open my $output_fh, ">:raw", $tmp_file or die "Can't open temporary file '${tmp_file}':$!" 1276 0 1276
198 or die "Can't open temporary file '$tmp_file':$!";
199 84 print $output_fh $content;
200 84 close $output_fh;
201
202 # Atomically replace the target file
203 84 ▶ move($tmp_file, $output_file)
Branch: 2 branches
BranchTrueFalseTotal
move($tmp_file, $output_file) or confess("Can't move '${tmp_file}' to '${output_file}':$!") 249 0 249
move($tmp_file, $output_file) or confess("Can't move '${tmp_file}' to '${output_file}':$!") 1276 0 1276
204 or confess("Can't move '$tmp_file' to '$output_file':$!");
205 }
206 };
207  
208 84 my $error = $@;
209  
210 84 ▶ if ($global_lock_fh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($global_lock_fh) 250 0 250
if ($global_lock_fh) 979 386 1365
211 84 flock($global_lock_fh, LOCK_UN);
212 }
213  
214 84 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 250 250
if ($error) 0 1365 1365
215 0 confess $error;
216 }
217 }
218  
219 2 ▶sub unindent {
Subroutine unindent: called CC=3 SCAR=11.9
Pod undocumented
220 84 ▶ my $str = shift;
Subroutine unindent: called CC=3 SCAR=11.9
Pod undocumented
221 84 ▶ my $min = min map { m/^([ \t]*)/; length $1 || () } split "\n", $str;
Condition33%2/6
length $1 || ()
l!l&&r!l&&!r
000
length $1 || ()
l!l&&r!l&&!r
4620
MC/DC0%0/2
length $1 || ()
length $1 ()
MC/DC0%0/2
length $1 || ()
length $1 ()
Truth table0%0/3
A || B
A= length $1B= ()
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= length $1B= ()
ABresult
1X1
011
000
222 84 ▶ $str =~ s/^[ \t]{0,$min}//gm if $min;
Branch: 2 branches
BranchTrueFalseTotal
if $min 0 0 0
if $min 2 0 2
223 84 return $str;
224 }
225  
226 2 ▶sub extract_usage {
Subroutine extract_usage: called CC=2 SCAR=9.2
Pod undocumented
227 84 ▶ my $file = @_ ? "$_[0]" : (caller)[1];
Subroutine extract_usage: called CC=2 SCAR=9.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 0 0 0
@_ ? : 0 2 2
228
229 0 open my $handle, '>', \my $output;
230
231 0 pod2usage -exitval => 'noexit', -input => $file, -output => $handle, -verbose => 99, -sections => "Usage";
232 0 $output =~ s/^.*\n|\n$//;
233 0 $output =~ s/\n$//;
234  
235 0 return SPVM::Builder::Util::unindent($output);
236 }
237  
238 172 ▶sub getopt {
Subroutine getopt: called CC=3 SCAR=13.5
Pod undocumented
239 0 ▶ my ($array, $opts) = map { ref $_[0] eq 'ARRAY' ? shift : $_ } \@ARGV, [];
Subroutine getopt: called CC=3 SCAR=13.5
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 4 4
ref $_[0] eq 'ARRAY' ? : 0 344 344
240 0 my $save = Getopt::Long::Configure(qw(default no_auto_abbrev no_ignore_case),
241 @$opts);
242 0 my $success = GetOptionsFromArray $array, @_;
243 0 Getopt::Long::Configure($save);
244
245 0 ▶ unless ($success) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($success) 0 2 2
unless ($success) 4 168 172
246 0 Carp::confess("Failed to parse command line options.");
247 }
248 }
249  
250 1966 ▶sub convert_class_file_to_dynamic_lib_file {
Subroutine convert_class_file_to_dynamic_lib_file: called CC=2 SCAR=7.2
Pod undocumented
251 0 ▶ my ($class_file, $category) = @_;
Subroutine convert_class_file_to_dynamic_lib_file: called CC=2 SCAR=7.2
Pod undocumented
252
253 0 my $so = $Config{so};
254 0 $class_file =~ s/\.[^.]+$//;
255 0 my $dynamic_lib_category_file = $class_file;
256 1 ▶ $dynamic_lib_category_file .= $category eq 'native' ? ".$so" : ".$category.$so";
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'native' ? : 96 237 333
$category eq 'native' ? : 802 1164 1966
257
258 1 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=11.2
Pod undocumented
262 0 ▶ my ($class_name, $category) = @_;
Subroutine convert_class_name_to_dynamic_lib_rel_file: called CC=2 SCAR=11.2
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 1635 ▶sub convert_class_name_to_category_rel_file {
Subroutine convert_class_name_to_category_rel_file: called CC=3 SCAR=11.9
Pod undocumented
272 216 ▶ my ($class_name, $category, $ext) = @_;
Subroutine convert_class_name_to_category_rel_file: called CC=3 SCAR=11.9
Pod undocumented
273
274 0 my $rel_file_with_ext = "SPVM::$class_name";
275 0 $rel_file_with_ext =~ s/::/\//g;
276 0 ▶ $rel_file_with_ext .= $category eq 'native' ? "" : ".$category";
Branch: 2 branches
BranchTrueFalseTotal
$category eq 'native' ? : 0 276 276
$category eq 'native' ? : 676 959 1635
277 0 ▶ if (defined $ext) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext) 222 54 276
if (defined $ext) 1470 165 1635
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=4.6
Pod undocumented
285 0 ▶ my ($class_name) = @_;
Subroutine convert_class_name_to_rel_dir: called CC=1 SCAR=4.6
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 6190 ▶sub convert_class_name_to_rel_file {
Subroutine convert_class_name_to_rel_file: called CC=2 SCAR=7.0
Pod undocumented
296 250 ▶ my ($class_name, $ext) = @_;
Subroutine convert_class_name_to_rel_file: called CC=2 SCAR=7.0
Pod undocumented
297
298 250 my $rel_file_with_ext = "SPVM::$class_name";
299 250 $rel_file_with_ext =~ s/::/\//g;
300
301 250 ▶ if (defined $ext) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ext) 492 0 492
if (defined $ext) 6190 0 6190
302 1 $rel_file_with_ext .= ".$ext";
303 }
304
305 1 return $rel_file_with_ext;
306 }
307  
308 245 ▶sub get_class_base_dir {
Subroutine get_class_base_dir: called CC=1 SCAR=0.0
Pod undocumented
309 1 ▶ my ($file, $class_name) = @_;
Subroutine get_class_base_dir: called CC=1 SCAR=0.0
Pod undocumented
310
311 1 $file =~ s/\.spvm$//;
312 1 my $class_file = "SPVM::$class_name";
313 1 $class_file =~ s/::/\//g;
314 250 $file =~ s/$class_file$//;
315 249 $file =~ s/[\\\/]$//;
316
317 249 return $file;
318 }
319  
320 1 ▶sub create_make_rule_native {
Subroutine create_make_rule_native: called CC=1 SCAR=0.0
Pod undocumented
321 249 ▶ my $class_name = shift;
Subroutine create_make_rule_native: called CC=1 SCAR=0.0
Pod undocumented
322
323 249 create_make_rule($class_name, 'native', @_);
324 }
325  
326 1 ▶sub create_make_rule_precompile {
Subroutine create_make_rule_precompile: called CC=1 SCAR=0.0
Pod undocumented
327 249 ▶ my $class_name = shift;
Subroutine create_make_rule_precompile: called CC=1 SCAR=0.0
Pod undocumented
328
329 249 create_make_rule($class_name, 'precompile', @_);
330 }
331  
332 2 ▶sub create_make_rule {
Subroutine create_make_rule: called CC=4 SCAR=15.8
Pod undocumented
333 249 ▶ my ($class_name, $category, $options) = @_;
Subroutine create_make_rule: called CC=4 SCAR=15.8
Pod undocumented
334  
335 250 ▶ $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 250 ▶ 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 250 $options->{native_classes} = [$class_name];
339 }
340 elsif ($category eq 'precompile') {
341 250 $options->{precompile_classes} = [$class_name];
342 }
343  
344 0 return create_make_rule_parallel($options);
345 }
346  
347 12 ▶sub create_make_rule_parallel {
Subroutine create_make_rule_parallel: called CC=20 SCAR=39.8
Pod undocumented
348 0 ▶ my ($options) = @_;
Subroutine create_make_rule_parallel: called CC=20 SCAR=39.8
Pod undocumented
349
350 0 ▶ $options ||= {};
Condition25%1/4
$options ||= {}
l!l
00
$options ||= {}
l!l
120
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
351
352 0 my $make_rule = '';
353
354 # Gen target name
355 0 my @target_parts;
356
357 # Target ID includes filenames for unique SHA1
358 0 ▶ if (my $precompile_classes = $options->{precompile_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes = $options->{"precompile_classes"}) 0 0 0
if (my $precompile_classes = $options->{"precompile_classes"}) 11 1 12
359 0 push @target_parts, map { "precompile|$_" } @$precompile_classes;
360 }
361 0 ▶ if (my $precompile_classes_file = $options->{precompile_classes_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes_file = $options->{"precompile_classes_file"}) 0 0 0
if (my $precompile_classes_file = $options->{"precompile_classes_file"}) 0 12 12
362 0 push @target_parts, "precompile_file|$precompile_classes_file";
363 }
364 0 ▶ if (my $native_classes = $options->{native_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes = $options->{"native_classes"}) 0 0 0
if (my $native_classes = $options->{"native_classes"}) 11 1 12
365 0 push @target_parts, map { "native|$_" } @$native_classes;
366 }
367 2 ▶ if (my $native_classes_file = $options->{native_classes_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes_file = $options->{"native_classes_file"}) 0 0 0
if (my $native_classes_file = $options->{"native_classes_file"}) 0 12 12
368 0 push @target_parts, "native_file|$native_classes_file";
369 }
370
371 # Create SHA1 hex digest
372 2 my $target_id = sha1_hex(join("\n", sort @target_parts));
373 2 my $target = "spvm-build-parallel-$target_id";
374
375 # Order-only deps
376 2 ▶ my $dependent_files = $options->{dependent_files} // [];
Condition25%1/4
$options->{"dependent_files"} // []
l!l
00
$options->{"dependent_files"} // []
l!l
012
MC/DC0%0/1
$options->{"dependent_files"} // []
$options->{"dependent_files"}
MC/DC0%0/1
$options->{"dependent_files"} // []
$options->{"dependent_files"}
Truth table0%0/2
A
A= $options->{"dependent_files"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"dependent_files"}
Aresult
11
00
377 2 ▶ my $dependent_files_string = @$dependent_files ? " " . join(' ', @$dependent_files) : "";
Branch: 2 branches
BranchTrueFalseTotal
@$dependent_files ? : 0 0 0
@$dependent_files ? : 0 12 12
378
379 0 ▶ my $order_only_dependent_files = $options->{order_only_dependent_files} // [];
Condition50%2/4
$options->{"order_only_dependent_files"} // []
l!l
00
$options->{"order_only_dependent_files"} // []
l!l
102
MC/DC0%0/1
$options->{"order_only_dependent_files"} // []
$options->{"order_only_dependent_files"}
MC/DC100%1/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
Truth table100%2/2
A
A= $options->{"order_only_dependent_files"}
Aresult
11
00
380 333 ▶ my $order_only_dependent_files_string = @$order_only_dependent_files ? " | " . join(' ', @$order_only_dependent_files) : "";
Branch: 2 branches
BranchTrueFalseTotal
@$order_only_dependent_files ? : 0 0 0
@$order_only_dependent_files ? : 10 2 12
381
382 # Dynamic target
383 333 $make_rule .= "dynamic :: $target\n";
384 333 $make_rule .= "\t\$(NOECHO) \$(NOOP)\n\n";
385
386 # Parallel build rule
387 333 $make_rule .= ".PHONY: $target\n";
388 333 $make_rule .= "$target :$dependent_files_string$order_only_dependent_files_string\n";
389
390 # Collect all build options
391 333 my @build_options;
392
393 0 ▶ my $build_dir = $options->{build_dir} // '.spvm_build';
Condition25%1/4
$options->{"build_dir"} // ".spvm_build"
l!l
00
$options->{"build_dir"} // ".spvm_build"
l!l
012
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 table0%0/2
A
A= $options->{"build_dir"}
Aresult
11
00
Truth table50%1/2
A
A= $options->{"build_dir"}
Aresult
11
00
394 0 push @build_options, "build_dir => '$build_dir'";
395
396 0 ▶ my $output_dir = $options->{output_dir} // 'blib/lib';
Condition50%2/4
$options->{"output_dir"} // "blib/lib"
l!l
00
$options->{"output_dir"} // "blib/lib"
l!l
210
MC/DC0%0/1
$options->{"output_dir"} // "blib/lib"
$options->{"output_dir"}
MC/DC100%1/1
$options->{"output_dir"} // "blib/lib"
$options->{"output_dir"}
Truth table0%0/2
A
A= $options->{"output_dir"}
Aresult
11
00
Truth table100%2/2
A
A= $options->{"output_dir"}
Aresult
11
00
397 0 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 0 0
if (defined(my $jobs = $options->{"jobs"})) 0 12 12
400 0 push @build_options, "jobs => $jobs";
401 }
402
403 276 ▶ if (defined(my $config_global_file = $options->{config_global_file})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $config_global_file = $options->{"config_global_file"})) 0 0 0
if (defined(my $config_global_file = $options->{"config_global_file"})) 10 2 12
404 276 push @build_options, "config_global_file => '$config_global_file'";
405 }
406
407 276 ▶ 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 0 0
if (defined(my $native_classes_file = $options->{"native_classes_file"})) 0 12 12
408 0 push @build_options, "native_classes_file => '$native_classes_file'";
409 }
410 222 ▶ 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 0 0
if (defined(my $precompile_classes_file = $options->{"precompile_classes_file"})) 0 12 12
411 0 push @build_options, "precompile_classes_file => '$precompile_classes_file'";
412 }
413
414 0 ▶ if (my $native_classes = $options->{native_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $native_classes = $options->{"native_classes"}) 0 0 0
if (my $native_classes = $options->{"native_classes"}) 11 1 12
415 0 push @build_options, "native_classes => [" . join(', ', map { "'$_'" } @$native_classes) . "]";
416 }
417 0 ▶ if (my $precompile_classes = $options->{precompile_classes}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $precompile_classes = $options->{"precompile_classes"}) 0 0 0
if (my $precompile_classes = $options->{"precompile_classes"}) 11 1 12
418 0 push @build_options, "precompile_classes => [" . join(', ', map { "'$_'" } @$precompile_classes) . "]";
419 }
420
421 492 my $build_options_hash_str = "{" . join(', ', @build_options) . "}";
422
423 # Build @INC string for -I
424 492 ▶ my $inc = $options->{class_search_dir} // [];
Condition50%2/4
$options->{"class_search_dir"} // []
l!l
00
$options->{"class_search_dir"} // []
l!l
210
MC/DC0%0/1
$options->{"class_search_dir"} // []
$options->{"class_search_dir"}
MC/DC100%1/1
$options->{"class_search_dir"} // []
$options->{"class_search_dir"}
Truth table0%0/2
A
A= $options->{"class_search_dir"}
Aresult
11
00
Truth table100%2/2
A
A= $options->{"class_search_dir"}
Aresult
11
00
425 492 my $inc_string = '';
426 492 ▶ if (@$inc) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$inc) 0 0 0
if (@$inc) 2 10 12
427 492 $inc_string = join(' ', map { "-I$_" } @$inc) . ' ';
428 }
429
430 # Build cmd
431 0 $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 0 return $make_rule;
434 }
435  
436 3542 ▶sub search_config_file {
Subroutine search_config_file: called CC=4 SCAR=17.9
Pod undocumented
437 0 ▶ my ($class_name, $mode) = @_;
Subroutine search_config_file: called CC=4 SCAR=17.9
Pod undocumented
438
439 0 my $ext = SPVM::Builder::Config::Util::get_config_ext();
440 0 ▶ if (defined $mode) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $mode) 0 48 48
if (defined $mode) 0 3542 3542
441 0 $ext = "$mode.$ext";
442 }
443
444 0 my $config_file_base = SPVM::Builder::Util::convert_class_name_to_rel_file($class_name, $ext);
445 0 my $config_file;
446 0 for my $inc (@INC) {
447 0 my $config_file_tmp = "$inc/$config_file_base";
448 0 ▶ 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)) 0 864 864
if (my $found_config_file = SPVM::Builder::Config::Util::exists_config_file($config_file_tmp)) 406 54738 55144
449 0 $config_file = $found_config_file;
450 0 last;
451 }
452 }
453
454 0 return $config_file;
455 }
456  
457 1990 ▶sub search_spvm_file {
Subroutine search_spvm_file: called CC=3 SCAR=13.0
Pod undocumented
458 0 ▶ my ($class_name) = @_;
Subroutine search_spvm_file: called CC=3 SCAR=13.0
Pod undocumented
459
460 0 my $ext = 'spvm';
461
462 0 my $spvm_file_base = SPVM::Builder::Util::convert_class_name_to_rel_file($class_name, $ext);
463 0 my $spvm_file;
464 0 for my $inc (@INC) {
465 0 my $spvm_file_tmp = "$inc/$spvm_file_base";
466 0 ▶ if (-f $spvm_file_tmp) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $spvm_file_tmp) 333 833 1166
if (-f $spvm_file_tmp) 1990 6400 8390
467 0 $spvm_file = $spvm_file_tmp;
468 0 last;
469 }
470 }
471
472 0 return $spvm_file;
473 }
474  
475 5832 ▶sub get_builder_dir {
Subroutine get_builder_dir: called CC=1 SCAR=1.2
Pod undocumented
476 0 ▶ my $builder_config_dir = $INC{"SPVM/Builder/Config.pm"};
Subroutine get_builder_dir: not called CC=1 SCAR=1.2
Pod undocumented
477 0 my $builder_dir = $builder_config_dir;
478 0 $builder_dir =~ s/\/Config\.pm$//;
479 0 return $builder_dir;
480 }
481  
482 1966 ▶sub get_dynamic_lib_file_dist {
Subroutine get_dynamic_lib_file_dist: called CC=1 SCAR=1.2
Pod undocumented
483 0 ▶ my ($class_file, $category) = @_;
Subroutine get_dynamic_lib_file_dist: not called CC=1 SCAR=1.2
Pod undocumented
484  
485 0 my $dynamic_lib_file = SPVM::Builder::Util::convert_class_file_to_dynamic_lib_file($class_file, $category);
486
487 0 return $dynamic_lib_file;
488 }
489  
490 3745 ▶sub create_default_config {
Subroutine create_default_config: called CC=1 SCAR=1.2
Pod undocumented
491
492 0 ▶ my $config = SPVM::Builder::Config->new_c99;
Subroutine create_default_config: called CC=1 SCAR=1.2
Pod undocumented
493
494 0 return $config;
495 }
496  
497 12103 ▶sub get_normalized_env {
Subroutine get_normalized_env: called CC=3 SCAR=14.2
Pod undocumented
498 0 ▶ my ($name) = @_;
Subroutine get_normalized_env: called CC=3 SCAR=14.2
Pod undocumented
499
500 0 my $value = $ENV{$name};
501
502 0 ▶ if (defined $value && !length $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and not length $value) 0 379 379
if (defined $value and not length $value) 0 12103 12103
Condition66%4/6
defined $value and not length $value
!ll&&!rl&&r
2541250
defined $value and not length $value
!ll&&!rl&&r
863934640
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 0 ▶sub get_version_string {
Subroutine get_version_string: not called CC=4 SCAR=30.0
Pod undocumented
510 0 ▶ my ($spvm_class_file) = @_;
Subroutine get_version_string: not called CC=4 SCAR=30.0
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 1 ▶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: not 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 577 ▶sub create_dl_func_list {
Subroutine create_dl_func_list: called CC=5 SCAR=19.6
Pod undocumented
549 0 ▶ my ($class_name, $method_names, $options) = @_;
Subroutine create_dl_func_list: called CC=5 SCAR=19.6
Pod undocumented
550
551 0 ▶ $options ||= {};
Condition50%2/4
$options ||= {}
l!l
2130
$options ||= {}
l!l
5770
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 0 ▶ my $category = $options->{category} || '';
Condition50%2/4
$options->{'category'} || ''
l!l
2130
$options->{'category'} || ''
l!l
5770
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 0 my $dl_func_list = [];
558 0 for my $method_name (@$method_names) {
559 0 my $cfunc_name = SPVM::Builder::Util::create_cfunc_name($class_name, $method_name, $category);
560 0 push @$dl_func_list, $cfunc_name;
561 }
562
563 # This is bad hack to suppress boot strap function error.
564 0 ▶ unless (@$dl_func_list) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@$dl_func_list) 0 213 213
unless (@$dl_func_list) 84 493 577
565 0 push @$dl_func_list, '';
566 }
567
568 48 return $dl_func_list;
569 }
570  
571 24483 ▶sub create_cfunc_name {
Subroutine create_cfunc_name: called CC=3 SCAR=11.1
Pod undocumented
572 48 ▶ my ($class_name, $method_name, $category) = @_;
Subroutine create_cfunc_name: not called CC=3 SCAR=11.1
Pod undocumented
573
574 48 my $prefix;
575 0 ▶ if ($category eq 'native') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'native') { } 204 1043 1247
elsif ($category eq 'precompile') { } 1043 0 1043
if ($category eq 'native') { } 11645 12838 24483
elsif ($category eq 'precompile') { } 12838 0 12838
576 48 $prefix = 'SPVM__';
577 }
578 elsif ($category eq 'precompile') {
579 48 $prefix = 'SPVMPRECOMPILE__'
580 }
581
582 # Precompile Method names
583 48 my $method_abs_name_under_score = "${class_name}::$method_name";
584 864 $method_abs_name_under_score =~ s/:/_/g;
585 864 my $cfunc_name = "$prefix$method_abs_name_under_score";
586
587 0 return $cfunc_name;
588 }
589  
590 133 ▶sub parse_lib_directive {
Subroutine parse_lib_directive: called CC=2 SCAR=7.0
Pod undocumented
591 0 ▶ my ($source, $bin) = @_;
Subroutine parse_lib_directive: not called CC=2 SCAR=7.0
Pod undocumented
592
593 48 my $class_search_dirs = [];
594
595 333 ▶ while ($source =~ /^#lib "([^"]+)"/gm) {
Branch: 2 branches
BranchTrueFalseTotal
if ($source =~ /^#lib "([^"]+)"/gm) 0 2 2
if ($source =~ /^#lib "([^"]+)"/gm) 2 133 135
596 333 my $class_search_dir = $1;
597
598 333 $class_search_dir =~ s/\$FindBin::Bin/$bin/g;
599
600 333 unshift @$class_search_dirs, $class_search_dir;
601 }
602
603 333 return $class_search_dirs;
604 }
605  
606 # Create C string literal for MSVC/TCC compatibility
607 my $CREATE_C_STRING_LITERAL_HEX_TABLE = [map { sprintf("0x%02x", $_) } 0..255];
608 0 ▶sub create_c_string_literal {
Subroutine create_c_string_literal: not called CC=2 SCAR=10.9
Pod undocumented
609 0 ▶ my ($content) = @_;
Subroutine create_c_string_literal: called CC=2 SCAR=10.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 0 ▶sub build_precompile_header_content_c_source {
Subroutine build_precompile_header_content_c_source: not called CC=3 SCAR=20.4
Pod undocumented
625 0 ▶ my ($options) = @_;
Subroutine build_precompile_header_content_c_source: called CC=3 SCAR=20.4
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 0 ▶ my $process_header = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
693 0 ▶ my ($path) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 1918 ▶sub get_cpu_count {
Subroutine get_cpu_count: called CC=6 SCAR=27.9
Pod undocumented
728 0 ▶ my $cpu_count;
Subroutine get_cpu_count: called CC=6 SCAR=27.9
Pod undocumented
729  
730 0 ▶ if (SPVM::Builder::Util::is_windows()) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_windows()) { } 0 62 62
if (is_windows()) { } 0 1918 1918
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) { } 62 0 62
if ($nproc) { } 1918 0 1918
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 213 return $cpu_count;
755 }
756  
757 67307 ▶sub check_option_names {
Subroutine check_option_names: called CC=5 SCAR=17.0
Pod undocumented
758 807 ▶ my ($options, $available_option_names) = @_;
Subroutine check_option_names: not called CC=5 SCAR=17.0
Pod undocumented
759
760 # Check if options exists
761 807 ▶ unless ($options) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($options) 0 3110 3110
unless ($options) 0 67307 67307
762 0 return;
763 }
764
765 # Create a hash for fast lookup
766 0 my $available_option_names_h = {};
767 213 for my $available_option_name (@$available_option_names) {
768 1247 $available_option_names_h->{$available_option_name} = 1;
769 }
770
771 # Validate each option name (Iterate through hash keys)
772 1247 for my $option_name (keys %$options) {
773 # Check if the option name is available
774 1247 ▶ unless ($available_option_names_h->{$option_name}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($available_option_names_h->{$option_name}) 0 8384 8384
unless ($available_option_names_h->{$option_name}) 0 200402 200402
775 0 Carp::confess("'$option_name' option is not available.");
776 }
777 }
778 }
779  
780 2705 ▶sub normalize_path {
Subroutine normalize_path: called CC=3 SCAR=11.7
Pod undocumented
781 1043 ▶ my ($path, $base_dir) = @_;
Subroutine normalize_path: called CC=3 SCAR=11.7
Pod undocumented
782
783 1247 ▶ unless (defined $path) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $path) 0 242 242
unless (defined $path) 0 2705 2705
784 0 confess("The path \$path must be defined.");
785 }
786
787 1247 ▶ unless (defined $base_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $base_dir) 0 242 242
unless (defined $base_dir) 0 2705 2705
788 0 confess("The base directory \$base_dir must be defined.");
789 }
790
791 2 my $abs_path = File::Spec->rel2abs($path);
792 2 my $abs_base_dir = File::Spec->rel2abs($base_dir);
793
794 2 my $normalized_path = File::Spec->abs2rel($abs_path, $abs_base_dir);
795 0 $normalized_path =~ s|\\|/|g;
796
797 0 return $normalized_path;
798 }
799  
800 2401575 ▶sub quote_literal {
Subroutine quote_literal: called CC=6 SCAR=27.4
Pod undocumented
801 0 ▶ my ($string) = @_;
Subroutine quote_literal: not called CC=6 SCAR=27.4
Pod undocumented
802  
803 2 ▶ if (SPVM::Builder::Util::is_windows()) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_windows()) { } 0 256748 256748
if (is_windows()) { } 0 2401575 2401575
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 0 ▶ if (length $string && $string !~ /[^a-zA-Z0-9,._+@%\/-]/) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]) 238598 18150 256748
if (length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]) 2203156 198419 2401575
Condition66%4/6
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
!ll&&!rl&&r
018150238598
length $string and not $string =~ m[[^a-zA-Z0-9,._+\@%/-]]
!ll&&!rl&&r
01984192203156
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 0 return $string;
818 }
819  
820 0 $string =~ s{'}{'\\''}g;
821
822 0 my $quoted_string = "'$string'";
823
824 0 return $quoted_string;
825 }
826 }
827  
828 133 ▶sub setup_spvm_command_environment {
Subroutine setup_spvm_command_environment: called CC=6 SCAR=23.5
Pod undocumented
829 0 ▶ my ($script_name, $source, $blib, $class_search_dirs, $build_dir, $quiet, $no_quiet, $warning, $force) = @_;
Subroutine setup_spvm_command_environment: not called CC=6 SCAR=23.5
Pod undocumented
830
831 0 $0 = $script_name;
832 0 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) 0 2 2
if (defined $build_dir) 40 93 133
837 0 $ENV{SPVM_BUILD_DIR} = $build_dir;
838 }
839
840 0 ▶ if (defined $quiet) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $quiet) { } 1 1 2
elsif (defined $no_quiet) { } 0 1 1
if (defined $quiet) { } 112 21 133
elsif (defined $no_quiet) { } 0 21 21
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 2 2
if (defined $force) 0 133 133
848 0 $ENV{SPVM_FORCE_BUILD} = !!$force;
849 }
850
851 0 ▶ if ($warning) {
Branch: 2 branches
BranchTrueFalseTotal
if ($warning) 0 2 2
if ($warning) 0 133 133
852 0 $^W = 1;
853 }
854 }
855  
856 133 ▶sub resolve_spvm_command_inc {
Subroutine resolve_spvm_command_inc: called CC=2 SCAR=7.6
Pod undocumented
857 0 ▶ my ($current_inc, $source, $blib, $class_search_dirs) = @_;
Subroutine resolve_spvm_command_inc: not called CC=2 SCAR=7.6
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) 0 2 2
if ($blib) 1 132 133
864 # Temporarily isolate @INC to get paths from the blib module
865 0 local @INC;
866 0 require blib;
867 0 blib->import;
868 0 @blib_class_search_dirs = @INC;
869 }
870
871 # Final include directories
872 62 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 62 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 62 unshift @inc, @$class_search_dirs_by_lib_directive;
885
886 62 return \@inc;
887 }
888  
889 2441999 ▶sub is_windows { $^O eq 'MSWin32' }
Subroutine is_windows: called CC=1 SCAR=0.0
Pod undocumented
890  
891  
892 sub search_gnu_make_command {
893 11 ▶ my @commands = ('gmake', 'make');
Subroutine search_gnu_make_command: called CC=5 SCAR=16.4
Pod undocumented
894  
895 11 for my $command (@commands) {
896 11 my $tempdir_obj = File::Temp->newdir;
897 11 my $tempdir = $tempdir_obj->dirname;
898 11 my $filename = "$tempdir/Makefile";
899  
900 73 ▶ if (open my $fh, '>', $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, ">", $filename) 11 0 11
901 3121 print $fh "all:\n\t\@: # do nothing\n";
902 3121 print $fh "\$(info gnu-make-confirmed)\n";
903 11 close $fh;
904  
905 3121 my $output = `$command -f $filename 2>&1`;
906 3121 my $exit_code = $?;
907  
908 180180 ▶ 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 3121 return $command;
910 }
911 }
912 }
913  
914 8384 return undef;
915 }
916  
917 sub build_parallel_libspvm {
918 0 ▶ my ($options) = @_;
Subroutine build_parallel_libspvm: called CC=4 SCAR=16.9
Pod undocumented
919
920 # For recursive loading
921 242 require SPVM::Builder;
922
923 242 my @new_option_names = (
924 'build_dir',
925 'output_dir',
926 'jobs',
927 );
928
929 0 my @build_parallel_option_names = (
930 'config_global_file',
931 );
932
933 242 my @all_option_names = (@new_option_names, @build_parallel_option_names);
934 0 SPVM::Builder::Util::check_option_names($options, \@all_option_names);
935
936 242 my %is_new_option = map { $_ => 1 } @new_option_names;
937
938 242 my $new_options = {};
939 242 my $build_parallel_options = {};
940
941 242 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 0 0 0
942 256748 for my $key (keys %$options) {
943 256748 ▶ if ($is_new_option{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_new_option{$key}) { } 0 0 0
944 0 $new_options->{$key} = $options->{$key};
945 }
946 else {
947 0 $build_parallel_options->{$key} = $options->{$key};
948 }
949 }
950 }
951
952 0 my $builder = SPVM::Builder->new(%$new_options);
953 0 my $link_info = $builder->prepare_compile_spvm_core_source_files($build_parallel_options);
954 0 $link_info->config->category('libspvm');
955 0 $link_info->config->class_name('libspvm');
956
957 256748 $link_info->dl_func_list(['SPVM_NATIVE_new_env']);
958
959 238598 my $diagnostic_message = "[Generate Static Link Library for libpsvm]";
960 18150 $link_info->diagnostic_message($diagnostic_message);
961 18150 $link_info->config->output_type('static_lib');
962
963 18150 my $link_infos = [$link_info];
964
965 2 $builder->build_parallel_with_link_infos($link_infos, $options);
966 }
967  
968 sub create_make_rule_parallel_libspvm {
969 2 ▶ my ($options) = @_;
Subroutine create_make_rule_parallel_libspvm: called CC=10 SCAR=35.2
Pod undocumented
970  
971 2 ▶ $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 2 my $make_rule = '';
974  
975 # Gen target name
976 2 my $target = "blib/lib/SPVM/Builder/lib/libspvm.a";
977  
978 # Order-only deps
979 2 ▶ 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 0 ▶ my $dependent_files_string = @$dependent_files ? " " . join(' ', @$dependent_files) : "";
Branch: 1 branch
BranchTrueFalseTotal
@$dependent_files ? : 0 0 0
981
982 2 ▶ 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 1 ▶ 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 0 $make_rule .= "dynamic :: $target\n";
987 2 $make_rule .= "\t\$(NOECHO) \$(NOOP)\n\n";
988  
989 # Parallel build rule
990 0 $make_rule .= ".PHONY: $target\n";
991 2 $make_rule .= "$target :$dependent_files_string$order_only_dependent_files_string\n";
992  
993 # Collect all build options
994 0 my @build_options;
995
996 2 ▶ 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 2 push @build_options, "build_dir => '$build_dir'";
998
999 2 ▶ 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 2 push @build_options, "output_dir => '$output_dir'";
1001
1002 0 ▶ if (defined(my $jobs = $options->{jobs})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $jobs = $options->{"jobs"})) 0 0 0
1003 0 push @build_options, "jobs => $jobs";
1004 }
1005
1006 0 ▶ 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 0 push @build_options, "config_global_file => '$config_global_file'";
1008 }
1009
1010 2 my $build_options_hash_str = "{" . join(', ', @build_options) . "}";
1011  
1012 # Build cmd
1013 2 $make_rule .= "\t\$(FULLPERLRUN) -Mblib -MSPVM::Builder::Util -e \"SPVM::Builder::Util::build_parallel_libspvm($build_options_hash_str)\"\n\n";
1014  
1015 2 return $make_rule;
1016 }
1017  
1018 sub is_msvc {
1019 70 ▶ my ($cc) = @_;
Subroutine is_msvc: called CC=3 SCAR=11.6
Pod undocumented
1020
1021 70 ▶ unless (defined $cc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cc) 0 90 90
1022 258783 confess("Compiler \$cc must be defined.");
1023 }
1024
1025 68 my $is_msvc = 0;
1026 68 ▶ if ($cc =~ /cl(\.exe)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc =~ /cl(\.exe)?$/) 0 90 90
1027 0 $is_msvc = 1;
1028 }
1029
1030 68 return $is_msvc;
1031 }
1032  
1033 sub is_gcc_compatible {
1034 0 ▶ my ($cc) = @_;
Subroutine is_gcc_compatible: called CC=2 SCAR=17.9
Pod undocumented
1035
1036 0 my $is_gcc_compatible = 0;
1037 0 ▶ if ($cc =~ /(?:gcc|clang|g\+\+|clang\+\+)(?:\.exe)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc =~ /(?:gcc|clang|g\+\+|clang\+\+)(?:\.exe)?$/) 0 0 0
1038 0 $is_gcc_compatible = 1;
1039 }
1040
1041 0 return $is_gcc_compatible;
1042 }
1043  
1044  
1045 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/Config.pm ↑ Summary blib/lib/SPVM/ExchangeAPI.pm »