From 079035dbf4890e667c5e931295a1c24f28d9c0df Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 15 Feb 2026 17:54:30 +0100 Subject: build: Add git hash to version string Embed git commit hash into version.h and irmd --version output using git describe. Regenerated at build time to stay current across commits. Ouroboros version MAJOR.MINOR.PATCH (TAG-COMMITS-GHASH-dirty) Example for dirty work tree (uncommitted changes): $ irmd --version Ouroboros version 0.22.0 (0.22.0-36-g86dba544-dirty) Example after commit: $ sudo irmd --version Ouroboros version 0.22.0-37-g55fa9445 Officical release (on tag): Ouroboros version 0.22.0 Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- cmake/include.cmake | 3 --- cmake/utils/GenVersionHeader.cmake | 11 +++++++++++ cmake/utils/GetGitHash.cmake | 16 ++++++++++++++++ cmake/version.cmake | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 cmake/utils/GenVersionHeader.cmake create mode 100644 cmake/utils/GetGitHash.cmake (limited to 'cmake') diff --git a/cmake/include.cmake b/cmake/include.cmake index ace51d0f..414ea0ad 100644 --- a/cmake/include.cmake +++ b/cmake/include.cmake @@ -2,9 +2,6 @@ set(HEADERS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/include/ouroboros") # SOCK_BUF_SIZE is in cmake/config/global.cmake -configure_file("${CMAKE_SOURCE_DIR}/include/ouroboros/version.h.in" - "${CMAKE_BINARY_DIR}/include/ouroboros/version.h" @ONLY) - configure_file("${CMAKE_SOURCE_DIR}/include/ouroboros/sockets.h.in" "${CMAKE_BINARY_DIR}/include/ouroboros/sockets.h" @ONLY) diff --git a/cmake/utils/GenVersionHeader.cmake b/cmake/utils/GenVersionHeader.cmake new file mode 100644 index 00000000..5f5821b9 --- /dev/null +++ b/cmake/utils/GenVersionHeader.cmake @@ -0,0 +1,11 @@ +include(${CMAKE_CURRENT_LIST_DIR}/GetGitHash.cmake) +get_git_hash(${GIT_DIR} ${PACKAGE_VERSION_MAJOR} ${PACKAGE_VERSION_MINOR} ${PACKAGE_VERSION_PATCH} PACKAGE_VERSION_STRING) + +configure_file(${INPUT_FILE} ${OUTPUT_FILE}.tmp @ONLY) + +execute_process( + COMMAND ${CMAKE_COMMAND} -E copy_if_different + ${OUTPUT_FILE}.tmp ${OUTPUT_FILE} +) + +file(REMOVE ${OUTPUT_FILE}.tmp) diff --git a/cmake/utils/GetGitHash.cmake b/cmake/utils/GetGitHash.cmake new file mode 100644 index 00000000..8a9be41d --- /dev/null +++ b/cmake/utils/GetGitHash.cmake @@ -0,0 +1,16 @@ +function(get_git_hash WORKING_DIR VERSION_MAJ VERSION_MIN VERSION_PAT OUTPUT_VAR) + execute_process( + COMMAND git describe --always --dirty + WORKING_DIRECTORY ${WORKING_DIR} + OUTPUT_VARIABLE _hash + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + + if(NOT _hash) + message(WARNING "Could not determine git hash") + set(_hash "${VERSION_MAJ}.${VERSION_MIN}.${VERSION_PAT}-custom") + endif() + + set(${OUTPUT_VAR} "${_hash}" PARENT_SCOPE) +endfunction() diff --git a/cmake/version.cmake b/cmake/version.cmake index 00183d34..1998e874 100644 --- a/cmake/version.cmake +++ b/cmake/version.cmake @@ -3,3 +3,21 @@ set(PACKAGE_VERSION_MINOR 22) set(PACKAGE_VERSION_PATCH 0) set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}") + +include(utils/GetGitHash) +get_git_hash(${CMAKE_SOURCE_DIR} ${PACKAGE_VERSION_MAJOR} ${PACKAGE_VERSION_MINOR} ${PACKAGE_VERSION_PATCH} PACKAGE_VERSION_STRING) + +configure_file("${CMAKE_SOURCE_DIR}/include/ouroboros/version.h.in" + "${CMAKE_BINARY_DIR}/include/ouroboros/version.h" @ONLY) + +add_custom_target(version_header ALL + COMMAND ${CMAKE_COMMAND} + -DGIT_DIR=${CMAKE_SOURCE_DIR} + -DINPUT_FILE=${CMAKE_SOURCE_DIR}/include/ouroboros/version.h.in + -DOUTPUT_FILE=${CMAKE_BINARY_DIR}/include/ouroboros/version.h + -DPACKAGE_VERSION_MAJOR=${PACKAGE_VERSION_MAJOR} + -DPACKAGE_VERSION_MINOR=${PACKAGE_VERSION_MINOR} + -DPACKAGE_VERSION_PATCH=${PACKAGE_VERSION_PATCH} + -P ${CMAKE_SOURCE_DIR}/cmake/utils/GenVersionHeader.cmake + COMMENT "Updating git hash in version.h" +) -- cgit v1.2.3