From 9083c221fd7e7d529f3205491af79e92e26c7507 Mon Sep 17 00:00:00 2001 From: Eduard Drusa Date: Thu, 20 Aug 2026 11:18:22 +0200 Subject: [PATCH 1/3] Fix: FindCMSIS bugs FindCMSIS generated RTE_Headers.h which contained wrong header filename. It used DEVICE variable content instead of actual device header filename. More diagnostics for errors that are diagnosable during config time. --- cmake/FindCMSIS.cmake | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cmake/FindCMSIS.cmake b/cmake/FindCMSIS.cmake index 7f6c01d6..5a202dca 100644 --- a/cmake/FindCMSIS.cmake +++ b/cmake/FindCMSIS.cmake @@ -19,7 +19,16 @@ endif() # Find CMSIS components one by one file(GLOB_RECURSE DEVICE_INCLUDE ${CMSIS_ROOT}/*/${DEVICE_INCLUDE_FILENAME}) +list(LENGTH DEVICE_INCLUDE DEVICE_INCLUDE_LEN) +if (NOT "${DEVICE_INCLUDE_LEN}" EQUAL "1") + message(SEND_ERROR "Unable to find CMSIS device header ${DEVICE_INCLUDE_FILENAME}! Check if the device name passed in `DEVICE` variable is correct. If device header filename differs from device name (e.g. stm32f4xx.h vs. stm32f411xx) then explicitly specify device include filename in `DEVICE_INCLUDE_FILENAME` variable before including FindCMSIS.") +endif() + file(GLOB_RECURSE SYSTEM_INCLUDE ${CMSIS_ROOT}/*/${SYSTEM_INCLUDE_FILENAME}) +list(LENGTH SYSTEM_INCLUDE SYSTEM_INCLUDE_LEN) +if (NOT "${SYSTEM_INCLUDE_LEN}" EQUAL "1") + message(SEND_ERROR "Unable to find CMSIS system header ${SYSTEM_INCLUDE_FILENAME}! Check if the device name passed in `DEVICE` variable is correct. If system header filename differs from device name (e.g. system_stm32f4xx.h vs. stm32f411xx) then explicitly specify system include filename in `SYSTEM_INCLUDE_FILENAME` variable before including FindCMSIS.") +endif() file(GLOB_RECURSE CORES_INCLUDE ${CMSIS_ROOT}/*/core_cm*.h) file(GLOB_RECURSE SYSTEM_SOURCE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMSIS_ROOT}/*/system_${DEVICE}.c) file(GLOB_RECURSE STARTUP_SOURCE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMSIS_ROOT}/*/startup_${DEVICE}.c) @@ -52,7 +61,7 @@ file(COPY_FILE ${CMSIS_LINKER_FILE} ${CMAKE_BINARY_DIR}/gen.${DEVICE}.ld) file(WRITE ${CMAKE_BINARY_DIR}/cmsis_conf.h "#pragma once " - "#define CMSIS_device_header \"${DEVICE}.h\" + "#define CMSIS_device_header \"${DEVICE_INCLUDE_FILENAME}\" " ) @@ -62,6 +71,6 @@ set(CMSIS_SRCS ${SYSTEM_SOURCE} ${STARTUP_SOURCE}) add_library(cmsis_headers INTERFACE) set_property(TARGET cmsis_headers PROPERTY - INTERFACE_INCLUDE_DIRECTORIES ${INCLUDE_DIRS}) + INTERFACE_INCLUDE_DIRECTORIES ${INCLUDE_DIRS} ${CMAKE_BINARY_DIR}) From 056a1c0f2242a40f7565982440a326cd9d1e5c8e Mon Sep 17 00:00:00 2001 From: Eduard Drusa Date: Thu, 20 Aug 2026 11:21:41 +0200 Subject: [PATCH 2/3] Improvement: STM32 - Improve CubeMX compatibility Improve compatibility with both older and newer versions of CubeMX CMake generator. Newer CubeMX generates CMakeLists.txt that requires cmake/stm32cubemx be included after executable has been created. This version of platform support supports that by deferring of stm32cubemx_headers generation to the end of configuration step. Detection of linker script with name both in uppercase and lowercase is supported as different CubeMX generators and different packages use different case. Define with device name is made automatically, so CubeMX headers don't complain about specific device not being selected. --- cmake/platform/stm32.cmake | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/cmake/platform/stm32.cmake b/cmake/platform/stm32.cmake index c6badf39..3f0b0add 100644 --- a/cmake/platform/stm32.cmake +++ b/cmake/platform/stm32.cmake @@ -7,8 +7,10 @@ string(TOUPPER "${CMRX_DEVICE}" _CMRX_DEVICE_UPCASE) add_definitions(-D${_CMRX_DEVICE_UPCASE}) string(SUBSTRING "${CMRX_DEVICE}" 0 7 _STM_FAMILY) string(TOLOWER "${_STM_FAMILY}" _STM_FAMILY) +string(TOUPPER "${_STM_FAMILY}" _STM_FAMILY_UC) set(SYSTEM_INCLUDE_FILENAME system_${_STM_FAMILY}xx.h) +set(DEVICE_INCLUDE_FILENAME ${_STM_FAMILY}xx.h) # Assume default CubeMX CMake project layout if (NOT EXISTS ${CMAKE_SOURCE_DIR}/Drivers/CMSIS) @@ -23,23 +25,35 @@ if (DEFINED CMRX_LINKER_FILE) endif() set(CMSIS_LINKER_FILE "${CMRX_LINKER_FILE}") else() - file(GLOB CMSIS_LINKER_FILE LIST_DIRECTORIES false "${CMAKE_SOURCE_DIR}/${_STM_FAMILY}*.ld") + file(GLOB CMSIS_LINKER_FILE LIST_DIRECTORIES false "${CMAKE_SOURCE_DIR}/${_STM_FAMILY}*.ld" "${CMAKE_SOURCE_DIR}/${_STM_FAMILY_UC}*.ld") list(LENGTH CMSIS_LINKER_FILE _LINKER_FILES_FOUND) if (NOT "${_LINKER_FILES_FOUND}" STREQUAL 1) message(FATAL_ERROR "Unable to identify project linker file automatically! Either set variable `CMRX_LINKER_FILE` to contain path to linker file used or place exactly one linker file having pattern ${_STM_FAMILY}*.ld into ${CMAKE_SOURCE_DIR}") endif() endif() +add_definitions(-D${CMRX_DEVICE}) include(${CMAKE_CURRENT_LIST_DIR}/../FindCMSIS.cmake) -if (NOT TARGET stm32cubemx) - message(FATAL_ERROR "Target stm32cubemx not defined! Please, add directory cmake/stm32cubemx before including CMRX CMake module so CubeMX can be detected properly.") -endif() +function(_create_stm32cube_headers) + if (NOT TARGET stm32cubemx) + message(FATAL_ERROR "Target stm32cubemx not defined! Your project is not including subdirectory cmake/stm32cubemx or the generated project is not compatible with this STM32 platform support module!") + endif() + get_target_property(CUBEMX_INCLUDE_DIRECTORIES stm32cubemx INTERFACE_INCLUDE_DIRECTORIES) + get_target_property(CUBEMX_COMPILE_DEFINITIONS stm32cubemx INTERFACE_COMPILE_DEFINITIONS) -get_target_property(CUBEMX_INCLUDE_DIRECTORIES stm32cubemx INTERFACE_INCLUDE_DIRECTORIES) -get_target_property(CUBEMX_COMPILE_DEFINITIONS stm32cubemx INTERFACE_COMPILE_DEFINITIONS) + add_library(stm32cubemx_headers INTERFACE) + target_include_directories(stm32cubemx_headers INTERFACE ${CUBEMX_INCLUDE_DIRECTORIES}) + target_compile_definitions(stm32cubemx_headers INTERFACE ${CUBEMX_COMPILE_DEFINITIONS}) + get_property(LINK_OPTS TARGET ${CMAKE_PROJECT_NAME} PROPERTY LINK_OPTIONS) +endfunction() -add_library(stm32cubemx_headers INTERFACE) -target_include_directories(stm32cubemx_headers INTERFACE ${CUBEMX_INCLUDE_DIRECTORIES}) -target_compile_definitions(stm32cubemx_headers INTERFACE ${CUBEMX_COMPILE_DEFINITIONS}) +if (TARGET stm32cubemx) + _create_stm32cube_headers() +else() + message(STATUS "Target stm32cubemx not defined. Deferring creation of stm32cubemx_headers.") + cmake_language(DEFER DIRECTORY ${CMAKE_SOURCE_DIR} CALL _create_stm32cube_headers) +endif() +string(REGEX REPLACE "-T \"[^\"]\+\"" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") +message("Linker flags: ${CMAKE_EXE_LINKER_FLAGS}") From d165293381d0e06ab2695598c787bbf5855234e5 Mon Sep 17 00:00:00 2001 From: Eduard Drusa Date: Thu, 20 Aug 2026 11:57:57 +0200 Subject: [PATCH 3/3] Fix: Add files that are required now New FindCMSIS requires the existence of system header file, so create empty files to make it believe it found legit CMSIS pack. --- testing/clang-tidy/mock/cmsis/include/system_cortex-m0+.h | 3 +++ testing/clang-tidy/mock/cmsis/include/system_cortex-m23.h | 3 +++ testing/clang-tidy/mock/cmsis/include/system_cortex-m3.h | 3 +++ testing/clang-tidy/mock/cmsis/include/system_cortex-m33.h | 3 +++ testing/clang-tidy/mock/cmsis/include/system_cortex-m4f.h | 3 +++ 5 files changed, 15 insertions(+) create mode 100644 testing/clang-tidy/mock/cmsis/include/system_cortex-m0+.h create mode 100644 testing/clang-tidy/mock/cmsis/include/system_cortex-m23.h create mode 100644 testing/clang-tidy/mock/cmsis/include/system_cortex-m3.h create mode 100644 testing/clang-tidy/mock/cmsis/include/system_cortex-m33.h create mode 100644 testing/clang-tidy/mock/cmsis/include/system_cortex-m4f.h diff --git a/testing/clang-tidy/mock/cmsis/include/system_cortex-m0+.h b/testing/clang-tidy/mock/cmsis/include/system_cortex-m0+.h new file mode 100644 index 00000000..70d8089c --- /dev/null +++ b/testing/clang-tidy/mock/cmsis/include/system_cortex-m0+.h @@ -0,0 +1,3 @@ +#pragma once + +/* This file is intentionally left empty */ diff --git a/testing/clang-tidy/mock/cmsis/include/system_cortex-m23.h b/testing/clang-tidy/mock/cmsis/include/system_cortex-m23.h new file mode 100644 index 00000000..70d8089c --- /dev/null +++ b/testing/clang-tidy/mock/cmsis/include/system_cortex-m23.h @@ -0,0 +1,3 @@ +#pragma once + +/* This file is intentionally left empty */ diff --git a/testing/clang-tidy/mock/cmsis/include/system_cortex-m3.h b/testing/clang-tidy/mock/cmsis/include/system_cortex-m3.h new file mode 100644 index 00000000..70d8089c --- /dev/null +++ b/testing/clang-tidy/mock/cmsis/include/system_cortex-m3.h @@ -0,0 +1,3 @@ +#pragma once + +/* This file is intentionally left empty */ diff --git a/testing/clang-tidy/mock/cmsis/include/system_cortex-m33.h b/testing/clang-tidy/mock/cmsis/include/system_cortex-m33.h new file mode 100644 index 00000000..70d8089c --- /dev/null +++ b/testing/clang-tidy/mock/cmsis/include/system_cortex-m33.h @@ -0,0 +1,3 @@ +#pragma once + +/* This file is intentionally left empty */ diff --git a/testing/clang-tidy/mock/cmsis/include/system_cortex-m4f.h b/testing/clang-tidy/mock/cmsis/include/system_cortex-m4f.h new file mode 100644 index 00000000..70d8089c --- /dev/null +++ b/testing/clang-tidy/mock/cmsis/include/system_cortex-m4f.h @@ -0,0 +1,3 @@ +#pragma once + +/* This file is intentionally left empty */