Skip to content

Commit ef4a389

Browse files
authored
Deprecate compile --build-properties flag in favor of new featureful --build-property flag (#1044)
* Add support for definitions containing quotes in compile --build-properties flag * Deprecate --build-properties flag in favor of --build-property * Fix some escaping issues on Windows * Simplify compile examples
1 parent 8f5d38b commit ef4a389

File tree

9 files changed

+249
-33
lines changed

9 files changed

+249
-33
lines changed

Diff for: cli/compile/compile.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ var (
5757
// NewCommand created a new `compile` command
5858
func NewCommand() *cobra.Command {
5959
command := &cobra.Command{
60-
Use: "compile",
61-
Short: "Compiles Arduino sketches.",
62-
Long: "Compiles Arduino sketches.",
63-
Example: " " + os.Args[0] + " compile -b arduino:avr:uno /home/user/Arduino/MySketch",
64-
Args: cobra.MaximumNArgs(1),
65-
Run: run,
60+
Use: "compile",
61+
Short: "Compiles Arduino sketches.",
62+
Long: "Compiles Arduino sketches.",
63+
Example: "" +
64+
" " + os.Args[0] + " compile -b arduino:avr:uno /home/user/Arduino/MySketch\n" +
65+
" " + os.Args[0] + ` compile -b arduino:avr:uno --build-property "build.extra_flags=\"-DMY_DEFINE=\"hello world\"\"" /home/user/Arduino/MySketch` + "\n" +
66+
" " + os.Args[0] + ` compile -b arduino:avr:uno --build-property "build.extra_flags=-DPIN=2 \"-DMY_DEFINE=\"hello world\"\"" /home/user/Arduino/MySketch` + "\n" +
67+
" " + os.Args[0] + ` compile -b arduino:avr:uno --build-property build.extra_flags=-DPIN=2 --build-property "compiler.cpp.extra_flags=\"-DSSID=\"hello world\"\"" /home/user/Arduino/MySketch` + "\n",
68+
Args: cobra.MaximumNArgs(1),
69+
Run: run,
6670
}
6771

6872
command.Flags().StringVarP(&fqbn, "fqbn", "b", "", "Fully Qualified Board Name, e.g.: arduino:avr:uno")
@@ -74,6 +78,8 @@ func NewCommand() *cobra.Command {
7478
"Path where to save compiled files. If omitted, a directory will be created in the default temporary path of your OS.")
7579
command.Flags().StringSliceVar(&buildProperties, "build-properties", []string{},
7680
"List of custom build properties separated by commas. Or can be used multiple times for multiple properties.")
81+
command.Flags().StringArrayVar(&buildProperties, "build-property", []string{},
82+
"Override a build property with a custom value. Can be used multiple times for multiple properties.")
7783
command.Flags().StringVar(&warnings, "warnings", "none",
7884
`Optional, can be "none", "default", "more" and "all". Defaults to "none". Used to tell gcc which warning level to use (-W flag).`)
7985
command.Flags().BoolVarP(&verbose, "verbose", "v", false, "Optional, turns on verbose mode.")
@@ -95,6 +101,8 @@ func NewCommand() *cobra.Command {
95101

96102
configuration.Settings.BindPFlag("sketch.always_export_binaries", command.Flags().Lookup("export-binaries"))
97103

104+
command.Flags().MarkDeprecated("build-properties", "please use --build-property instead.")
105+
98106
return command
99107
}
100108

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/sirupsen/logrus v1.4.2
4141
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c
4242
github.com/spf13/jwalterweatherman v1.0.0
43+
github.com/spf13/pflag v1.0.3
4344
github.com/spf13/viper v1.6.2
4445
github.com/stretchr/testify v1.6.1
4546
go.bug.st/cleanup v1.0.0

Diff for: test/conftest.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,14 @@ def detected_boards(run_command):
212212

213213
@pytest.fixture(scope="function")
214214
def copy_sketch(working_dir):
215-
# Copies sketch for testing
216-
sketch_path = Path(__file__).parent / "testdata" / "sketch_simple"
217-
test_sketch_path = Path(working_dir) / "sketch_simple"
218-
shutil.copytree(sketch_path, test_sketch_path)
219-
yield str(test_sketch_path)
215+
def _copy(sketch_name):
216+
# Copies sketch to working directory for testing
217+
sketch_path = Path(__file__).parent / "testdata" / sketch_name
218+
test_sketch_path = Path(working_dir, sketch_name)
219+
shutil.copytree(sketch_path, test_sketch_path)
220+
return str(test_sketch_path)
221+
222+
return _copy
220223

221224

222225
@pytest.fixture(scope="function")

Diff for: test/test_compile.py

+127
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,133 @@ def test_compile_without_precompiled_libraries(run_command, data_dir):
233233
assert result.ok
234234

235235

236+
def test_compile_with_build_properties_flag(run_command, data_dir, copy_sketch):
237+
# Init the environment explicitly
238+
assert run_command("core update-index")
239+
240+
# Install Arduino AVR Boards
241+
assert run_command("core install arduino:[email protected]")
242+
243+
sketch_path = copy_sketch("sketch_with_single_string_define")
244+
fqbn = "arduino:avr:uno"
245+
246+
# Compile using a build property with quotes
247+
res = run_command(
248+
f"compile -b {fqbn} "
249+
+ '--build-properties="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
250+
+ f"{sketch_path} --verbose --clean"
251+
)
252+
assert res.failed
253+
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
254+
255+
# Try again with quotes
256+
res = run_command(
257+
f"compile -b {fqbn} "
258+
+ '--build-properties="build.extra_flags=-DMY_DEFINE=\\"hello\\"" '
259+
+ f"{sketch_path} --verbose --clean"
260+
)
261+
assert res.failed
262+
assert "Flag --build-properties has been deprecated, please use --build-property instead." not in res.stderr
263+
264+
# Try without quotes
265+
sketch_path = copy_sketch("sketch_with_single_int_define")
266+
res = run_command(
267+
f"compile -b {fqbn} "
268+
+ '--build-properties="build.extra_flags=-DMY_DEFINE=1" '
269+
+ f"{sketch_path} --verbose --clean"
270+
)
271+
assert res.ok
272+
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
273+
assert "-DMY_DEFINE=1" in res.stdout
274+
275+
sketch_path = copy_sketch("sketch_with_multiple_int_defines")
276+
res = run_command(
277+
f"compile -b {fqbn} "
278+
+ '--build-properties="build.extra_flags=-DFIRST_PIN=1,compiler.cpp.extra_flags=-DSECOND_PIN=2" '
279+
+ f"{sketch_path} --verbose --clean"
280+
)
281+
assert res.ok
282+
assert "Flag --build-properties has been deprecated, please use --build-property instead." in res.stderr
283+
assert "-DFIRST_PIN=1" in res.stdout
284+
assert "-DSECOND_PIN=2" in res.stdout
285+
286+
287+
def test_compile_with_build_property_containing_quotes(run_command, data_dir, copy_sketch):
288+
# Init the environment explicitly
289+
assert run_command("core update-index")
290+
291+
# Install Arduino AVR Boards
292+
assert run_command("core install arduino:[email protected]")
293+
294+
sketch_path = copy_sketch("sketch_with_single_string_define")
295+
fqbn = "arduino:avr:uno"
296+
297+
# Compile using a build property with quotes
298+
res = run_command(
299+
f"compile -b {fqbn} "
300+
+ '--build-property="build.extra_flags=\\"-DMY_DEFINE=\\"hello world\\"\\"" '
301+
+ f"{sketch_path} --verbose"
302+
)
303+
assert res.ok
304+
assert '-DMY_DEFINE=\\"hello world\\"' in res.stdout
305+
306+
307+
def test_compile_with_multiple_build_property_flags(run_command, data_dir, copy_sketch, working_dir):
308+
# Init the environment explicitly
309+
assert run_command("core update-index")
310+
311+
# Install Arduino AVR Boards
312+
assert run_command("core install arduino:[email protected]")
313+
314+
sketch_path = copy_sketch("sketch_with_multiple_defines")
315+
fqbn = "arduino:avr:uno"
316+
317+
# Compile using multiple build properties separated by a space
318+
res = run_command(
319+
f"compile -b {fqbn} "
320+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2 -DSSID=\\"This is a String\\"\\"" '
321+
+ f"{sketch_path} --verbose --clean"
322+
)
323+
assert res.failed
324+
325+
# Compile using multiple build properties separated by a space and properly quoted
326+
res = run_command(
327+
f"compile -b {fqbn} "
328+
+ '--build-property="compiler.cpp.extra_flags=-DPIN=2 \\"-DSSID=\\"This is a String\\"\\"" '
329+
+ f"{sketch_path} --verbose --clean"
330+
)
331+
assert res.ok
332+
assert '-DPIN=2 "-DSSID=\\"This is a String\\""' in res.stdout
333+
334+
# Tries compilation using multiple build properties separated by a comma
335+
res = run_command(
336+
f"compile -b {fqbn} "
337+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2,-DSSID=\\"This is a String\\"\\"\\" '
338+
+ f"{sketch_path} --verbose --clean"
339+
)
340+
assert res.failed
341+
342+
res = run_command(
343+
f"compile -b {fqbn} "
344+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
345+
+ '--build-property="compiler.cpp.extra_flags=\\"-DSSID=\\"This is a String\\"\\"" '
346+
+ f"{sketch_path} --verbose --clean"
347+
)
348+
assert res.failed
349+
assert "-DPIN=2" not in res.stdout
350+
assert '-DSSID=\\"This is a String\\"' in res.stdout
351+
352+
res = run_command(
353+
f"compile -b {fqbn} "
354+
+ '--build-property="compiler.cpp.extra_flags=\\"-DPIN=2\\"" '
355+
+ '--build-property="build.extra_flags=\\"-DSSID=\\"hello world\\"\\"" '
356+
+ f"{sketch_path} --verbose --clean"
357+
)
358+
assert res.ok
359+
assert "-DPIN=2" in res.stdout
360+
assert '-DSSID=\\"hello world\\"' in res.stdout
361+
362+
236363
def test_compile_with_output_dir_flag(run_command, data_dir):
237364
# Init the environment explicitly
238365
run_command("core update-index")

0 commit comments

Comments
 (0)