Undefines in build_flags don't work if there's a space after -U
I'm on PlatformIO 6.1.18 on Linux, latest version at the time. This is a bug report.
When using -U in build_flags and running a build with --verbose, they're shown in the opening line:
Processing WHY2025_badge_companion_radio_ble (extends: WHY2025_badge; build_flags: -w [...] -D LORA_TX_POWER=27, -U LORA_TX_POWER, -D MAX_CONTACTS=100, [...])
but when gcc is being called, only -U is given to gcc (which incidentally also breaks my build):
[...] -DARDUINO_PARTITION_default_8MB -U -I.pio/libdeps/WHY2025_badge_companion_rad[...]
I confirmed this by sneaking in some print debugging to platformio/builder/tools/piobuild.py's ProcessFlags:
]
print("undefines found", undefines)
if undefines:
outputs:
undefines found ['-U']
Removing the space workarounds the issue, but including a space is in the default examples of both gcc and platformio, so I think it's confusing:
- https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html
- https://docs.platformio.org/en/stable/projectconf/sections/env/options/build/build_flags.html
I think this is something that is also a problem for other flags normally, but ParseFlagsExtended explicitly fixes it for those classified as CPPDEFINES through use of functions like Util.is_Sequence.
I think scons.ParseFlags does not explicitly support -U (like it does -D) and as such is falling back to CCFLAGS for it, the corrective code on ParseFlagsExtended for CPPDEFINES isn't fixing it. Could be escalated to scons, or could be solved in ParseFlagsExtended the same way CPPDEFINES are fixed.
I'd like to work on this issue.
I've identified the root cause: when -U MACRO is written with a space, SCons' ParseFlags() splits it into two separate items in CCFLAGS: ["-U", "MACRO"]. The current code only captures "-U" and attempts undef[2:] which results in an empty string.
I'm working on a fix that handles both formats:
-UMACRO(no space) - already working-U MACRO(with space) - needs fix
The solution involves iterating through CCFLAGS with an index to detect standalone -U flags and pairing them with the following macro name, similar to how SCons handles -D flags.
I'll have a PR ready within the next few days with tests.