mirror of
https://github.com/Z3Prover/z3
synced 2025-08-25 04:26:00 +00:00
[CMake] Support including Git hash and description into the build.
CMake will automatically pick up changes in git's HEAD so that the necessary code is rebuilt when the build system is invoked. Two new options `INCLUDE_GIT_HASH` and `INCLUDE_GIT_DESCRIBE` have been added that enable/disable including the git hash and the output of `git describe` respectively. By default if the source tree is a git repository both options are on, otherwise they are false by default. To support the `Z3GITHASH` macro a different implementation is used from the old build system. In that build system the define is passed on the command line. This would not work well for CMake because CMake conservatively (and correctly) rebuilds *everything* if the flags given to the compiler change. This would result in the entire project being rebuilt everytime git's `HEAD` changed. Instead in this implementation a CMake specific version of `version.h.in` (named `version.h.cmake.in`) is added that uses the `#cmakedefine` feature of CMake's `configure_file()` command to define `Z3GITHASH` if it is available and not define it otherwise. This way only object files that depend on `version.h` get re-built rather than the whole project. It is unfortunate that the build systems now have different `version.h` file templates. However they are very simple and I don't want to modify how templates are handled in the python/Makefile build system.
This commit is contained in:
parent
1f4f4514bf
commit
2cb4223979
5 changed files with 245 additions and 2 deletions
|
@ -35,8 +35,8 @@ set(Z3_VERSION_MAJOR 4)
|
|||
set(Z3_VERSION_MINOR 5)
|
||||
set(Z3_VERSION_PATCH 1)
|
||||
set(Z3_VERSION_TWEAK 0)
|
||||
set(Z3_FULL_VERSION 0)
|
||||
set(Z3_VERSION "${Z3_VERSION_MAJOR}.${Z3_VERSION_MINOR}.${Z3_VERSION_PATCH}.${Z3_VERSION_TWEAK}")
|
||||
set(Z3_FULL_VERSION_STR "${Z3_VERSION}") # Note this might be modified
|
||||
message(STATUS "Z3 version ${Z3_VERSION}")
|
||||
|
||||
################################################################################
|
||||
|
@ -75,6 +75,64 @@ endif()
|
|||
################################################################################
|
||||
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
|
||||
|
||||
################################################################################
|
||||
# Handle git hash and description
|
||||
################################################################################
|
||||
include(${CMAKE_SOURCE_DIR}/cmake/git_utils.cmake)
|
||||
macro(disable_git_describe)
|
||||
message(WARNING "Disabling INCLUDE_GIT_DESCRIBE")
|
||||
set(INCLUDE_GIT_DESCRIBE OFF CACHE BOOL "Include git describe output in version output" FORCE)
|
||||
endmacro()
|
||||
macro(disable_git_hash)
|
||||
message(WARNING "Disabling INCLUDE_GIT_HASH")
|
||||
set(INCLUDE_GIT_HASH OFF CACHE BOOL "Include git hash in version output" FORCE)
|
||||
unset(Z3GITHASH) # Used in configure_file()
|
||||
endmacro()
|
||||
option(INCLUDE_GIT_HASH "Include git hash in version output" ON)
|
||||
option(INCLUDE_GIT_DESCRIBE "Include git describe output in version output" ON)
|
||||
|
||||
set(GIT_DIR "${CMAKE_SOURCE_DIR}/.git")
|
||||
if (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)
|
||||
if (ADD_GIT_DEP_SUCCESS)
|
||||
if (INCLUDE_GIT_HASH)
|
||||
get_git_head_hash("${GIT_DIR}" Z3GITHASH)
|
||||
if (NOT Z3GITHASH)
|
||||
message(WARNING "Failed to get Git hash")
|
||||
disable_git_hash()
|
||||
endif()
|
||||
message(STATUS "Using Git hash in version output: ${Z3GITHASH}")
|
||||
# This mimics the behaviour of the old build system.
|
||||
string(APPEND Z3_FULL_VERSION_STR " ${Z3GITHASH}")
|
||||
else()
|
||||
message(STATUS "Not using Git hash in version output")
|
||||
unset(Z3GITHASH) # Used in configure_file()
|
||||
endif()
|
||||
if (INCLUDE_GIT_DESCRIBE)
|
||||
get_git_head_describe("${GIT_DIR}" Z3_GIT_DESCRIPTION)
|
||||
if (NOT Z3_GIT_DESCRIPTION)
|
||||
message(WARNING "Failed to get Git description")
|
||||
disable_git_describe()
|
||||
endif()
|
||||
message(STATUS "Using Git description in version output: ${Z3_GIT_DESCRIPTION}")
|
||||
# This mimics the behaviour of the old build system.
|
||||
string(APPEND Z3_FULL_VERSION_STR " ${Z3_GIT_DESCRIPTION}")
|
||||
else()
|
||||
message(STATUS "Not including git descrption in version")
|
||||
endif()
|
||||
else()
|
||||
message(WARNING "Failed to add git dependency.")
|
||||
disable_git_describe()
|
||||
disable_git_hash()
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "Failed to find git directory.")
|
||||
disable_git_describe()
|
||||
disable_git_hash()
|
||||
endif()
|
||||
|
||||
################################################################################
|
||||
# Useful CMake functions/Macros
|
||||
################################################################################
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue