From b6b0b754d99dc2298930cb5db42950bf7c08cc08 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Sun, 15 Feb 2026 18:54:42 +0100 Subject: build: Automatically parse version from tag The build will now parse tags so the git tag is now the single source of truth for a version. We do not need to manually maintain the version header. The VERSION file uses git's export-subst feature. When git archive creates a snapshot (e.g. cgit), git substitutes the $Format:...$ placeholder with the output of git describe, embedding the version string directly in the archive. This requires the export-subst attribute to be set for the VERSION file in the repository's .gitattributes. The version is resolved in the following order: - git describe (inside a git repository with tags) - the VERSION file (archives/snapshots) - 0.0.0 (fallback) The build system will warn when it can't set a correct version, such as in a git repo without tags, or an archive without VERSION. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- cmake/utils/GetGitHash.cmake | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) (limited to 'cmake/utils/GetGitHash.cmake') diff --git a/cmake/utils/GetGitHash.cmake b/cmake/utils/GetGitHash.cmake index 8a9be41d..e399216d 100644 --- a/cmake/utils/GetGitHash.cmake +++ b/cmake/utils/GetGitHash.cmake @@ -1,15 +1,39 @@ function(get_git_hash WORKING_DIR VERSION_MAJ VERSION_MIN VERSION_PAT OUTPUT_VAR) execute_process( - COMMAND git describe --always --dirty + COMMAND git describe --tags --always --dirty + --match "[0-9]*.[0-9]*.[0-9]*" 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") + if(_hash MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+") + # git describe returned a tag-based version string + elseif(_hash) + # No version tag found, construct full version string + execute_process( + COMMAND git rev-list --count HEAD + WORKING_DIRECTORY ${WORKING_DIR} + OUTPUT_VARIABLE _count + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + execute_process( + COMMAND git describe --always --dirty + WORKING_DIRECTORY ${WORKING_DIR} + OUTPUT_VARIABLE _desc + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + set(_hash + "${VERSION_MAJ}.${VERSION_MIN}.${VERSION_PAT}-${_count}-g${_desc}") + elseif(EXISTS "${WORKING_DIR}/VERSION") + # No git, use VERSION file (git archive / cgit snapshot) + file(READ "${WORKING_DIR}/VERSION" _hash) + string(STRIP "${_hash}" _hash) + else() + set(_hash "${VERSION_MAJ}.${VERSION_MIN}.${VERSION_PAT}") endif() set(${OUTPUT_VAR} "${_hash}" PARENT_SCOPE) -- cgit v1.2.3