From ed5ab54ab60657f7e646346860c005889ae18fa5 Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Sat, 2 Dec 2023 18:47:57 +0100 Subject: [PATCH] CMake: Improve handling of git hash/describe (#7028) 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. --- CMakeLists.txt | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8144e132887..0ab59f18f46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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) @@ -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