blib/lib/SPVM/Builder.pm

filter: Statementstmt 46.4% 484 / 1042 Branchbran 33.3% 108 / 324 Conditioncond 32.1% 36 / 112 MC/DCmcdc 23.6% 17 / 72 Subroutinesub 55.0% 44 / 80 Podpod 0.0% 0 / 48 totaltotal 41.0% 689 / 1678 CC 233 SCAR 41.4
CC
233
Cov
42%
finalize_link_info
143.1
finalize_link_info
143.1
build_parallel_with_link_infos
129.2
CRAP 62.5
« blib/lib/SPVM/Builder/Config/Global/MSVC.pm ↑ Summary blib/lib/SPVM/Builder/Config/Linker.pm »
1 package SPVM::Builder;
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 684 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 684 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 684 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 684 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 684 ▶use File::Copy 'copy', 'move';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 684 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 684 ▶use File::Find 'find';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 684 ▶use File::Basename 'dirname', 'basename';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 684 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 684 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 684 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 684 ▶use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
19 684 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 684 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 684 ▶use SPVM::Builder::Config::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 684 ▶use SPVM::Builder::CompileInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 684 ▶use SPVM::Builder::LinkInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 684 ▶use SPVM::Builder::ObjectFileInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 684 ▶use SPVM::Builder::Ninja;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 684 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Fields
31 684 has [qw(
32 build_dir
33 jobs
34
35 quiet
36 work_dir
37 class_search_dirs
38 global_lock_fh
39 ninja
40 is_jit
41 output_dir
42 )];
43  
44 1012 ▶sub import {
Subroutine import: called CC=2 SCAR=9.2
45 0 ▶ my $build_dir = SPVM::Builder::Util::get_normalized_env('SPVM_BUILD_DIR');
Subroutine import: not called CC=2 SCAR=9.2
46 0 ▶ unless (defined $build_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_dir) 459 553 1012
unless (defined $build_dir) 0 0 0
47 0 my $build_tmp_dir = File::Temp->newdir(CLEANUP => 0);
48 0 $SPVM::Global::BUILD_TMP_DIR = $build_tmp_dir;
49 0 $ENV{SPVM_BUILD_DIR} = $build_tmp_dir->dirname;
50 }
51 }
52  
53 # Class Methods
54 1971 ▶sub new {
Subroutine new: called CC=4 SCAR=20.3
Pod undocumented
55 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=20.3
Pod undocumented
56
57 my $self = {
58 build_dir => $ENV{SPVM_BUILD_DIR},
59 0 jobs => SPVM::Builder::Util::get_cpu_count() + 2,
60 class_search_dirs => \@INC,
61 work_dir => 'work',
62 @_
63 };
64
65 0 ▶ bless $self, ref $class || $class;
Condition16%1/6
ref $class || $class
l!l&&r!l&&!r
019710
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
66
67 0 my $build_dir = $self->build_dir;
68
69 0 mkpath $build_dir;
70
71 # Ensure the global lock file is opened once
72 0 my $global_lock_file = $self->get_global_lock_file;
73
74 # Open with append mode to avoid truncating existing data
75 0 ▶ open my $global_lock_fh, '>>', $global_lock_file
Branch: 2 branches
BranchTrueFalseTotal
open my $global_lock_fh, ">>", $global_lock_file or die "Can't open global lock file '${global_lock_file}': $!" 1971 0 1971
open my $global_lock_fh, ">>", $global_lock_file or die "Can't open global lock file '${global_lock_file}': $!" 0 0 0
76 or die "Can't open global lock file '$global_lock_file': $!";
77
78 0 $self->{global_lock_fh} = $global_lock_fh;
79
80 0 ▶ unless (exists $self->{ninja}) {
Branch: 2 branches
BranchTrueFalseTotal
unless (exists $self->{"ninja"}) 1971 0 1971
unless (exists $self->{"ninja"}) 0 0 0
81 0 $self->{ninja} = SPVM::Builder::Ninja->new(builder => $self);
82 }
83
84 0 return $self;
85 }
86  
87 # Instance Methods
88 1151 ▶sub build_parallel_with_link_infos {
Subroutine build_parallel_with_link_infos: called CC=26 SCAR=48.6
Pod undocumented
89 0 ▶ my ($self, $link_infos, $options) = @_;
Subroutine build_parallel_with_link_infos: not called CC=26 SCAR=48.6
Pod undocumented
90
91 0 ▶ $options ||= {};
Condition25%1/4
$options ||= {}
l!l
11510
$options ||= {}
l!l
00
MC/DC0%0/1
$options ||= {}
$options
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
Truth table0%0/2
A
A= $options
Aresult
11
00
92
93 0 my $config_global;
94 0 ▶ if (exists $options->{config_global_file} && exists $options->{config_global}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options->{"config_global_file"} and exists $options->{"config_global"}) 0 1151 1151
if (exists $options->{"config_global_file"} and exists $options->{"config_global"}) 0 0 0
Condition33%2/6
exists $options->{"config_global_file"} and exists $options->{"config_global"}
!ll&&!rl&&r
115010
exists $options->{"config_global_file"} and exists $options->{"config_global"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $options->{"config_global_file"} and exists $options->{"config_global"}
exists $options->{"config_global_file"} exists $options->{"config_global"}
MC/DC0%0/2
exists $options->{"config_global_file"} and exists $options->{"config_global"}
exists $options->{"config_global_file"} exists $options->{"config_global"}
Truth table66%2/3
A and B
A= exists $options->{"config_global_file"}B= exists $options->{"config_global"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= exists $options->{"config_global_file"}B= exists $options->{"config_global"}
ABresult
0X0
100
111
95 0 confess("'config_global_file' and 'config_global' cannot be used at the same time.");
96 }
97 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'})) { } 1 1150 1151
if (defined(my $config_global_file = $options->{'config_global_file'})) { } 0 0 0
98 0 $config_global = SPVM::Builder::Config::Util::load_config_global($config_global_file);
99 }
100 else {
101 0 $config_global = $options->{config_global};
102 }
103
104 0 my @all_compile_infos;
105 0 for my $link_info (@$link_infos) {
106 0 my $compile_infos = $link_info->compile_infos;
107 0 for my $compile_info (@$compile_infos) {
108 0 my $config = $compile_info->config;
109
110 0 ▶ if ($config_global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config_global) 7567 603 8170
if ($config_global) 0 0 0
111 0 $compile_info->config->global($config_global);
112 }
113
114 0 my $env_spvm_force_build_type = SPVM::Builder::Util::get_normalized_env('SPVM_FORCE_BUILD_TYPE');
115 0 ▶ if (length $env_spvm_force_build_type) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $env_spvm_force_build_type) 412 7758 8170
if (length $env_spvm_force_build_type) 0 0 0
116 0 $compile_info->config->global->build_type($env_spvm_force_build_type);
117 }
118
119 0 $config->global->apply_build_rules($compile_info->config);
120
121 0 $self->finalize_compile_info($compile_info);
122
123 0 push @all_compile_infos, $compile_info;
124 }
125 }
126
127 # Execute all compilations in parallel
128 0 $self->command_parallel(\@all_compile_infos);
129
130 # link_to
131 0 my $new_link_infos = [];
132 0 my $moved_compile_infos_h = {};
133 0 my $moved_dl_func_list_h = {};
134 0 my $link_to_h = {};
135 0 for my $link_info (@$link_infos) {
136 0 my $config = $link_info->config;
137 0 my $compile_infos = $link_info->compile_infos;
138 0 ▶ my $dl_func_list = $link_info->dl_func_list // [];
Condition25%1/4
$link_info->dl_func_list // []
l!l
2240
$link_info->dl_func_list // []
l!l
00
MC/DC0%0/1
$link_info->dl_func_list // []
$link_info->dl_func_list
MC/DC0%0/1
$link_info->dl_func_list // []
$link_info->dl_func_list
Truth table50%1/2
A
A= $link_info->dl_func_list
Aresult
11
00
Truth table0%0/2
A
A= $link_info->dl_func_list
Aresult
11
00
139 0 my $link_to = $config->link_to;
140 0 my $class_name = $config->class_name;
141
142 0 ▶ if ($link_to) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link_to) 13 211 224
if ($link_to) 0 0 0
143 0 my ($found_link_to) = grep { $link_to eq $_->config->class_name } @$link_infos;
144 0 ▶ unless ($found_link_to) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($found_link_to) 0 13 13
unless ($found_link_to) 0 0 0
145 0 Carp::confess("Cannot find $link_to class specifed by link_to field.");
146 }
147
148 0 ▶ $moved_dl_func_list_h->{$link_to} //= [];
Condition50%2/4
$moved_dl_func_list_h->{$link_to} //= []
l!l
211
$moved_dl_func_list_h->{$link_to} //= []
l!l
00
MC/DC100%1/1
$moved_dl_func_list_h->{$link_to} //= []
$moved_dl_func_list_h->{$link_to}
MC/DC0%0/1
$moved_dl_func_list_h->{$link_to} //= []
$moved_dl_func_list_h->{$link_to}
Truth table100%2/2
A
A= $moved_dl_func_list_h->{$link_to}
Aresult
11
00
Truth table0%0/2
A
A= $moved_dl_func_list_h->{$link_to}
Aresult
11
00
149 0 push @{$moved_dl_func_list_h->{$link_to}}, @$dl_func_list;
150 0 $link_info->dl_func_list([]);
151
152 0 ▶ $moved_compile_infos_h->{$link_to} //= [];
Condition50%2/4
$moved_compile_infos_h->{$link_to} //= []
l!l
211
$moved_compile_infos_h->{$link_to} //= []
l!l
00
MC/DC100%1/1
$moved_compile_infos_h->{$link_to} //= []
$moved_compile_infos_h->{$link_to}
MC/DC0%0/1
$moved_compile_infos_h->{$link_to} //= []
$moved_compile_infos_h->{$link_to}
Truth table100%2/2
A
A= $moved_compile_infos_h->{$link_to}
Aresult
11
00
Truth table0%0/2
A
A= $moved_compile_infos_h->{$link_to}
Aresult
11
00
153 0 push @{$moved_compile_infos_h->{$link_to}}, @$compile_infos;
154 0 $link_info->compile_infos([]);
155
156 0 $link_to_h->{$class_name} = $link_to;
157 }
158
159 0 ▶ if (!defined $link_to || $link_to eq $class_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $link_to or $link_to eq $class_name) 222 2 224
if (not defined $link_to or $link_to eq $class_name) 0 0 0
Condition50%3/6
not defined $link_to or $link_to eq $class_name
l!l&&r!l&&!r
211112
not defined $link_to or $link_to eq $class_name
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $link_to or $link_to eq $class_name
not defined $link_to $link_to eq $class_name
MC/DC0%0/2
not defined $link_to or $link_to eq $class_name
not defined $link_to $link_to eq $class_name
Truth table100%3/3
not(A) or B
A= not defined $link_toB= $link_to eq $class_name
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $link_toB= $link_to eq $class_name
ABresult
1X1
011
000
160 0 push @$new_link_infos, $link_info;
161 }
162 }
163 0 $link_infos = $new_link_infos;
164 0 for my $link_info (@$link_infos) {
165 0 my $class_name = $link_info->config->class_name;
166
167 0 my $moved_compile_infos = $moved_compile_infos_h->{$class_name};
168 0 ▶ if ($moved_compile_infos) {
Branch: 2 branches
BranchTrueFalseTotal
if ($moved_compile_infos) 11 211 222
if ($moved_compile_infos) 0 0 0
169 0 push @{$link_info->compile_infos}, @$moved_compile_infos;
170 }
171
172 0 my $moved_dl_func_list = $moved_dl_func_list_h->{$class_name};
173 0 ▶ if ($moved_dl_func_list) {
Branch: 2 branches
BranchTrueFalseTotal
if ($moved_dl_func_list) 11 211 222
if ($moved_dl_func_list) 0 0 0
174 0 push @{$link_info->dl_func_list}, @$moved_dl_func_list;
175 }
176 }
177
178 # Prepare all link information
179 0 my @all_link_infos;
180 0 for my $link_info (@$link_infos) {
181 0 my $config = $link_info->config;
182
183 0 ▶ if ($config_global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config_global) 3 219 222
if ($config_global) 0 0 0
184 0 $link_info->config->global($config_global);
185 }
186
187 0 $config->global->apply_build_rules($link_info->config);
188
189 0 $self->finalize_link_info($link_info);
190
191 0 push @all_link_infos, $link_info;
192 }
193
194 # Execute all links in parallel
195 0 $self->command_parallel(\@all_link_infos);
196
197 # Execute after_link_cbs
198 0 for my $link_info (@all_link_infos) {
199 0 my $config = $link_info->config;
200 0 my $after_link_cbs = $config->global->after_link_cbs;
201 0 for my $after_link_cb (@$after_link_cbs) {
202 0 $after_link_cb->($link_info);
203 }
204 }
205
206 # Store result in the return hash
207 0 my $output_files_h = {};
208 0 for my $link_info (@all_link_infos) {
209 0 my $config = $link_info->config;
210 0 my $class_name = $config->class_name;
211 0 my $category = $config->category;
212 0 $output_files_h->{$category}{$class_name} = $link_info->output_file;
213
214 0 for my $merged_class_name (keys %$link_to_h) {
215 0 $output_files_h->{$category}{$merged_class_name} = $link_info->output_file;
216 }
217 }
218
219 0 return $output_files_h;
220 }
221  
222 0 ▶sub create_build_work_path {
Subroutine create_build_work_path: not called CC=2 SCAR=17.9
Pod undocumented
223 0 ▶ my ($self, $rel_file) = @_;
Subroutine create_build_work_path: not called CC=2 SCAR=17.9
Pod undocumented
224
225 0 my $build_dir = $self->build_dir;
226
227 0 my $work_dir = $self->work_dir;
228
229 0 my $build_work_path = "$build_dir/$work_dir";
230 0 ▶ if (defined $rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_file) 0 0 0
if (defined $rel_file) 0 0 0
231 0 $build_work_path .= "/$rel_file";
232 }
233
234 0 return $build_work_path;
235 }
236  
237 770 ▶sub create_build_src_path {
Subroutine create_build_src_path: called CC=2 SCAR=9.2
Pod undocumented
238 0 ▶ my ($self, $rel_file) = @_;
Subroutine create_build_src_path: not called CC=2 SCAR=9.2
Pod undocumented
239
240 0 my $build_dir = $self->build_dir;
241
242 0 my $work_dir = $self->work_dir;
243
244 0 my $build_src_path = "$build_dir/$work_dir/src";
245 0 ▶ if (defined $rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_file) 647 123 770
if (defined $rel_file) 0 0 0
246 0 $build_src_path .= "/$rel_file";
247 }
248
249 0 return $build_src_path;
250 }
251  
252 0 ▶sub create_build_include_path {
Subroutine create_build_include_path: not called CC=2 SCAR=17.9
Pod undocumented
253 0 ▶ my ($self, $rel_file) = @_;
Subroutine create_build_include_path: not called CC=2 SCAR=17.9
Pod undocumented
254
255 0 my $build_dir = $self->build_dir;
256
257 0 my $work_dir = $self->work_dir;
258
259 0 my $build_include_path = "$build_dir/$work_dir/include";
260 0 ▶ if (defined $rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_file) 0 0 0
if (defined $rel_file) 0 0 0
261 0 $build_include_path .= "/$rel_file";
262 }
263
264 0 return $build_include_path;
265 }
266  
267 8170 ▶sub create_build_object_path {
Subroutine create_build_object_path: called CC=2 SCAR=10.7
Pod undocumented
268 0 ▶ my ($self, $rel_file) = @_;
Subroutine create_build_object_path: not called CC=2 SCAR=10.7
Pod undocumented
269
270 0 my $build_dir = $self->build_dir;
271
272 0 my $work_dir = $self->work_dir;
273
274 0 my $build_output_path = "$build_dir/$work_dir/object";
275 0 ▶ if (defined $rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_file) 0 8170 8170
if (defined $rel_file) 0 0 0
276 0 $build_output_path .= "/$rel_file";
277 }
278
279 0 return $build_output_path;
280 }
281  
282 219 ▶sub create_build_lib_path {
Subroutine create_build_lib_path: called CC=2 SCAR=9.9
Pod undocumented
283 0 ▶ my ($self, $rel_file) = @_;
Subroutine create_build_lib_path: not called CC=2 SCAR=9.9
Pod undocumented
284
285 0 my $build_dir = $self->build_dir;
286
287 0 my $work_dir = $self->work_dir;
288
289 0 my $build_lib_path = "$build_dir/$work_dir/lib";
290 0 ▶ if (defined $rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rel_file) 219 0 219
if (defined $rel_file) 0 0 0
291 0 $build_lib_path .= "/$rel_file";
292 }
293
294 0 return $build_lib_path;
295 }
296  
297 23608 ▶sub global_file_lock {
Subroutine global_file_lock: called CC=3 SCAR=16.0
Pod undocumented
298 0 ▶ my ($self, $cb) = @_;
Subroutine global_file_lock: not called CC=3 SCAR=16.0
Pod undocumented
299
300 0 my $lock_fh = $self->{global_lock_fh};
301
302 # Wait for an exclusive lock (Blocking)
303 0 ▶ flock($lock_fh, LOCK_EX)
Branch: 2 branches
BranchTrueFalseTotal
flock $lock_fh, 2 or die "[Internal Error]Can't get exclusive lock on global lock file: $!" 23608 0 23608
flock $lock_fh, 2 or die "[Internal Error]Can't get exclusive lock on global lock file: $!" 0 0 0
304 or die "[Internal Error]Can't get exclusive lock on global lock file: $!";
305
306 # Execute the callback and ensure unlock happens even on failure
307 0 my $result;
308 my $error;
309 0 eval {
310 0 $result = $cb->();
311 };
312 0 $error = $@;
313
314 # Release the lock but keep the file handle for future reuse
315 0 flock($lock_fh, LOCK_UN);
316
317 # Rethrow if something went wrong inside the callback
318 0 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 23608 23608
if ($error) 0 0 0
319 0 die $error;
320 }
321
322 0 return $result;
323 }
324  
325 1971 ▶sub global_file_read_lock {
Subroutine global_file_read_lock: called CC=3 SCAR=16.0
Pod undocumented
326 0 ▶ my ($self, $cb) = @_;
Subroutine global_file_read_lock: not called CC=3 SCAR=16.0
Pod undocumented
327
328 0 my $lock_fh = $self->{global_lock_fh};
329
330 # Wait for an exclusive read lock (Blocking)
331 0 ▶ flock($lock_fh, LOCK_SH)
Branch: 2 branches
BranchTrueFalseTotal
flock $lock_fh, 1 or die "[Internal Error]Can't get exclusive lock on global lock file: $!" 1971 0 1971
flock $lock_fh, 1 or die "[Internal Error]Can't get exclusive lock on global lock file: $!" 0 0 0
332 or die "[Internal Error]Can't get exclusive lock on global lock file: $!";
333
334 # Execute the callback and ensure unlock happens even on failure
335 0 my $result;
336 my $error;
337 0 eval {
338 0 $result = $cb->();
339 };
340 0 $error = $@;
341
342 # Release the lock but keep the file handle for future reuse
343 0 flock($lock_fh, LOCK_UN);
344
345 # Rethrow if something went wrong inside the callback
346 0 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) 0 1971 1971
if ($error) 0 0 0
347 0 die $error;
348 }
349
350 0 return $result;
351 }
352  
353 1971 ▶sub get_global_lock_file {
Subroutine get_global_lock_file: called CC=1 SCAR=1.2
Pod undocumented
354 0 ▶ my ($self) = @_;
Subroutine get_global_lock_file: not called CC=1 SCAR=1.2
Pod undocumented
355
356 0 my $build_dir = $self->build_dir;
357
358 0 my $global_lock_file = "$build_dir/.global.lock";
359
360 0 return $global_lock_file;
361 }
362  
363 3417 ▶sub detect_quiet {
Subroutine detect_quiet: called CC=6 SCAR=24.2
Pod undocumented
364 0 ▶ my ($self, $config) = @_;
Subroutine detect_quiet: not called CC=6 SCAR=24.2
Pod undocumented
365
366 0 my $quiet;
367
368 0 ▶ if (length (my $env_spvm_force_quiet = SPVM::Builder::Util::get_normalized_env('SPVM_FORCE_QUIET'))) {
Branch: 8 branches
BranchTrueFalseTotal
if (length(my $env_spvm_force_quiet = SPVM::Builder::Util::get_normalized_env('SPVM_FORCE_QUIET'))) { } 2624 793 3417
elsif (length $self->quiet) { } 3 790 793
elsif (defined $config and length $config->{'quiet'}) { } 3 787 790
elsif ($self->is_jit) { } 681 106 787
if (length(my $env_spvm_force_quiet = SPVM::Builder::Util::get_normalized_env('SPVM_FORCE_QUIET'))) { } 0 0 0
elsif (length $self->quiet) { } 0 0 0
elsif (defined $config and length $config->{'quiet'}) { } 0 0 0
elsif ($self->is_jit) { } 0 0 0
369 0 $quiet = $env_spvm_force_quiet;
370 }
371 elsif (length $self->quiet) {
372 0 $quiet = $self->quiet;
373 }
374 ▶ elsif (defined $config && length $config->{quiet}) {
Condition33%2/6
defined $config and length $config->{"quiet"}
!ll&&!rl&&r
07873
defined $config and length $config->{"quiet"}
!ll&&!rl&&r
000
MC/DC50%1/2
defined $config and length $config->{"quiet"}
defined $config length $config->{"quiet"}
MC/DC0%0/2
defined $config and length $config->{"quiet"}
defined $config length $config->{"quiet"}
Truth table66%2/3
A and B
A= defined $configB= length $config->{"quiet"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $configB= length $config->{"quiet"}
ABresult
0X0
100
111
375 0 $quiet = $config->quiet;
376 }
377 elsif ($self->is_jit) {
378 0 $quiet = 1;
379 }
380 else {
381 0 $quiet = 0;
382 }
383
384 0 return $quiet;
385 }
386  
387 2044 ▶sub command_parallel {
Subroutine command_parallel: called CC=18 SCAR=41.2
Pod undocumented
388 0 ▶ my ($self, $command_infos) = @_;
Subroutine command_parallel: not called CC=18 SCAR=41.2
Pod undocumented
389
390 # Remove duplicate commands based on command_hash
391 0 my @unique_command_infos;
392 my %seen_command_hashes_h;
393 0 for my $command_info (@$command_infos) {
394 0 my $hash = $command_info->command_hash;
395 0 ▶ unless ($seen_command_hashes_h{$hash}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($seen_command_hashes_h{$hash}) 8380 12 8392
unless ($seen_command_hashes_h{$hash}) 0 0 0
396 0 push @unique_command_infos, $command_info;
397 0 $seen_command_hashes_h{$hash} = 1;
398 }
399 }
400
401 0 my $max_jobs = $self->jobs;
402 0 my @waiting_command_infos = @unique_command_infos;
403 0 my %running_processes;
404 my %errors_h;
405
406 # Main loop for parallel processing
407 0 ▶ while (@waiting_command_infos || %running_processes) {
Branch: 2 branches
BranchTrueFalseTotal
if (@waiting_command_infos or %running_processes) 110206 1700 111906
if (@waiting_command_infos or %running_processes) 0 0 0
Condition50%3/6
@waiting_command_infos or %running_processes
l!l&&r!l&&!r
88671013391700
@waiting_command_infos or %running_processes
l!l&&r!l&&!r
000
MC/DC100%2/2
@waiting_command_infos or %running_processes
@waiting_command_infos %running_processes
MC/DC0%0/2
@waiting_command_infos or %running_processes
@waiting_command_infos %running_processes
Truth table100%3/3
A or B
A= @waiting_command_infosB= %running_processes
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= @waiting_command_infosB= %running_processes
ABresult
1X1
011
000
408
409 # Spawn new processes if slots are available
410 0 ▶ while (@waiting_command_infos && keys %running_processes < $max_jobs) {
Branch: 2 branches
BranchTrueFalseTotal
if (@waiting_command_infos and keys %running_processes < $max_jobs) 3410 109869 113279
if (@waiting_command_infos and keys %running_processes < $max_jobs) 0 0 0
Condition50%3/6
@waiting_command_infos and keys %running_processes < $max_jobs
!ll&&!rl&&r
10159582743410
@waiting_command_infos and keys %running_processes < $max_jobs
!ll&&!rl&&r
000
MC/DC100%2/2
@waiting_command_infos and keys %running_processes < $max_jobs
@waiting_command_infos keys %running_processes < $max_jobs
MC/DC0%0/2
@waiting_command_infos and keys %running_processes < $max_jobs
@waiting_command_infos keys %running_processes < $max_jobs
Truth table100%3/3
A and B
A= @waiting_command_infosB= keys %running_processes < $max_jobs
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= @waiting_command_infosB= keys %running_processes < $max_jobs
ABresult
0X0
100
111
411 0 my $command_info = shift @waiting_command_infos;
412 0 my $spawn_method_name;
413
414 0 ▶ if ($command_info->isa('SPVM::Builder::CompileInfo')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($command_info->isa('SPVM::Builder::CompileInfo')) { } 3208 202 3410
elsif ($command_info->isa('SPVM::Builder::LinkInfo')) { } 202 0 202
if ($command_info->isa('SPVM::Builder::CompileInfo')) { } 0 0 0
elsif ($command_info->isa('SPVM::Builder::LinkInfo')) { } 0 0 0
415 0 $spawn_method_name = 'spawn_compile_source_file';
416 }
417 elsif ($command_info->isa('SPVM::Builder::LinkInfo')) {
418 0 $spawn_method_name = 'spawn_link';
419 }
420 else {
421 0 confess("[Unexpected Error]Invalid class")
422 }
423
424 0 my $process_id = $self->$spawn_method_name($command_info);
425
426 0 ▶ if (defined $process_id && $process_id > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $process_id and $process_id > 0) 2351 722 3073
if (defined $process_id and $process_id > 0) 0 0 0
Condition50%3/6
defined $process_id and $process_id > 0
!ll&&!rl&&r
87142351
defined $process_id and $process_id > 0
!ll&&!rl&&r
000
MC/DC100%2/2
defined $process_id and $process_id > 0
defined $process_id $process_id > 0
MC/DC0%0/2
defined $process_id and $process_id > 0
defined $process_id $process_id > 0
Truth table100%3/3
A and B
A= defined $process_idB= $process_id > 0
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $process_idB= $process_id > 0
ABresult
0X0
100
111
427 0 $running_processes{$process_id} = $command_info;
428 }
429 }
430
431 # Check status of running processes
432 0 for my $process_id (keys %running_processes) {
433 0 my $command_info = $running_processes{$process_id};
434
435 # Check if the command has finished
436 0 my $wait_command_status;
437 0 eval {
438 0 $wait_command_status = $self->wait_command($command_info);
439 };
440
441 0 my $error = $@;
442 0 my $process_finished;
443 0 ▶ if ($error) {
Branch: 4 branches
BranchTrueFalseTotal
if ($error) { } 22 258946 258968
elsif ($wait_command_status != 0) { } 1087 257859 258946
if ($error) { } 0 0 0
elsif ($wait_command_status != 0) { } 0 0 0
444 0 $process_finished = 1;
445 0 @waiting_command_infos = ();
446 0 $max_jobs = 0;
447 0 $errors_h{$process_id} = $error;
448 }
449 elsif ($wait_command_status != 0) {
450 0 $process_finished = 1;
451 0 $self->add_ninja_log($command_info);
452 }
453
454 0 ▶ if ($process_finished) {
Branch: 2 branches
BranchTrueFalseTotal
if ($process_finished) 1109 257859 258968
if ($process_finished) 0 0 0
455 0 $command_info->process_id(undef);
456 0 delete $running_processes{$process_id};
457 }
458 }
459
460 0 ▶ if (!keys %running_processes && keys %errors_h) {
Branch: 2 branches
BranchTrueFalseTotal
if (not keys %running_processes and keys %errors_h) 7 109862 109869
if (not keys %running_processes and keys %errors_h) 0 0 0
Condition50%3/6
not keys %running_processes and keys %errors_h
!ll&&!rl&&r
1096102527
not keys %running_processes and keys %errors_h
!ll&&!rl&&r
000
MC/DC100%2/2
not keys %running_processes and keys %errors_h
not keys %running_processes keys %errors_h
MC/DC0%0/2
not keys %running_processes and keys %errors_h
not keys %running_processes keys %errors_h
Truth table100%3/3
not(A) and B
A= not keys %running_processesB= keys %errors_h
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not keys %running_processesB= keys %errors_h
ABresult
0X0
100
111
461 0 my $errors_count = keys %errors_h;
462 0 confess "$errors_count commands failed.";
463 }
464
465 # Sleep to reduce CPU usage if processes are still running
466 0 ▶ if (%running_processes) {
Branch: 2 branches
BranchTrueFalseTotal
if (%running_processes) 109610 252 109862
if (%running_processes) 0 0 0
467 0 Time::HiRes::sleep(0.01);
468 }
469 }
470 }
471  
472 194 ▶sub spawn_link_command {
Subroutine spawn_link_command: called CC=1 SCAR=1.2
Pod undocumented
473 0 ▶ my (@args) = @_;
Subroutine spawn_link_command: not called CC=1 SCAR=1.2
Pod undocumented
474
475 0 my $link_script_path = &get_link_script_path();
476
477 0 my $process_id = &spawn_perl($link_script_path, @args);
478
479 0 return $process_id;
480 }
481  
482 202 ▶sub spawn_link {
Subroutine spawn_link: called CC=8 SCAR=30.3
Pod undocumented
483 0 ▶ my ($self, $link_info) = @_;
Subroutine spawn_link: not called CC=8 SCAR=30.3
Pod undocumented
484
485 0 my $config_global = $link_info->config->global;
486 0 ▶ if ($config_global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config_global) 202 0 202
if ($config_global) 0 0 0
487 0 for my $before_link_cb (@{$config_global->before_link_cbs}) {
488 0 $before_link_cb->($link_info);
489 }
490 }
491
492 0 my $config = $link_info->config;
493
494 0 my $class_name = $config->class_name;
495
496 0 my $object_file_infos = $link_info->object_file_infos;
497
498 0 ▶ unless (@$object_file_infos) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@$object_file_infos) 0 202 202
unless (@$object_file_infos) 0 0 0
499 0 confess("[Unexpected Error]Object files must be at least one.");
500 }
501
502 0 my $quiet = $self->detect_quiet($config);
503 0 my $hint_cc = $config->hint_cc;
504 0 my $ld = $config->ld;
505 0 my $output_type = $config->output_type;
506
507 0 my $command_hash = $link_info->command_hash;
508 0 my $output_file = $link_info->output_file;
509 0 my $dl_func_list = $link_info->dl_func_list;
510
511 0 my $ld_cmd = $link_info->create_command;
512 0 my $ld_command_string = $link_info->create_command_string;
513
514 0 my $link_info_object_files = $link_info->object_file_infos;
515
516 0 my %seen;
517 0 my $object_file_names = [grep { !$seen{$_}++ } map { $_->to_string; } @$link_info_object_files];
518
519 0 my $ninja = $self->ninja;
520
521 0 mkpath dirname $output_file;
522
523 0 my $force = !!$ENV{SPVM_FORCE_BUILD};
524
525 0 my $need_generate_options = {
526 command_hash => $link_info->command_hash,
527 output_file => $link_info->output_file,
528 };
529 0 my $need_generate = $self->ninja->need_generate($need_generate_options);
530
531 0 my $process_id;
532 0 ▶ if ($force || $need_generate) {
Branch: 2 branches
BranchTrueFalseTotal
if ($force or $need_generate) 194 8 202
if ($force or $need_generate) 0 0 0
Condition33%2/6
$force or $need_generate
l!l&&r!l&&!r
01948
$force or $need_generate
l!l&&r!l&&!r
000
MC/DC50%1/2
$force or $need_generate
$force $need_generate
MC/DC0%0/2
$force or $need_generate
$force $need_generate
Truth table66%2/3
A or B
A= $forceB= $need_generate
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $forceB= $need_generate
ABresult
1X1
011
000
533 0 mkpath dirname $output_file;
534
535 # Prepare command for intermediate Perl process
536 0 my $command_tmp_dir = File::Temp->newdir;
537
538 0 my $start_time = int(Time::HiRes::time() * 1000);
539 0 $link_info->start_time($start_time);
540 0 $link_info->tmp_dir($command_tmp_dir);
541
542 # Prepare arguments for link.pl
543 0 my $dl_func_list_file = "$command_tmp_dir/dl_func_list.txt";
544 0 ▶ if (defined $dl_func_list) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $dl_func_list) 194 0 194
if (defined $dl_func_list) 0 0 0
545 0 SPVM::Builder::Util::spurt_binary($dl_func_list_file, join("\n", @$dl_func_list), $self->global_lock_fh);
546 }
547
548 0 my $object_file_names_file = "$command_tmp_dir/object_file_names.txt";
549 0 SPVM::Builder::Util::spurt_binary($object_file_names_file, join("\n", @$object_file_names), $self->global_lock_fh);
550
551 0 my $ldflags_file = "$command_tmp_dir/ldflags.txt";
552 0 my $link_info_ldflags = $link_info->create_ldflags;
553 0 SPVM::Builder::Util::spurt_binary($ldflags_file, join("\n", @$link_info_ldflags), $self->global_lock_fh);
554
555 # [Added] Generate lock file path using SHA1 of the normalized output file path
556 0 my $build_dir = $self->build_dir;
557
558 0 ▶ unless ($quiet) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($quiet) 0 194 194
unless ($quiet) 0 0 0
559 0 my $ld_command_diagnostic_message= $link_info->diagnostic_message;
560 0 ▶ $self->global_file_lock(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
561 0 ▶ print STDERR "$ld_command_diagnostic_message\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
562 0 print STDERR "$ld_command_string\n";
563 0 });
564 }
565
566 # Spawn link process
567 0 $process_id = &spawn_link_command(
568 $command_tmp_dir,
569 $output_file,
570 $class_name,
571 $hint_cc,
572 $output_type,
573 $ld,
574 $dl_func_list_file,
575 $object_file_names_file,
576 $ldflags_file,
577 );
578 0 $link_info->process_id($process_id);
579 }
580
581 0 return $process_id;
582 }
583  
584 3208 ▶sub spawn_compile_source_file {
Subroutine spawn_compile_source_file: called CC=7 SCAR=27.0
Pod undocumented
585 0 ▶ my ($self, $compile_info) = @_;
Subroutine spawn_compile_source_file: not called CC=7 SCAR=27.0
Pod undocumented
586
587 0 my $config_global = $compile_info->config->global;
588 0 ▶ if ($config_global) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config_global) 3208 0 3208
if ($config_global) 0 0 0
589 0 for my $before_compile_cb (@{$config_global->before_compile_cbs}) {
590 0 $before_compile_cb->($compile_info);
591 }
592 }
593
594 0 my $config = $compile_info->config;
595
596 0 my $quiet = $self->detect_quiet($config);
597
598 0 my $command_hash = $compile_info->command_hash;
599
600 0 my $output_file = $compile_info->output_file;
601
602 0 my $ninja = $self->ninja;
603
604 0 my $need_generate_options = {
605 command_hash => $compile_info->command_hash,
606 output_file => $compile_info->output_file,
607 };
608
609 0 my $force = !!$ENV{SPVM_FORCE_BUILD};
610 0 my $need_generate = $ninja->need_generate($need_generate_options);
611
612 0 my $process_id = 0;
613 0 ▶ if ($force || $need_generate) {
Branch: 2 branches
BranchTrueFalseTotal
if ($force or $need_generate) 2494 714 3208
if ($force or $need_generate) 0 0 0
Condition33%2/6
$force or $need_generate
l!l&&r!l&&!r
02494714
$force or $need_generate
l!l&&r!l&&!r
000
MC/DC50%1/2
$force or $need_generate
$force $need_generate
MC/DC0%0/2
$force or $need_generate
$force $need_generate
Truth table66%2/3
A or B
A= $forceB= $need_generate
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $forceB= $need_generate
ABresult
1X1
011
000
614 0 mkpath dirname $output_file;
615
616 0 my $cc_command_diagnostic_message = $compile_info->diagnostic_message;
617
618 0 ▶ unless ($cc_command_diagnostic_message) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($cc_command_diagnostic_message) 0 2494 2494
unless ($cc_command_diagnostic_message) 0 0 0
619 0 confess("'diagnostic_message' must be defined.");
620 }
621
622 # Prepare command for intermediate Perl process
623 0 my $command_tmp_dir = File::Temp->newdir;
624 0 my $cc_cmd = $compile_info->create_command;
625 0 my $cc_command_string = $compile_info->create_command_string;
626
627 0 my $start_time = int(Time::HiRes::time() * 1000);
628 0 $compile_info->start_time($start_time);
629 0 $compile_info->tmp_dir($command_tmp_dir);
630
631 0 ▶ unless ($quiet) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($quiet) 46 2448 2494
unless ($quiet) 0 0 0
632 46 ▶ $self->global_file_lock(sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
633 0 ▶ print STDERR "$cc_command_diagnostic_message\n";
Subroutine __ANON__: not called CC=1 SCAR=1.2
634 0 print STDERR "$cc_command_string\n";
635 0 });
636 }
637
638 0 my $cc_cmd_no_output_option = $compile_info->create_command({no_output_option => 1});
639 0 my $output_option_without_value = $config->create_option_short($config->cc_output_option_name, '');
640
641 0 $process_id = &spawn_compile_command($command_tmp_dir, $output_file, @$cc_cmd_no_output_option, $output_option_without_value);
642 0 $compile_info->process_id($process_id);
643 }
644
645 0 return $process_id;
646 }
647  
648 1087 ▶sub add_ninja_log {
Subroutine add_ninja_log: called CC=1 SCAR=1.2
Pod undocumented
649 0 ▶ my ($self, $command_info) = @_;
Subroutine add_ninja_log: not called CC=1 SCAR=1.2
Pod undocumented
650
651 0 my $output_file = $command_info->output_file;
652 0 my $command_hash = $command_info->command_hash;
653 0 my $start_time = $command_info->start_time;
654 0 my $ninja = $self->ninja;
655
656 # Set end_time to command_info
657 0 my $end_time = int(Time::HiRes::time() * 1000);
658 0 $command_info->end_time($end_time);
659
660 # Get mtime of the output file
661 0 my $mtime = int((Time::HiRes::stat $output_file)[9] * 1000);
662
663 0 my $log_entry = {
664 output_file => $output_file,
665 command_hash => $command_hash,
666 start_time => $start_time,
667 end_time => $end_time,
668 mtime => $mtime,
669 };
670
671 0 $ninja->add_log($log_entry);
672 }
673  
674 2494 ▶sub spawn_compile_command {
Subroutine spawn_compile_command: called CC=1 SCAR=1.2
Pod undocumented
675 0 ▶ my ($output_file, $command_tmp_dir, @cc_cmd) = @_;
Subroutine spawn_compile_command: not called CC=1 SCAR=1.2
Pod undocumented
676
677 0 my $compile_script_path = &get_compile_script_path();
678
679 0 my $process_id = &spawn_perl($compile_script_path, $output_file, $command_tmp_dir, @cc_cmd);
680
681 0 return $process_id;
682 }
683  
684 2688 ▶sub spawn_perl {
Subroutine spawn_perl: called CC=6 SCAR=30.0
Pod undocumented
685 0 ▶ my ($perl_script_file, @args) = @_;
Subroutine spawn_perl: not called CC=6 SCAR=30.0
Pod undocumented
686
687 0 my $args_base64 = encode_base64(join("\0", @args), "");
688
689 0 my @cmd = ($^X, $perl_script_file, $args_base64);
690
691 0 my $process_id;
692 0 ▶ if (SPVM::Builder::Util::is_windows()) {
Branch: 2 branches
BranchTrueFalseTotal
if (SPVM::Builder::Util::is_windows()) { } 0 2688 2688
if (SPVM::Builder::Util::is_windows()) { } 0 0 0
693 # Windows spawn
694 0 $process_id = system(1, @cmd);
695 0 ▶ if (!defined $process_id || $process_id <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $process_id or $process_id <= 0) 0 0 0
if (not defined $process_id or $process_id <= 0) 0 0 0
Condition0%0/6
not defined $process_id or $process_id <= 0
l!l&&r!l&&!r
000
not defined $process_id or $process_id <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $process_id or $process_id <= 0
not defined $process_id $process_id <= 0
MC/DC0%0/2
not defined $process_id or $process_id <= 0
not defined $process_id $process_id <= 0
Truth table0%0/3
not(A) or B
A= not defined $process_idB= $process_id <= 0
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $process_idB= $process_id <= 0
ABresult
1X1
011
000
696 0 confess("Failed to spawn Windows process for executing a perl script: $!");
697 }
698 }
699 else {
700 # Linux/Unix fork
701 0 $process_id = fork();
702 0 ▶ if (!defined $process_id) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $process_id) 0 2688 2688
unless (defined $process_id) 0 0 0
703 0 confess("Failed to fork for executing a perl script: $!");
704 }
705
706 # Child process
707 0 ▶ if ($process_id == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($process_id == 0) 337 2351 2688
if ($process_id == 0) 0 0 0
708 0 exec(@cmd);
709 0 exit(1);
710 }
711 }
712
713 0 return $process_id;
714 }
715  
716 2494 ▶sub get_compile_script_path {
Subroutine get_compile_script_path: called CC=1 SCAR=1.2
Pod undocumented
717
718 0 ▶ my $compile_script_path = dirname(__FILE__) . "/Builder/compile.pl";
Subroutine get_compile_script_path: not called CC=1 SCAR=1.2
Pod undocumented
719
720 0 return $compile_script_path;
721 }
722  
723 194 ▶sub get_link_script_path {
Subroutine get_link_script_path: called CC=1 SCAR=1.2
Pod undocumented
724
725 0 ▶ my $link_script_path = dirname(__FILE__) . "/Builder/link.pl";
Subroutine get_link_script_path: not called CC=1 SCAR=1.2
Pod undocumented
726
727 0 return $link_script_path;
728 }
729  
730 258968 ▶sub wait_command {
Subroutine wait_command: called CC=14 SCAR=46.8
Pod undocumented
731 0 ▶ my ($self, $command_info) = @_;
Subroutine wait_command: not called CC=14 SCAR=46.8
Pod undocumented
732
733 0 my $process_id = $command_info->process_id;
734
735 # Wait for completion
736 0 my $wait_process_id = waitpid($process_id, WNOHANG);
737
738 0 my $exit_status = $? >> 8;
739
740 0 my $command_string = $command_info->create_command_string;
741
742 # Alive
743 0 ▶ if ($wait_process_id == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($wait_process_id == 0) { } 257859 1109 258968
elsif ($wait_process_id == -1) { } 0 1109 1109
if ($wait_process_id == 0) { } 0 0 0
elsif ($wait_process_id == -1) { } 0 0 0
744 0 return 0;
745 }
746 # Error
747 elsif ($wait_process_id == -1) {
748 0 confess("The waited command failed. Process not found or already reaped: \$!=$!, , \$command_string='$command_string'");
749 }
750
751 0 my $command_tmp_dir = $command_info->tmp_dir;
752
753 # Read output files from temp directory
754 0 my $stdout_file = "$command_tmp_dir/stdout.log";
755 0 my $stdout_output = SPVM::Builder::Util::slurp_binary($stdout_file);
756 0 ▶ if (length $stdout_output) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $stdout_output) 0 1087 1087
if (length $stdout_output) 0 0 0
757 0 ▶ $self->global_file_lock(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
758 0 ▶ print $stdout_output;
Subroutine __ANON__: not called CC=1 SCAR=6.9
759 0 });
760 }
761
762 0 my $stderr_file = "$command_tmp_dir/stderr.log";
763 0 my $stderr_output = SPVM::Builder::Util::slurp_binary($stderr_file);
764 0 ▶ if (length $stderr_output) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $stderr_output) 0 1087 1087
if (length $stderr_output) 0 0 0
765 0 ▶ $self->global_file_lock(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
766 0 ▶ print STDERR $stderr_output;
Subroutine __ANON__: not called CC=1 SCAR=6.9
767 0 });
768 }
769
770 0 ▶ if ($ENV{SPVM_CC_DEBUG}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{"SPVM_CC_DEBUG"}) 0 1087 1087
if ($ENV{"SPVM_CC_DEBUG"}) 0 0 0
771 0 ▶ opendir(my $command_tmp_dh, $command_tmp_dir)
Branch: 2 branches
BranchTrueFalseTotal
opendir my $command_tmp_dh, $command_tmp_dir or confess("Can't open directory '${command_tmp_dir}': $!") 0 0 0
opendir my $command_tmp_dh, $command_tmp_dir or confess("Can't open directory '${command_tmp_dir}': $!") 0 0 0
772 or confess("Can't open directory '$command_tmp_dir': $!");
773
774 0 ▶ while (my $tmp_file = readdir($command_tmp_dh)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $tmp_file = readdir $command_tmp_dh)) 0 0 0
if (defined(my $tmp_file = readdir $command_tmp_dh)) 0 0 0
775 0 ▶ next if $tmp_file eq 'stdout.log' || $tmp_file eq 'stderr.log';
Branch: 2 branches
BranchTrueFalseTotal
if $tmp_file eq "stdout.log" or $tmp_file eq "stderr.log" 0 0 0
if $tmp_file eq "stdout.log" or $tmp_file eq "stderr.log" 0 0 0
Condition0%0/6
$tmp_file eq "stdout.log" or $tmp_file eq "stderr.log"
l!l&&r!l&&!r
000
$tmp_file eq "stdout.log" or $tmp_file eq "stderr.log"
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp_file eq "stdout.log" or $tmp_file eq "stderr.log"
$tmp_file eq "stdout.log" $tmp_file eq "stderr.log"
MC/DC0%0/2
$tmp_file eq "stdout.log" or $tmp_file eq "stderr.log"
$tmp_file eq "stdout.log" $tmp_file eq "stderr.log"
Truth table0%0/3
A or B
A= $tmp_file eq "stdout.log"B= $tmp_file eq "stderr.log"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $tmp_file eq "stdout.log"B= $tmp_file eq "stderr.log"
ABresult
1X1
011
000
776
777 0 my $tmp_file_path = "$command_tmp_dir/$tmp_file";
778
779 0 ▶ if (-f $tmp_file_path && -T $tmp_file_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $tmp_file_path and -T $tmp_file_path) 0 0 0
if (-f $tmp_file_path and -T $tmp_file_path) 0 0 0
Condition0%0/6
-f $tmp_file_path and -T $tmp_file_path
!ll&&!rl&&r
000
-f $tmp_file_path and -T $tmp_file_path
!ll&&!rl&&r
000
MC/DC0%0/2
-f $tmp_file_path and -T $tmp_file_path
-f $tmp_file_path -T $tmp_file_path
MC/DC0%0/2
-f $tmp_file_path and -T $tmp_file_path
-f $tmp_file_path -T $tmp_file_path
Truth table0%0/3
A and B
A= -f $tmp_file_pathB= -T $tmp_file_path
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= -f $tmp_file_pathB= -T $tmp_file_path
ABresult
0X0
100
111
780 0 my $tmp_content = SPVM::Builder::Util::slurp_binary($tmp_file_path);
781 0 ▶ $self->global_file_lock(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
782 0 ▶ print STDERR "[SPVM_CC_DEBUG]Content of '$tmp_file_path':\n$tmp_content\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
783 0 });
784 }
785 }
786 }
787
788 0 ▶ if ($exit_status != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exit_status != 0) 0 1087 1087
if ($exit_status != 0) 0 0 0
789 0 confess("The waited command failed. The command returned an error status code. \$exit_status=$exit_status, \$command_string='$command_string'");
790 }
791
792 # Check output file
793 0 my $output_file = $command_info->output_file;
794 0 ▶ unless (-f $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-f $output_file) 0 1087 1087
unless (-f $output_file) 0 0 0
795 0 confess("[Unexpected Error]The output file '$output_file' does not exist.");
796 }
797
798 0 return $wait_process_id;
799 }
800  
801 8170 ▶sub finalize_compile_info {
Subroutine finalize_compile_info: called CC=4 SCAR=20.1
Pod undocumented
802 0 ▶ my ($self, $compile_info) = @_;
Subroutine finalize_compile_info: not called CC=4 SCAR=20.1
Pod undocumented
803
804 0 my $config = $compile_info->config;
805
806 0 my $source_rel_file = $compile_info->source_rel_file;
807
808 0 ▶ unless (defined $source_rel_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $source_rel_file) 0 8170 8170
unless (defined $source_rel_file) 0 0 0
809 0 confess("\$source_rel_file must be defined.");
810 }
811
812 0 my $source_file = $compile_info->source_file;
813
814 0 my $cc_version = $config->cc_version;
815
816 0 my $cc_command_no_output_option = $compile_info->create_command({no_output_option => 1});
817 0 my $cc_command_string_no_output_option = "@$cc_command_no_output_option";
818
819 0 my $dependent_files = $compile_info->dependent_files;
820 0 my $ninja = $self->ninja;
821 0 ▶ unless (defined $source_file && -f $source_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $source_file and -f $source_file) 0 8170 8170
unless (defined $source_file and -f $source_file) 0 0 0
Condition16%1/6
defined $source_file and -f $source_file
!ll&&!rl&&r
008170
defined $source_file and -f $source_file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $source_file and -f $source_file
defined $source_file -f $source_file
MC/DC0%0/2
defined $source_file and -f $source_file
defined $source_file -f $source_file
Truth table33%1/3
A and B
A= defined $source_fileB= -f $source_file
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $source_fileB= -f $source_file
ABresult
0X0
100
111
822 0 confess("source_file \"$source_file\" must be defined and exist.");
823 }
824 0 my $create_command_hash_options = {
825 command => $cc_command_string_no_output_option,
826 command_version => $cc_version,
827 dependent_files => [$source_file, @$dependent_files],
828 };
829
830 # Get command hash
831 0 my $command_hash = $ninja->create_command_hash($create_command_hash_options);
832
833 # Split hash into directory and filename (e.g., abcdef... -> ab/cdef...)
834 0 my $hash_dir = $command_hash;
835 0 $hash_dir =~ s/^(..)/$1\//;
836  
837 # Build output file path
838 0 my $object_rel_file = $source_rel_file;
839 0 $object_rel_file =~ s/\.[^\.]+$/.o/;
840 0 my $cc_output_dir = $self->create_build_object_path;
841  
842 # Final output path: [build_dir]/[ab/hash_rest]/[source].o
843 0 my $output_file = "$cc_output_dir/$hash_dir/$object_rel_file";
844  
845 # Set output file
846 0 $compile_info->output_file($output_file);
847
848 0 $compile_info->command_hash($command_hash);
849 }
850  
851 222 ▶sub finalize_link_info {
Subroutine finalize_link_info: called CC=22 SCAR=49.6
Pod undocumented
852 0 ▶ my ($self, $link_info) = @_;
Subroutine finalize_link_info: not called CC=22 SCAR=49.6
Pod undocumented
853
854 0 my $config = $link_info->config;
855
856 0 ▶ unless ($config) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($config) 0 222 222
unless ($config) 0 0 0
857 0 confess("[Unexpected Error]A config must be defined.");
858 }
859
860 0 my $class_name = $link_info->config->class_name;
861 0 ▶ unless (defined $class_name) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $class_name) 0 222 222
unless (defined $class_name) 0 0 0
862 0 confess("A class name must be defined.");
863 }
864
865 0 ▶ if (ref $class_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $class_name) 0 222 222
if (ref $class_name) 0 0 0
866 0 confess("[Unexpected Error]A class name must be non-reference.");
867 }
868
869 0 my $compile_infos = $link_info->compile_infos;
870
871 0 my $object_file_infos = [map { SPVM::Builder::ObjectFileInfo->new(compile_info => $_, file => $_->output_file) } @$compile_infos];
872
873 0 for my $external_object_file (@{$config->external_object_files}) {
874 0 push @$object_file_infos, SPVM::Builder::ObjectFileInfo->new(file => $external_object_file);
875 }
876
877 0 ▶ unless (@$object_file_infos) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@$object_file_infos) 0 222 222
unless (@$object_file_infos) 0 0 0
878 0 confess("[Unexpected Error]\$object_file_infos must have object files for $class_name.");
879 }
880
881 0 $link_info->object_file_infos($object_file_infos);
882
883 0 my $build_dir = $self->build_dir;
884
885 0 my $ld = $config->ld;
886
887 0 ▶ unless (defined $build_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_dir) 0 222 222
unless (defined $build_dir) 0 0 0
888 0 confess("[Unexpected Error]A build directory must be defined.");
889 }
890
891 0 ▶ unless (-d $build_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (-d $build_dir) 0 222 222
unless (-d $build_dir) 0 0 0
892 0 confess("[Unexpected Error]A build directory must exists.");
893 }
894
895 0 my @object_file_names = map { "$_" } @{$link_info->object_file_infos};
896
897 0 ▶ unless ($config->isa('SPVM::Builder::Config::Linker')) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($config->isa("SPVM::Builder::Config::Linker")) 0 222 222
unless ($config->isa("SPVM::Builder::Config::Linker")) 0 0 0
898 0 confess("[Unexpected Error]The config must be an SPVM::Builder::Config object");
899 }
900
901 0 my $hint_cc = $config->hint_cc;
902
903 0 my $link_info_output_file = $config->output_file;
904
905 0 my $link_info_object_files = $link_info->object_file_infos;
906
907 0 my %seen;
908 0 my $object_file_names = [grep { !$seen{$_}++ } map { $_->to_string; } @$link_info_object_files];
909
910 0 my $ld_version = $config->ld_version;
911
912 0 my $ld_command_no_output_option = $link_info->create_command({no_output_option => 1});
913 0 my $ld_command_string_no_output_option = "@$ld_command_no_output_option";
914
915 0 my $ninja = $self->ninja;
916 0 my $create_command_hash_options = {
917 command => $ld_command_string_no_output_option,
918 command_version => $ld_version,
919 dependent_files => [@object_file_names],
920 };
921 0 my $command_hash = $ninja->create_command_hash($create_command_hash_options);
922
923 0 $link_info->command_hash($command_hash);
924 0 $link_info->config($config);
925
926 0 my $output_file = $config->output_file;
927
928 # Output file
929 0 ▶ unless (defined $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $output_file) 220 2 222
unless (defined $output_file) 0 0 0
930 0 my $output_dir = $self->output_dir;
931 0 ▶ unless (defined $output_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $output_dir) 219 1 220
unless (defined $output_dir) 0 0 0
932 0 my $is_jit = $self->is_jit;
933 0 ▶ if ($is_jit) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_jit) { } 219 0 219
if ($is_jit) { } 0 0 0
934 0 $output_dir = $self->create_build_lib_path($command_hash =~ s|^(..)|$1/|r);
935 }
936 else {
937 0 confess("[Unexpected Error]A output directory must exists.");
938 }
939 }
940
941 0 my $category = $config->category;
942 0 ▶ if ($category eq 'native' || $category eq 'precompile') {
Branch: 4 branches
BranchTrueFalseTotal
if ($category eq 'native' or $category eq 'precompile') { } 219 1 220
elsif ($category eq 'libspvm') { } 1 0 1
if ($category eq 'native' or $category eq 'precompile') { } 0 0 0
elsif ($category eq 'libspvm') { } 0 0 0
Condition50%3/6
$category eq "native" or $category eq "precompile"
l!l&&r!l&&!r
321871
$category eq "native" or $category eq "precompile"
l!l&&r!l&&!r
000
MC/DC100%2/2
$category eq "native" or $category eq "precompile"
$category eq "native" $category eq "precompile"
MC/DC0%0/2
$category eq "native" or $category eq "precompile"
$category eq "native" $category eq "precompile"
Truth table100%3/3
A or B
A= $category eq "native"B= $category eq "precompile"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $category eq "native"B= $category eq "precompile"
ABresult
1X1
011
000
943 0 my $output_rel_file = SPVM::Builder::Util::convert_class_name_to_category_rel_file($class_name, $category);
944 0 $output_file = "$output_dir/$output_rel_file";
945 }
946 elsif ($category eq 'libspvm') {
947 0 $output_file = "$output_dir/SPVM/Builder/lib/libspvm";
948 }
949 }
950
951 0 ▶ unless (defined $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $output_file) 0 222 222
unless (defined $output_file) 0 0 0
952 0 confess("Cannot determine output file for link.");
953 }
954
955 # Output file extension
956 0 my $output_type = $config->output_type;
957 0 my $output_file_base = basename $output_file;
958 0 ▶ if ($output_file_base =~ /\.precompile$/ || $output_file_base !~ /\./) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./) 222 0 222
if ($output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./) 0 0 0
Condition33%2/6
$output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./
l!l&&r!l&&!r
187350
$output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./
l!l&&r!l&&!r
000
MC/DC0%0/2
$output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./
$output_file_base =~ /\.precompile$/ not $output_file_base =~ /\./
MC/DC0%0/2
$output_file_base =~ /\.precompile$/ or not $output_file_base =~ /\./
$output_file_base =~ /\.precompile$/ not $output_file_base =~ /\./
Truth table66%2/3
A or not(B)
A= $output_file_base =~ /\.precompile$/B= not $output_file_base =~ /\./
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $output_file_base =~ /\.precompile$/B= not $output_file_base =~ /\./
ABresult
1X1
011
000
959 0 my $output_file_ext;
960
961 0 ▶ if ($output_type eq 'dynamic_lib') {
Branch: 6 branches
BranchTrueFalseTotal
if ($output_type eq 'dynamic_lib') { } 219 3 222
elsif ($output_type eq 'static_lib') { } 1 2 3
elsif ($output_type eq 'exe') { } 2 0 2
if ($output_type eq 'dynamic_lib') { } 0 0 0
elsif ($output_type eq 'static_lib') { } 0 0 0
elsif ($output_type eq 'exe') { } 0 0 0
962 0 $output_file_ext = ".$Config{so}"
963 }
964 elsif ($output_type eq 'static_lib') {
965 0 $output_file_ext = $Config{lib_ext};
966 }
967 elsif ($output_type eq 'exe') {
968 0 $output_file_ext = $Config{exe_ext};
969 }
970
971 0 $output_file .= $output_file_ext;
972 }
973
974 0 $config->output_file($output_file);
975
976 0 $link_info->output_file($output_file);
977
978 0 my $enable_ld_debug_output_option = $config->enable_ld_debug_output_option;
979 0 ▶ if ($enable_ld_debug_output_option) {
Branch: 2 branches
BranchTrueFalseTotal
if ($enable_ld_debug_output_option) 0 222 222
if ($enable_ld_debug_output_option) 0 0 0
980 0 my $debug_output_file_ext = $config->debug_output_file_ext;
981
982 0 my $debug_output_file = $output_file;
983 0 $debug_output_file =~ s/\.[^.]+$//;
984
985 0 $debug_output_file .= ".$debug_output_file_ext";
986
987 0 $config->debug_output_file($debug_output_file);
988
989 0 my $ld_debug_output_option_name = $config->ld_debug_output_option_name;
990 0 my $debug_output_file_option = $config->create_option_long($ld_debug_output_option_name, $debug_output_file);
991
992 0 $config->debug_output_file_ldflags([$debug_output_file_option]);
993 }
994
995 0 return $link_info;
996 }
997  
998 124 ▶sub prepare_compile_spvm_core_source_files {
Subroutine prepare_compile_spvm_core_source_files: called CC=2 SCAR=9.2
Pod undocumented
999 0 ▶ my ($self) = @_;
Subroutine prepare_compile_spvm_core_source_files: not called CC=2 SCAR=9.2
Pod undocumented
1000
1001 0 my $config = SPVM::Builder::Util::API::create_default_config();
1002
1003 0 my $builder_dir = SPVM::Builder::Util::get_builder_dir();
1004
1005 0 my $builder_src_dir = "$builder_dir/src";
1006
1007 0 my $builder_include_dir = "$builder_dir/include";
1008
1009 0 my $source_dir = $builder_dir;
1010 0 $source_dir =~ s|/SPVM/Builder$||;
1011
1012 0 my $spvm_core_source_file_names = SPVM::Builder::Util::get_spvm_core_source_file_names();
1013
1014 # Compile source files
1015 0 my $compile_infos = [];
1016 0 for my $spvm_core_source_file_name (@$spvm_core_source_file_names) {
1017 0 my $source_rel_file = "SPVM/Builder/src/$spvm_core_source_file_name";
1018
1019 0 my $compile_info = SPVM::Builder::CompileInfo->new(
1020 source_dir => $source_dir,
1021 source_rel_file => $source_rel_file,
1022 config => $config,
1023 category => 'spvm_core',
1024 dependent_files => [$builder_include_dir],
1025 diagnostic_message => "[Compile SPVM Source File]",
1026 );
1027
1028 0 push @$compile_infos, $compile_info;
1029 }
1030
1031 0 my $link_info = SPVM::Builder::LinkInfo->new(config => $config, compile_infos => $compile_infos);
1032
1033 0 return $link_info;
1034 }
1035  
1036 684 1;
1037  
1038 =encoding utf8
1039  
1040 =head1 Name
1041  
1042 SPVM::Builder - Build Dynamic Libraries for SPVM Distribution
1043  
1044 =head1 Description
1045  
1046 The SPVM::Builder class has methods to build dynamic librares for an SPVM distribution.
1047  
1048 =head1 Copyright & License
1049  
1050 Copyright (c) 2023 Yuki Kimoto
1051  
1052 MIT License
« blib/lib/SPVM/Builder/Config/Global/MSVC.pm ↑ Summary blib/lib/SPVM/Builder/Config/Linker.pm »