CMake: Avoid call of find_package(Git) for every compile with buildinfo enabled
authorSergey Sharybin <sergey.vfx@gmail.com>
Fri, 11 Mar 2016 08:05:44 +0000 (13:05 +0500)
committerSergey Sharybin <sergey.vfx@gmail.com>
Fri, 11 Mar 2016 09:46:18 +0000 (14:46 +0500)
This is a bit tricky, find_package(Git) was only used to check if git is installed
and actual git command was assumed to be in the PATH (while ideally it should have
been GIT_COMMAND variable).

This commit makes re-compile output cleaner, especially when using Ninja.

Adding proper GIT_COMMAND to buildinfo.cmake is also possible via argument.

Reviewers: campbellbarton

Reviewed By: campbellbarton

Differential Revision: https://developer.blender.org/D1847

CMakeLists.txt
build_files/cmake/buildinfo.cmake

index f149cfbcdc4aeeea47cf3325841a98426caa79a6..b24945d7ebdf8086e31358cb5bcaab6145e75491 100644 (file)
@@ -704,6 +704,13 @@ else()
        set(COMPILER_SSE2_FLAG)
 endif()
 
+if(WITH_BUILDINFO)
+       find_package(Git)
+       if(NOT GIT_FOUND)
+               set(WITH_BUILDINFO OFF)
+       endif()
+endif()
+
 TEST_SHARED_PTR_SUPPORT()
 TEST_UNORDERED_MAP_SUPPORT()
 
index 1f996b30ba6ea9199ca161823e78c11c9387b9ab..4caa4a954ea5b349c2a1d4628d4f8adcc3154f1d 100644 (file)
@@ -9,124 +9,120 @@ set(MY_WC_COMMIT_TIMESTAMP 0)
 
 # Guess if this is a git working copy and then look up the revision
 if(EXISTS ${SOURCE_DIR}/.git)
-       # The FindGit.cmake module is part of the standard distribution
-       find_package(Git)
-       if(GIT_FOUND)
-               execute_process(COMMAND git rev-parse --abbrev-ref HEAD
+       execute_process(COMMAND git rev-parse --abbrev-ref HEAD
+                       WORKING_DIRECTORY ${SOURCE_DIR}
+                       OUTPUT_VARIABLE MY_WC_BRANCH
+                       OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+       if(MY_WC_BRANCH STREQUAL "HEAD")
+               # Detached HEAD, check whether commit hash is reachable
+               # in the master branch
+               execute_process(COMMAND git rev-parse --short HEAD
                                WORKING_DIRECTORY ${SOURCE_DIR}
-                               OUTPUT_VARIABLE MY_WC_BRANCH
+                               OUTPUT_VARIABLE MY_WC_HASH
                                OUTPUT_STRIP_TRAILING_WHITESPACE)
 
-               if(MY_WC_BRANCH STREQUAL "HEAD")
-                       # Detached HEAD, check whether commit hash is reachable
-                       # in the master branch
-                       execute_process(COMMAND git rev-parse --short HEAD
+               execute_process(COMMAND git branch --list master --contains ${MY_WC_HASH}
+                               WORKING_DIRECTORY ${SOURCE_DIR}
+                               OUTPUT_VARIABLE _git_contains_check
+                               OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+               STRING(REGEX REPLACE "^[ \t]+" "" _git_contains_check "${_git_contains_check}")
+               if(_git_contains_check STREQUAL "master")
+                       set(MY_WC_BRANCH "master")
+               else()
+                       execute_process(COMMAND git show-ref --tags -d
                                        WORKING_DIRECTORY ${SOURCE_DIR}
-                                       OUTPUT_VARIABLE MY_WC_HASH
+                                       OUTPUT_VARIABLE _git_tag_hashes
                                        OUTPUT_STRIP_TRAILING_WHITESPACE)
 
-                       execute_process(COMMAND git branch --list master --contains ${MY_WC_HASH}
+                       execute_process(COMMAND git rev-parse HEAD
                                        WORKING_DIRECTORY ${SOURCE_DIR}
-                                       OUTPUT_VARIABLE _git_contains_check
+                                       OUTPUT_VARIABLE _git_head_hash
                                        OUTPUT_STRIP_TRAILING_WHITESPACE)
 
-                       STRING(REGEX REPLACE "^[ \t]+" "" _git_contains_check "${_git_contains_check}")
-                       if(_git_contains_check STREQUAL "master")
+                       if(_git_tag_hashes MATCHES "${_git_head_hash}")
                                set(MY_WC_BRANCH "master")
-                       else()
-                               execute_process(COMMAND git show-ref --tags -d
-                                               WORKING_DIRECTORY ${SOURCE_DIR}
-                                               OUTPUT_VARIABLE _git_tag_hashes
-                                               OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-                               execute_process(COMMAND git rev-parse HEAD
-                                               WORKING_DIRECTORY ${SOURCE_DIR}
-                                               OUTPUT_VARIABLE _git_head_hash
-                                               OUTPUT_STRIP_TRAILING_WHITESPACE)
-
-                               if(_git_tag_hashes MATCHES "${_git_head_hash}")
-                                       set(MY_WC_BRANCH "master")
-                               endif()
-
-                               unset(_git_tag_hashes)
-                               unset(_git_head_hashs)
                        endif()
 
+                       unset(_git_tag_hashes)
+                       unset(_git_head_hashs)
+               endif()
+
 
-                       unset(_git_contains_check)
+               unset(_git_contains_check)
+       else()
+               execute_process(COMMAND git log HEAD..@{u}
+                               WORKING_DIRECTORY ${SOURCE_DIR}
+                               OUTPUT_VARIABLE _git_below_check
+                               OUTPUT_STRIP_TRAILING_WHITESPACE
+                               ERROR_QUIET)
+               if(NOT _git_below_check STREQUAL "")
+                       # If there're commits between HEAD and upstream this means
+                       # that we're reset-ed to older revision. Use it's hash then.
+                       execute_process(COMMAND git rev-parse --short HEAD
+                                       WORKING_DIRECTORY ${SOURCE_DIR}
+                                       OUTPUT_VARIABLE MY_WC_HASH
+                                       OUTPUT_STRIP_TRAILING_WHITESPACE)
                else()
-                       execute_process(COMMAND git log HEAD..@{u}
+                       execute_process(COMMAND git rev-parse --short @{u}
                                        WORKING_DIRECTORY ${SOURCE_DIR}
-                                       OUTPUT_VARIABLE _git_below_check
+                                       OUTPUT_VARIABLE MY_WC_HASH
                                        OUTPUT_STRIP_TRAILING_WHITESPACE
                                        ERROR_QUIET)
-                       if(NOT _git_below_check STREQUAL "")
-                               # If there're commits between HEAD and upstream this means
-                               # that we're reset-ed to older revision. Use it's hash then.
+
+                       if(MY_WC_HASH STREQUAL "")
+                               # Local branch, not set to upstream.
+                               # Well, let's use HEAD for now
                                execute_process(COMMAND git rev-parse --short HEAD
                                                WORKING_DIRECTORY ${SOURCE_DIR}
                                                OUTPUT_VARIABLE MY_WC_HASH
                                                OUTPUT_STRIP_TRAILING_WHITESPACE)
-                       else()
-                               execute_process(COMMAND git rev-parse --short @{u}
-                                               WORKING_DIRECTORY ${SOURCE_DIR}
-                                               OUTPUT_VARIABLE MY_WC_HASH
-                                               OUTPUT_STRIP_TRAILING_WHITESPACE
-                                               ERROR_QUIET)
-
-                               if(MY_WC_HASH STREQUAL "")
-                                       # Local branch, not set to upstream.
-                                       # Well, let's use HEAD for now
-                                       execute_process(COMMAND git rev-parse --short HEAD
-                                                       WORKING_DIRECTORY ${SOURCE_DIR}
-                                                       OUTPUT_VARIABLE MY_WC_HASH
-                                                       OUTPUT_STRIP_TRAILING_WHITESPACE)
-                               endif()
-                       endif()
-
-                       if(MY_WC_BRANCH MATCHES "^blender-v")
-                               set(MY_WC_BRANCH "master")
                        endif()
-
-                       unset(_git_below_check)
                endif()
 
-               execute_process(COMMAND git log -1 --format=%ct
-                               WORKING_DIRECTORY ${SOURCE_DIR}
-                               OUTPUT_VARIABLE MY_WC_COMMIT_TIMESTAMP
-                               OUTPUT_STRIP_TRAILING_WHITESPACE)
-               # May fail in rare cases
-               if(MY_WC_COMMIT_TIMESTAMP STREQUAL "")
-                       set(MY_WC_COMMIT_TIMESTAMP 0)
+               if(MY_WC_BRANCH MATCHES "^blender-v")
+                       set(MY_WC_BRANCH "master")
                endif()
 
-               # Update GIT index before getting dirty files
-               execute_process(COMMAND git update-index -q --refresh
-                               WORKING_DIRECTORY ${SOURCE_DIR}
-                               OUTPUT_STRIP_TRAILING_WHITESPACE)
+               unset(_git_below_check)
+       endif()
 
-               execute_process(COMMAND git diff-index --name-only HEAD --
-                               WORKING_DIRECTORY ${SOURCE_DIR}
-                               OUTPUT_VARIABLE _git_changed_files
-                               OUTPUT_STRIP_TRAILING_WHITESPACE)
+       execute_process(COMMAND git log -1 --format=%ct
+                       WORKING_DIRECTORY ${SOURCE_DIR}
+                       OUTPUT_VARIABLE MY_WC_COMMIT_TIMESTAMP
+                       OUTPUT_STRIP_TRAILING_WHITESPACE)
+       # May fail in rare cases
+       if(MY_WC_COMMIT_TIMESTAMP STREQUAL "")
+               set(MY_WC_COMMIT_TIMESTAMP 0)
+       endif()
 
-               if(NOT _git_changed_files STREQUAL "")
+       # Update GIT index before getting dirty files
+       execute_process(COMMAND git update-index -q --refresh
+                       WORKING_DIRECTORY ${SOURCE_DIR}
+                       OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+       execute_process(COMMAND git diff-index --name-only HEAD --
+                       WORKING_DIRECTORY ${SOURCE_DIR}
+                       OUTPUT_VARIABLE _git_changed_files
+                       OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+       if(NOT _git_changed_files STREQUAL "")
+               set(MY_WC_BRANCH "${MY_WC_BRANCH} (modified)")
+       else()
+               # Unpushed commits are also considered local modifications
+               execute_process(COMMAND git log @{u}..
+                               WORKING_DIRECTORY ${SOURCE_DIR}
+                               OUTPUT_VARIABLE _git_unpushed_log
+                               OUTPUT_STRIP_TRAILING_WHITESPACE
+                               ERROR_QUIET)
+               if(NOT _git_unpushed_log STREQUAL "")
                        set(MY_WC_BRANCH "${MY_WC_BRANCH} (modified)")
-               else()
-                       # Unpushed commits are also considered local modifications
-                       execute_process(COMMAND git log @{u}..
-                                       WORKING_DIRECTORY ${SOURCE_DIR}
-                                       OUTPUT_VARIABLE _git_unpushed_log
-                                       OUTPUT_STRIP_TRAILING_WHITESPACE
-                                       ERROR_QUIET)
-                       if(NOT _git_unpushed_log STREQUAL "")
-                               set(MY_WC_BRANCH "${MY_WC_BRANCH} (modified)")
-                       endif()
-                       unset(_git_unpushed_log)
                endif()
-
-               unset(_git_changed_files)
+               unset(_git_unpushed_log)
        endif()
+
+       unset(_git_changed_files)
 endif()
 
 # BUILD_PLATFORM and BUILD_PLATFORM are taken from CMake