cancel
Showing results for 
Search instead for 
Did you mean: 

How to customize your STM32CubeMX generated CMake project

Intro

STM32CubeMX supports CMake since 6.11.0 as a tool chain option. Sometimes you want to customize your project, but you don't want generated files to be overwritten. I will reply to this post how to do these things and this will be the accepted answer. It's like a knowledge base article. Please expand it with more ideas and I will add it to that answer.

Generated files

STM32CubeMX generates:

  • ./CMakeLists.txt:This file can be customized
  • cmake/stm32cubemx/CMakeLists.txt: file with all cube generated source files and defines. It's an interface library. This file can not be modified as it will be overwritten when regenerating with STM32CubeMX
  • cmake/touchgfx/CMakeLists.txt: if you have touchgfx in your project this will be added too. It's an interface library. This file can not be modified as it will be overwritten when regenerating with TouchGFX Designer
  • potentially more subfolders with CMakeLists.txt files
  • gcc-arm-none-eabi.cmake: toolchain file for gcc. Used if you use gcc as compiler and linker. Can be modified, but shouldn't be needed.
  • starm-clang.cmake: toolchain file for clang. Used if you use clang as compiler. clang linker is recommended as it is required if you want to use clang-tidy. But it also supports "hybrid" where clang is used as compiler and gcc linker. Can be modified, but shouldn't be needed (I did need to make one modification to use clang-tidy, but that's a separate topic).

You can add your own sources and settings in ./CMakeLists.txt. But sometimes you want to change the way stm32cubeMX generated code is compiled:

  • Some of the sources in stm32cubemx don't have any user code sections. If you want to modify those you may need to exclude it from build and add your own substitute
  • if you #include header files in user code sections they need to be in the include paths. Otherwise you would need to add modify the include paths.
  • You may need to add, remove or change compile definitions

 

 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.
1 ACCEPTED SOLUTION

Accepted Solutions

Adding compile definitions, adding include paths and removing source files 

The generated CMakeLists.txt files are cmake interface libraries and not compiled libraries. They are basically a list of source files and compile definitions. When they are included in your ./CMakeLists.txt this information can be modified without modifying those files. This is what you want, because you don't want to constantly have to manually change (re)generated files.

Add the following to your ./CMakeLists.txt file:

# modifications to stm32cubemx interface library without modifying its CMakeLists.txt file:

# add a define to stm32cubemx interface libary:
target_compile_definitions(stm32cubemx INTERFACE
HTTPD_FSDATA_FILE="../httpd/fsdata.c"
)

# Add extra include directories to stm32cubemx interface libary:
target_include_directories(stm32cubemx INTERFACE
httpd
Communication
Config
Common
Process
Display
)

# Remove sources from stm32cubemx interface libary:
get_target_property(APP_SOURCES ${CMAKE_PROJECT_NAME} SOURCES)
#message(WARNING ${APP_SOURCES})
list(REMOVE_ITEM APP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../USB_HOST/App/usb_host.c
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../TouchGFX/target/generated/OSWrappers.cpp
)
#message(WARNING ${APP_SOURCES})
set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY SOURCES "${APP_SOURCES}")

Output a graph with library dependencies

You can generate a dependency graph of your CMake project:

# add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} "--graphviz=cmake_dependency_graph.dot" .
COMMAND dot -Tpng cmake_dependency_graph.dot -o cmake_dependency_graph.png
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)

This graph will contain all the library and executable dependencies in the project showing the dependencies between the targets in a project, as well as external libraries which are linked against.

This will generate something like:

unsigned_char_array_1-1763486651604.png

With me it works, but it requires a successful build first. There is some type of racing condition. So I need to comment it out first, build and then I can uncomment it again. If you know how to do this let me know.

Remove external FLASH sector from elf file to speed up debugging sessions

My TouchGFX project uses external flash. In order to debug without always flashing the entire external flash you can create a separate elf file that has those sections removed. You can use the following command for that:

add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}_NO_OFLASH.elf --remove-section=.textoctoflash --remove-section=ExtFlashSection
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)

This saves a lot of time when debugging.

 

 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

View solution in original post

1 REPLY 1

Adding compile definitions, adding include paths and removing source files 

The generated CMakeLists.txt files are cmake interface libraries and not compiled libraries. They are basically a list of source files and compile definitions. When they are included in your ./CMakeLists.txt this information can be modified without modifying those files. This is what you want, because you don't want to constantly have to manually change (re)generated files.

Add the following to your ./CMakeLists.txt file:

# modifications to stm32cubemx interface library without modifying its CMakeLists.txt file:

# add a define to stm32cubemx interface libary:
target_compile_definitions(stm32cubemx INTERFACE
HTTPD_FSDATA_FILE="../httpd/fsdata.c"
)

# Add extra include directories to stm32cubemx interface libary:
target_include_directories(stm32cubemx INTERFACE
httpd
Communication
Config
Common
Process
Display
)

# Remove sources from stm32cubemx interface libary:
get_target_property(APP_SOURCES ${CMAKE_PROJECT_NAME} SOURCES)
#message(WARNING ${APP_SOURCES})
list(REMOVE_ITEM APP_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../USB_HOST/App/usb_host.c
${CMAKE_CURRENT_SOURCE_DIR}/cmake/stm32cubemx/../../TouchGFX/target/generated/OSWrappers.cpp
)
#message(WARNING ${APP_SOURCES})
set_property(TARGET ${CMAKE_PROJECT_NAME} PROPERTY SOURCES "${APP_SOURCES}")

Output a graph with library dependencies

You can generate a dependency graph of your CMake project:

# add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} "--graphviz=cmake_dependency_graph.dot" .
COMMAND dot -Tpng cmake_dependency_graph.dot -o cmake_dependency_graph.png
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)

This graph will contain all the library and executable dependencies in the project showing the dependencies between the targets in a project, as well as external libraries which are linked against.

This will generate something like:

unsigned_char_array_1-1763486651604.png

With me it works, but it requires a successful build first. There is some type of racing condition. So I need to comment it out first, build and then I can uncomment it again. If you know how to do this let me know.

Remove external FLASH sector from elf file to speed up debugging sessions

My TouchGFX project uses external flash. In order to debug without always flashing the entire external flash you can create a separate elf file that has those sections removed. You can use the following command for that:

add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}_NO_OFLASH.elf --remove-section=.textoctoflash --remove-section=ExtFlashSection
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
)

This saves a lot of time when debugging.

 

 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.