It is possible, nay likely, that I am stupid, but it has taken me half a day to achieve something that I thout would be simple. I need to build several versions of a sketch depending on setting a #define and I also want to set the maximum optimization. There appears to be no obvious help anywhere on how to do this. My first attempt used the following on the command line (after the arduino-cli) based on clues in other posts that claimed to have got this to work:
This seems to be ignored. I then tried using "compiler.cpp.extra_flags=-D MYSYMBOL" following the pattern suggested by the forum thread where --build-property was developed. Also ignored.
Finally, I used --show-properties to see what is being set and came up with:
This does work. I am happy with the optimize as this replaces the original entirely. But replacing build.flags.defs is a mess because it means if either of the other two definitions changes, or new system ones are added, I must re-edit.
I have seen other requests for a command line -D SYMBOL[=VALUE] option, which does not seem unreasonable. Perhaps --build-property "build.extra_flags=-D MYSYMBOL -O3 -flto" is intended to work and its failure is a bug?
So some requests for what I consider to be the most likely things folks will need:
Please document how we are to set definitions in addition to the existing ones
I am targetting a Teensy 4.0, so Teensy:avr:Teensy40 (if this is what you are asking). This is using the Arduino support described at: https://www.pjrc.com/teensy/teensyduino.html
When you add a flag like this to your arduino-cli compile command:
--build-property "build.extra_flags=-D MYSYMBOL"
It sets the value of the build.extra_flags platform property to -D MYSYMBOL. All template references ({build.extra_flags}) in the platform configuration files (e.g., boards.txt, platform.txt) will be replaced with that value.
But the choice of which property references to include in the various templates, and how those properties might be used internally is completely up to each platform author. This means that you must have a good understanding of the configuration of the platform in order to use this very advanced --build-property flag. The examples in the arduino-cli compile --help command line reference are only intended to demonstrate how you might set the value of an arbitrary platform property, not as a universally valid recipe.
Great work on finding a solution! Although there is a common convention for platforms to provide the ability for the user to easily inject arbitrary flags into the compilation commands without colliding with internal usage of platform properties, as is done in all the official Arduino boards platforms, unfortunately this was not done in the Teensy boards platform so I don't think there is any better solution than the one you found, even though it is not ideal.
There is a proposal for formalizing a standard convention for platform developers to provide the ability for users to reliably inject arbitrary flags into compilation commands here:
If you have a GitHub account, you can subscribe to that thread to get notifications of any new developments related to this subject:
Please only comment on the GitHub issue thread if you have new technical information that will assist with the resolution. General discussion and support requests are always welcome here on the Arduino Forum.
You are free to use any property name you like with the --build-property flag. For example:
--build-property "foo=bar"
Will define a property named foo with the value bar. But the property will only have an effect if the developer of the platform you are using referenced it somewhere in the platform configuration files.
Arduino has implemented a standard system for doing this, which is documented in the the arduino-cli compile --help command line reference:
But each platform developer has the freedom to chose whether or not they want to make use of that flag. The Teensy platform does not, so the --optimize-for-debug flag has no effect when compiling for Teensy boards.
A custom system has been implemented in the Teensy platform for controlling optimization (which is may be why they didn't set up a system for --optimize-for-debug). You can see it by running the following arduino-cli command:
When you only use the base FQBN (in this case teensy:avr:teensy40) in the arduino-cli command, the default settings of any custom board options are used, but you can select other settings via the FQBN, which has this format:
So if, for example, you wanted to select the "Fastest with LTO" (option ID o3lto) option of the the "Optimize" custom board option (menu ID opt), you would use this FQBN in your arduino-cli commands:
teensy:avr:teensy40:opt=o3lto
If possible, you should use this user friendly formal public API provided by the Teensy boards platform to configure the optimization level rather than resorting to tampering with internal platform properties via the advanced --build-property flag.
Many thanks for your very clear and comprehensive answer; given the effort involved I hope this is a useful resource for others.
I have changed the optimization as you suggested, I guess the -D stuff is my problem until a general solution is agreed on
and implemented.