FindGit.cmake 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindGit
  5. -------
  6. The module defines the following variables:
  7. ``GIT_EXECUTABLE``
  8. Path to Git command-line client.
  9. ``Git_FOUND``, ``GIT_FOUND``
  10. True if the Git command-line client was found.
  11. ``GIT_VERSION_STRING``
  12. The version of Git found.
  13. .. versionadded:: 3.14
  14. The module defines the following ``IMPORTED`` targets (when
  15. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  16. ``Git::Git``
  17. Executable of the Git command-line client.
  18. Example usage:
  19. .. code-block:: cmake
  20. find_package(Git)
  21. if(Git_FOUND)
  22. message("Git found: ${GIT_EXECUTABLE}")
  23. endif()
  24. #]=======================================================================]
  25. # Look for 'git'
  26. #
  27. set(git_names git)
  28. # Prefer .cmd variants on Windows unless running in a Makefile
  29. # in the MSYS shell.
  30. #
  31. if(CMAKE_HOST_WIN32)
  32. if(NOT CMAKE_GENERATOR MATCHES "MSYS")
  33. set(git_names git.cmd git)
  34. # GitHub search path for Windows
  35. file(GLOB github_path
  36. "$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd"
  37. "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin"
  38. )
  39. # SourceTree search path for Windows
  40. set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
  41. endif()
  42. endif()
  43. # First search the PATH and specific locations.
  44. find_program(GIT_EXECUTABLE
  45. NAMES ${git_names}
  46. PATHS ${github_path} ${_git_sourcetree_path}
  47. DOC "Git command line client"
  48. )
  49. if(CMAKE_HOST_WIN32)
  50. # Now look for installations in Git/ directories under typical installation
  51. # prefixes on Windows. Exclude PATH from this search because VS 2017's
  52. # command prompt happens to have a PATH entry with a Git/ subdirectory
  53. # containing a minimal git not meant for general use.
  54. find_program(GIT_EXECUTABLE
  55. NAMES ${git_names}
  56. PATH_SUFFIXES Git/cmd Git/bin
  57. NO_SYSTEM_ENVIRONMENT_PATH
  58. DOC "Git command line client"
  59. )
  60. endif()
  61. mark_as_advanced(GIT_EXECUTABLE)
  62. unset(git_names)
  63. unset(_git_sourcetree_path)
  64. if(GIT_EXECUTABLE)
  65. # Avoid querying the version if we've already done that this run. For
  66. # projects that use things like ExternalProject or FetchContent heavily,
  67. # this saving can be measurable on some platforms.
  68. #
  69. # This is an internal property, projects must not try to use it.
  70. # We don't want this stored in the cache because it might still change
  71. # between CMake runs, but it shouldn't change during a run for a given
  72. # git executable location.
  73. set(__doGitVersionCheck TRUE)
  74. get_property(__gitVersionProp GLOBAL
  75. PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  76. )
  77. if(__gitVersionProp)
  78. list(GET __gitVersionProp 0 __gitExe)
  79. list(GET __gitVersionProp 1 __gitVersion)
  80. if(__gitExe STREQUAL GIT_EXECUTABLE AND NOT __gitVersion STREQUAL "")
  81. set(GIT_VERSION_STRING "${__gitVersion}")
  82. set(__doGitVersionCheck FALSE)
  83. endif()
  84. unset(__gitExe)
  85. unset(__gitVersion)
  86. endif()
  87. unset(__gitVersionProp)
  88. if(__doGitVersionCheck)
  89. execute_process(COMMAND ${GIT_EXECUTABLE} --version
  90. OUTPUT_VARIABLE git_version
  91. ERROR_QUIET
  92. OUTPUT_STRIP_TRAILING_WHITESPACE)
  93. if (git_version MATCHES "^git version [0-9]")
  94. string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
  95. set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  96. "${GIT_EXECUTABLE};${GIT_VERSION_STRING}"
  97. )
  98. endif()
  99. unset(git_version)
  100. endif()
  101. unset(__doGitVersionCheck)
  102. get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE)
  103. if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git)
  104. add_executable(Git::Git IMPORTED)
  105. set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
  106. endif()
  107. unset(_findgit_role)
  108. endif()
  109. include(FindPackageHandleStandardArgs)
  110. find_package_handle_standard_args(Git
  111. REQUIRED_VARS GIT_EXECUTABLE
  112. VERSION_VAR GIT_VERSION_STRING)