cmake: handle the version robustly and re-run configure on git changes

A source tarball has no .git directory, so git describe never runs and
PACKAGE_VERSION stayed empty - CPackDeb then aborted with "Debian package
requires a package version" (OpenHantek/OpenHantek6022#457). Provide a
release version from project() as a fallback, quote the string(FIND) calls
so an empty describe string cannot abort the configure, and never let an
empty VERSION unset PACKAGE_VERSION.

Also put HEAD, index and packed-refs into CMAKE_CONFIGURE_DEPENDS: VERSION
is baked into the compile definitions at configure time, so without it
--version kept reporting the commit the build tree was created from, no
matter how often the working tree moved on.

Signed-off-by: Martin Homuth-Rosemann <Ho-Ro@users.noreply.github.com>
This commit is contained in:
Martin Homuth-Rosemann 2026-10-08 15:26:30 +02:00
parent 4abdcce9be
commit 01e099121b
2 changed files with 39 additions and 6 deletions

View file

@ -1,3 +1,4 @@
2026-05-03 (4abdcce): update README; new icon (thx MantsoftCR); update deb dependencies
2026-04-29 (95f11e6): Add persistent font weight option
2026-04-27 (e97c9a7): update translations
2026-04-25 (cf3b0db): fix: reconnect scope after USB disconnect, close #467

View file

@ -1,8 +1,18 @@
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(OpenHantek)
# Release version. It is also the fallback when there is no usable git
# description: a source tarball has no .git directory, so the `git describe`
# block below never runs. Without a fallback PACKAGE_VERSION stays empty and
# CPackDeb aborts with "Debian package requires a package version"
# (OpenHantek/OpenHantek6022#457). Keep it in sync with the release tag.
project(OpenHantek VERSION 3.4.1)
find_package(Git)
# Overridden by `git describe` below whenever that succeeds.
set( VERSION "${PROJECT_VERSION}" )
set( PACKAGE_VERSION "${PROJECT_VERSION}" )
if (GIT_EXECUTABLE AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
# show modified files
execute_process(
@ -16,9 +26,9 @@ if (GIT_EXECUTABLE AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
OUTPUT_VARIABLE VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# devdrop or release?
string(FIND ${VERSION} "devdrop-0-g" DEVDROP_POS)
string(FIND ${VERSION} "-0-g" ZERO_POS)
# devdrop or release? (an empty VERSION means `git describe` failed above)
string(FIND "${VERSION}" "devdrop-0-g" DEVDROP_POS)
string(FIND "${VERSION}" "-0-g" ZERO_POS)
if( ${DEVDROP_POS} EQUAL 0 ) # "x.y.z-p-gxxxxxxx(-dirty)-devdrop"
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --long --dirty
@ -43,7 +53,29 @@ if (GIT_EXECUTABLE AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
set( PACKAGE_VERSION ${VERSION} )
if( VERSION )
set( PACKAGE_VERSION "${VERSION}" )
endif()
# Re-run configure automatically when the git state changes. Otherwise VERSION is
# baked into the compile definitions at configure time and `--version` keeps
# reporting the commit that was checked out when the build tree was created,
# no matter how often the user commits or switches branches afterwards.
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --absolute-git-dir
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE OH_GIT_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE OH_GIT_DIR_RESULT
)
if( OH_GIT_DIR_RESULT EQUAL 0 )
foreach( OH_GIT_STATE_FILE HEAD index packed-refs )
if( EXISTS "${OH_GIT_DIR}/${OH_GIT_STATE_FILE}" )
set_property( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
"${OH_GIT_DIR}/${OH_GIT_STATE_FILE}" )
endif()
endforeach()
endif()
endif()
message(STATUS "PACKAGE_VERSION: ${PACKAGE_VERSION}")
@ -79,7 +111,7 @@ endif()
# 'add_compile_definitions()' was introduced with CMake 3.12
add_compile_definitions( _GLIBCXX_ASSERTIONS )
add_compile_definitions( _USE_MATH_DEFINES )
if ( DEFINED VERSION )
if ( VERSION )
add_compile_definitions( VERSION="${VERSION}" )
endif()