Skip to main content
Associate III
October 25, 2024
Question

CMAKE_BUILD_TYPE not passed to CMakeLists.txt for the generated project via STM32CubeMX

  • October 25, 2024
  • 2 replies
  • 2828 views

Hi,

I have setup and configured an STM32H5-based MCU via STM32CubeMX and with cmake as build system.

 

I managed to build it successfully using Ninja generator. However, I can't change the configuration to Release and it is always Debug even though I used -DCMAKE_BUILD_TYPE=Release. 

 

I tried this in command line and using STM32 VSCODE Extension and all with the same results. 

 

I even tried to pass -DCMAKE_CONFIGURATION_TYPES="Debug;Release".

 

Here is my build script:

 

@echo off

set TargetBuild=%1

:CHECK_INPUT

IF [%TargetBuild%] == [] GOTO :ERROR

:PROCESS

cd ../ &

rm -rf Secure/build &

rm -rf NonSecure/build &

rm -rf build

mkdir build &

cd build &

cmake -DCMAKE_CONFIGURATION_TYPES=%TargetBuild% -DCMAKE_BUILD_TYPE=%TargetBuild% -DCMAKE_TOOLCHAIN_FILE=gcc-arm-none-eabi.cmake -B%TargetBuild% -G Ninja -S../  &

cd %TargetBuild% &

ninja

goto :eof

:ERROR

echo "Error: No target build given"

echo "Usage Example:""

echo "_build.cmd Release"

goto :eof

Cheers,
Hani

2 replies

Ghofrane GSOURI
Technical Moderator
October 25, 2024

Hello @hakeila 

Before reconfiguring, make sure to clean any previous build artifacts. You can do this by removing the build directory entirely:

rm -rf build
mkdir build
cd build

Visual Studio Code Settings:
If you're using Visual Studio Code with the STM32 extension, ensure that your tasks and launch configurations are set up correctly to reflect the desired build type.

Example Build Script Adjustment

Here’s a refined version of your batch script that includes checks and ensures proper handling of the build type:

@echo off

set TargetBuild=%1

:CHECK_INPUT

IF [%TargetBuild%] == [] GOTO :ERROR

:PROCESS

cd ..\ &

rm -rf Secure\build & rm -rf NonSecure\build & rm -rf build 

mkdir build & cd build &

cmake -DCMAKE_CONFIGURATION_TYPES="%TargetBuild%" -DCMAKE_BUILD_TYPE="%TargetBuild%" -DCMAKE_TOOLCHAIN_FILE=gcc-arm-none-eabi.cmake -G Ninja -S .. &

cd %TargetBuild% & ninja

goto :eof

:ERROR

echo "Error: No target build given"
echo "Usage Example:"
echo "_build.cmd Release"

goto :eof
  • After running CMake, check the output logs for any warnings or messages related to configuration.
  • Run ninja clean before building again to ensure no stale objects are present.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
hakeilaAuthor
Associate III
October 28, 2024

Hi Ghofrane,

Thank you so much reply.

 

I actually tried your suggestion and copied your refined version of the script. I also made sure that my build starts with a clean build. But the generated build is always set at debug. Here is the CMake output:

 

 

 

Generated with config types: Release
-- The C compiler identification is GNU 12.3.1
-- The CXX compiler identification is GNU 12.3.1
 Build context: Secure
 Build context: NonSecure
-- Configuring done (2.2s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/hakeila/projects/WxplNG/build
[1/1] Cleaning all built files...
Cleaning... 0 files.
[5/16] Performing configure step for 'WxplNG_S'
Build type: Debug
-- The C compiler identification is GNU 12.3.1
-- The CXX compiler identification is GNU 12.3.1
-- The ASM compiler identification is GNU
-- Found assembler: C:/ST/STM32CubeCLT_1.16.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe
-- Configuring done (2.4s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/hakeila/projects/WxplNG/Secure/build
[6/16] Performing build step for 'WxplNG_S'
[1/25] Building C object CMakeFiles/WxplNG_S.dir/Src/icache.c.obj
[2/25] Building C object CMakeFiles/WxplNG_S.dir/Src/main.c.obj
[3/25] Building C object CMakeFiles/WxplNG_S.dir/Src/stm32h5xx_it.c.obj
[4/25] Building C object CMakeFiles/WxplNG_S.dir/Src/gtzc_s.c.obj
[5/25] Building C object CMakeFiles/WxplNG_S.dir/Src/stm32h5xx_hal_msp.c.obj
[6/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c.obj
[7/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_cortex.c.obj
[8/25] Building C object CMakeFiles/WxplNG_S.dir/Src/secure_nsc.c.obj
[9/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_gtzc.c.obj
[10/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc_ex.c.obj
[11/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_flash.c.obj
[12/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_flash_ex.c.obj
[13/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_gpio.c.obj
[14/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c.obj
[15/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_dma.c.obj
[16/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_icache.c.obj
[17/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal.c.obj
[18/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_exti.c.obj
[19/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr_ex.c.obj
[20/25] Building C object CMakeFiles/WxplNG_S.dir/Src/sysmem.c.obj
[21/25] Building ASM object CMakeFiles/WxplNG_S.dir/startup/startup_stm32h523xx.s.obj
[22/25] Building C object CMakeFiles/WxplNG_S.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_dma_ex.c.obj
[23/25] Building C object CMakeFiles/WxplNG_S.dir/Src/syscalls.c.obj
[24/25] Building C object CMakeFiles/WxplNG_S.dir/Src/system_stm32h5xx_s.c.obj
[25/25] Linking C executable WxplNG_S.elf
Memory region Used Size Region Size %age Used
 RAM: 1592 B 208 KB 0.75%
 FLASH: 6028 B 248 KB 2.37%
 FLASH_NSC: 32 B 8 KB 0.39%
[13/16] Performing configure step for 'WxplNG_NS'
Build type: Debug
-- The C compiler identification is GNU 12.3.1
-- The CXX compiler identification is GNU 12.3.1
-- The ASM compiler identification is GNU
-- Found assembler: C:/ST/STM32CubeCLT_1.16.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe
-- Configuring done (2.2s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/hakeila/projects/WxplNG/NonSecure/build
[14/16] Performing build step for 'WxplNG_NS'
[1/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/main.c.obj
[2/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_flash.c.obj
[3/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/stm32h5xx_it.c.obj
[4/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_cortex.c.obj
[5/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_gpio.c.obj
[6/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/stm32h5xx_hal_msp.c.obj
[7/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_flash_ex.c.obj
[8/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c.obj
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c: In function 'HAL_RCC_ConfigAttributes':
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c:1754:40: warning: unused parameter 'Item' [-Wunused-parameter]
 1754 | void HAL_RCC_ConfigAttributes(uint32_t Item, uint32_t Attributes)
 | ~~~~~~~~~^~~~
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c: In function 'HAL_RCC_GetConfigAttributes':
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc.c:1834:56: warning: unused parameter 'Item' [-Wunused-parameter]
 1834 | HAL_StatusTypeDef HAL_RCC_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
 | ~~~~~~~~~^~~~
[9/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_dma.c.obj
[10/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_rcc_ex.c.obj
[11/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/syscalls.c.obj
[12/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c.obj
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c: In function 'HAL_PWR_ConfigAttributes':
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c:527:40: warning: unused parameter 'Item' [-Wunused-parameter]
 527 | void HAL_PWR_ConfigAttributes(uint32_t Item, uint32_t Attributes)
 | ~~~~~~~~~^~~~
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c: In function 'HAL_PWR_GetConfigAttributes':
C:/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr.c:610:56: warning: unused parameter 'Item' [-Wunused-parameter]
 610 | HAL_StatusTypeDef HAL_PWR_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
 | ~~~~~~~~~^~~~
[13/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/sysmem.c.obj
[14/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_pwr_ex.c.obj
[15/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_tim.c.obj
[16/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_tim_ex.c.obj
[17/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_exti.c.obj
[18/22] Building ASM object CMakeFiles/WxplNG_NS.dir/startup/startup_stm32h523xx.s.obj
[19/22] Building C object CMakeFiles/WxplNG_NS.dir/Src/system_stm32h5xx_ns.c.obj
[20/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal.c.obj
[21/22] Building C object CMakeFiles/WxplNG_NS.dir/C_/Users/hakeila/projects/WxplNG/Drivers/STM32H5xx_HAL_Driver/Src/stm32h5xx_hal_dma_ex.c.obj
[22/22] Linking C executable WxplNG_NS.elf
Memory region Used Size Region Size %age Used
 RAM: 1584 B 64 KB 2.42%
 FLASH: 5988 B 256 KB 2.28%
[16/16] Completed 'WxplNG_NS'

 

 

You can see the Build Type: Debug from the attached cmake output above.

the main CMakeLists.txt doesn't see my CMAKE_BUILD_TYPE variable and thus it set it to debug.

I even tried to set the CMAKE_BUILD_TYPE in STMCubeCX generated parent CMakeLists.txt with Release version, but the successive .cmake files don't see this variable.

 

hakeilaAuthor
Associate III
October 28, 2024

With more digging into this cmake issue, I found out that this system is considered as multi-configuration setting 

I typed the CMAKE_BUILD_TYPE in the parent CMakeLists.txt and it was indeed a Release build but for some reason, this variable is not passed to the successive CMakeLists.txt in Secure and NonSecure sub-folders for some reason

I also made sure that the generated .ninja file does contain the build configuration as release 

What is going wrong here?

hakeilaAuthor
Associate III
October 29, 2024

Hi @Ghofrane GSOURI

 

I managed to fix the issue but I had to manually modify the mx-generated.cmake file (which is automatically generated from STMCubeMX). I added -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} into the CMAKE_ARGS as shown in the attached mx-generated.cmake file. 

 

I think the ExternalProject_Add() function in the original mx-generated.cmake has its own cmake args scope which made the CMAKE_BUILD_TYPE variable always null and thus, the successive CMakeLists.txt in Secure and NonSecure folders always set it to Debug due to failed if(NOT CMAKE_BUILD_TYPE) check

 
I believe you need to fix the STMCubeMX project CMake autogeneration (i.e. mx-generated.cmake) to pass the CMAKE_BUILD_TYPE definition as shown in my modified file.
 
Kind Regards,
Hani