2024-11-01 01:11 PM
Hello,
I recently noticed an issue with the CMake project generated by CubeMX targeting the STM32G4CETx MCU.
The generated gcc-arm-none-eabi.cmake sets the optimization flags using `CMAKE_C_FLAGS`, which are then overwritten by the default optimization flags used by CMake.
I've attached the compile_commands.json (though I've had to rename it to `.json.txt`) from a Release build, which is generated by CMake by using `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`. As you can see, the optimization flag `-Os` is immediately overwritten by `-O3`, which is the default for CMake.
It appears that projects for the STM32H7 do not have this issue, as they correctly use CMAKE_<LANG>_FLAGS_<CONFIG> (https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_%3CLANG%3E_FLAGS_%3CCONFIG%3E)
Also, as a side note, it appears that -Og should be used for debug builds, as that is preferred for the typical debug experience (per https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
2024-11-04 07:09 PM
@drewhanshaw wrote:Hello,
I recently noticed an issue with the CMake project generated by CubeMX targeting the STM32G4CETx MCU.
The generated gcc-arm-none-eabi.cmake sets the optimization flags using `CMAKE_C_FLAGS`, which are then overwritten by the default optimization flags used by CMake.
I've attached the compile_commands.json (though I've had to rename it to `.json.txt`) from a Release build, which is generated by CMake by using `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON`. As you can see, the optimization flag `-Os` is immediately overwritten by `-O3`, which is the default for CMake.
It appears that projects for the STM32H7 do not have this issue, as they correctly use CMAKE_<LANG>_FLAGS_<CONFIG> (https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_CONFIG.html#variable:CMAKE_%3CLANG%3E_FLAGS_%3CCONFIG%3E)
Also, as a side note, it appears that -Og should be used for debug builds, as that is preferred for the typical debug experience (per https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
It looks like CubeMX-generated CMake projects for STM32G4CETx overwrite custom optimization flags with CMake’s defaults, such as -O3, due to the use of CMAKE_C_FLAGS instead of CMAKE_<LANG>_FLAGS_<CONFIG>. STM32H7 projects handle this correctly.