From f533f36f27ed23210c285e4b4bf590dcba9da840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Betto?= <niccolo.betto@skywarder.eu> Date: Thu, 10 Apr 2025 18:02:29 +0200 Subject: [PATCH] use cpp translation unit and linking --- cmake/sbs.cmake | 27 +++++++++++++++------------ cmake/version.cmake | 4 ++-- cmake/version.cpp.in | 19 +++++++++++++++++++ cmake/version.h.in | 18 ------------------ 4 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 cmake/version.cpp.in delete mode 100644 cmake/version.h.in diff --git a/cmake/sbs.cmake b/cmake/sbs.cmake index 9df48c04a..266519e93 100644 --- a/cmake/sbs.cmake +++ b/cmake/sbs.cmake @@ -39,30 +39,33 @@ function(sbs_target TARGET OPT_BOARD) target_include_directories(${TARGET} PRIVATE src/shared) - add_custom_target(${TARGET}-version-info + # Define version information generation command + add_custom_target(${TARGET}-version-info-gen COMMAND "${CMAKE_COMMAND}" "-D" "TARGET_NAME=${TARGET}" "-D" "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" "-D" "BOARDCORE_PATH=${BOARDCORE_PATH}" - "-D" "BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}" + "-D" "OUT_DIR=${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}" "-P" "${BOARDCORE_PATH}/cmake/version.cmake" - OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}/version.h" + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}/version.cpp" COMMENT "Generating version information file for ${TARGET}" VERBATIM ) - add_dependencies(${TARGET} ${TARGET}-version-info) + # Create the version information interface library target + # Linking with this target will add the include directory to the target + add_library(${TARGET}-version-info STATIC + ${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}/version.cpp + ) + # The version info needs to be generated before building the target + add_dependencies(${TARGET}-version-info ${TARGET}-version-info-gen) + + # Link the version information library to the target + target_link_libraries(${TARGET} PRIVATE ${TARGET}-version-info) if(CMAKE_CROSSCOMPILING) # Link the embedded Boardcore library target_link_libraries(${TARGET} PRIVATE Skyward::Boardcore::${OPT_BOARD}) - - # Add the generated include directory to the target - # This also works for injecting include directories into consumed libs - # E.g. adding version information to the kernel library - target_include_directories(${TARGET} PRIVATE - $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}> - ) - + # Linker script and linking options are inherited from the kernel library # Add a post build command to create the hex file to flash on the board diff --git a/cmake/version.cmake b/cmake/version.cmake index 9b2b977ad..f53e7b597 100644 --- a/cmake/version.cmake +++ b/cmake/version.cmake @@ -88,6 +88,6 @@ set(GIT_VERSION_STRING "${GIT_VERSION_INFO}${GIT_DIRTY_SUFFIX}") # - CMAKE_BUILD_TYPE configure_file( - "${BOARDCORE_PATH}/cmake/version.h.in" - "${BIN_DIR}/version.h" + "${BOARDCORE_PATH}/cmake/version.cpp.in" + "${OUT_DIR}/version.cpp" ) diff --git a/cmake/version.cpp.in b/cmake/version.cpp.in new file mode 100644 index 000000000..008d4a909 --- /dev/null +++ b/cmake/version.cpp.in @@ -0,0 +1,19 @@ +#pragma once + +// This file is generated by CMake. Do not edit it directly. +// See cmake/version.cmake for details. + +constexpr auto GIT_REV = "@GIT_REV@@GIT_DIRTY_SUFFIX@"; +constexpr auto GIT_TAG = "@GIT_TAG@"; +constexpr auto GIT_BRANCH = "@GIT_BRANCH@"; + +constexpr auto GIT_VERSION_STRING = "@GIT_VERSION_STRING@"; + +constexpr auto TARGET_NAME = "@TARGET_NAME@"; +constexpr auto BUILD_TYPE = "@CMAKE_BUILD_TYPE@"; + +constexpr auto VERSION_STRING = "@TARGET_NAME@ @ @GIT_VERSION_STRING@"; + +constexpr auto SKYWARD_VERSION_STRING = + "Skyward Eperimental Rocketry (c) @TARGET_NAME@ @ @GIT_VERSION_STRING@ " + "(@CMAKE_BUILD_TYPE@)"; diff --git a/cmake/version.h.in b/cmake/version.h.in deleted file mode 100644 index 69b974da6..000000000 --- a/cmake/version.h.in +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -// This file is generated by CMake. Do not edit it directly. -// See cmake/version.cmake for details. - -#define GIT_REV "@GIT_REV@@GIT_DIRTY_SUFFIX@" -#define GIT_TAG "@GIT_TAG@" -#define GIT_BRANCH "@GIT_BRANCH@" - -#define GIT_VERSION_STRING "@GIT_VERSION_STRING@" - -#define TARGET_NAME "@TARGET_NAME@" -#define BUILD_TYPE "@CMAKE_BUILD_TYPE@" - -#define VERSION_STRING "@TARGET_NAME@ @ @GIT_VERSION_STRING@" - -#define SKYWARD_VERSION_STRING \ - "Skyward Eperimental Rocketry (c) " VERSION_STRING " (" BUILD_TYPE ")" -- GitLab