blib/lib/SPVM/Builder/Ninja.pm

filter: Statementstmt 50.6% 237 / 468 Branchbran 33.5% 53 / 158 Conditioncond 30.6% 19 / 62 MC/DCmcdc 18.4% 7 / 38 Subroutinesub 54.5% 36 / 66 Podpod 0.0% 0 / 40 totaltotal 42.3% 352 / 832 CC 109 SCAR 29.2
CC
109
Cov
45%
create_command_hash
60.8
create_command_hash
60.8
add_log_without_lock
30.9
CRAP 18.6
« blib/lib/SPVM/Builder/API.pm ↑ Summary blib/lib/SPVM/Builder/Config.pm »
1 package SPVM::Builder::Ninja;
2  
3 # SPVM::Builder::Ninja is used from Makefile.PL
4 # so this class must be wrote as pure perl. Do not contain XS functions.
5  
6 781 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 781 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 781 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 781 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 781 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 781 ▶use Fcntl qw(:seek :mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 781 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 781 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 781 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 781 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 781 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 781 has [qw(
19 builder
20 log_file_base_name
21 log_fh
22 entries_h
23 entries_length
24 )];
25  
26 2066 ▶sub new {
Subroutine new: called CC=3 SCAR=17.1
Pod undocumented
27 0 ▶ my $class = shift;
Subroutine new: not called CC=3 SCAR=17.1
Pod undocumented
28
29 0 my $self = {
30 entries_h => {},
31 log_file_base_name => '.ninja_log',
32 entries_length => 0,
33 @_
34 };
35
36 0 ▶ bless $self, ref $class || $class;
Condition16%1/6
ref $class || $class
l!l&&r!l&&!r
020660
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
37
38 0 ▶ unless ($self->builder) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->builder) 0 2066 2066
unless ($self->builder) 0 0 0
39 0 confess("'builder' field must be defined");
40 }
41
42 0 weaken $self->{builder};
43
44 0 $self->prepare;
45
46 0 return $self;
47 }
48  
49 2066 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=1.2
Pod undocumented
50 0 ▶ my ($self) = @_;
Subroutine prepare: not called CC=1 SCAR=1.2
Pod undocumented
51
52 0 $self->create_log;
53
54 0 $self->open_log('+<');
55
56 0 $self->recompact;
57
58 0 $self->load_log;
59 }
60  
61 4132 ▶sub log_file {
Subroutine log_file: called CC=2 SCAR=10.7
Pod undocumented
62 0 ▶ my ($self) = @_;
Subroutine log_file: not called CC=2 SCAR=10.7
Pod undocumented
63  
64 0 my $build_dir = $self->builder->build_dir;
65  
66 # Raise an exception if build_dir is not defined
67 0 ▶ unless (defined $build_dir) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_dir) 0 4132 4132
unless (defined $build_dir) 0 0 0
68 0 confess("The \"build_dir\" field must be defined");
69 }
70  
71 0 my $log_file_base_name = $self->log_file_base_name;
72 0 my $log_file = "$build_dir/$log_file_base_name";
73  
74 0 return $log_file;
75 }
76  
77 4132 ▶sub open_log {
Subroutine open_log: called CC=4 SCAR=22.0
Pod undocumented
78 0 ▶ my ($self, $mode) = @_;
Subroutine open_log: not called CC=4 SCAR=22.0
Pod undocumented
79
80 0 ▶ unless ($mode) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($mode) 0 4132 4132
unless ($mode) 0 0 0
81 0 confess "The open mode \$mode must be defined.";
82 }
83
84 0 my $log_file = $self->log_file;
85
86 0 ▶ if ($self->{log_fh}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{"log_fh"}) 0 4132 4132
if ($self->{"log_fh"}) 0 0 0
87 0 confess "The file '$log_file' is already opened";
88 }
89
90 0 ▶ open my $fh, $mode, $log_file
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, $mode, $log_file or confess("Can't open '${log_file}' with the mode '${mode}': $!") 4132 0 4132
open my $fh, $mode, $log_file or confess("Can't open '${log_file}' with the mode '${mode}': $!") 0 0 0
91 or confess("Can't open '$log_file' with the mode '$mode': $!");
92
93 0 $self->{log_fh} = $fh;
94 }
95  
96 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod undocumented
97 0 ▶ my ($self) = @_;
Subroutine opened: not called CC=2 SCAR=17.9
Pod undocumented
98
99 0 ▶ return $self->{log_fh} ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$self->{'log_fh'} ? : 0 0 0
$self->{'log_fh'} ? : 0 0 0
100 }
101  
102 1600 ▶sub add_log {
Subroutine add_log: called CC=1 SCAR=1.8
Pod undocumented
103 0 ▶ my ($self, @args) = @_;
Subroutine add_log: not called CC=1 SCAR=1.8
Pod undocumented
104
105 1600 ▶ $self->write_lock_with_flush(sub {
Subroutine __ANON__: called CC=2 SCAR=10.9
106 0 ▶ seek($self->log_fh, 0, SEEK_END)
Subroutine __ANON__: not called CC=2 SCAR=10.9
Branch: 2 branches
BranchTrueFalseTotal
seek $self->log_fh, 0, 2 or confess("Can't seek to end: $!") 1600 0 1600
seek $self->log_fh, 0, 2 or confess("Can't seek to end: $!") 0 0 0
107 or confess("Can't seek to end: $!");
108 0 $self->add_log_without_lock(@args);
109 0 });
110 }
111  
112 6159 ▶sub add_log_without_lock {
Subroutine add_log_without_lock: called CC=9 SCAR=34.3
Pod undocumented
113 0 ▶ my ($self, $new_entry_h, $options) = @_;
Subroutine add_log_without_lock: not called CC=9 SCAR=34.3
Pod undocumented
114
115 0 ▶ $options //= {};
Condition50%2/4
$options //= {}
l!l
45591600
$options //= {}
l!l
00
MC/DC100%1/1
$options //= {}
$options
MC/DC0%0/1
$options //= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
Truth table0%0/2
A
A= $options
Aresult
11
00
116
117 0 my $fh = $self->{log_fh};
118 0 ▶ unless (defined $fh) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $fh) 0 6159 6159
unless (defined $fh) 0 0 0
119 0 confess("Ninja log is not open. Call open_log() first.");
120 }
121  
122 0 my $output_file = $new_entry_h->{output_file};
123 0 ▶ unless (defined $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $output_file) 0 6159 6159
unless (defined $output_file) 0 0 0
124 0 confess("output_file must be defined.");
125 }
126  
127 0 my $command_hash = $new_entry_h->{command_hash};
128 0 ▶ unless (defined $command_hash) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $command_hash) 0 6159 6159
unless (defined $command_hash) 0 0 0
129 0 confess("command_hash must be defined.");
130 }
131  
132 0 my $start_time = $new_entry_h->{start_time};
133 0 ▶ unless (defined $start_time) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $start_time) 0 6159 6159
unless (defined $start_time) 0 0 0
134 0 confess("start_time must be defined.");
135 }
136  
137 0 my $end_time = $new_entry_h->{end_time};
138 0 ▶ unless (defined $end_time) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $end_time) 0 6159 6159
unless (defined $end_time) 0 0 0
139 0 confess("end_time must be defined.");
140 }
141  
142 0 my $mtime = $new_entry_h->{mtime};
143 0 ▶ unless (defined $mtime) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $mtime) 0 6159 6159
unless (defined $mtime) 0 0 0
144 0 confess("mtime must be defined.");
145 }
146  
147 0 ▶ my $normalized_output_file = $options->{no_normalize_output_file} ? $output_file : SPVM::Builder::Util::normalize_path($output_file, $self->builder->build_dir);
Branch: 2 branches
BranchTrueFalseTotal
$options->{'no_normalize_output_file'} ? : 4559 1600 6159
$options->{'no_normalize_output_file'} ? : 0 0 0
148
149 0 my $entry_line = sprintf("%d\t%d\t%d\t%s\t%s\x0A",
150 $start_time,
151 $end_time,
152 $mtime,
153 $normalized_output_file,
154 $command_hash);
155
156 0 print $fh $entry_line;
157
158 0 $new_entry_h->{mtime} = $mtime;
159 0 $self->entries_h->{$normalized_output_file} = $new_entry_h;
160 }
161  
162 3740 ▶sub close_log {
Subroutine close_log: called CC=2 SCAR=10.5
Pod undocumented
163 0 ▶ my ($self) = @_;
Subroutine close_log: not called CC=2 SCAR=10.5
Pod undocumented
164
165 0 ▶ if (my $fh = delete $self->{log_fh}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $fh = delete $self->{"log_fh"}) 3740 0 3740
if (my $fh = delete $self->{"log_fh"}) 0 0 0
166 0 close $fh;
167 }
168 }
169  
170 0 ▶sub add_log_header {
Subroutine add_log_header: not called CC=1 SCAR=6.9
Pod undocumented
171 0 ▶ my ($self, @args) = @_;
Subroutine add_log_header: not called CC=1 SCAR=6.9
Pod undocumented
172
173 0 ▶ $self->write_lock_with_flush(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
174 0 ▶ seek($self->log_fh, 0, SEEK_END)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
seek $self->log_fh, 0, 2 or confess("Can't seek to end: $!") 0 0 0
seek $self->log_fh, 0, 2 or confess("Can't seek to end: $!") 0 0 0
175 or confess("Can't seek to end: $!");
176 0 $self->add_log_header_without_lock(@args);
177 0 });
178
179 }
180  
181 733 ▶sub add_log_header_without_lock {
Subroutine add_log_header_without_lock: called CC=1 SCAR=1.2
Pod undocumented
182 0 ▶ my ($self) = @_;
Subroutine add_log_header_without_lock: not called CC=1 SCAR=1.2
Pod undocumented
183
184 0 my $log_fh = $self->log_fh;
185
186 0 print $log_fh "# ninja log v5\x0A";
187 }
188  
189 2066 ▶sub load_log {
Subroutine load_log: called CC=1 SCAR=1.2
Pod undocumented
190 0 ▶ my ($self, @args) = @_;
Subroutine load_log: not called CC=1 SCAR=1.2
Pod undocumented
191
192 2066 ▶ $self->read_lock(sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
193 0 ▶ $self->load_log_without_lock(@args);
Subroutine __ANON__: not called CC=1 SCAR=1.2
194 0 });
195
196 }
197  
198 2799 ▶sub load_log_without_lock {
Subroutine load_log_without_lock: called CC=4 SCAR=18.4
Pod undocumented
199 0 ▶ my ($self) = @_;
Subroutine load_log_without_lock: not called CC=4 SCAR=18.4
Pod undocumented
200
201 0 my $entries_h = {};
202 0 my $entries_length = 0;
203
204 0 my $log_fh = $self->log_fh;
205 0 seek($log_fh, 0, SEEK_SET);
206
207 0 ▶ while (my $line = <$log_fh>) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $line = readline $log_fh)) 11465 2799 14264
if (defined(my $line = readline $log_fh)) 0 0 0
208 0 $line =~ s/[\x0A\x0D]+$//;
209  
210 0 ▶ next if $line =~ /^#/;
Branch: 2 branches
BranchTrueFalseTotal
if $line =~ /^#/ 2344 9121 11465
if $line =~ /^#/ 0 0 0
211  
212 0 my @fields = split(/\t/, $line);
213  
214 0 ▶ if (@fields >= 5) {
Branch: 2 branches
BranchTrueFalseTotal
if (@fields >= 5) 9121 0 9121
if (@fields >= 5) 0 0 0
215 0 my ($start_time, $end_time, $mtime, $normalized_output_file, $command_hash) = @fields;
216  
217 0 $entries_h->{$normalized_output_file} = {
218 start_time => $start_time,
219 end_time => $end_time,
220 mtime => $mtime,
221 output_file => $normalized_output_file,
222 command_hash => $command_hash,
223 };
224
225 0 $entries_length++;
226 }
227 }
228
229 0 $self->entries_length($entries_length);
230
231 0 $self->entries_h($entries_h);
232
233 0 seek($log_fh, 0, SEEK_END);
234 }
235  
236 3338 ▶sub need_generate {
Subroutine need_generate: called CC=6 SCAR=26.0
Pod undocumented
237 0 ▶ my ($self, $options) = @_;
Subroutine need_generate: not called CC=6 SCAR=26.0
Pod undocumented
238
239 0 my $command_hash = $options->{command_hash};
240 0 ▶ unless (defined $command_hash) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $command_hash) 0 3338 3338
unless (defined $command_hash) 0 0 0
241 0 confess("'command_hash' option must be defined.");
242 }
243
244 0 my $output_file = $options->{output_file};
245 0 ▶ unless (defined $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $output_file) 0 3338 3338
unless (defined $output_file) 0 0 0
246 0 confess("'output_file' option must be defined.");
247 }
248
249 0 my $need_generate = 0;
250
251 0 ▶ if (!-f $output_file) {
Branch: 2 branches
BranchTrueFalseTotal
if (not -f $output_file) { } 2969 369 3338
if (not -f $output_file) { } 0 0 0
252 0 $need_generate = 1;
253 }
254 else {
255 # Retrieve the recorded log entry for the output file
256 0 my $entries_h = $self->entries_h;
257 0 my $normalized_output_file = SPVM::Builder::Util::normalize_path($output_file, $self->builder->build_dir);
258 0 my $entry = $entries_h->{$normalized_output_file};
259
260 # If the entry doesn't exist, or the hash simply doesn't match, rebuild.
261 0 ▶ if (!$entry || $command_hash ne $entry->{command_hash}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $entry or $command_hash ne $entry->{"command_hash"}) 4 365 369
if (not $entry or $command_hash ne $entry->{"command_hash"}) 0 0 0
Condition33%2/6
not $entry or $command_hash ne $entry->{"command_hash"}
l!l&&r!l&&!r
04365
not $entry or $command_hash ne $entry->{"command_hash"}
l!l&&r!l&&!r
000
MC/DC50%1/2
not $entry or $command_hash ne $entry->{"command_hash"}
not $entry $command_hash ne $entry->{"command_hash"}
MC/DC0%0/2
not $entry or $command_hash ne $entry->{"command_hash"}
not $entry $command_hash ne $entry->{"command_hash"}
Truth table66%2/3
not(A) or B
A= not $entryB= $command_hash ne $entry->{"command_hash"}
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $entryB= $command_hash ne $entry->{"command_hash"}
ABresult
1X1
011
000
262 0 $need_generate = 1;
263 }
264 }
265  
266 0 return $need_generate;
267 }
268  
269 781 my %NORMALIZE_PATH_CACHE;
270 my %DEPENDENT_CONTENT_HASH_CACHE;
271 781 my %DEPENDANT_FILE_HASH_CACHE;
272  
273 6021 ▶sub create_command_hash {
Subroutine create_command_hash: called CC=16 SCAR=41.1
Pod undocumented
274 0 ▶ my ($self, $options) = @_;
Subroutine create_command_hash: not called CC=16 SCAR=41.1
Pod undocumented
275
276 my $command = $options->{command}
277 0 ▶ // confess("'command' option must be defined.");
Condition25%1/4
$options->{"command"} // confess("'command' option must be defined.")
l!l
60210
$options->{"command"} // confess("'command' option must be defined.")
l!l
00
MC/DC0%0/1
$options->{"command"} // confess("'command' option must be defined.")
$options->{"command"}
MC/DC0%0/1
$options->{"command"} // confess("'command' option must be defined.")
$options->{"command"}
Truth table50%1/2
A
A= $options->{"command"}
Aresult
11
00
Truth table0%0/2
A
A= $options->{"command"}
Aresult
11
00
278
279 my $command_version = $options->{command_version}
280 0 ▶ // confess("'command_version' option must be defined.");
Condition25%1/4
$options->{"command_version"} // confess("'command_version' option must be defined.")
l!l
60210
$options->{"command_version"} // confess("'command_version' option must be defined.")
l!l
00
MC/DC0%0/1
$options->{"command_version"} // confess("'command_version' option must be defined.")
$options->{"command_version"}
MC/DC0%0/1
$options->{"command_version"} // confess("'command_version' option must be defined.")
$options->{"command_version"}
Truth table50%1/2
A
A= $options->{"command_version"}
Aresult
11
00
Truth table0%0/2
A
A= $options->{"command_version"}
Aresult
11
00
281
282 my $dependent_files = $options->{dependent_files}
283 0 ▶ // confess("'dependent_files' optioon must be defined.");
Condition25%1/4
$options->{"dependent_files"} // confess("'dependent_files' optioon must be defined.")
l!l
60210
$options->{"dependent_files"} // confess("'dependent_files' optioon must be defined.")
l!l
00
MC/DC0%0/1
$options->{"dependent_files"} // confess("'dependent_files' optioon must be defined.")
$options->{"dependent_files"}
MC/DC0%0/1
$options->{"dependent_files"} // confess("'dependent_files' optioon must be defined.")
$options->{"dependent_files"}
Truth table50%1/2
A
A= $options->{"dependent_files"}
Aresult
11
00
Truth table0%0/2
A
A= $options->{"dependent_files"}
Aresult
11
00
284
285 0 my $build_dir = $self->builder->build_dir;
286
287 # Check if the file has an extension
288 0 my $has_ext_re = qr/\.[^.\\\/]+$/;
289
290 # Create regex for dependent file exclusion extensions
291 # Get exclude extensions from environment variable: Comma-separated, no dots, trim whitespace
292 0 ▶ my $env_dependent_file_exclude_exts = $ENV{SPVM_DEPENDENT_FILE_EXCLUDE_EXTS} // '';
Condition50%2/4
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"} // ""
l!l
36018
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"} // ""
l!l
00
MC/DC100%1/1
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"} // ""
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"}
MC/DC0%0/1
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"} // ""
$ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"}
Truth table100%2/2
A
A= $ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"}
Aresult
11
00
Truth table0%0/2
A
A= $ENV{"SPVM_DEPENDENT_FILE_EXCLUDE_EXTS"}
Aresult
11
00
293 0 my @dependent_file_exclude_exts_list = grep { length $_ } map { s/^\s+|\s+$//gr } split(/,/, $env_dependent_file_exclude_exts);
294 0 my $dependent_file_exclude_exts_pattern = join('|', map { quotemeta $_ } @dependent_file_exclude_exts_list);
295 0 ▶ my $dependent_file_exclude_exts_re = $dependent_file_exclude_exts_pattern ? qr/(?:$dependent_file_exclude_exts_pattern)$/i : qr/$^/;
Branch: 2 branches
BranchTrueFalseTotal
$dependent_file_exclude_exts_pattern ? : 3 6018 6021
$dependent_file_exclude_exts_pattern ? : 0 0 0
296
297 0 my $sha = Digest::SHA->new(1);
298 0 $sha->add(Digest::SHA::sha1_hex($command));
299 0 $sha->add(Digest::SHA::sha1_hex($command_version));
300
301 0 my %seen_dependent_files_h;
302 0 ▶ @$dependent_files = grep { length $_ && !$seen_dependent_files_h{$_}++ } @$dependent_files;
Condition50%3/6
length $_ && !$seen_dependent_files_h{$_}++
!ll&&!rl&&r
43697312483
length $_ && !$seen_dependent_files_h{$_}++
!ll&&!rl&&r
000
MC/DC100%2/2
length $_ && !$seen_dependent_files_h{$_}++
length $_ !$seen_dependent_files_h{$_}++
MC/DC0%0/2
length $_ && !$seen_dependent_files_h{$_}++
length $_ !$seen_dependent_files_h{$_}++
Truth table100%3/3
A && not(B)
A= length $_B= !$seen_dependent_files_h{$_}++
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= length $_B= !$seen_dependent_files_h{$_}++
ABresult
0X0
100
111
303 0 @$dependent_files = sort @$dependent_files;
304 0 for my $dependent_file (@$dependent_files) {
305 0 my $dependant_file_hash = $DEPENDANT_FILE_HASH_CACHE{$dependent_file};
306 0 ▶ unless (defined $dependant_file_hash) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $dependant_file_hash) 7250 5233 12483
unless (defined $dependant_file_hash) 0 0 0
307 0 my @child_dependent_files;
308 File::Find::find({
309 8464 ▶ wanted => sub {
Subroutine __ANON__: called CC=6 SCAR=24.1
310 0 ▶ my $child_dependent_file = $File::Find::name;
Subroutine __ANON__: not called CC=6 SCAR=24.1
311
312 # Fetch from cache or execute stat
313 0 my @stat_result = stat $child_dependent_file;
314 0 ▶ return unless @stat_result;
Branch: 2 branches
BranchTrueFalseTotal
unless @stat_result 63 8401 8464
unless @stat_result 0 0 0
315
316 # Get mode and check file types using constants
317 0 my $mode = $stat_result[2];
318 0 my $is_file = S_ISREG($mode);
319
320 # Prune hidden files and directories
321 0 ▶ if ($child_dependent_file =~ m|[\\/]\.[^\\/]*|) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child_dependent_file =~ m[[\\/]\.[^\\/]*]) 5456 2945 8401
if ($child_dependent_file =~ m[[\\/]\.[^\\/]*]) 0 0 0
322 0 $File::Find::prune = 1;
323 0 return;
324 }
325
326 # File handling
327 0 ▶ if ($is_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_file) 2633 312 2945
if ($is_file) 0 0 0
328 # Match specs: has extension, not excluded, not in hidden dir
329 0 ▶ if ($child_dependent_file =~ $has_ext_re && $child_dependent_file !~ $dependent_file_exclude_exts_re) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/) 2631 2 2633
if ($child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/) 0 0 0
Condition33%2/6
$child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
!ll&&!rl&&r
022631
$child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
!ll&&!rl&&r
000
MC/DC50%1/2
$child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
$child_dependent_file =~ /$has_ext_re/ not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
MC/DC0%0/2
$child_dependent_file =~ /$has_ext_re/ and not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
$child_dependent_file =~ /$has_ext_re/ not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
Truth table66%2/3
A and not(B)
A= $child_dependent_file =~ /$has_ext_re/B= not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $child_dependent_file =~ /$has_ext_re/B= not $child_dependent_file =~ /$dependent_file_exclude_exts_re/
ABresult
0X0
100
111
330 0 push @child_dependent_files, $child_dependent_file;
331 }
332 }
333 },
334 0 no_chdir => 1,
335 follow => 1,
336 }, $dependent_file);
337
338 0 my $dependent_file_sha = Digest::SHA->new(1);
339
340 0 my %seen_child_dependent_files_h;
341 0 ▶ @child_dependent_files = grep { length $_ && !$seen_child_dependent_files_h{$_}++ } @child_dependent_files;
Condition16%1/6
length $_ && !$seen_child_dependent_files_h{$_}++
!ll&&!rl&&r
002631
length $_ && !$seen_child_dependent_files_h{$_}++
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && !$seen_child_dependent_files_h{$_}++
length $_ !$seen_child_dependent_files_h{$_}++
MC/DC0%0/2
length $_ && !$seen_child_dependent_files_h{$_}++
length $_ !$seen_child_dependent_files_h{$_}++
Truth table33%1/3
A && not(B)
A= length $_B= !$seen_child_dependent_files_h{$_}++
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= length $_B= !$seen_child_dependent_files_h{$_}++
ABresult
0X0
100
111
342 0 @child_dependent_files = sort @child_dependent_files;
343 0 for my $child_dependent_file (@child_dependent_files) {
344 0 my $is_under_current_dir = $self->is_under_current_dir_without_build_dir($child_dependent_file);
345
346 # Path hash
347 0 ▶ my $normalized = $NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} //=
Condition16%1/6
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} //= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
l!l&&r!l&&!r
026310
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} //= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
l!l&&r!l&&!r
000
MC/DC0%0/2
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} //= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
MC/DC0%0/2
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} //= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
$NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir} SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
Truth table33%1/3
A //= B
A= $NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir}B= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $NORMALIZE_PATH_CACHE{$child_dependent_file}{$build_dir}B= SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir)
ABresult
1X1
011
000
348 SPVM::Builder::Util::normalize_path($child_dependent_file, $build_dir);
349 0 $dependent_file_sha->add(Digest::SHA::sha1_hex($normalized));
350
351 # Content or Mtime hash
352 0 ▶ if ($is_under_current_dir && -T $child_dependent_file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_under_current_dir and -T $child_dependent_file) { } 684 1947 2631
if ($is_under_current_dir and -T $child_dependent_file) { } 0 0 0
Condition33%2/6
$is_under_current_dir and -T $child_dependent_file
!ll&&!rl&&r
19470684
$is_under_current_dir and -T $child_dependent_file
!ll&&!rl&&r
000
MC/DC50%1/2
$is_under_current_dir and -T $child_dependent_file
$is_under_current_dir -T $child_dependent_file
MC/DC0%0/2
$is_under_current_dir and -T $child_dependent_file
$is_under_current_dir -T $child_dependent_file
Truth table66%2/3
A and B
A= $is_under_current_dirB= -T $child_dependent_file
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $is_under_current_dirB= -T $child_dependent_file
ABresult
0X0
100
111
353 0 my $content_hash = $DEPENDENT_CONTENT_HASH_CACHE{$child_dependent_file};
354 0 ▶ unless (defined $content_hash) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $content_hash) 684 0 684
unless (defined $content_hash) 0 0 0
355 0 my $content_hash_sha = Digest::SHA->new(1);
356 0 $content_hash_sha->addfile($child_dependent_file);
357 0 $content_hash = $DEPENDENT_CONTENT_HASH_CACHE{$child_dependent_file} = $content_hash_sha->hexdigest;
358 }
359 0 $dependent_file_sha->add($content_hash);
360 }
361 else {
362 # Retrieve the stat object from cache
363 0 my @stat_result = stat $child_dependent_file;
364 0 ▶ unless (@stat_result) {
Branch: 2 branches
BranchTrueFalseTotal
unless (@stat_result) 0 1947 1947
unless (@stat_result) 0 0 0
365 0 confess("[Unexpected Error]stat failed. \$child_dependent_file='$child_dependent_file'.");
366 }
367
368 # Use mtime and size for the hash
369 0 my $mitme_and_size_json = qq|{mtime":$stat_result[9],"size":$stat_result[7]}|;
370 0 $dependent_file_sha->add($mitme_and_size_json);
371 }
372 }
373 0 $dependant_file_hash = $DEPENDANT_FILE_HASH_CACHE{$dependent_file} = $dependent_file_sha->hexdigest;
374 }
375 0 $sha->add($dependant_file_hash);
376 }
377
378 0 return $sha->hexdigest;
379 }
380  
381 781 my $RECOMPACTED = 0;
382 2066 ▶sub recompact {
Subroutine recompact: called CC=2 SCAR=9.7
Pod undocumented
383 0 ▶ my ($self) = @_;
Subroutine recompact: not called CC=2 SCAR=9.7
Pod undocumented
384
385 0 ▶ if ($RECOMPACTED) {
Branch: 2 branches
BranchTrueFalseTotal
if ($RECOMPACTED) 1333 733 2066
if ($RECOMPACTED) 0 0 0
386 0 return;
387 }
388
389 733 ▶ $self->write_lock_with_flush(sub {
Subroutine __ANON__: called CC=4 SCAR=19.3
390 0 ▶ $self->load_log_without_lock;
Subroutine __ANON__: not called CC=4 SCAR=19.3
391
392 0 my $log_fh = $self->log_fh;
393
394 # Truncate the file to 0 bytes
395 # This is safe because we hold the write lock.
396 0 ▶ truncate($log_fh, 0)
Branch: 2 branches
BranchTrueFalseTotal
truncate $log_fh, 0 or confess("Can't truncate log file: $!") 733 0 733
truncate $log_fh, 0 or confess("Can't truncate log file: $!") 0 0 0
397 or confess("Can't truncate log file: $!");
398
399 # Move the file pointer back to the beginning
400 # Crucial for Windows to avoid sparse files (null bytes at the start).
401 0 ▶ seek($log_fh, 0, SEEK_SET)
Branch: 2 branches
BranchTrueFalseTotal
seek $log_fh, 0, 0 or confess("Can't seek to the start of log file: $!") 733 0 733
seek $log_fh, 0, 0 or confess("Can't seek to the start of log file: $!") 0 0 0
402 or confess("Can't seek to the start of log file: $!");
403
404 # Write header
405 0 $self->add_log_header_without_lock;
406
407 0 my $entries_h = $self->entries_h;
408
409 # Sort by start_time (ascending)
410 my @normalized_output_files = sort {
411 0 $entries_h->{$a}{start_time} <=> $entries_h->{$b}{start_time}
412 0 } keys %$entries_h;
413
414 # Write each valid log entry
415 0 for my $normalized_output_file (@normalized_output_files) {
416 0 my $entory_h = $entries_h->{$normalized_output_file};
417 0 $self->add_log_without_lock($entory_h, {no_normalize_output_file => 1});
418 }
419 0 });
420
421 0 $RECOMPACTED = 1;
422 }
423  
424 2066 ▶sub read_lock {
Subroutine read_lock: called CC=1 SCAR=1.2
Pod undocumented
425 0 ▶ my ($self, $cb) = @_;
Subroutine read_lock: not called CC=1 SCAR=1.2
Pod undocumented
426
427 0 return $self->builder->global_file_read_lock($cb);
428 }
429  
430 2333 ▶sub write_lock {
Subroutine write_lock: called CC=1 SCAR=1.2
Pod undocumented
431 0 ▶ my ($self, $cb) = @_;
Subroutine write_lock: not called CC=1 SCAR=1.2
Pod undocumented
432
433 0 return $self->builder->global_file_lock($cb);
434 }
435  
436 2333 ▶sub write_lock_with_flush {
Subroutine write_lock_with_flush: called CC=1 SCAR=1.2
Pod undocumented
437 0 ▶ my ($self, $cb) = @_;
Subroutine write_lock_with_flush: not called CC=1 SCAR=1.2
Pod undocumented
438
439 2333 ▶ return $self->write_lock(sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
440 0 ▶ my $result = $cb->();
Subroutine __ANON__: not called CC=1 SCAR=1.2
441
442 # Flush the log file handle directly
443 0 $self->log_fh->flush;
444
445 0 return $result;
446 0 });
447 }
448  
449 2066 ▶sub create_log {
Subroutine create_log: called CC=1 SCAR=1.2
Pod undocumented
450 0 ▶ my ($self) = @_;
Subroutine create_log: not called CC=1 SCAR=1.2
Pod undocumented
451
452 0 mkpath $self->builder->build_dir;
453
454 0 $self->open_log('>>');
455 0 $self->close_log;
456 }
457  
458 781 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
459  
460 # Get the normalized real physical path for current directory at startup
461 781 my $CURRENT_DIR_ABS = abs_path(Cwd::getcwd());
462 781 $CURRENT_DIR_ABS =~ s|\\|/|g;
463 781 ▶unless ($CURRENT_DIR_ABS =~ m|/$|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CURRENT_DIR_ABS =~ m[/$]) 781 0 781
464 781 $CURRENT_DIR_ABS .= '/';
465 }
466  
467 781 my %ABS_PATH_CACHE;
468 2631 ▶sub is_under_current_dir_without_build_dir {
Subroutine is_under_current_dir_without_build_dir: called CC=5 SCAR=22.5
Pod undocumented
469 0 ▶ my ($self, $path) = @_;
Subroutine is_under_current_dir_without_build_dir: not called CC=5 SCAR=22.5
Pod undocumented
470
471 # Use abs_path to resolve symlinks (especially for Mac /var -> /private/var)
472 0 my $build_dir_abs = $ABS_PATH_CACHE{$self->builder->build_dir};
473 0 ▶ unless (defined $build_dir_abs) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $build_dir_abs) 489 2142 2631
unless (defined $build_dir_abs) 0 0 0
474 0 my $tmp_build_dir_abs = abs_path($self->builder->build_dir);
475 0 $tmp_build_dir_abs =~ s|\\|/|g;
476 0 $build_dir_abs = $ABS_PATH_CACHE{$self->builder->build_dir} = $tmp_build_dir_abs;
477 }
478
479 # Resolve the input path to its real physical path
480 0 my $path_abs = $ABS_PATH_CACHE{$path};
481 0 ▶ unless (defined $path_abs) {
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $path_abs) 2631 0 2631
unless (defined $path_abs) 0 0 0
482 0 my $tmp_path_abs = abs_path($path);
483 0 $tmp_path_abs =~ s|\\|/|g;
484 0 $path_abs = $ABS_PATH_CACHE{$path} = $tmp_path_abs;
485 }
486
487 # Now the comparison should work even on Mac's temp dirs
488 0 ▶ if (index($path_abs, $CURRENT_DIR_ABS) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($path_abs, $CURRENT_DIR_ABS) == 0) 684 1947 2631
if (index($path_abs, $CURRENT_DIR_ABS) == 0) 0 0 0
489 0 ▶ if (index($path_abs, $build_dir_abs) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($path_abs, $build_dir_abs) == 0) 0 684 684
if (index($path_abs, $build_dir_abs) == 0) 0 0 0
490 0 return 0;
491 }
492 0 return 1;
493 }
494
495 0 return 0;
496 }
497  
498 1674 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=1.2
499 0 ▶ my ($self) = @_;
Subroutine DESTROY: not called CC=1 SCAR=1.2
500
501 # Ensure the log file handle is closed
502 0 $self->close_log;
503 }
504  
505 # Parallel Note
506 # Use open mode '>>' and '+<' for other process to open the same file.
507 # Open the log file in new and close the log file in DESTROY.
508  
509 781 1;
510  
511 =head1 Name
512  
513 SPVM::Builder::Ninja - Incremental Build Management using Ninja Log Format
514  
515 =head1 Description
516  
517 L<SPVM::Builder::Ninja> manages incremental builds for the SPVM build system. It uses a build log format compatible with the Ninja build tool (C<.ninja_log>) to determine if a target file needs to be regenerated.
518  
519 Unlike traditional C<make>, which relies solely on file modification times (mtime), this class provides a more robust re-generation logic by considering command-line arguments and the content hashes of dependent files.
520  
521 =head1 Overview of the Build System
522  
523 This class implements a high-performance and reliable build system based on the following four pillars:
524  
525 =head2 1. Content-Based Re-generation (Command Hash)
526  
527 To ensure build correctness, this system generates a B<SHA-1 hash (Command Hash)> by combining various factors:
528  
529 =over 2
530  
531 =item * The command-line string itself.
532  
533 =item * The version of the compiler or tool used.
534  
535 =item * The paths and B<content hashes> of all dependent files (e.g., header files).
536  
537 =back
538  
539 This allows the system to accurately detect when a rebuild is necessary, such as when a source file is modified, an include file is changed, or a compiler flag is updated (e.g., from C<-O2> to C<-O3>).
540  
541 =head2 2. Ninja-Compatible Log Management
542  
543 Each successful build entry is recorded in C<.ninja_log>, including the output file name, command hash, start/end times, and mtime. This format is compatible with Ninja v5. The class also includes a "Recompact" feature that cleans up old entries and rewrites the log to prevent it from growing indefinitely.
544  
545 =head2 3. Concurrency Safety via File Locking
546  
547 SPVM supports parallel builds to leverage multi-core processors. To prevent data corruption when multiple processes write to the same log file, this class implements strict B<advisory locking (LOCK_SH / LOCK_EX)> using a C<.ninja_lock> file. This ensures that concurrent build processes can operate safely and efficiently.
548  
549 =head2 4. Dependency Scanning
550  
551 The system recursively scans specified directories to collect dependencies such as C/C++ header files. Since the state of these files is reflected in the command hash, any change to a deep-level header file will correctly trigger a re-compilation of the dependent source files.
552  
553 =cut
« blib/lib/SPVM/Builder/API.pm ↑ Summary blib/lib/SPVM/Builder/Config.pm »