]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Improve handling of ccache version in the source release archive
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 31 Aug 2020 08:30:38 +0000 (10:30 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 31 Aug 2020 08:34:35 +0000 (10:34 +0200)
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.

cmake/CcachePackConfig.cmake
cmake/GenerateVersionFile.cmake
cmake/PreparePackage.cmake.in [new file with mode: 0644]

index 601a1a77370d41dabe293f164a580bfb90b67250..71b3d7bb759e614925d35fbd685069af2d4f0ef1 100644 (file)
@@ -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)
index 83b9558e500ab4a6008c747e9482d99e10835bce..37baa9ee526a8e94f8b99504327cc36dcf95c1a0 100644 (file)
@@ -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 (file)
index 0000000..c2adfbc
--- /dev/null
@@ -0,0 +1 @@
+file(WRITE "${CMAKE_INSTALL_PREFIX}/VERSION" "@VERSION@\n")