From: Joel Rosdahl Date: Mon, 31 Aug 2020 08:30:38 +0000 (+0200) Subject: Improve handling of ccache version in the source release archive X-Git-Tag: v4.0~148 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cb407bc07239a305a2ec39fdb85024f1c4871005;p=thirdparty%2Fccache.git Improve handling of ccache version in the source release archive The version.cpp file is currently generated in the source directory. This is a bit unclean since generated files are supposed to be put in the build directory. Also, when building from a source release archive outside a Git repository the CMake scripts still try to get the version from Git and then emit an ugly warning message: fatal: not a git repository (or any of the parent directories): .git fatal: not a git repository (or any of the parent directories): .git CMake Warning at cmake/GenerateVersionFile.cmake:42 (message): Running git failed Call Stack (most recent call first): cmake/GenerateVersionFile.cmake:50 (get_version_from_git) CMakeLists.txt:32 (include) Fix this by including a VERSION file in the source release archive. If the VERSION file exists, GenerateVersionFile.cmake just uses the version from the file and doesn’t try to get a version from Git. If the file doesn’t exist, the version is looked up from Git like before, but it’s a fatal error if the version cannot be determined. --- diff --git a/cmake/CcachePackConfig.cmake b/cmake/CcachePackConfig.cmake index 601a1a773..71b3d7bb7 100644 --- a/cmake/CcachePackConfig.cmake +++ b/cmake/CcachePackConfig.cmake @@ -28,4 +28,16 @@ set( ) set(CPACK_SOURCE_PACKAGE_FILE_NAME "ccache-${VERSION}") +configure_file( + ${CMAKE_SOURCE_DIR}/cmake/PreparePackage.cmake.in + ${CMAKE_BINARY_DIR}/PreparePackage.cmake + @ONLY +) + +if(${CMAKE_VERSION} VERSION_LESS "3.16") + set(CPACK_INSTALL_SCRIPT ${CMAKE_BINARY_DIR}/PreparePackage.cmake) +else() + set(CPACK_INSTALL_SCRIPTS ${CMAKE_BINARY_DIR}/PreparePackage.cmake) +endif() + include(CPack) diff --git a/cmake/GenerateVersionFile.cmake b/cmake/GenerateVersionFile.cmake index 83b9558e5..37baa9ee5 100644 --- a/cmake/GenerateVersionFile.cmake +++ b/cmake/GenerateVersionFile.cmake @@ -1,11 +1,11 @@ -# Determines VERSION from Git. See also VERSION_ERROR and VERSION_DIRTY. +# Determine VERSION from a Git repository. VERSION_ERROR is set to a non-empty +# string on error. function(get_version_from_git) + set(VERSION_ERROR "" PARENT_SCOPE) + find_package(Git) if(NOT GIT_FOUND) - message(STATUS "Git not found") - set(VERSION_ERROR TRUE PARENT_SCOPE) - set(VERSION_DIRTY TRUE PARENT_SCOPE) - set(VERSION "unknown" PARENT_SCOPE) + set(VERSION_ERROR "Git not found" PARENT_SCOPE) return() endif() @@ -13,46 +13,55 @@ function(get_version_from_git) COMMAND git describe --exact-match WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} OUTPUT_VARIABLE git_tag - ERROR_VARIABLE git_tag + ERROR_VARIABLE git_tag_error # silence error RESULT_VARIABLE cmd_result OUTPUT_STRIP_TRAILING_WHITESPACE) if(cmd_result EQUAL 0) - set(VERSION_ERROR FALSE PARENT_SCOPE) - set(VERSION_DIRTY FALSE PARENT_SCOPE) set(VERSION ${git_tag} PARENT_SCOPE) - else() - execute_process( - COMMAND git rev-parse --abbrev-ref HEAD - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE git_branch OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE cmd_branch_result) - - execute_process( - COMMAND git rev-parse --short=8 HEAD - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} - OUTPUT_VARIABLE git_hash OUTPUT_STRIP_TRAILING_WHITESPACE - RESULT_VARIABLE cmd_hash_result) - - if(cmd_branch_result EQUAL 0 AND cmd_hash_result EQUAL 0) - set(VERSION_ERROR FALSE PARENT_SCOPE) - set(VERSION_DIRTY TRUE PARENT_SCOPE) - set(VERSION "${git_branch}.${git_hash}" PARENT_SCOPE) - else() - message(WARNING "Running git failed") - set(VERSION_ERROR TRUE PARENT_SCOPE) - set(VERSION_DIRTY TRUE PARENT_SCOPE) - set(VERSION "unknown" PARENT_SCOPE) - endif() + return() endif() + + execute_process( + COMMAND git rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE git_branch OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_VARIABLE git_branch_error + RESULT_VARIABLE cmd_branch_result) + if(NOT cmd_branch_result EQUAL 0) + set(VERSION_ERROR "Failed to run Git: ${git_branch_error}" PARENT_SCOPE) + return() + endif() + + execute_process( + COMMAND git rev-parse --short=8 HEAD + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} + OUTPUT_VARIABLE git_hash OUTPUT_STRIP_TRAILING_WHITESPACE + ERROR_VARIABLE git_hash_error + RESULT_VARIABLE cmd_hash_result) + if(NOT cmd_hash_result EQUAL 0) + set(VERSION_ERROR "Failed to run Git: ${git_hash_error}" PARENT_SCOPE) + return() + endif() + + set(VERSION "${git_branch}.${git_hash}" PARENT_SCOPE) endfunction() -get_version_from_git() -if(VERSION_ERROR) - message(STATUS "Not within git repository") +set(version_file ${CMAKE_SOURCE_DIR}/VERSION) + +if(EXISTS ${version_file}) + file(READ ${version_file} VERSION) + string(STRIP ${VERSION} VERSION) else() - configure_file( - ${CMAKE_SOURCE_DIR}/cmake/version.cpp.in - ${CMAKE_SOURCE_DIR}/src/version.cpp - @ONLY) + get_version_from_git() + if(NOT ${VERSION_ERROR} STREQUAL "") + message(SEND_ERROR "Cannot determine ccache version: ${VERSION_ERROR}") + endif() endif() + +configure_file( + ${CMAKE_SOURCE_DIR}/cmake/version.cpp.in + ${CMAKE_BINARY_DIR}/src/version.cpp + @ONLY) + +message(STATUS "Ccache version: ${VERSION}") diff --git a/cmake/PreparePackage.cmake.in b/cmake/PreparePackage.cmake.in new file mode 100644 index 000000000..c2adfbc82 --- /dev/null +++ b/cmake/PreparePackage.cmake.in @@ -0,0 +1 @@ +file(WRITE "${CMAKE_INSTALL_PREFIX}/VERSION" "@VERSION@\n")