Skip to content

Commit 12e9ad0

Browse files
committed
Add Arduino CLI compile flags input
This input allows the user to customize the sketch compilation process by defining arbitrary flags to be added to the `arduino-cli compile` command. In order to be as flexible as possible, the input uses a YAML list format: cli-compile-flags: | - --build-property - compiler.cpp.extra_flags="-DFOO="hello world"" -DPIN=2 - --export-binaries
1 parent dfba7a8 commit 12e9ad0

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This action checks whether [Arduino](https://www.arduino.cc/) sketches compile a
2727
- [Repository](#repository-1)
2828
- [Archive download](#archive-download-1)
2929
- [`sketch-paths`](#sketch-paths)
30+
- [`cli-compile-flags`](#cli-compile-flags)
3031
- [`verbose`](#verbose)
3132
- [`sketches-report-path`](#sketches-report-path)
3233
- [`github-token`](#github-token)
@@ -162,6 +163,12 @@ Keys:
162163

163164
**Default**: `"- examples"`
164165

166+
### `cli-compile-flags`
167+
168+
YAML-format list of flags to add to the Arduino CLI command used to compile the sketches. For the available flags, see [the Arduino CLI command reference](https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_compile/#options).
169+
170+
**Default**: `""`
171+
165172
### `verbose`
166173

167174
Set to true to show verbose output in the log.

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ inputs:
2121
description: 'YAML-format list of paths containing sketches to compile.'
2222
default: '- examples'
2323
required: true
24+
cli-compile-flags:
25+
description: 'YAML-format list of flags to add to the Arduino CLI sketch compilation command.'
26+
default: ''
27+
required: false
2428
verbose:
2529
description: 'Set to true to show verbose output in the log'
2630
default: 'false'
@@ -62,6 +66,7 @@ runs:
6266
INPUT_LIBRARIES: ${{ inputs.libraries }}
6367
INPUT_PLATFORMS: ${{ inputs.platforms }}
6468
INPUT_SKETCH-PATHS: ${{ inputs.sketch-paths }}
69+
INPUT_CLI-COMPILE-FLAGS: ${{ inputs.cli-compile-flags }}
6570
INPUT_VERBOSE: ${{ inputs.verbose }}
6671
INPUT_GITHUB-TOKEN: ${{ inputs.github-token }}
6772
INPUT_ENABLE-DELTAS-REPORT: ${{ inputs.enable-deltas-report }}

compilesketches/compilesketches.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def main():
4343
platforms=os.environ["INPUT_PLATFORMS"],
4444
libraries=os.environ["INPUT_LIBRARIES"],
4545
sketch_paths=os.environ["INPUT_SKETCH-PATHS"],
46+
cli_compile_flags=os.environ["INPUT_CLI-COMPILE-FLAGS"],
4647
verbose=os.environ["INPUT_VERBOSE"],
4748
github_token=os.environ["INPUT_GITHUB-TOKEN"],
4849
enable_deltas_report=os.environ["INPUT_ENABLE-DELTAS-REPORT"],
@@ -64,6 +65,7 @@ class CompileSketches:
6465
libraries -- YAML-format or space-separated list of libraries to install
6566
sketch_paths -- space-separated list of paths containing sketches to compile. These paths will be searched
6667
recursively for sketches.
68+
cli_compile_flags -- Arbitrary Arduino CLI flags to add to the compile command.
6769
verbose -- set to "true" for verbose output ("true", "false")
6870
github_token -- GitHub access token
6971
enable_deltas_report -- set to "true" to cause the action to determine the change in memory usage
@@ -115,8 +117,8 @@ class ReportKeys:
115117

116118
latest_release_indicator = "latest"
117119

118-
def __init__(self, cli_version, fqbn_arg, platforms, libraries, sketch_paths, verbose, github_token,
119-
enable_deltas_report, enable_warnings_report, sketches_report_path):
120+
def __init__(self, cli_version, fqbn_arg, platforms, libraries, sketch_paths, cli_compile_flags, verbose,
121+
github_token, enable_deltas_report, enable_warnings_report, sketches_report_path):
120122
"""Process, store, and validate the action's inputs."""
121123
self.cli_version = cli_version
122124

@@ -131,6 +133,7 @@ def __init__(self, cli_version, fqbn_arg, platforms, libraries, sketch_paths, ve
131133
absolute_sketch_paths = [absolute_path(path=sketch_path) for sketch_path in sketch_paths.value]
132134
self.sketch_paths = absolute_sketch_paths
133135

136+
self.cli_compile_flags = yaml.load(stream=cli_compile_flags, Loader=yaml.SafeLoader)
134137
self.verbose = parse_boolean_input(boolean_input=verbose)
135138

136139
if github_token == "":
@@ -871,7 +874,10 @@ def compile_sketch(self, sketch_path, clean_build_cache):
871874
sketch_path -- path of the sketch to compile
872875
clean_build_cache -- whether to delete cached compiled from previous compilations before compiling
873876
"""
874-
compilation_command = ["compile", "--warnings", "all", "--fqbn", self.fqbn, sketch_path]
877+
compilation_command = ["compile", "--warnings", "all", "--fqbn", self.fqbn]
878+
if self.cli_compile_flags is not None:
879+
compilation_command.extend(self.cli_compile_flags)
880+
compilation_command.append(sketch_path)
875881

876882
if clean_build_cache:
877883
for cache_path in pathlib.Path("/tmp").glob(pattern="arduino*"):

compilesketches/tests/test_compilesketches.py

+14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def get_compilesketches_object(
2525
platforms="- name: FooVendor:BarArchitecture",
2626
libraries="foo libraries",
2727
sketch_paths="foo sketch_paths",
28+
cli_compile_flags="--foo",
2829
verbose="false",
2930
github_token="",
3031
github_api=unittest.mock.sentinel.github_api,
@@ -41,6 +42,7 @@ def get_compilesketches_object(
4142
platforms=platforms,
4243
libraries=libraries,
4344
sketch_paths=sketch_paths,
45+
cli_compile_flags=cli_compile_flags,
4446
verbose=verbose,
4547
github_token=github_token,
4648
enable_deltas_report=enable_deltas_report,
@@ -89,6 +91,7 @@ class ActionInputs:
8991
platforms = "- name: FooVendor:BarArchitecture"
9092
libraries = "foo libraries"
9193
sketch_paths = "foo/Sketch bar/OtherSketch"
94+
cli_compile_flags = "--foo"
9295
verbose = "true"
9396
github_token = "FooGitHubToken"
9497
enable_size_deltas_report = "FooEnableSizeDeltasReport"
@@ -102,6 +105,7 @@ class ActionInputs:
102105
monkeypatch.setenv("INPUT_PLATFORMS", ActionInputs.platforms)
103106
monkeypatch.setenv("INPUT_LIBRARIES", ActionInputs.libraries)
104107
monkeypatch.setenv("INPUT_SKETCH-PATHS", ActionInputs.sketch_paths)
108+
monkeypatch.setenv("INPUT_CLI-COMPILE-FLAGS", ActionInputs.cli_compile_flags)
105109
monkeypatch.setenv("INPUT_VERBOSE", ActionInputs.verbose)
106110
monkeypatch.setenv("INPUT_GITHUB-TOKEN", ActionInputs.github_token)
107111
monkeypatch.setenv("INPUT_ENABLE-DELTAS-REPORT", ActionInputs.enable_deltas_report)
@@ -227,6 +231,7 @@ def compile_sketches(self):
227231
platforms=setup_action_inputs.platforms,
228232
libraries=setup_action_inputs.libraries,
229233
sketch_paths=setup_action_inputs.sketch_paths,
234+
cli_compile_flags=setup_action_inputs.cli_compile_flags,
230235
verbose=setup_action_inputs.verbose,
231236
github_token=setup_action_inputs.github_token,
232237
enable_deltas_report=setup_action_inputs.enable_deltas_report,
@@ -246,6 +251,8 @@ def test_compilesketches():
246251
sketch_paths = "examples/FooSketchPath examples/BarSketchPath"
247252
expected_sketch_paths_list = [compilesketches.absolute_path(path="examples/FooSketchPath"),
248253
compilesketches.absolute_path(path="examples/BarSketchPath")]
254+
cli_compile_flags = "- --foo\n- --bar"
255+
expected_cli_compile_flags = ["--foo", "--bar"]
249256
verbose = "false"
250257
github_token = "fooGitHubToken"
251258
expected_deltas_base_ref = unittest.mock.sentinel.deltas_base_ref
@@ -262,6 +269,7 @@ def test_compilesketches():
262269
platforms=platforms,
263270
libraries=libraries,
264271
sketch_paths=sketch_paths,
272+
cli_compile_flags=cli_compile_flags,
265273
verbose=verbose,
266274
github_token=github_token,
267275
enable_deltas_report=enable_deltas_report,
@@ -275,12 +283,18 @@ def test_compilesketches():
275283
assert compile_sketches.platforms == platforms
276284
assert compile_sketches.libraries == libraries
277285
assert compile_sketches.sketch_paths == expected_sketch_paths_list
286+
assert compile_sketches.cli_compile_flags == expected_cli_compile_flags
278287
assert compile_sketches.verbose is False
279288
assert compile_sketches.deltas_base_ref == expected_deltas_base_ref
280289
assert compile_sketches.enable_deltas_report is True
281290
assert compile_sketches.enable_warnings_report is True
282291
assert compile_sketches.sketches_report_path == pathlib.PurePath(sketches_report_path)
283292

293+
assert get_compilesketches_object(cli_compile_flags="").cli_compile_flags is None
294+
assert get_compilesketches_object(cli_compile_flags="- --foo").cli_compile_flags == ["--foo"]
295+
assert get_compilesketches_object(cli_compile_flags="- --foo\n- \"bar baz\"").cli_compile_flags == ["--foo",
296+
"bar baz"]
297+
284298
# Test invalid enable_deltas_report value
285299
with pytest.raises(expected_exception=SystemExit, match="1"):
286300
get_compilesketches_object(enable_deltas_report="fooInvalidEnableSizeDeltasBoolean")

0 commit comments

Comments
 (0)