FindGit.cmake 4.9 KB

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