diff options
| author | Dimitri Staessens <dimitri@ouroboros.rocks> | 2026-02-15 18:54:42 +0100 |
|---|---|---|
| committer | Sander Vrijders <sander@ouroboros.rocks> | 2026-02-18 08:02:26 +0100 |
| commit | b6b0b754d99dc2298930cb5db42950bf7c08cc08 (patch) | |
| tree | a0f9da590f7a40f4af8b818f26241fa973fa4e97 /cmake | |
| parent | 03b3f0c5345e4ddc4cdfad050e9b28e79ffa6721 (diff) | |
| download | ouroboros-b6b0b754d99dc2298930cb5db42950bf7c08cc08.tar.gz ouroboros-b6b0b754d99dc2298930cb5db42950bf7c08cc08.zip | |
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 <dimitri@ouroboros.rocks>
Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
Diffstat (limited to 'cmake')
| -rw-r--r-- | cmake/utils/GenVersionHeader.cmake | 3 | ||||
| -rw-r--r-- | cmake/utils/GetGitHash.cmake | 32 | ||||
| -rw-r--r-- | cmake/utils/ParseGitTag.cmake | 56 | ||||
| -rw-r--r-- | cmake/version.cmake | 12 |
4 files changed, 94 insertions, 9 deletions
diff --git a/cmake/utils/GenVersionHeader.cmake b/cmake/utils/GenVersionHeader.cmake index 5f5821b9..2dfe6a2b 100644 --- a/cmake/utils/GenVersionHeader.cmake +++ b/cmake/utils/GenVersionHeader.cmake @@ -1,5 +1,6 @@ include(${CMAKE_CURRENT_LIST_DIR}/GetGitHash.cmake) -get_git_hash(${GIT_DIR} ${PACKAGE_VERSION_MAJOR} ${PACKAGE_VERSION_MINOR} ${PACKAGE_VERSION_PATCH} PACKAGE_VERSION_STRING) +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) 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) diff --git a/cmake/utils/ParseGitTag.cmake b/cmake/utils/ParseGitTag.cmake new file mode 100644 index 00000000..80e47608 --- /dev/null +++ b/cmake/utils/ParseGitTag.cmake @@ -0,0 +1,56 @@ +function(parse_git_tag WORKING_DIR OUTPUT_MAJOR OUTPUT_MINOR OUTPUT_PATCH) + + # Check if we're in a git repo + execute_process( + COMMAND git rev-parse --git-dir + WORKING_DIRECTORY ${WORKING_DIR} + OUTPUT_VARIABLE _git_dir + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + + if(_git_dir) + # Get the latest version tag reachable from the current commit + execute_process( + COMMAND git describe --tags --abbrev=0 + --match "[0-9]*.[0-9]*.[0-9]*" + WORKING_DIRECTORY ${WORKING_DIR} + OUTPUT_VARIABLE _latest_tag + OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_QUIET + ) + + if(_latest_tag MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)$") + set(_major "${CMAKE_MATCH_1}") + set(_minor "${CMAKE_MATCH_2}") + set(_patch "${CMAKE_MATCH_3}") + message(STATUS "Version from git tag: ${_latest_tag}") + else() + string(ASCII 27 _esc) + set(_W "${_esc}[38;5;208m") + set(_R "${_esc}[0m") + message(STATUS "${_W}WARNING: No version tags found. " + "Try: git fetch --tags${_R}") + endif() + elseif(EXISTS "${WORKING_DIR}/VERSION") + # Not a git repo, try VERSION file (release tarball / git archive) + file(READ "${WORKING_DIR}/VERSION" _ver) + string(STRIP "${_ver}" _ver) + if(_ver MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)") + set(_major "${CMAKE_MATCH_1}") + set(_minor "${CMAKE_MATCH_2}") + set(_patch "${CMAKE_MATCH_3}") + message(STATUS "Version from VERSION file: ${_ver}") + endif() + endif() + + if(NOT DEFINED _major) + set(_major "0") + set(_minor "0") + set(_patch "0") + endif() + + set(${OUTPUT_MAJOR} "${_major}" PARENT_SCOPE) + set(${OUTPUT_MINOR} "${_minor}" PARENT_SCOPE) + set(${OUTPUT_PATCH} "${_patch}" PARENT_SCOPE) +endfunction() diff --git a/cmake/version.cmake b/cmake/version.cmake index 518fa5bf..75a4ac59 100644 --- a/cmake/version.cmake +++ b/cmake/version.cmake @@ -1,11 +1,15 @@ -set(PACKAGE_VERSION_MAJOR 0) -set(PACKAGE_VERSION_MINOR 22) -set(PACKAGE_VERSION_PATCH 0) +include(utils/ParseGitTag) + +# Parse version from git tag or use custom version if unavailable +parse_git_tag(${CMAKE_SOURCE_DIR} PACKAGE_VERSION_MAJOR PACKAGE_VERSION_MINOR + PACKAGE_VERSION_PATCH) + 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) +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) |
