Unfortunately, I didn’t find a sufficient answer to my problem. I’m using ST’s CubeMX for STM32 uC to generate CMake projects. This generates a project hierarchy with cross-compiler and all works fine. Now I have to test some of my own code on my developer host (Windows10-X64 with VS 2022 Community). My problem is to set up the build tools for the tests.
CubeMX generates (shortened) CMakeLists.txt in the project’s root:
...
set(CMAKE_CXX_STANDARD 17) // added by me
...
set(CMAKE_PROJECT_NAME my_Project)
include("cmake/gcc-arm-none-eabi.cmake")
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
enable_language(C ASM)
project(${CMAKE_PROJECT_NAME})
add_executable(${CMAKE_PROJECT_NAME})
add_subdirectory(cmake/stm32cubemx)
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
)
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
Src/stm32xxxx.c
Src/cool_stuff.cpp
)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
User/Inc
)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
)
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
)
The “magic” happens inside the cmake/gcc-arm-none-eabi.cmake
and the folder stm32cubemx
‘s CMakeFile.txt (also shortened)
...
project(stm32cubemx)
add_library(stm32cubemx INTERFACE)
...
So, here I am. On bottom of project’s root CMakeLists.txt I append my Test sub-folder add_subdirectory(Test)
. This Test folders conatins the sub-folders Inc
and Src
for my (Catch2) tests with following CMakeLists.txt:
...
set(CMAKE_GENERATOR_TOOLSET "Ninja")
set(CMAKE_CXX_STANDARD 17)
project(my_tests LANGUAGES C CXX)
Include(FetchContent)
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.6.0
)
FetchContent_MakeAvailable(Catch2)
enable_testing()
add_executable(${CMAKE_PROJECT_NAME})
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
Src/test_ringbuffer.cpp
)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
Inc/
)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
)
target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE
Catch2::Catch2
)
include(CTest)
include(Catch)
catch_discover_tests(${CMAKE_PROJECT_NAME})
All my host’s build and compiler settings/flags etc. are overwritten. How can I ‘restart’ with defaults (by searching the default Compiler, here VS2022 used, or even Clang) and Ninja as generator? How can I add the main project’s include and src (e.g. Src/cool_stuff.cpp
) path to my_tests project? My test depends on some headers and sources from that.
The tests shall be compiled on each build of the embedded target into the same build folder as set from command line’s CMake call during configure time.