From 0bc40f92221e5e8c669e2409ff3120d5c2257aec Mon Sep 17 00:00:00 2001 From: Mathias Preiner Date: Mon, 15 Oct 2018 16:46:17 -0700 Subject: [PATCH] cmake: Generate git_versioninfo.cpp on build time. (#2640) --- src/CMakeLists.txt | 2 +- src/base/CMakeLists.txt | 39 ++++----------------------------------- src/base/GitInfo.cmake.in | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 src/base/GitInfo.cmake.in diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ea7a6837..e353d53c5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -708,7 +708,7 @@ target_compile_definitions(cvc4 -D__STDC_FORMAT_MACROS ) # Add libcvc4 dependencies for generated sources. -add_dependencies(cvc4 gen-expr gen-options gen-tags gen-theory) +add_dependencies(cvc4 gen-expr gen-gitinfo gen-options gen-tags gen-theory) # Add library/include dependencies if(ENABLE_VALGRIND) diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 532ed475d..db8eb8c36 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -1,44 +1,13 @@ #-----------------------------------------------------------------------------# # Extract info from Git for git_versioninfo.cpp +# Note: GitInfo.cmake generates git_versioninfo.cpp. find_package(Git) - -set(GIT_BUILD "false") -set(GIT_IS_DIRTY "false") -set(GIT_SHA1 "") -set(GIT_BRANCH "") - -if(GIT_FOUND) - # Get current git branch, result is != 0 if this is not a git repository - execute_process( - COMMAND ${GIT_EXECUTABLE} -C ${PROJECT_SOURCE_DIR} rev-parse --abbrev-ref HEAD - RESULT_VARIABLE GIT_RESULT - OUTPUT_VARIABLE GIT_BRANCH - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - if("${GIT_RESULT}" STREQUAL "0") - set(GIT_BUILD "true") - # Extract sha1 of HEAD - execute_process( - COMMAND ${GIT_EXECUTABLE} -C ${PROJECT_SOURCE_DIR} rev-parse HEAD - OUTPUT_VARIABLE GIT_SHA1 - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - # Result is != 0 if worktree is dirty - execute_process( - COMMAND ${GIT_EXECUTABLE} -C ${PROJECT_SOURCE_DIR} diff --quiet - RESULT_VARIABLE GIT_RESULT - ) - if(NOT "${GIT_RESULT}" STREQUAL "0") - set(GIT_IS_DIRTY "true") - endif() - endif() -endif() +configure_file(GitInfo.cmake.in GitInfo.cmake @ONLY) +add_custom_target(gen-gitinfo + COMMAND ${CMAKE_COMMAND} -DGIT_FOUND=${GIT_FOUND} -P GitInfo.cmake) #-----------------------------------------------------------------------------# -# Generate git_versioninfo.cpp - -configure_file(git_versioninfo.cpp.in git_versioninfo.cpp) libcvc4_add_sources( configuration.cpp diff --git a/src/base/GitInfo.cmake.in b/src/base/GitInfo.cmake.in new file mode 100644 index 000000000..2d02095dd --- /dev/null +++ b/src/base/GitInfo.cmake.in @@ -0,0 +1,35 @@ +set(GIT_BUILD "false") +set(GIT_IS_DIRTY "false") +set(GIT_SHA1 "") +set(GIT_BRANCH "") + +if(GIT_FOUND) + # Get current git branch, result is != 0 if this is not a git repository + execute_process( + COMMAND @GIT_EXECUTABLE@ -C @PROJECT_SOURCE_DIR@ rev-parse --abbrev-ref HEAD + RESULT_VARIABLE GIT_RESULT + OUTPUT_VARIABLE GIT_BRANCH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if("${GIT_RESULT}" STREQUAL "0") + set(GIT_BUILD "true") + # Extract sha1 of HEAD + execute_process( + COMMAND @GIT_EXECUTABLE@ -C @PROJECT_SOURCE_DIR@ rev-parse HEAD + OUTPUT_VARIABLE GIT_SHA1 + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + # Result is != 0 if worktree is dirty + # Note: git diff HEAD shows both staged and unstaged changes. + execute_process( + COMMAND @GIT_EXECUTABLE@ -C @PROJECT_SOURCE_DIR@ diff HEAD --quiet + RESULT_VARIABLE GIT_RESULT + ) + if(NOT "${GIT_RESULT}" STREQUAL "0") + set(GIT_IS_DIRTY "true") + endif() + endif() +endif() + +configure_file( + @CMAKE_CURRENT_SOURCE_DIR@/git_versioninfo.cpp.in git_versioninfo.cpp) -- 2.30.2