Hi @flounder
A separate tool named Arduino CLI provides more advanced capabilities like these. You can set build properties via the arduino-cli compile --build-property
flag:
https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_compile/#options
Some demonstrations of how to use it are provided in the "examples" section of the documentation:
arduino-cli compile -b arduino:avr:uno --build-property "build.extra_flags=\"-DMY_DEFINE=\"hello world\"\"" /home/user/Arduino/MySketch
You might be interested in some related discussion here:
opened 03:04PM - 20 Jul 20 UTC
type: enhancement
topic: documentation
criticality: high
topic: build-process
### Describe the request
Establish two sets of standard "extra flags" build p… roperties:
- one set for use by platform developer
- one set for use by the user
These should be clearly documented as being dedicated to the exclusive use by either the platform developer or the user.
The properties proposed here are only arbitrary properties, without any special use by the build system, so no changes would required in the build system. It is only a matter of setting an example for 3rd party platform developers by adding these additional properties to the `platform.txt` files of the official platforms with comments clearly explaining their intent.
Ideally, the properties would be documented in the [Arduino platform specification](https://arduino.github.io/arduino-cli/latest/platform-specification/) as well (https://github.com/arduino/arduino-cli/issues/985).
### Describe the current behavior
Arduino has established a convention of providing "extra flags" properties in [`platform.txt`](https://arduino.github.io/arduino-cli/latest/platform-specification/#platformtxt). These properties are intended to allow the compilation process to be customized by the addition of arbitrary flags in the compilation commands.
For example:
https://github.com/arduino/ArduinoCore-avr/blob/1.8.6/platform.txt#L39-L49
```text
# This can be overridden in boards.txt
build.extra_flags=
# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=
```
These properties are referenced in the appropriate compilation recipes in `platform.txt` ([example](https://github.com/arduino/ArduinoCore-avr/blob/1.8.6/platform.txt#L58)).
The empty definitions in `platform.txt` ensure that if the extra flags are not defined elsewhere, the compilation recipes referencing them will still work. Those default empty definitions can be overrriden from any of the other platform configuration files:
- [`boards.txt`](https://arduino.github.io/arduino-cli/latest/platform-specification/#boardstxt)
- [`boards.local.txt`](https://arduino.github.io/arduino-cli/latest/platform-specification/#boardslocaltxt)
- [`platform.local.txt`](https://arduino.github.io/arduino-cli/latest/platform-specification/#platformlocaltxt)
- [`global platform.txt`](https://arduino.github.io/arduino-cli/latest/platform-specification/#global-platformtxt)
or from the command line:
- [`arduino-cli compile --build-property`](https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_compile/#options)
- [`arduino --pref`](https://github.com/arduino/Arduino/blob/master/build/shared/manpage.adoc#options)
---
From the comments in the official `platform.txt` files, it seems that `build.extra_flags` is intended to be for the use of the boards platform developer, while the other "extra flags" properties are intended to be for the use of the user (because a platform developer has no need for `platform.local.txt`).
A common use case of these properties for the user would be to add `-D` flags (e.g., https://github.com/arduino/arduino-cli/issues/210#issuecomment-493892258, https://github.com/arduino/Arduino/issues/6344), but there are other uses.
However, platform developers sometimes use these properties in their `boards.txt` file ([example](https://github.com/MCUdude/MightyCore/blob/v2.0.5/avr/boards.txt#L103-L111)). This presents the problem that the user risks inadvertently overriding the flags defined by the boards platform if they use the "extra flags" properties to modify the compilation command ([example](https://github.com/arduino/arduino-cli/issues/2041)).
### Arduino CLI version
N/A
### Operating system
All
### Operating system version
Any
### Additional context
#### Re: Possible Harmful Effect on APIs
On a certain level, this does provide the user the global macro setting capability that has been often requested, and often rejected:
- https://github.com/arduino/arduino-builder/issues/15
- https://github.com/arduino/arduino-builder/pull/29
- https://github.com/arduino/arduino-builder/pull/282
- https://github.com/arduino/arduino-builder/issues/283
- https://github.com/arduino/arduino-builder/issues/354
- https://github.com/arduino/arduino-ide/issues/1837
- https://github.com/arduino/Arduino/issues/421
- https://github.com/arduino/Arduino/issues/2547
- https://github.com/arduino/Arduino/issues/3635
- https://github.com/arduino/Arduino/pull/3717
- https://github.com/arduino/Arduino/pull/3757
- https://github.com/arduino/Arduino/issues/4528
- https://github.com/arduino/Arduino/issues/5084
- https://github.com/arduino/Arduino/issues/5390
- https://github.com/arduino/Arduino/issues/7189
- https://github.com/arduino/Arduino/issues/11108
- https://github.com/arduino/arduino-cli/issues/159
- https://github.com/arduino/arduino-cli/issues/210
- https://github.com/arduino/arduino-cli/pull/1117
- https://github.com/arduino/arduino-cli/pull/1517
- https://github.com/arduino/tooling-rfcs/issues/9
- https://github.com/arduino/tooling-rfcs/issues/22
However, I think this proposal strikes the right balance of:
- Allowing advanced users to do advanced things
- Not exposing standard users to additional complexity that makes the Arduino development software less beginner friendly
- Being very easy for Arduino to implement
The concern has been that allowing users to set global macros will result in library developers unnecessarily using these as part of the API, making that API less beginner friendly (https://github.com/arduino/arduino-builder/issues/15#issuecomment-145558252). However, if the user is required to use a command line interface or create a specially formatted configuration file in a difficult to access location, the average user won't be interested, and so it will only make sense for a library developer to use this feature for advanced, rarely used features of the library (e.g., enable debug output for troubleshooting of the library). We have already had this "extra flags" capability [since 2014](https://github.com/arduino/ArduinoCore-avr/commit/2b9f022eaad0d107b1516c22ac02bdac4977223b), yet I haven't seen it result in any significant incorporation of user set global macros into the user-level API of Arduino libraries.
### Issue checklist
- [X] I searched for previous requests in [the issue tracker](https://github.com/arduino/arduino-cli/issues?q=)
- [X] I verified the feature was still missing when using the latest [nightly build](https://arduino.github.io/arduino-cli/dev/installation/#nightly-builds)
- [X] My request contains all necessary details
There is a Sketch > Optimize for Debugging menu option for this purpose. The exact behavior, including whether it does anything at all is up to the author of the platform of the board you are compiling for, as is the case with all compilation parameters.
You can see the flags it adds when compiling for a board of the "Arduino SAMD Boards (32-bits ARM Cortex-M0+) " platform here:
https://github.com/arduino/ArduinoCore-samd/blob/1.8.13/platform.txt#L38
compiler.optimization_flags.debug=-Og -g3
If by "preprocessed" you mean Arduino sketch preprocessing , you can get it from arduino-cli compile --preprocess
.
Alternatively, you can see the .cpp
file generated from the sketch after sketch preprocessing in the sketch
subfolder of the temporary build folder.
If by "preprocessed" you mean the C++ preprocessor, you can see this by looking at the file in the preproc
subfolder of the temporary build folder.
The path of the temporary build folder is shown in the verbose compilation output. For example, from this line of output:
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-5F56F5A11E5AD1C1C0AA709A99664C46/Associated2.ino.elf"
you can see the build folder is here:
C:\Users\per\AppData\Local\Temp\arduino-sketch-5F56F5A11E5AD1C1C0AA709A99664C46