Skip to content

Commit 220875d

Browse files
committed
Support for checking space usage in compiled sketches via command line
1 parent d8f9dcb commit 220875d

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
- Support for `dtostrf()`
1515
- Added a CI workflow to lint the code base
1616
- Added a CI workflow to check for spelling errors
17+
- Extraction of byes usage in a compiled sketch is now calculated in a method: `ArduinoBackend.last_bytes_usage`
1718

1819
### Changed
1920
- We now compile a shared library to be used for each test.

Diff for: exe/arduino_ci.rb

+11-14
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def self.parse(options)
2424
ci_config: {
2525
"unittest" => unit_config
2626
},
27-
min_free_space: 0,
27+
min_free_space: nil,
2828
}
2929

3030
opt_parser = OptionParser.new do |opts|
@@ -50,7 +50,7 @@ def self.parse(options)
5050
unit_config["testfiles"]["reject"] << p
5151
end
5252

53-
opts.on("--min-free-space=VALUE", "Minimum free SRAM memory for stack/heap") do |p|
53+
opts.on("--min-free-space=VALUE", "Minimum free SRAM memory for stack/heap, in bytes") do |p|
5454
output_options[:min_free_space] = p.to_i
5555
end
5656

@@ -502,23 +502,20 @@ def perform_example_compilation_tests(cpp_library, config)
502502
board = ovr_config.platform_info[p][:board]
503503
attempt("Compiling #{example_name} for #{board}") do
504504
ret = @backend.compile_sketch(example_path, board)
505-
puts
506-
if ret
507-
output = @backend.last_msg
508-
puts output
509-
i = output.index("leaving")
510-
free_space = output[i + 8..-1].to_i
511-
min_free_space = @cli_options[:min_free_space]
512-
if free_space < min_free_space
513-
puts "Free space of #{free_space} is less than minimum of #{min_free_space}"
514-
ret = false
515-
end
516-
else
505+
unless ret
517506
puts "Last command: #{@backend.last_msg}"
518507
puts @backend.last_err
519508
end
520509
ret
521510
end
511+
512+
next if @cli_options[:min_free_space].nil?
513+
514+
usage = @backend.last_bytes_usage
515+
min_free_space = @cli_options[:min_free_space]
516+
attempt("Checking that free space of #{usage[:free]} is less than desired minimum #{min_free_space}") do
517+
min_free_space <= usage[:free]
518+
end
522519
end
523520
end
524521
end

Diff for: lib/arduino_ci/arduino_backend.rb

+16
Original file line numberDiff line numberDiff line change
@@ -235,5 +235,21 @@ def install_local_library(path)
235235
Host.symlink(src_path, destination_path)
236236
cpp_library
237237
end
238+
239+
# extract the "Free space remaining" amount from the last run
240+
# @return [Hash] the usage, as a hash with keys :free, :max, and :globals
241+
def last_bytes_usage
242+
# Free-spacing syntax for regexes is not working today, not sure why. Make a string and convert to regex.
243+
re_str = [
244+
'Global variables use (?<globals>\d+) bytes',
245+
'\(\d+%\) of dynamic memory,',
246+
'leaving (?<free>\d+) bytes for local variables.',
247+
'Maximum is (?<max>\d+) bytes.'
248+
].join(" ")
249+
mem_info = Regexp.new(re_str).match(@last_msg)
250+
return {} if mem_info.nil?
251+
252+
Hash[mem_info.names.map(&:to_sym).zip(mem_info.captures.map(&:to_i))]
253+
end
238254
end
239255
end

Diff for: spec/arduino_backend_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,13 @@ def get_sketch(dir, file)
103103
it "Passes a simple INO sketch at #{sketch_path_ino}" do
104104
expect(backend.compile_sketch(sketch_path_ino, "arduino:avr:uno")).to be true
105105
end
106+
107+
it "Detects the bytes usage after compiling a sketch" do
108+
expect(backend.compile_sketch(sketch_path_ino, "arduino:avr:uno")).to be true
109+
the_bytes = backend.last_bytes_usage
110+
expect(the_bytes[:globals]).to eq 9
111+
expect(the_bytes[:free]).to eq 2039
112+
expect(the_bytes[:max]).to eq 2048
113+
end
106114
end
107115
end

0 commit comments

Comments
 (0)