Debian Codegraph @

CMakeModules/FindGit.cmake

raw
1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindGit
6 -------
7
8 The module defines the following ``IMPORTED`` targets (when
9 :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
10
11 ``Git::Git``
12 Executable of the Git command-line client.
13
14 The module defines the following variables:
15
16 ``GIT_EXECUTABLE``
17 Path to Git command-line client.
18 ``Git_FOUND``, ``GIT_FOUND``
19 True if the Git command-line client was found.
20 ``GIT_VERSION_STRING``
21 The version of Git found.
22
23 Example usage:
24
25 .. code-block:: cmake
26
27 find_package(Git)
28 if(Git_FOUND)
29 message("Git found: ${GIT_EXECUTABLE}")
30 endif()
31 #]=======================================================================]
32
33 include(FindPackageHandleStandardArgs)
34
35 # Look for 'git' or 'eg' (easy git)
36 #
37 set(git_names git eg)
38
39 # Prefer .cmd variants on Windows unless running in a Makefile
40 # in the MSYS shell.
41 #
42 if(CMAKE_HOST_WIN32)
43 if(NOT CMAKE_GENERATOR MATCHES "MSYS")
44 set(git_names git.cmd git eg.cmd eg)
45 # GitHub search path for Windows
46 file(GLOB github_path
47 "$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd"
48 "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin"
49 )
50 # SourceTree search path for Windows
51 set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
52 endif()
53 endif()
54
55 # First search the PATH and specific locations.
56 find_program(GIT_EXECUTABLE
57 NAMES ${git_names}
58 PATHS ${github_path} ${_git_sourcetree_path}
59 DOC "Git command line client"
60 )
61
62 if(CMAKE_HOST_WIN32)
63 # Now look for installations in Git/ directories under typical installation
64 # prefixes on Windows. Exclude PATH from this search because VS 2017's
65 # command prompt happens to have a PATH entry with a Git/ subdirectory
66 # containing a minimal git not meant for general use.
67 find_program(GIT_EXECUTABLE
68 NAMES ${git_names}
69 PATH_SUFFIXES Git/cmd Git/bin
70 NO_SYSTEM_ENVIRONMENT_PATH
71 DOC "Git command line client"
72 )
73 endif()
74
75 mark_as_advanced(GIT_EXECUTABLE)
76
77 unset(git_names)
78 unset(_git_sourcetree_path)
79
80 if(GIT_EXECUTABLE)
81 execute_process(COMMAND ${GIT_EXECUTABLE} --version
82 OUTPUT_VARIABLE git_version
83 ERROR_QUIET
84 OUTPUT_STRIP_TRAILING_WHITESPACE)
85 if (git_version MATCHES "^git version [0-9]")
86 string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
87 endif()
88 unset(git_version)
89
90 get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE)
91 if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git)
92 add_executable(Git::Git IMPORTED)
93 set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
94 endif()
95 endif()
96
97 find_package_handle_standard_args(Git
98 REQUIRED_VARS GIT_EXECUTABLE
99 VERSION_VAR GIT_VERSION_STRING)