From c91a78a91dfaff25f9e40d3976b2ec0480769200 Mon Sep 17 00:00:00 2001 From: robtor Date: Wed, 18 Jun 2025 13:13:14 +0200 Subject: [PATCH] Update presets and integrate testing concept --- .gitignore | 2 +- CMakeLists.txt | 6 +++-- CMakePresets.json | 50 ++++++++++++++++++++++++++++++++--- Tests/CMakeLists.txt | 10 +++++-- floatpump/CMake/githash.cmake | 14 ++++++++++ floatpump/CMakeLists.txt | 32 +++++++++------------- 6 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 floatpump/CMake/githash.cmake diff --git a/.gitignore b/.gitignore index e5951c4..84c048a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/.out/ +/build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 41d8412..19dbc81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,11 +10,13 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_C_STANDARD 11) # General Project -add_subdirectory(floatpump) +if(NOT ENABLE_UNITTESTS) + add_subdirectory(floatpump) +endif () # Testing Project if(ENABLE_UNITTESTS) message(STATUS "Enabled Unit Tests") - add_subirectory(Catch2) + add_subdirectory(Catch2) add_subdirectory(Tests) endif() \ No newline at end of file diff --git a/CMakePresets.json b/CMakePresets.json index 7f3442b..39f0172 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -1,4 +1,9 @@ { + "version": 10, + "cmakeMinimumRequired": { + "major": 3, + "minor": 22 + }, "configurePresets": [ { "name": "debug@firmware", @@ -10,24 +15,61 @@ }, "binaryDir": "build/debug@firmware" }, + { + "name": "release@firmware", + "description": "Firmware Release Build", + "generator": "Ninja", + "cacheVariables": { + "CMAKE_TOOLCHAIN_FILE": "toolchain/arm-none-eabi-stm32f411ceu6-toolchain.cmake", + "CMAKE_BUILD_TYPE": "Release" + }, + "binaryDir": "build/release@firmware" + }, + { + "name": "minrelease@firmware", + "description": "Firmware Minimized Release Build", + "inherits": ["release@firmware"], + "cacheVariables": { + "CMAKE_BUILD_TYPE": "MinSizeRel" + }, + "binaryDir": "build/minrelease@firmware" + }, { "name": "debug@tests", - "description": "UnitTests Debug Buil", + "description": "UnitTests Debug Build", "generator": "Ninja", "cacheVariables": { "ENABLE_UNITTESTS": "ON" - } + }, + "binaryDir": "build/debug@tests" } ], "buildPresets": [ { - "name": "build-debug@firmware", + "name": "build-debug", "description": "Builds the debugging firmware", "cleanFirst": false, - "configuration": "debug@firmware", + "configurePreset": "debug@firmware", "targets": [ "FloatPUMP.elf" ] + }, + { + "name": "build-release", + "description": "Builds the normal release firmware", + "cleanFirst": true, + "configurePreset": "minrelease@firmware", + "targets": [ + "FloatPUMP.elf" + ] + }, + { + "name": "build-tests", + "description": "Creates a debug build of all tests", + "configurePreset": "debug@tests", + "targets": [ + "testtests" + ] } ] } \ No newline at end of file diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 9b96e29..534e69c 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1,4 +1,10 @@ -find_package(Catch2 REQUIRED) +# Catch 2 is already loaded from the main CMakeLists.txt as a subdirectory add_executable(testtests experiments/example-test.cpp) -target_link_libraries(testtests PRIVATE Catch2::Catch2WithMain) \ No newline at end of file +target_link_libraries(testtests PRIVATE Catch2::Catch2WithMain) + +# General catch configuration +include(CTest) +include(Catch) + +catch_discover_tests(testtests) \ No newline at end of file diff --git a/floatpump/CMake/githash.cmake b/floatpump/CMake/githash.cmake new file mode 100644 index 0000000..f03bef3 --- /dev/null +++ b/floatpump/CMake/githash.cmake @@ -0,0 +1,14 @@ +# Obtain the short git hash for burning it into the firmware +set(GIT_HASH "undefined") +find_package(Git QUIET) +if(GIT_FOUND) + execute_process( + COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h + OUTPUT_VARIABLE GIT_HASH + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + message(STATUS "Burning in git hash: ${GIT_HASH}") +else () + message(WARNING "Cannot obtain git hash for burning into firmware") +endif() \ No newline at end of file diff --git a/floatpump/CMakeLists.txt b/floatpump/CMakeLists.txt index 05b91df..3b94eb2 100644 --- a/floatpump/CMakeLists.txt +++ b/floatpump/CMakeLists.txt @@ -1,22 +1,6 @@ -set(GIT_HASH "undefined") -find_package(Git QUIET) -if(GIT_FOUND) - execute_process( - COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h - OUTPUT_VARIABLE GIT_HASH - OUTPUT_STRIP_TRAILING_WHITESPACE - ERROR_QUIET - ) -endif() - -message(STATUS "Burning in git hash: ${GIT_HASH}") - -add_definitions("-DGIT_HASH=\"${GIT_HASH}\"") - -#include(${CMAKE_CURRENT_LIST_DIR}/../toolchain/arm-none-eabi-stm32f411ceu6-toolchain.cmake) -# uncomment to mitigate c++17 absolute addresses warnings -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register") +include(CMake/githash.cmake) +# All required source definitions file(GLOB_RECURSE SOURCES "Core/*.*" "Middlewares/*.*" @@ -24,9 +8,17 @@ file(GLOB_RECURSE SOURCES "USB_DEVICE/*.*" ) -message(WARNING "linker script is : ${LINKER_SCRIPT}") +# Check if the correct toolchain was used and the LINKER_SCRIPT is defined +if(NOT DEFINED LINKER_SCRIPT) + message(FATAL_ERROR "You need to define the LINKER_SCRIPT variable from within the toolchain for the correct MCU") +endif() -add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) +add_executable(FloatPUMP.elf ${SOURCES} ${LINKER_SCRIPT}) + +# Custom target options +target_compile_definitions(FloatPUMP.elf PUBLIC + "-DGIT_HASH=\"${GIT_HASH}\"" +) target_include_directories(${PROJECT_NAME}.elf PUBLIC USB_DEVICE/App