FindGit.cmake 2.4 KB

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