Description
Describe the bug
The Arduino boards platform system offers a custom board options capability. The platform properties associated with a given option are defined only when that option is selected (e.g., arduino:avr:nano:cpu=atmega328old
or Tools > Processor > ATmega328P (Old Bootloader)).
Because the properties defined in boards.txt
override those in platform.txt
, default property definitions are often made in platform.txt
. This allows non-default property definitions to be made only for those board configurations that need it. A prominent example of this practice is the build.extra_flags
property (e.g., here).
🐛 If a property was defined in a custom board option of a board previously compiled for, that board option associated definition persists into subsequent compilations with another board option selection that does not define the property.
To Reproduce
Set up
-
Download this
boards.local.txt
file: boards.local.txtThis file defines a
some_menu
custom board option for thearduino:avr:uno
board.Click to see file contents
menu.some_menu=Some Custom Menu uno.menu.some_menu.bad=Bad Option uno.menu.some_menu.bad.build.extra_flags=-bad-build-extraflags uno.menu.some_menu.bad.compiler.cpp.extra_flags=-bad-compiler-cpp-extraflags uno.menu.some_menu.good=Good Option uno.menu.some_menu.good.compiler.cpp.extra_flags=-DGOOD_COMPILER_CPP_EXTRA_FLAGS
-
Put the
boards.local.txt
file in thearduino:avr
boards platform installation folder (e.g.,~/.arduino15/packages/arduino/hardware/avr/1.8.4/
)
Demo for Arduino IDE 2.x
- Quit the IDE if it is running.
- Delete the configuration folder:
- Windows:
C:\Users\<user name>\AppData\Roaming\arduino-ide\
- Linux:
~/.config/arduino-ide/
- macOS:
~/Library/Application Support/arduino-ide/
https://github.com/arduino/arduino-ide/issues/591
) - Windows:
- Start the Arduino IDE.
- Select File > Preferences... from the Arduino IDE menus.
- Check the box next to "Show verbose output during ☐ compile".
(This is done only so you can see the flags added to the compilation commands by the custom board option properties.) - Click the OK button.
- Select Tools > Board > Arduino AVR Boards > Arduino Uno from the Arduino IDE menus.
- Select Tools > Some Custom Menu > Good Option from the Arduino IDE menus.
- Select Sketch > Verify/Compile from the Arduino IDE menus.
- Wait for compilation to finish.
🐛 Compilation fails unexpectedly:
avr-g++: error: unrecognized command line option '-bad-build-extraflags'
Note that the flag that caused the compilation failure is only defined via the build.extra_flags
property of the Tools > Some Custom Menu > Bad Option custom board option. The expected behavior would be that the default empty property definition from platform.txt
be used instead.
Note that the compilation commands do contain the expected -DGOOD_COMPILER_CPP_EXTRA_FLAGS
flag which was defined via the compiler.cpp.extra_flags
property of the Tools > Some Custom Menu > Good Option custom board option, which proves that option was used:
Detecting libraries used...
"C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10607 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR -DGOOD_COMPILER_CPP_EXTRA_FLAGS -bad-build-extraflags "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.4\\cores\\arduino" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.4\\variants\\standard" "C:\\Users\\per\\AppData\\Local\\Temp\\arduino-sketch-762B53F80F856C25998A7E9799EC2C98\\sketch\\sketch_jan4a.ino.cpp" -o nul
This behavior is extra confusing since the erroneous property definition could only have been produced by a compilation for the Tools > Some Custom Menu > Bad Option custom board option, but no compilation was initiated for that option in this demo. The explanation is that the Arduino IDE automatically compiled for that option as soon as you selected the Arduino Uno board in order to generate the language server data.
Demo for Arduino CLI
- Use the gRPC interface to Create and Init an "Arduino Core" instance.
- Use that instance to compile any valid sketch (e.g.,
arduino-cli sketch new
) forarduino:avr:uno:some_menu=bad
🙂 The compilation fails as expected due to the invalid compiler flags specified by this board option configuration:avr-g++: error: unrecognized command line option '-bad-compiler-cpp-extraflags' avr-g++: error: unrecognized command line option '-bad-build-extraflags'
- Use that instance to compile any valid sketch for
arduino:avr:uno:some_menu=good
🐛 Compilation fails unexpectedly:
avr-g++: error: unrecognized command line option '-bad-build-extraflags'
Expected behavior
Property definitions from previous compilations should not be persistent.
Desktop
- OS: Windows 10
- Version: git-snapshot Commit: 60c1c98 Date: 2022-01-05T01:37:22Z
Additional context
Originally reported at https://forum.arduino.cc/t/stm32f-board-will-not-select/941102
Although I demonstrated the issue via a minimal contrived platform configuration, the report was from a user of the STMicroelectronics:stm32
("STM32Duino") boards platform, which provides a real world occurrence of this bug. You can reproduce it using that platform via the following instructions:
Click to see instructions
- Start the Arduino IDE.
- Select File > Preferences from the Arduino IDE menus.
- Add the following URL:
https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json
- Click the OK button.
- Select File > Quit from the Arduino IDE menus.
(this is necessary due to the unrelated bughttps://github.com/arduino/arduino-ide/issues/637
) - Start the Arduino IDE.
- Select File > New from the Arduino IDE menus.
- Use Boards Manager to install the "STM32 MCU based boards" platform.
- Select Tools > Board > STM32 MCU based boards > Generic STM32F4 series" from the Arduino IDE menus.
- Select Tools > Board part number > Generic F401CCYx" from the Arduino IDE menus.
(This particular board configuration is selected arbitrarily for the demo. Other board configurations in this platform are also affected by the bug.) - Select Sketch > Verify/Compile from the Arduino IDE menus.
🐛 Compilation fails unexpectedly:<command-line>: fatal error: variant_BLACK_F407VX.h: No such file or directory
This error is caused by a property definition from another option of the "Board part number" menu than the "Generic F401CCYx" option that was selected:
https://github.com/stm32duino/Arduino_Core_STM32/blob/2.2.0/boards.txt#L2117
The "Generic F401CCYx" option that was selected does not define the property:
https://github.com/stm32duino/Arduino_Core_STM32/blob/2.2.0/boards.txt#L2270-L2276
so the default definition should have been used:
https://github.com/stm32duino/Arduino_Core_STM32/blob/2.2.0/platform.txt#L56
Another sighting of this bug here:
https://forum.arduino.cc/t/problems-with-megatinycore-in-ide-2/959081