From f37b991c14b48f1f1ce34f169511ce8c2581b7e9 Mon Sep 17 00:00:00 2001 From: Sit Melai Date: Sat, 7 Apr 2018 17:19:01 +0200 Subject: [PATCH] CMakeModule to find Git (previously only Mercurial) --- code/CMakeModules/FindGit.cmake | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 code/CMakeModules/FindGit.cmake diff --git a/code/CMakeModules/FindGit.cmake b/code/CMakeModules/FindGit.cmake new file mode 100644 index 000000000..883357d79 --- /dev/null +++ b/code/CMakeModules/FindGit.cmake @@ -0,0 +1,106 @@ +# Distributed under the OSI-approved BSD 3-Clause License. See accompanying +# file Copyright.txt or https://cmake.org/licensing for details. + +#.rst: +# FindGit +# ------- +# +# The module defines the following variables: +# +# ``GIT_EXECUTABLE`` +# Path to Git command-line client. +# ``Git_FOUND``, ``GIT_FOUND`` +# True if the Git command-line client was found. +# ``GIT_VERSION_STRING`` +# The version of Git found. +# +# Example usage: +# +# .. code-block:: cmake +# +# find_package(Git) +# if(Git_FOUND) +# message("Git found: ${GIT_EXECUTABLE}") +# endif() + +# Look for 'git' or 'eg' (easy git) +# +set(git_names git eg) + +# Prefer .cmd variants on Windows unless running in a Makefile +# in the MSYS shell. +# +if(CMAKE_HOST_WIN32) + if(NOT CMAKE_GENERATOR MATCHES "MSYS") + set(git_names git.cmd git eg.cmd eg) + # GitHub search path for Windows + file(GLOB github_path + "$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd" + "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin" + ) + # SourceTree search path for Windows + set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin") + endif() +endif() + +# First search the PATH and specific locations. +find_program(GIT_EXECUTABLE + NAMES ${git_names} + PATHS ${github_path} ${_git_sourcetree_path} + DOC "Git command line client" + ) + +if(CMAKE_HOST_WIN32) + # Now look for installations in Git/ directories under typical installation + # prefixes on Windows. Exclude PATH from this search because VS 2017's + # command prompt happens to have a PATH entry with a Git/ subdirectory + # containing a minimal git not meant for general use. + find_program(GIT_EXECUTABLE + NAMES ${git_names} + PATH_SUFFIXES Git/cmd Git/bin + NO_SYSTEM_ENVIRONMENT_PATH + DOC "Git command line client" + ) +endif() + +mark_as_advanced(GIT_EXECUTABLE) + +MACRO(Git_WC_INFO dir prefix) + EXECUTE_PROCESS(COMMAND ${Git_EXECUTABLE} log -r --pretty=format:"%d;%H;%an" . + WORKING_DIRECTORY ${dir} + OUTPUT_VARIABLE ${prefix}_WC_INFO + ERROR_VARIABLE Git_info_error + RESULT_VARIABLE Git_info_result + OUTPUT_STRIP_TRAILING_WHITESPACE) + + IF(NOT ${Git_info_result} EQUAL 0) + MESSAGE(SEND_ERROR "Command \"${GIT_EXECUTABLE} log\" failed with output:\n${Git_info_error}") + ELSE() + LIST(LENGTH ${prefix}_WC_INFO _COUNT) + IF(_COUNT EQUAL 4) + LIST(GET ${prefix}_WC_INFO 0 ${prefix}_WC_REVISION) + LIST(GET ${prefix}_WC_INFO 1 ${prefix}_WC_CHANGESET) + LIST(GET ${prefix}_WC_INFO 3 ${prefix}_WC_LAST_CHANGED_AUTHOR) + ELSE() + MESSAGE(STATUS "Bad output from HG") + SET(${prefix}_WC_REVISION "unknown") + SET(${prefix}_WC_CHANGESET "unknown") + SET(${prefix}_WC_BRANCH "unknown") + ENDIF() + ENDIF() + + ENDMACRO() + +unset(git_names) +unset(_git_sourcetree_path) + +if(GIT_EXECUTABLE) + execute_process(COMMAND ${GIT_EXECUTABLE} --version + OUTPUT_VARIABLE git_version + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + if (git_version MATCHES "^git version [0-9]") + string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}") + endif() + unset(git_version) +endif()