FindGit.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. Example usage:
  14. .. code-block:: cmake
  15. find_package(Git)
  16. if(Git_FOUND)
  17. message("Git found: ${GIT_EXECUTABLE}")
  18. endif()
  19. #]=======================================================================]
  20. # Look for 'git' or 'eg' (easy git)
  21. #
  22. set(git_names git eg)
  23. # Prefer .cmd variants on Windows unless running in a Makefile
  24. # in the MSYS shell.
  25. #
  26. if(CMAKE_HOST_WIN32)
  27. if(NOT CMAKE_GENERATOR MATCHES "MSYS")
  28. set(git_names git.cmd git eg.cmd eg)
  29. # GitHub search path for Windows
  30. file(GLOB github_path
  31. "$ENV{LOCALAPPDATA}/Github/PortableGit*/cmd"
  32. "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin"
  33. )
  34. # SourceTree search path for Windows
  35. set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
  36. endif()
  37. endif()
  38. # First search the PATH and specific locations.
  39. find_program(GIT_EXECUTABLE
  40. NAMES ${git_names}
  41. PATHS ${github_path} ${_git_sourcetree_path}
  42. DOC "Git command line client"
  43. )
  44. if(CMAKE_HOST_WIN32)
  45. # Now look for installations in Git/ directories under typical installation
  46. # prefixes on Windows. Exclude PATH from this search because VS 2017's
  47. # command prompt happens to have a PATH entry with a Git/ subdirectory
  48. # containing a minimal git not meant for general use.
  49. find_program(GIT_EXECUTABLE
  50. NAMES ${git_names}
  51. PATH_SUFFIXES Git/cmd Git/bin
  52. NO_SYSTEM_ENVIRONMENT_PATH
  53. DOC "Git command line client"
  54. )
  55. endif()
  56. mark_as_advanced(GIT_EXECUTABLE)
  57. unset(git_names)
  58. unset(_git_sourcetree_path)
  59. if(GIT_EXECUTABLE)
  60. execute_process(COMMAND ${GIT_EXECUTABLE} --version
  61. OUTPUT_VARIABLE git_version
  62. ERROR_QUIET
  63. OUTPUT_STRIP_TRAILING_WHITESPACE)
  64. if (git_version MATCHES "^git version [0-9]")
  65. string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
  66. endif()
  67. unset(git_version)
  68. endif()
  69. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  70. find_package_handle_standard_args(Git
  71. REQUIRED_VARS GIT_EXECUTABLE
  72. VERSION_VAR GIT_VERSION_STRING)