FindGit.cmake 5.3 KB

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