FindGit.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindGit
  5. -------
  6. Finds the Git distributed version control system:
  7. .. code-block:: cmake
  8. find_package(Git [<version>] [...])
  9. Imported Targets
  10. ^^^^^^^^^^^^^^^^
  11. This module provides the following :ref:`Imported Targets` when the
  12. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``:
  13. ``Git::Git``
  14. .. versionadded:: 3.14
  15. Target that encapsulates Git command-line client executable. It can be used
  16. in :manual:`generator expressions <cmake-generator-expressions(7)>`, and
  17. commands like :command:`add_custom_target` and :command:`add_custom_command`.
  18. This target is available only if Git is found.
  19. Result Variables
  20. ^^^^^^^^^^^^^^^^
  21. This module defines the following variables:
  22. ``Git_FOUND``
  23. .. versionadded:: 3.3
  24. Boolean indicating whether (the requested version of) Git was found.
  25. ``Git_VERSION``
  26. .. versionadded:: 4.2
  27. The version of Git found.
  28. Cache Variables
  29. ^^^^^^^^^^^^^^^
  30. The following cache variables may also be set:
  31. ``GIT_EXECUTABLE``
  32. Path to the ``git`` command-line client executable.
  33. Deprecated Variables
  34. ^^^^^^^^^^^^^^^^^^^^
  35. The following variables are provided for backward compatibility:
  36. ``GIT_FOUND``
  37. .. deprecated:: 4.2
  38. Use ``Git_FOUND``, which has the same value.
  39. Boolean indicating whether (the requested version of) Git was found.
  40. ``GIT_VERSION_STRING``
  41. .. deprecated:: 4.2
  42. Use ``Git_VERSION``, which has the same value.
  43. The version of Git found.
  44. Examples
  45. ^^^^^^^^
  46. Finding Git and retrieving the latest commit from the project repository:
  47. .. code-block:: cmake
  48. find_package(Git)
  49. if(Git_FOUND)
  50. execute_process(
  51. COMMAND ${GIT_EXECUTABLE} --no-pager log -n 1 HEAD "--pretty=format:%h %s"
  52. OUTPUT_VARIABLE output
  53. RESULT_VARIABLE result
  54. ERROR_QUIET
  55. OUTPUT_STRIP_TRAILING_WHITESPACE
  56. )
  57. if(result EQUAL 0)
  58. message(STATUS "Last Git commit: ${output}")
  59. endif()
  60. endif()
  61. #]=======================================================================]
  62. # Look for 'git'
  63. #
  64. set(git_names git)
  65. # Prefer .cmd variants on Windows unless running in a Makefile
  66. # in the MSYS shell.
  67. #
  68. if(CMAKE_HOST_WIN32)
  69. if(NOT CMAKE_GENERATOR MATCHES "MSYS")
  70. set(git_names git.cmd git)
  71. # GitHub search path for Windows
  72. file(GLOB github_path
  73. "$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd"
  74. "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin"
  75. )
  76. # SourceTree search path for Windows
  77. set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
  78. endif()
  79. endif()
  80. # First search the PATH and specific locations.
  81. find_program(GIT_EXECUTABLE
  82. NAMES ${git_names}
  83. PATHS ${github_path} ${_git_sourcetree_path}
  84. DOC "Git command line client"
  85. )
  86. if(CMAKE_HOST_WIN32)
  87. # Now look for installations in Git/ directories under typical installation
  88. # prefixes on Windows. Exclude PATH from this search because VS 2017's
  89. # command prompt happens to have a PATH entry with a Git/ subdirectory
  90. # containing a minimal git not meant for general use.
  91. find_program(GIT_EXECUTABLE
  92. NAMES ${git_names}
  93. PATH_SUFFIXES Git/cmd Git/bin
  94. NO_SYSTEM_ENVIRONMENT_PATH
  95. DOC "Git command line client"
  96. )
  97. endif()
  98. mark_as_advanced(GIT_EXECUTABLE)
  99. unset(git_names)
  100. unset(_git_sourcetree_path)
  101. if(GIT_EXECUTABLE)
  102. # Avoid querying the version if we've already done that this run. For
  103. # projects that use things like ExternalProject or FetchContent heavily,
  104. # this saving can be measurable on some platforms.
  105. #
  106. # This is an internal property, projects must not try to use it.
  107. # We don't want this stored in the cache because it might still change
  108. # between CMake runs, but it shouldn't change during a run for a given
  109. # git executable location.
  110. set(__doGitVersionCheck TRUE)
  111. get_property(__gitVersionProp GLOBAL
  112. PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  113. )
  114. if(__gitVersionProp)
  115. list(GET __gitVersionProp 0 __gitExe)
  116. list(GET __gitVersionProp 1 __gitVersion)
  117. if(__gitExe STREQUAL GIT_EXECUTABLE AND NOT __gitVersion STREQUAL "")
  118. set(Git_VERSION "${__gitVersion}")
  119. set(GIT_VERSION_STRING "${Git_VERSION}")
  120. set(__doGitVersionCheck FALSE)
  121. endif()
  122. unset(__gitExe)
  123. unset(__gitVersion)
  124. endif()
  125. unset(__gitVersionProp)
  126. if(__doGitVersionCheck)
  127. execute_process(COMMAND ${GIT_EXECUTABLE} --version
  128. OUTPUT_VARIABLE git_version
  129. ERROR_QUIET
  130. OUTPUT_STRIP_TRAILING_WHITESPACE)
  131. if (git_version MATCHES "^git version [0-9]")
  132. string(REPLACE "git version " "" Git_VERSION "${git_version}")
  133. set(GIT_VERSION_STRING "${Git_VERSION}")
  134. set_property(GLOBAL PROPERTY _CMAKE_FindGit_GIT_EXECUTABLE_VERSION
  135. "${GIT_EXECUTABLE};${Git_VERSION}"
  136. )
  137. endif()
  138. unset(git_version)
  139. endif()
  140. unset(__doGitVersionCheck)
  141. get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE)
  142. if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git)
  143. add_executable(Git::Git IMPORTED)
  144. set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
  145. endif()
  146. unset(_findgit_role)
  147. endif()
  148. include(FindPackageHandleStandardArgs)
  149. find_package_handle_standard_args(Git
  150. REQUIRED_VARS GIT_EXECUTABLE
  151. VERSION_VAR Git_VERSION)