ArduinoBuilder.pm

filter: Statementstmt 16.6% 68 / 408 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 37.9% 22 / 58 Podpod 0.0% 0 / 9 totaltotal 13.8% 90 / 648 CC 81 SCAR 57.0
CC
81
Cov
12%
generate_project_config
756.0
build
702.0
Run
156.0
CRAP 300.0
↑ Summary ArduinoBuilder/DepCheck.pm »
1 package App::ArduinoBuilder;
2  
3 1 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use App::ArduinoBuilder::Builder 'build_archive', 'build_object_files', 'link_executable', 'run_hook';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use App::ArduinoBuilder::Config 'get_os_name';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::ArduinoBuilder::Discovery;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use App::ArduinoBuilder::FilePath 'find_latest_revision_dir', 'list_sub_directories', 'find_all_files_with_extensions';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use App::ArduinoBuilder::Monitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use App::ArduinoBuilder::System 'find_arduino_dir', 'system_cwd', 'execute_cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Data::Section::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Path 'remove_tree';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use List::Util 'any', 'none', 'first';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Log::Any::Adapter;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Log::Any::Simple ':default';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Parallel::TaskExecutor 'default_executor';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 our $VERSION = '0.08';
28  
29 # User agent used for the pluggable discovery and pluggable monitor tools.
30 1 our $TOOLS_USER_AGENT = "\"App::ArduinoBuilder ${VERSION}\"";
31  
32 0 ▶sub _string_to_log4perl_level {
Subroutine _string_to_log4perl_level: not called CC=7 SCAR=40.3
33 0 my ($level) = @_;
34 0 ▶ return "FATAL" if $level =~ m/^FATAL$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^FATAL$/iu 0 0 0
35 0 ▶ return "ERROR" if $level =~ m/^ERR(?:OR)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^ERR(?:OR)?$/iu 0 0 0
36 0 ▶ return "WARNING" if $level =~ m/^WARN(:?ING)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^WARN(:?ING)?$/iu 0 0 0
37 0 ▶ return "INFO" if $level =~ m/^INFO?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^INFO?$/iu 0 0 0
38 0 ▶ return "DEBUG" if $level =~ m/^(?:DBG|DEBUG)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^(?:DBG|DEBUG)$/iu 0 0 0
39 0 ▶ return "TRACE" if $level =~ m/^(?:TRACE|FULL(:?_?(?:DBG|DEBUG))?)$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $level =~ /^(?:TRACE|FULL(:?_?(?:DBG|DEBUG))?)$/iu 0 0 0
40 0 fatal "Unknown log level: ${level}";
41 }
42  
43 0 ▶sub set_log_level {
Subroutine set_log_level: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ($str_level) = @_;
45 0 my $log4perl_priority = Log::Log4perl::Level::to_priority(_string_to_log4perl_level($str_level));
46 0 Log::Log4perl->get_logger("")->level($log4perl_priority);
47 0 return;
48 }
49  
50 0 ▶sub Run {
Subroutine Run: not called CC=12 SCAR=50.5
Pod undocumented
51 # We initialize Log4perl here because we might need to log early during the
52 # initialization. But the default level or even the full config can be
53 # overriden later.
54 0 Log::Log4perl->init(\Data::Section::Simple::get_data_section('log4perl.conf'));
55 0 Log::Any::Adapter->set('Log4perl');
56 0 ▶ set_log_level($ENV{ARDUINO_BUILDER_LOG_LEVEL}) if exists $ENV{ARDUINO_BUILDER_LOG_LEVEL};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"ARDUINO_BUILDER_LOG_LEVEL"} 0 0 0
57  
58 0 my $config = App::ArduinoBuilder::Config->new();
59  
60 0 my (@skip, @force, @only);
61 GetOptions(
62 0 ▶ 'help|h' => sub { pod2usage(-exitval => 0, -verbose => 2)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 ▶ 'project-dir|project|p=s' => sub { $config->set('builder.project_dir' => $_[1], allow_override => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 ▶ 'build-dir|build|b=s' => sub { $config->set('builder.internal.build_dir' => $_[1], allow_override => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 ▶ 'log-level|l=s' => sub { set_log_level($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 ▶ 'config|c=s%' => sub { $config->set($_[1] => $_[2], allow_override => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 ▶ 'menu=s%' => sub { $config->set('builder.menu.'.$_[1] => $_[2], allow_override => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 ▶ 'skip=s@' => sub { push @skip, split /,/, $_[1] }, # skip this step
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 ▶ 'force=s@' => sub { push @force, split /,/, $_[1] }, # even if it would be skipped by the dependency checker
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 ▶ 'only=s@' => sub { push @only, split /,/, $_[1] }, # run only these steps (skip all others)
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 ▶ 'stack-trace-on-error|stack' => sub { Log::Any::Simple::die_with_stack_trace('long') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 0 ▶ 'parallelize|j=i' => sub { $config->set('builder.parallelize' => $_[1], allow_override => 1) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ 'target-port|port=s' => sub { $config->append('builder.upload.port' => $_[1], ',')},
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ 'force-port=s' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 my ($protocol, $address) = split(/:/, $_[1]);
76 0 $config->set('builder.forced_port.protocol' => $protocol);
77 0 $config->set('builder.forced_port.address' => $address);
78 0 $config->set('builder.forced_port.forced' => 1);
79 },
80 0 ▶ ) or pod2usage(-exitval => 2, -verbose =>0);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help|h", sub { pod2usage("-exitval", 0, "-verbose", 2); } , "project-dir|project|p=s", sub { $config->set("builder.project_dir", $_[1], "allow_override", 1); } , "build-dir|build|b=s", sub { $config->set("builder.internal.build_dir", $_[1], "allow_override", 1); } , "log-level|l=s", sub { set_log_level($_[1]); } , "config|c=s%", sub { $config->set($_[1], $_[2], "allow_override", 1); } , "menu=s%", sub { $config->set('builder.menu.' . $_[1], $_[2], "allow_override", 1); } , "skip=s\@", sub { push @skip, split(/,/u, $_[1], 0); } , "force=s\@", sub { push @force, split(/,/u, $_[1], 0); } , "only=s\@", sub { push @only, split(/,/u, $_[1], 0); } , "stack-trace-on-error|stack", sub { Log::Any::Simple::die_with_stack_trace("long"); } , "parallelize|j=i", sub { $config->set("builder.parallelize", $_[1], "allow_override", 1); } , "target-port|port=s", sub { $config->append("builder.upload.port", $_[1], ","); } , "force-port=s", sub { my($protocol, $address) = split(/:/u, $_[1], 3); $config->set("builder.forced_port.protocol", $protocol); $config->set("builder.forced_port.address", $address); $config->set("builder.forced_port.forced", 1); } ) or pod2usage("-exitval", 2, "-verbose", 0) 0 0 0
81  
82 0 ▶ if (my @unknown = grep { !/^(clean|build|discover|upload|monitor)$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@unknown) = grep({not /^(clean|build|discover|upload|monitor)$/u;} @ARGV)) 0 0 0
83 0 ▶ fatal "Unknown command%s: %s", (@unknown > 1 ? 's' : ''), join(', ', @unknown);
Branch: 1 branch
BranchTrueFalseTotal
@unknown > 1 ? : 0 0 0
84 }
85  
86 0 generate_project_config($config);
87  
88 0 ▶ push @ARGV, 'build' unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
89  
90 0 ▶ trace "Executing the following command: %s", sub { join(', ', @ARGV) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91  
92 0 ▶ if (grep { /^clean$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^clean$/u;} @ARGV) 0 0 0
93 0 clean($config);
94 }
95 0 ▶ if (grep { /^build$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^build$/u;} @ARGV) 0 0 0
96 0 build($config, \@skip, \@force, \@only);
97 }
98 0 ▶ if (grep { /^discover$/ } @ARGV and not grep { /^(upload|monitor)$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^discover$/u;} @ARGV and not grep({/^(upload|monitor)$/u;} @ARGV)) 0 0 0
Condition0%0/3
grep {/^discover$/u;} @ARGV and not grep({/^(upload|monitor)$/u;} @ARGV)
!ll&&!rl&&r
000
MC/DC0%0/2
grep {/^discover$/u;} @ARGV and not grep({/^(upload|monitor)$/u;} @ARGV)
grep {/^discover$/u;} @ARGV not grep({/^(upload|monitor)$/u;} @ARGV)
Truth table0%0/3
A and not(B)
A= grep {/^discover$/u;} @ARGVB= not grep({/^(upload|monitor)$/u;} @ARGV)
ABresult
0X0
100
111
99 0 discover($config);
100 }
101 0 ▶ if (grep { /^upload$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^upload$/u;} @ARGV) 0 0 0
102 0 discover($config);
103 0 upload($config);
104 }
105 0 ▶ if (grep { /^monitor$/ } @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^monitor$/u;} @ARGV) 0 0 0
106 # The upload process potentially modifies the board port, so we run the
107 # discovery here even if it was run on upload.
108 0 discover($config);
109 0 monitor($config);
110 }
111 }
112  
113 0 ▶sub generate_project_config {
Subroutine generate_project_config: not called CC=27 SCAR=66.3
Pod undocumented
114 0 my ($config) = @_;
115  
116 0 my $project_dir_is_cwd = 0;
117 0 my $project_dir;
118 0 ▶ if (!$config->exists('builder.project_dir')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config->exists('builder.project_dir')) { } 0 0 0
119 0 $project_dir_is_cwd = 1;
120 0 $project_dir = system_cwd();
121 0 $config->set('builder.project_dir' => $project_dir);
122 0 debug 'Using the current directory as the project dir: %s', $project_dir;
123 } else {
124 0 $project_dir = $config->get('builder.project_dir');
125 0 debug 'Using the specified project dir: %s', $project_dir;
126 }
127  
128 0 $config->read_file(catfile($project_dir, 'arduino_builder.local'), allow_missing => 1);
129 0 $config->read_file(catfile($project_dir, 'arduino_builder.config'), allow_missing => 1);
130  
131 0 my $build_dir;
132 0 ▶ if (!$config->exists('builder.internal.build_dir')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config->exists('builder.internal.build_dir')) { } 0 0 0
133 0 ▶ if ($config->exists('builder.default_build_dir')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($config->exists('builder.default_build_dir')) { } 0 0 0
elsif (not $project_dir_is_cwd) { } 0 0 0
134 0 $build_dir = $config->get('builder.default_build_dir');
135 0 $config->set('builder.internal.build_dir_from_default' => 1);
136 0 debug 'Using the default build dir: %s', $build_dir;
137 } elsif (!$project_dir_is_cwd) {
138 0 $build_dir = system_cwd();
139 0 debug 'Using the current directory as the build dir: %s', $build_dir;
140 } else {
141 0 fatal 'No builder.default_build_dir config and --build_dir was not passed when building from the project directory.';
142 }
143 0 $config->set('builder.internal.build_dir' => $build_dir);
144 } else {
145 0 debug 'Using the explicitly specified build dir: %s', $config->get('builder.internal.build_dir')
146 }
147 0 $config->set('build.path' => $config->get('builder.internal.build_dir'));
148  
149 0 ▶ if (!$config->exists('builder.source.path')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config->exists('builder.source.path')) { } 0 0 0
150 0 ▶ my $d = first { -d catdir($project_dir, $_) } qw(src srcs source sources);
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 ▶ if (defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) { } 0 0 0
152 0 $config->set('builder.source.path' => catdir($project_dir, $d));
153 0 $config->set('builder.source.is_recursive' => 1, ignore_existing => 1);
154 0 debug 'Using the following directory as the source dir (recursively): %s', $d;
155 } else {
156 0 $config->set('builder.source.path' => $project_dir);
157 0 $config->set('builder.source.is_recursive' => 0, ignore_existing => 1);
158 0 debug 'Using the project dir (non-recursively) as the source dir.';
159 }
160 } else {
161 0 $config->set('builder.source.is_recursive' => 1, ignore_existing => 1);
162 0 ▶ debug 'Using an explicitly specified source dir (%s): %s', ($config->get('builder.source.is_recursive') ? 'recursively' : 'non-recursively'), $config->get('builder.source.path');
Branch: 1 branch
BranchTrueFalseTotal
$config->get('builder.source.is_recursive') ? : 0 0 0
163 }
164  
165 0 my $arduino_dir;
166 0 ▶ if ($config->exists('builder.arduino.install_dir')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->exists('builder.arduino.install_dir')) { } 0 0 0
167 0 $arduino_dir = $config->get('builder.arduino.install_dir');
168 0 debug 'Using the explicitly specified arduino directory: %s', $arduino_dir;
169 } else {
170 0 $arduino_dir = find_arduino_dir();
171 0 debug 'Using the discovered arduino directory: %s', $arduino_dir;
172 }
173  
174 0 ▶ if (!$config->exists('builder.package.path')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $config->exists('builder.package.path')) { } 0 0 0
175 0 ▶ fatal 'At least one of builder.package.path or builder.package.name must be specified in the config' unless $config->exists('builder.package.name');
Branch: 1 branch
BranchTrueFalseTotal
unless $config->exists("builder.package.name") 0 0 0
176 # TODO: the core package can also be installed in a "hardware" directory in
177 # the sketch directory. We should search for it there.
178 0 ▶ fatal "The builder.package.path config is not set and Arduino installation directory not found" unless $arduino_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $arduino_dir 0 0 0
179 0 my $package_name = $config->get('builder.package.name');
180 0 my @tests = (catdir($arduino_dir, 'packages', $package_name), catdir($arduino_dir, 'hardware', $package_name));
181 0 my @dirs = grep { -d } @tests;
182 0 ▶ fatal "Cannot find the package directory for '${package_name}' inside Arduino directory: ${arduino_dir}" unless @dirs;
Branch: 1 branch
BranchTrueFalseTotal
unless @dirs 0 0 0
183 0 debug 'Using package directory found from its name (%s): %s', $package_name, $dirs[0];
184 0 $config->set('builder.package.path' => $dirs[0]);
185 } else {
186 0 ▶ if ($config->exists('builder.package.name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->exists('builder.package.name')) { } 0 0 0
187 0 warning 'Both builder.package.path and builder.package.name were specified in the config. Only the former will be used.';
188 } else {
189 0 $config->set('builder.package.name' => basename($config->get('builder.package.path')));
190 }
191 0 debug 'Using the explicitly specified package directory: %s', $config->get('builder.package.path');
192 }
193  
194 0 my $package_path = $config->get('builder.package.path');
195 0 ▶ fatal "Package path does not exist: ${package_path}" unless -d $package_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $package_path 0 0 0
196 0 my $hardware_dir = catdir($package_path, 'hardware');
197 0 ▶ $hardware_dir = $package_path unless -d $hardware_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $hardware_dir 0 0 0
198 0 ▶ if (!$config->exists('builder.package.arch')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->exists("builder.package.arch")) 0 0 0
199 0 my @arch_dirs = list_sub_directories($hardware_dir);
200 0 ▶ if (@arch_dirs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arch_dirs == 1) { } 0 0 0
201 0 debug "Using arch '${arch_dirs[0]}'";
202 0 $config->set('builder.package.arch' => $arch_dirs[0]);
203 } else {
204 0 fatal 'The builder.package.arch config is not set and more than one arch is present in the package: '.$hardware_dir;
205 }
206 }
207 0 ▶ debug "Project config:\n%s", sub { $config->dump(' ') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
208  
209 0 my $hardware_path = find_latest_revision_dir(catdir($hardware_dir, $config->get('builder.package.arch')));
210  
211 0 my $all_boards_config = App::ArduinoBuilder::Config->new(
212 files => [catfile($hardware_path, 'boards.local.txt'),
213 catfile($hardware_path, 'boards.txt')],
214 allow_missing => 1);
215 0 ▶ warning "Could not find any board config file" unless $all_boards_config->nb_files();
Branch: 1 branch
BranchTrueFalseTotal
unless $all_boards_config->nb_files 0 0 0
216 0 my $board_name = $config->get('builder.package.board');
217 0 my $board_config = $all_boards_config->filter($board_name);
218 # In $all_board_config we may have interesting values (or maybe even important
219 # ones – altough not with the core I checked). In particular the name of each
220 # menu. However, merging it here result in a dump that is far too large when
221 # we are in full debug mode.
222 #$board_config->merge($all_boards_config);
223  
224 # We should check whether a menu can be configured in the platform.txt, if so
225 # this block should be moved to below (and platform.txt should be merged into
226 # what is currently called board_config).
227 0 my $menu_config = $config->filter('builder.menu');
228 0 my $board_menu = $board_config->filter('menu');
229 0 for my $m ($menu_config->keys()) {
230 0 my $v = $menu_config->get($m);
231 # This is one of the rare case where we want a merge to override previously
232 # existing config. Although we still don’t want to override config set
233 # directly by the user (e.g. on the command line), which is way all this is
234 # done in a temporary config.
235 0 my $menu_value = $board_menu->filter("${m}.${v}");
236 0 ▶ trace "Merging menu values for menu '${m}' with key '${v}':\n%s", sub { $menu_value->dump(' ') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 $board_config->merge($menu_value, allow_override => 1);
238 }
239 0 $config->merge($board_config);
240 # TODO: warn about unset menus
241  
242 # TODO: Handles core, variant and tools references:
243 # https://arduino.github.io/arduino-cli/0.32/platform-specification/#core-reference
244  
245 0 my @package_config_files = grep { -f } map { catfile($hardware_path, $_) } qw(platform.local.txt platform.txt programmers.local.txt programmers.txt);
246 0 map { $config->read_file($_) } @package_config_files;
247  
248 # https://arduino.github.io/arduino-cli/0.32/platform-specification/#global-predefined-properties
249 0 $config->set('runtime.platform.path' => $hardware_path);
250 # Unclear what the runtime.hardware.path variable is supposed to point to.
251 0 $config->set('runtime.os' => get_os_name());
252 0 $config->set('runtime.ide.version' => '10607');
253 0 $config->set('ide_version' => '10607');
254 0 $config->set('software' => 'ARDUINO');
255 # todo: name, _id, build.fqbn, and the time options
256 0 $config->set('build.source.path' => $config->get('builder.source.path'));
257 0 $config->set('sketch_path' => $config->get('builder.source.path'));
258 0 $config->set('build.project_name' => $config->get('builder.project_name'));
259 0 $config->set('build.arch' => uc($config->get('builder.package.arch'))); # Undocumented but it seems that it’s always upper case.
260 0 $config->set('build.core.path', catdir($hardware_path, 'cores', $config->get('build.core')));
261 0 $config->set('build.system.path', catdir($hardware_path, 'system'));
262 0 $config->set('build.variant.path', catdir($hardware_path, 'variants', $config->get('build.variant')));
263  
264 0 my @tools_dirs = (catdir($package_path, 'tools'));
265 0 ▶ if ($arduino_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arduino_dir) { } 0 0 0
266 0 push @tools_dirs, catdir($arduino_dir, 'packages', 'builtin', 'tools');
267 0 push @tools_dirs, catdir($arduino_dir, 'packages', 'arduino', 'tools');
268 } else {
269 0 warning 'The Arduino GUI directory could not be found, we won’t use the builtin tools.';
270 }
271  
272 0 my @all_tools;
273 0 for my $tools_dir (@tools_dirs) {
274 0 ▶ next unless -d $tools_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $tools_dir 0 0 0
275 0 my @tools = list_sub_directories($tools_dir);
276 0 for my $t (@tools) {
277 0 my $tool_path = catdir($tools_dir, $t);
278 0 my $latest_tool_path = find_latest_revision_dir($tool_path);
279 # That one could point to the latest version found across all packages
280 # instead of the first version (possibly that of "our" package).
281 0 $config->set("runtime.tools.${t}.path", $latest_tool_path, ignore_existing => 1);
282 0 for my $v (list_sub_directories($tools_dir)) {
283 0 $config->set("runtime.tools.${t}-${v}.path", catdir($tool_path, $v), ignore_existing => 1);
284 }
285 }
286 0 push @all_tools, splice @tools;
287 }
288 0 debug "Found tools: %s", [sort @all_tools];
289  
290 0 my $config_append = $config->filter('builder.config.append');
291 0 for my $k ($config_append->keys()) {
292 0 $config->append($k, $config_append->get($k));
293 }
294  
295 # TODO: we should create config for all the tools defined by all the other
296 # platform (not overriding the existing definitions). There are some other
297 # considerations that we are not handling yet from:
298 # https://arduino.github.io/arduino-cli/0.32/package_index_json-specification/#how-a-tools-path-is-determined-in-platformtxt
299  
300 0 ▶ trace "Complete configuration: \n%s", sub { $config->dump(' ') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
301  
302 0 ▶ if ($config->exists('builder.parallelize')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->exists("builder.parallelize")) 0 0 0
303 0 default_executor()->set_max_parallel_tasks($config->get('builder.parallelize'));
304 }
305  
306 0 return 1;
307 }
308  
309 0 ▶sub clean {
Subroutine clean: not called CC=2 SCAR=17.9
Pod undocumented
310 0 my ($config) = @_;
311  
312 0 info 'Cleaning the build directory...';
313  
314 # TODO: add a way to clean only parts of the projects.
315 0 my $build_dir = $config->get('builder.internal.build_dir');
316 0 ▶ if ($config->get('builder.internal.build_dir_from_default', default => 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get('builder.internal.build_dir_from_default', 'default', 0)) { } 0 0 0
317 0 remove_tree($build_dir, {safe => 1, keep_root => 1});
318 } else {
319 0 warning "For safety reason we can only clean build directory specified in the project";
320 0 warning "config. You should run `rm -rf` manually.";
321 0 fatal "Not cleaning context dependent directory: ${build_dir}";
322 }
323 }
324  
325 0 ▶sub build {
Subroutine build: not called CC=26 SCAR=65.5
Pod undocumented
326 0 my ($config, @array_args) = @_;
327 0 my @skip = @{$array_args[0]};
328 0 my @force = @{$array_args[1]};
329 0 my @only = @{$array_args[2]};
330  
331 0 ▶ my $run_step = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
332 0 my ($step) = @_;
333 0 ▶ return (none { $_ eq $step } @skip) && (!@only || any { $_ eq $step } @only);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/6
&none(sub { $_ eq $step; } , @skip) && (!@only || &any(sub { $_ eq $step; } , @only))
!ll&&!rl&&r
000
not @only or &any(sub { $_ eq $step; } , @only)
l!l&&r!l&&!r
000
MC/DC0%0/3
&none(sub { $_ eq $step; } , @skip) && (!@only || &any(sub { $_ eq $step; } , @only))
&none(sub { $_ eq $step; } , @skip) not @only &any(sub { $_ eq $step; } , @only)
Truth table0%0/4
A && not(B) or C
A= &none(sub { $_ eq $step; } , @skip)B= not @onlyC= &any(sub { $_ eq $step; } , @only)
ABCresult
0XX0
1000
11X1
1011
334 0 };
335 0 ▶ my $force = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
336 0 my ($step) = @_;
337 0 ▶ return any { $_ eq $step } @force;
Subroutine __ANON__: not called CC=1 SCAR=6.9
338 0 };
339  
340  
341 0 my $builder = App::ArduinoBuilder::Builder->new($config);
342 0 my $built_something = 0;
343  
344 0 $builder->run_hook('prebuild');
345  
346 0 $config->append('includes', '"-I'.$config->get('build.core.path').'"');
347 0 $config->append('includes', '"-I'.$config->get('build.variant.path').'"');
348  
349 # This should be set to 1 first, if we start with doing library discovery at
350 # some point.
351 0 $config->set('build.library_discovery_phase' => 0);
352  
353 0 ▶ if ($run_step->('core')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$run_step("core")) 0 0 0
354 0 info 'Building core...';
355 0 $builder->run_hook('core.prebuild');
356 0 my $built_core = $builder->build_archive([$config->get('build.core.path'), $config->get('build.variant.path')], catdir($config->get('build.path'), 'core'), 'core.a', $force->('core'));
357 0 ▶ info ($built_core ? ' Success' : ' Already up-to-date');
Branch: 1 branch
BranchTrueFalseTotal
$built_core ? : 0 0 0
358 0 ▶ $built_something ||= $built_core;
Condition0%0/3
$built_something ||= $built_core
l!l&&r!l&&!r
000
MC/DC0%0/2
$built_something ||= $built_core
$built_something $built_core
Truth table0%0/3
A ||= B
A= $built_somethingB= $built_core
ABresult
1X1
011
000
359 0 $builder->run_hook('core.postbuild');
360 }
361  
362 # Reference for all this library part:
363 # https://arduino.github.io/arduino-cli/0.32/library-specification/
364 # For now, we are not doing library discovery automatically. See also this:
365 # https://arduino.github.io/arduino-cli/0.32/sketch-build-process/#dependency-resolution
366 0 my $lib_config = $config->filter('builder.library');
367 0 my @all_libs = $lib_config->keys(); # We will add config in lib_config, so let’s get the set of keys now.
368 # We are first adding all the includes path for all the library before building any of them.
369 0 for my $l ($lib_config->keys()) {
370 0 my $lib_dir = $lib_config->get($l);
371 0 ▶ fatal "Library directory does not exist for library '${l}': ${lib_dir}" unless -d $lib_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $lib_dir 0 0 0
372 0 my $lib_properties_file = catfile($lib_dir, 'library.properties');
373 0 my $has_lib_properties = -f $lib_properties_file;
374 0 ▶ my $recursive_lib = $has_lib_properties && -d catdir($lib_dir, 'src');
Condition0%0/3
$has_lib_properties && -d catdir($lib_dir, 'src')
!ll&&!rl&&r
000
MC/DC0%0/2
$has_lib_properties && -d catdir($lib_dir, 'src')
$has_lib_properties -d catdir($lib_dir, 'src')
Truth table0%0/3
A && B
A= $has_lib_propertiesB= -d catdir($lib_dir, 'src')
ABresult
0X0
100
111
375 # These config entry (and any other added to $lib_config) are undocummented and should never be
376 # set by the user.
377 0 $lib_config->set($l.'.is_flat' => !$recursive_lib);
378 0 ▶ if ($recursive_lib) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursive_lib) { } 0 0 0
379 0 $config->append('includes', '"-I'.catdir($lib_dir, 'src').'"');
380 } else {
381 0 $config->append('includes', "\"-I${lib_dir}\"");
382 }
383 0 ▶ if ($has_lib_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_lib_properties) 0 0 0
384 0 my $lib_properties = App::ArduinoBuilder::Config->new(file => $lib_properties_file);
385 0 $lib_config->set($l.'.name' => $lib_properties->get('name', default => $l));
386 }
387 }
388 # TODO: we are ignoring the dot_a_linkage properties of the libraries (unclear what it brings)
389 # and also the precompiled
390 0 ▶ if ($run_step->('libraries')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$run_step("libraries")) 0 0 0
391 0 info 'Building libraries...';
392 0 $builder->run_hook('libraries.prebuild');
393 0 for my $l (@all_libs) {
394 # Todo: we could add "lib-$l" pseudo-steps, but we need to take care of the --only
395 # interaction with the 'libraries' step.
396 0 info ' Building library %s...', $lib_config->get("${l}.name");
397 0 my $base_dir = $lib_config->get($l);
398 0 my $output_dir = catdir($config->get('build.path'), 'libs', $l);
399 0 my $built_lib;
400 0 ▶ if ($lib_config->get("${l}.is_flat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib_config->get("$l.is_flat")) { } 0 0 0
401 0 my $utility_dir = catdir($base_dir, 'utility');
402 0 ▶ my @dirs = ($base_dir, (-d $utility_dir ? $utility_dir : ()));
Branch: 1 branch
BranchTrueFalseTotal
-d $utility_dir ? : 0 0 0
403 0 $built_lib = $builder->build_object_files(\@dirs, $output_dir, [], $force->('libraries'), 1);
404 } else {
405 0 $built_lib = $builder->build_object_files(catdir($base_dir, 'src'), $output_dir, [], $force->('libraries'));
406 }
407 0 ▶ info ($built_lib ? ' Success' : ' Already up-to-date');
Branch: 1 branch
BranchTrueFalseTotal
$built_lib ? : 0 0 0
408 0 $built_something |= $built_lib;
409 }
410 0 $builder->run_hook('libraries.postbuild');
411 }
412  
413 0 $config->append('includes', '"-I'.$config->get('builder.source.path').'"');
414  
415 0 ▶ if ($run_step->('sketch')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$run_step("sketch")) 0 0 0
416 0 info 'Building sketch...';
417 0 $builder->run_hook('sketch.prebuild');
418 # TODO: add configuration option for the ignored directories and also a way to
419 # build only the code inside the src/ directory
420 0 my $built_sketch = $builder->build_object_files(
421 $config->get('builder.source.path'), catdir($config->get('build.path'), 'sketch'),
422 [], $force->('sketch'), !$config->get('builder.source.is_recursive'));
423 0 ▶ info ($built_sketch ? ' Success' : ' Already up-to-date');
Branch: 1 branch
BranchTrueFalseTotal
$built_sketch ? : 0 0 0
424 0 $built_something |= $built_sketch;
425 0 $builder->run_hook('sketch.postbuild');
426 }
427 # Bug: there is a similar bug to the one in build_archive: if a source file is
428 # removed, we won’t remove it’s object file. I guess we could try to detect it.
429 # Meanwhile it’s probably acceptable to ask for a cleanup from time to time.
430 0 my @object_files = find_all_files_with_extensions(catdir($config->get('build.path'), 'sketch'), ['o']);
431 0 for my $l (@all_libs) {
432 0 push @object_files, find_all_files_with_extensions(catdir($config->get('build.path'), 'libs', $l), ['o']);
433 }
434 0 debug 'Object files: '.join(', ', @object_files);
435  
436 0 info 'Linking binary...';
437 0 ▶ if (($built_something && $run_step->('link')) || $force->('link')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($built_something and &$run_step('link') or &$force('link')) { } 0 0 0
Condition0%0/6
$built_something and &$run_step("link") or &$force("link")
l!l&&r!l&&!r
000
$built_something and &$run_step("link")
!ll&&!rl&&r
000
MC/DC0%0/3
$built_something and &$run_step("link") or &$force("link")
$built_something &$run_step("link") &$force("link")
Truth table0%0/5
A and B or C
A= $built_somethingB= &$run_step("link")C= &$force("link")
ABCresult
11X1
0X11
1011
0X00
1000
438 0 $built_something = 1;
439 0 $builder->run_hook('linking.prelink');
440 0 $builder->link_executable(\@object_files, 'core.a');
441 0 $builder->run_hook('linking.postlink');
442 0 info ' Success';
443 } else {
444 0 info ' Already up-to-date';
445 }
446  
447 0 info 'Extracting binary data';
448 0 ▶ if (($built_something && $run_step->('objcopy')) || $force->('objcopy')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($built_something and &$run_step('objcopy') or &$force('objcopy')) { } 0 0 0
Condition0%0/6
$built_something and &$run_step("objcopy") or &$force("objcopy")
l!l&&r!l&&!r
000
$built_something and &$run_step("objcopy")
!ll&&!rl&&r
000
MC/DC0%0/3
$built_something and &$run_step("objcopy") or &$force("objcopy")
$built_something &$run_step("objcopy") &$force("objcopy")
Truth table0%0/5
A and B or C
A= $built_somethingB= &$run_step("objcopy")C= &$force("objcopy")
ABCresult
11X1
0X11
1011
0X00
1000
449 0 $builder->run_hook('objcopy.preobjcopy');
450 0 $builder->objcopy();
451 0 $builder->run_hook('objcopy.postobjcopy');
452 0 info ' Success';
453 } else {
454 0 info ' Already up-to-date';
455 }
456  
457 0 info 'Computing binary sketch size';
458 0 ▶ if (($built_something && $run_step->('size')) || $force->('size')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($built_something and &$run_step('size') or &$force('size')) { } 0 0 0
Condition0%0/6
$built_something and &$run_step("size") or &$force("size")
l!l&&r!l&&!r
000
$built_something and &$run_step("size")
!ll&&!rl&&r
000
MC/DC0%0/3
$built_something and &$run_step("size") or &$force("size")
$built_something &$run_step("size") &$force("size")
Truth table0%0/5
A and B or C
A= $built_somethingB= &$run_step("size")C= &$force("size")
ABCresult
11X1
0X11
1011
0X00
1000
459 0 $builder->compute_binary_size();
460 # Not printing 'Success' here because the command already has an output.
461 } else {
462 0 info ' No new binary built';
463 }
464  
465 0 info 'Success!';
466 }
467  
468 0 ▶sub discover {
Subroutine discover: not called CC=4 SCAR=30.0
Pod undocumented
469 0 my ($config) = @_;
470  
471 0 ▶ if ($config->get('builder.forced_port.forced', default => 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("builder.forced_port.forced", "default", 0)) 0 0 0
472 0 info 'Skipping board discovery';
473 0 return;
474 }
475  
476 0 info 'Running board discovery...';
477 0 my @ports = App::ArduinoBuilder::Discovery::discover($config);
478 # Discovery can be run more than once, as the port of a board can be changed
479 # after upload. So we override any previous discovered ports.
480 0 $config->set('builder.internal.ports' => \@ports, allow_override =>1);
481 0 ▶ if (@ports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ports) { } 0 0 0
482 0 ▶ debug 'Found port%s: %s', (@ports > 1 ? 's' : ''), join(', ', map { $_->get('upload.port.label') } @ports);
Branch: 1 branch
BranchTrueFalseTotal
@ports > 1 ? : 0 0 0
483 } else {
484 0 warning 'No port found.';
485 }
486 }
487  
488 0 ▶sub select_port {
Subroutine select_port: not called CC=6 SCAR=37.4
Pod undocumented
489 0 my ($config) = @_;
490  
491 0 ▶ if ($config->get('builder.forced_port.forced', default => 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("builder.forced_port.forced", "default", 0)) 0 0 0
492 # A forced port does not match a found port (as there are none), so we can’t
493 # just set selected_port here.
494 0 return $config->filter('builder.forced_port')->prefix('upload.port');
495 }
496  
497 {
498 0 my $port = $config->get('builder.internal.selected_port', default => undef);
499 0 ▶ if (defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $port) 0 0 0
500 0 debug 'Using previously selected port: %s', $port->get('upload.port.label');
501 0 return $port;
502 }
503 }
504  
505 0 my @ports = @{$config->get('builder.internal.ports')};
506 # TODO: implement an exact match selection and an interactive selection.
507 0 ▶ fatal "You must pass the --target-port option to select the upload target" unless $config->exists('builder.upload.port');
Branch: 1 branch
BranchTrueFalseTotal
unless $config->exists("builder.upload.port") 0 0 0
508 0 my @targets = map { qr/^$_$/i } split(/\s*,\s*/, $config->get('builder.upload.port'));
509 0 ▶ @ports = grep { my $port = $_; any { $port->get('upload.port.lc_label') =~ m/$_/ || $port->get('upload.port.lc_address') =~ m/$_/ } @targets } @ports;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$port->get("upload.port.lc_label") =~ /$_/u or $port->get("upload.port.lc_address") =~ /$_/u 0 0 0
510 0 ▶ unless (@ports) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ports) 0 0 0
511 0 fatal "None of the specified ports (%s) can be found, can your target be found by the 'discover' command?", join(', ', @targets);
512 }
513 0 ▶ warn "More than one found port match with builder.upload.port. Picking the firt one." if @ports > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ports > 1 0 0 0
514 0 my $port = $ports[0];
515 0 info 'Using the first match port from the configuration: %s', $port->get('upload.port.address');
516  
517 0 $config->set('builder.internal.selected_port' => $port);
518 0 return $port;
519 }
520  
521 0 ▶sub upload {
Subroutine upload: not called CC=1 SCAR=6.9
Pod undocumented
522 0 my ($config) = @_;
523  
524 0 info 'Uploading binary to the board...';
525  
526 0 my $port = select_port($config);
527 0 my $protocol = $port->get('upload.port.protocol');
528 0 my $tool = $config->get("upload.tool.${protocol}", default => $config->get('upload.tool.default', default => $config->get('upload.tool')));
529 0 my $tool_config = $config->filter("tools.${tool}");
530  
531 # TODO: add a way to set the verbose mode, in which case the upload.params.verbose
532 # property should be copied, instead of upload.params.quiet.
533 # Reference: https://arduino.github.io/arduino-cli/0.32/platform-specification/#verbose-parameter
534 0 $tool_config->set('upload.verbose' => $tool_config->get('upload.params.quiet'), allow_override => 1);
535  
536 0 my $upload_config = $config->filter("upload.${protocol}")->prefix('upload');
537 0 $upload_config->merge($tool_config);
538 0 $upload_config->merge($port);
539 # Note: $config is in the recursive base in $upload_config.
540  
541 # TODO: Before executing the command, some boards require that we manually
542 # reset them through their Serial port.
543 # See the code: https://github.com/arduino/arduino-cli/blob/ad9ddb882016c2af10e0db3785a46122bc9cfb1f/commands/upload/upload.go#L370
544 # And the doc: https://arduino.github.io/arduino-cli/0.32/platform-specification/#1200-bps-bootloader-reset
545  
546 0 my $cmd = $upload_config->get('upload.pattern');
547 0 ▶ debug "Upload configuration:\n%s", sub { $upload_config->dump(' ') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
548 0 ▶ default_executor()->run_now(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
549 0 close STDIN;
550 0 execute_cmd($cmd);
551 0 });
552  
553 0 info 'Success!';
554  
555 }
556  
557 0 ▶sub monitor {
Subroutine monitor: not called CC=1 SCAR=6.9
Pod undocumented
558 0 my ($config) = @_;
559  
560 0 info 'Running board monitor. Press ctrl+c to exit...';
561  
562 0 my $port = select_port($config);
563 0 App::ArduinoBuilder::Monitor::monitor($config, $port);
564  
565 0 info 'Success!';
566 }
567  
568 1 1;
569  
570 __DATA__
↑ Summary ArduinoBuilder/DepCheck.pm »