cancel
Showing results for 
Search instead for 
Did you mean: 

Generated toolchain files should use CMAKE_C_FLAGS_INIT and set it instead of appending to avoid flags being duplicated in the build command

JesperEC
Associate III

Generate code from attached .ioc

Build with cmake:

cmake --preset Debug && cmake --build --preset Debug

Inspect NonSecure/build/build.ninja and we find

 

build CMakeFiles/myproject_NS.dir/AZURE_RTOS/App/app_azure_rtos.c.obj: C_COMPILE
R__myproject_NS_Debug ... || c
make_object_order_depends_target_myproject_NS
...
  FLAGS = -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard  -Wall -fdata-sect
ions -ffunction-sections -mcpu=cortex-m33 -mfpu=fpv5-sp-d16 -mfloat-abi=hard  -Wall -fdata-sections -ffunction-sections -O0 -g3 -std=gnu11

 

Note that the flags set in gcc-arm-none-eabi.cmake

 

# MCU specific flags
set(TARGET_FLAGS "${STM32_MCU_FLAGS}")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fdata-sections -ffunction-sections")

 

occurs twice!

The reason for this is that the toolchain file gcc-arm-none-eabi.cmake is evaluated multiple times. It is exactly the problem described at [3.31.6] Toolchain file gets parsed twice - Development - CMake Discourse but for an older CMake. And due to that the global variable is set multiple times each time amending to the existing value.

 

PROPOSED SOLUTION

Change the generated code to read

# MCU specific flags
set(TARGET_FLAGS "${STM32_MCU_FLAGS}")

set(CMAKE_C_FLAGS_INIT ${TARGET_FLAGS}")
set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -Wall -fdata-sections -ffunction-sections")

CMAKE_C_FLAGS_INIT is the proper variable to use here and will be combined with any CMAKE_C_FLAGS passed on the command line and CFLAGS from the environment to initialize CMAKE_C_FLAGS. By setting it instead of appending it, multiple evaluations of the toolchain files will still work. I believe this is how CMake toolchain files are intended to be setup.

Related problems:

  1. The same change should probably be done for CMAKE_ASM_FLAGS, CMAKE_CXX_FLAGS and CMAKE_EXE_LINKER_FLAGS but I haven't tested.
  2. It looks like starm-clang.cmake suffers from the same problems but I have not tested with that toolchain.
  3. Passing extra flags on the command line still won't work fully due to that the S and NS parts are setup as "external projects" instead of targets within a common project. But that is a different issue than what is reported here. Additions to the top level mx-generated.cmake is probably possible to make it work if desired.

 

Hopefully this can be fixed in a future version of STM32CubeMX. Please let me know if an internal ticket is created or if you want me to provide additional details.

 

ENVIRONMENT

STM32CubeMX 6.16.0 running on debian bookworm.

STM32CubeIDE 1.18.0 with /opt/st/stm32cubeide_1.18.0/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.13.3.rel1.linux64_1.0.0.202410170706/tools/bin  in the PATH

cmake version 3.25.1

ninja 1.11.1

 

      Best regards, Jesper

 

0 REPLIES 0