ArduinoBuilder/Builder.pm

filter: Statementstmt 25.0% 42 / 168 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 46.4% 13 / 28 Podpod 0.0% 0 / 8 totaltotal 19.6% 55 / 280 CC 39 SCAR 40.1
CC
39
Cov
18%
_run_recipe_pattern
156.0
compute_binary_size
90.0
build_archive
56.0
CRAP 55.4
« ArduinoBuilder/Discovery.pm ↑ Summary ArduinoBuilder/System.pm »
1 package App::ArduinoBuilder::Builder;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use App::ArduinoBuilder::Config 'get_os_name';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use App::ArduinoBuilder::DepCheck 'check_dep';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::ArduinoBuilder::FilePath 'find_all_files_with_extensions';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::ArduinoBuilder::System 'execute_cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Path 'make_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Parallel::TaskExecutor 'default_executor';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 my @supported_source_extensions = qw(c cpp S ino);
17  
18 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my ($class, $config) = @_;
20 0 return bless {config => $config}, $class;
21 }
22  
23 0 ▶sub _run_recipe_pattern {
Subroutine _run_recipe_pattern: not called CC=12 SCAR=50.5
24 # It’s undocumented but any recipe can have several patterns (and this is used
25 # by several cores).
26 0 my ($this, $recipe_name, %options) = @_;
27 0 my $recipes = $this->{config}->filter("recipe.${recipe_name}");
28 0 my $is_hook = $recipe_name =~ m/^hooks\./;
29 0 my $nb_recipes = $recipes->size();
30 0 ▶ unless ($is_hook || $nb_recipes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_hook or $nb_recipes) 0 0 0
Condition0%0/3
$is_hook or $nb_recipes
l!l&&r!l&&!r
000
MC/DC0%0/2
$is_hook or $nb_recipes
$is_hook $nb_recipes
Truth table0%0/3
A or B
A= $is_hookB= $nb_recipes
ABresult
1X1
011
000
31 0 error "Can’t find recipe for '${recipe_name}'";
32 0 return;
33 }
34 0 ▶ debug "Running %d hook%s for '${recipe_name}", $nb_recipes, ($nb_recipes > 1 ? 's' : '') if $is_hook && $nb_recipes;
Branch: 2 branches
BranchTrueFalseTotal
if $is_hook and $nb_recipes 0 0 0
$nb_recipes > 1 ? : 0 0 0
Condition0%0/3
$is_hook and $nb_recipes
!ll&&!rl&&r
000
MC/DC0%0/2
$is_hook and $nb_recipes
$is_hook $nb_recipes
Truth table0%0/3
A and B
A= $is_hookB= $nb_recipes
ABresult
0X0
100
111
35 0 for my $k (sort $recipes->keys()) {
36 0 ▶ if ($options{is_objcopy}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'is_objcopy'}) { } 0 0 0
elsif ($options{'is_size'}) { } 0 0 0
37 0 ▶ fatal "Invalid objcopy recipe name: recipe.${recipe_name}.${k}" unless $k =~ m/^\w+\.(?:\d+\.)?pattern$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^\w+\.(?:\d+\.)?pattern$/ 0 0 0
38 } elsif ($options{is_size}) {
39 0 ▶ next unless $k =~ m/^(?:\d+\.)?pattern$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(?:\d+\.)?pattern$/ 0 0 0
40 } else {
41 0 ▶ fatal "Invalid recipe name: recipe.${recipe_name}.${k}" unless $k =~ m/^(?:\d+\.)?pattern$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^(?:\d+\.)?pattern$/ 0 0 0
42 }
43 0 execute_cmd($recipes->get($k, base => $this->{config}, %options), %options);
44 }
45 0 return;
46 }
47  
48 # This method is a very partial implementation of:
49 # https://arduino.github.io/arduino-cli/0.32/sketch-build-process/#pre-processing
50 # TODO: theoritically all .ino and .pde (?) files should be concatenated together
51 # rather than processed separately.
52 # TODO: we need to generate prototypes for the methods, using ctags.
53 0 ▶sub _ino_to_cpp {
Subroutine _ino_to_cpp: not called CC=6 SCAR=37.4
54 0 my ($this, $source, $target) = @_;
55  
56 0 ▶ open my $fi, '<', "${source}" or fatal "Can’t open the source file '${source}' for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fi, "<", "$source" or fatal("Can\x{2019}t open the source file '${source}' for reading: $!") 0 0 0
57 0 ▶ open my $fo, '>', "${target}.cpp-pre" or fatal "Can’t open intermediate file '${target}.cpp-pre' for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fo, ">", "$target.cpp-pre" or fatal("Can\x{2019}t open intermediate file '$target.cpp-pre' for writing: $!") 0 0 0
58 0 print $fo "#include \"Arduino.h\"\n";
59 0 print $fo "#line 1 \"${source}\"\n";
60 0 ▶ while (my $l = <$fi>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $fi)) 0 0 0
61 0 print $fo $l;
62 }
63 0 close $fi;
64 0 close $fo;
65  
66 0 my $recipe;
67 # This is ugly but we can’t easily do this elsewhere because we need the recipe command to be
68 # resolved to be able to find the MMD flag, but we can’t resolve it too early because some
69 # parameters will vary with each call.
70 0 ▶ if (!$this->{config}->exists('recipe.preproc.macros')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $this->{'config'}->exists('recipe.preproc.macros')) { } 0 0 0
71 # We assume that this recipe exists (and that it’s not a multi-pattern recipe).
72 0 $recipe = $this->{config}->get('recipe.cpp.o.pattern', allow_partial => 1, with => {source_file => "${target}.cpp-pre", object_file => "${target}.cpp"});
73 # This is a heuristic because the '-x c++' must appear before the source file, so we can’t put
74 # it at the end. It’s unclear how this command is actually generated by the Arduino GUI.
75 0 ▶ $recipe =~ s/(?<=\s)-MMD\b/-w -x c++ -E -CC/ or fatal "Can’t modify the recipe.cpp.o.pattern command: ${recipe}";
Branch: 1 branch
BranchTrueFalseTotal
$recipe =~ s/(?<=\s)-MMD\b/-w -x c++ -E -CC/ or fatal("Can\x{2019}t modify the recipe.cpp.o.pattern command: $recipe") 0 0 0
76 } else {
77 # It’s weird but this is not a "pattern" recipe. Why?
78 0 $recipe = $this->{config}->get('recipe.preproc.macros', with => {source_file => "${target}.cpp-pre", preprocessed_file_path => "${target}.cpp"});
79 }
80 0 execute_cmd($recipe);
81  
82 # TODO: there is a step in the real Arduino tool that is badly documented but which uses ctags to extract the
83 # prototype of the functions in the .ino files (and adds them at the beginning of the C++ file), so that the
84 # functions can be called before they are declared.
85 # TODO: if the correct null output is needed again, move this line to System.pm
86 # my $null = get_os_name() eq 'windows' ? 'nul' : '/dev/null';
87 0 return;
88 }
89  
90 # dirname($target) must already exist.
91 0 ▶sub build_file {
Subroutine build_file: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my ($this, $source, $target) = @_;
93 0 my @ext = $source =~ m/\.([^.]+)$/;
94 0 ▶ fatal "File '$source' has no recognizable extension." unless @ext;
Branch: 1 branch
BranchTrueFalseTotal
unless @ext 0 0 0
95 0 my $ext = $ext[0];
96 0 ▶ if ($ext eq 'ino') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq "ino") 0 0 0
97 0 debug "Converting '${source}' to C++";
98 0 $this->_ino_to_cpp($source, $target);
99 0 $source = $target.'.cpp';
100 0 $ext = 'cpp';
101 }
102 0 debug "Building '${source}'";
103 0 $this->_run_recipe_pattern("${ext}.o", with => {source_file => $source, object_file => $target});
104 0 return;
105 }
106  
107 0 ▶sub _add_to_archive {
Subroutine _add_to_archive: not called CC=1 SCAR=6.9
108 0 my ($this, $object_file, $archive) = @_;
109 0 $this->_run_recipe_pattern('ar', with => {object_file => $object_file, archive_file => $archive, archive_file_path => catfile($this->{config}->get('build.path'), $archive)});
110 0 return;
111 }
112  
113 # Object to avoid collision in generated object file names. Both because we
114 # flatten the directory structure of the sources that we build and also because
115 # archive files may only reference the file names.
116 1 package ObjectNameBuilder {
117 1 ▶ use File::Basename 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 1 ▶ use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
121 0 my ($class, $target_dir) = @_;
122 0 return bless {target_dir => $target_dir, files => {}}, $class;
123 }
124  
125 0 ▶ sub object_for {
Subroutine object_for: not called CC=2 SCAR=17.9
126 0 my ($this, $source) = @_;
127 0 my $basename = fileparse($source);
128 0 my $count = $this->{files}{$basename}++;
129 0 ▶ $basename .= ".${count}" if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 0 0 0
130 0 return catfile($this->{target_dir}, "${basename}.o");
131 }
132 }
133  
134 # target_dir has all the intermediate file, $archive is a file name that goes in build.path.
135 0 ▶sub build_archive {
Subroutine build_archive: not called CC=7 SCAR=40.3
Pod undocumented
136 0 my ($this, $source_dirs, $target_dir, $archive, $force) = @_;
137 0 make_path($target_dir);
138 0 my $obj_name = ObjectNameBuilder->new($target_dir);
139 0 my @tasks;
140 0 for my $d (@{$source_dirs}) {
141 # BUG: There is still a bug here (and in build_object_files) which is that if a file is removed
142 # from the sources and there is another file with the same basename, we will do weird things
143 # with the object files that will be mixed. This is unavoidable for now, the project needs to be
144 # cleaned when files are deleted.
145 0 my @sources = sort (find_all_files_with_extensions($d, [@supported_source_extensions]));
146 0 for my $s (@sources) {
147 0 my $object_file = $obj_name->object_for($s);
148 # This line can be commented and the one inside the sub-task can be
149 # uncommented to execute the dependency check inside the forked process.
150 # However this does not seem to improve the execution speed.
151 0 ▶ next unless $force || check_dep($s, $object_file);
Branch: 1 branch
BranchTrueFalseTotal
unless $force or check_dep($s, $object_file) 0 0 0
Condition0%0/3
$force or check_dep($s, $object_file)
l!l&&r!l&&!r
000
MC/DC0%0/2
$force or check_dep($s, $object_file)
$force check_dep($s, $object_file)
Truth table0%0/3
A or B
A= $forceB= check_dep($s, $object_file)
ABresult
1X1
011
000
152 push @tasks, default_executor()->run(
153 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 # return unless $force || check_dep($s, $object_file);
155 0 $this->build_file($s, $object_file);
156 0 return $object_file;
157 0 });
158 }
159 }
160 0 default_executor->wait();
161 0 my $did_something;
162 0 for my $t (@tasks) {
163 0 my $o = $t->data();
164 0 ▶ if ($o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o) 0 0 0
165 0 $this->_add_to_archive($o, $archive);
166 0 $did_something = 1;
167 }
168 }
169 0 return $did_something;
170 }
171  
172 # Like in find_all_files_with_extensions, $source_dir can be a single directory (scalar)
173 # or an array-ref of directories.
174 # excluded_dirs must be an array-ref.
175 0 ▶sub build_object_files {
Subroutine build_object_files: not called CC=6 SCAR=37.4
Pod undocumented
176 0 my ($this, $source_dir, $target_dir, $excluded_dirs, $force, $no_recurse) = @_;
177 0 make_path($target_dir);
178 0 my @sources = sort (find_all_files_with_extensions($source_dir, [@supported_source_extensions], $excluded_dirs, $no_recurse));
179 0 my $obj_name = ObjectNameBuilder->new($target_dir);
180 0 my @tasks;
181 0 for my $s (@sources) {
182 0 my $object_file = $obj_name->object_for($s);
183 0 ▶ next unless $force || check_dep($s, $object_file);
Branch: 1 branch
BranchTrueFalseTotal
unless $force or check_dep($s, $object_file) 0 0 0
Condition0%0/3
$force or check_dep($s, $object_file)
l!l&&r!l&&!r
000
MC/DC0%0/2
$force or check_dep($s, $object_file)
$force check_dep($s, $object_file)
Truth table0%0/3
A or B
A= $forceB= check_dep($s, $object_file)
ABresult
1X1
011
000
184 push @tasks, default_executor()->run(
185 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
186 # return unless $force || check_dep($s, $object_file);
187 0 $this->build_file($s, $object_file);
188 0 return 1;
189 0 });
190 }
191 0 default_executor->wait();
192 0 for my $t (@tasks) {
193 0 my $o = $t->data();
194 0 ▶ if ($o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o) 0 0 0
195 0 return 1;
196 }
197 }
198 0 return 0;
199 }
200  
201 0 ▶sub link_executable {
Subroutine link_executable: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my ($this, $object_files, $archive) = @_;
203 0 $this->_run_recipe_pattern('c.combine', with => {object_files => '"'.join('" "', @{$object_files}).'"', archive_file => $archive, archive_file_path => catfile($this->{config}->get('build.path'), $archive)});
204 0 return;
205 }
206  
207 0 ▶sub compute_binary_size {
Subroutine compute_binary_size: not called CC=9 SCAR=45.0
Pod undocumented
208 0 my ($this) = @_;
209 0 my $output;
210 0 $this->_run_recipe_pattern('size', capture_output => \$output, is_size => 1);
211 # TODO: There is a variant using the 'advanced_size' recipe that can be
212 # implemented for more complex scenario and that we are not yet supporting.
213  
214 0 my $bin_size_re = $this->{config}->get('recipe.size.regex', default => undef);
215 0 ▶ if ($bin_size_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bin_size_re) 0 0 0
216 0 my $bin_size = 0;
217 0 ▶ while ($output =~ m/${bin_size_re}/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /$bin_size_re/gm) 0 0 0
218 0 $bin_size += $1;
219 }
220 0 my $max_bin_size = $this->{config}->get('upload.maximum_size', default => undef);
221 0 ▶ if ($max_bin_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_bin_size) { } 0 0 0
222 0 info ' Sketch uses %d bytes (%d%%) of program space. Maximum is %d bytes.', $bin_size, ($bin_size * 100 / $max_bin_size), $max_bin_size;
223 0 ▶ fatal 'Sketch is too large' if $bin_size > $max_bin_size;
Branch: 1 branch
BranchTrueFalseTotal
if $bin_size > $max_bin_size 0 0 0
224 } else {
225 0 info ' Sketch uses %d bytes of program space.', $bin_size;
226 }
227 }
228  
229 0 my $data_size_re =$this->{config}->get('recipe.size.regex.data', default => undef);
230 0 ▶ if ($data_size_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_size_re) 0 0 0
231 0 my $data_size = 0;
232 0 ▶ while ($output =~ m/${data_size_re}/mg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output =~ /$data_size_re/gm) 0 0 0
233 0 $data_size += $1;
234 }
235 0 my $max_data_size = $this->{config}->get('upload.maximum_data_size', default => undef);
236 0 ▶ if ($max_data_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_data_size) { } 0 0 0
237 0 info ' Global variables use %d bytes (%d%%) of dynamic memory, leaving %d bytes for local variables. Maximum is %d bytes.', $data_size, ($data_size * 100 / $max_data_size), ($max_data_size - $data_size), $max_data_size;
238 0 ▶ fatal 'Too much memory used' if $data_size > $max_data_size;
Branch: 1 branch
BranchTrueFalseTotal
if $data_size > $max_data_size 0 0 0
239 } else {
240 0 info ' Global variables use %d bytes of dynamic memory.', $data_size;
241 }
242 }
243  
244 0 return;
245 }
246  
247 0 ▶sub run_hook {
Subroutine run_hook: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my ($this, $hook_name) = @_;
249 0 $this->_run_recipe_pattern("hooks.${hook_name}");
250 0 return;
251 }
252  
253 0 ▶sub objcopy {
Subroutine objcopy: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my ($this) = @_;
255 0 $this->_run_recipe_pattern('objcopy', is_objcopy => 1);
256 }
257  
258 1 1;
« ArduinoBuilder/Discovery.pm ↑ Summary ArduinoBuilder/System.pm »