Skip to content

Commit

Permalink
CMake: Improve handling of git hash/describe (#7028)
Browse files Browse the repository at this point in the history
Only check for and depend on the .git folder if requested.
Only warn about disabling when enabled.
Only add git hash to version if valid.
  • Loading branch information
Flamefire committed Dec 2, 2023
1 parent a15a7ce commit ed5ab54
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,22 @@ list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
################################################################################
include(${PROJECT_SOURCE_DIR}/cmake/git_utils.cmake)
macro(disable_git_describe)
message(WARNING "Disabling Z3_INCLUDE_GIT_DESCRIBE")
set(Z3_INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE)
if(Z3_INCLUDE_GIT_DESCRIBE)
message(WARNING "Disabling Z3_INCLUDE_GIT_DESCRIBE")
set(Z3_INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE)
endif()
endmacro()
macro(disable_git_hash)
message(WARNING "Disabling Z3_INCLUDE_GIT_HASH")
set(Z3_INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE)
unset(Z3GITHASH) # Used in configure_file()
if(Z3_INCLUDE_GIT_HASH)
message(WARNING "Disabling Z3_INCLUDE_GIT_HASH")
set(Z3_INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE)
endif()
endmacro()
option(Z3_INCLUDE_GIT_HASH "Include git hash in version output" ON)
option(Z3_INCLUDE_GIT_DESCRIBE "Include git describe output in version output" ON)

set(GIT_DIR "${PROJECT_SOURCE_DIR}/.git")
if (EXISTS "${GIT_DIR}")
if ((Z3_INCLUDE_GIT_HASH OR Z3_INCLUDE_GIT_HASH) AND EXISTS "${GIT_DIR}")
# Try to make CMake configure depend on the current git HEAD so that
# a re-configure is triggered when the HEAD changes.
add_git_dir_dependency("${GIT_DIR}" ADD_GIT_DEP_SUCCESS)
Expand All @@ -63,13 +66,13 @@ if (EXISTS "${GIT_DIR}")
if (NOT Z3GITHASH)
message(WARNING "Failed to get Git hash")
disable_git_hash()
else()
message(STATUS "Using Git hash in version output: ${Z3GITHASH}")
# This mimics the behaviour of the old build system.
set(Z3_FULL_VERSION_STR "${Z3_FULL_VERSION_STR} ${Z3GITHASH}")
endif()
message(STATUS "Using Git hash in version output: ${Z3GITHASH}")
# This mimics the behaviour of the old build system.
set(Z3_FULL_VERSION_STR "${Z3_FULL_VERSION_STR} ${Z3GITHASH}")
else()
message(STATUS "Not using Git hash in version output")
unset(Z3GITHASH) # Used in configure_file()
endif()
if (Z3_INCLUDE_GIT_DESCRIBE)
get_git_head_describe("${GIT_DIR}" Z3_GIT_DESCRIPTION)
Expand All @@ -93,6 +96,9 @@ else()
disable_git_describe()
disable_git_hash()
endif()
if(NOT Z3_INCLUDE_GIT_HASH)
unset(Z3GITHASH) # Used in configure_file()
endif()

################################################################################
# Useful CMake functions/Macros
Expand Down

0 comments on commit ed5ab54

Please sign in to comment.