FindGit.cmake 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. find_program(GIT_EXECUTABLE
  43. NAMES ${git_names}
  44. PATHS ${github_path} ${_git_sourcetree_path}
  45. PATH_SUFFIXES Git/cmd Git/bin
  46. DOC "Git command line client"
  47. )
  48. mark_as_advanced(GIT_EXECUTABLE)
  49. unset(git_names)
  50. unset(_git_sourcetree_path)
  51. if(GIT_EXECUTABLE)
  52. execute_process(COMMAND ${GIT_EXECUTABLE} --version
  53. OUTPUT_VARIABLE git_version
  54. ERROR_QUIET
  55. OUTPUT_STRIP_TRAILING_WHITESPACE)
  56. if (git_version MATCHES "^git version [0-9]")
  57. string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
  58. endif()
  59. unset(git_version)
  60. endif()
  61. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  62. find_package_handle_standard_args(Git
  63. REQUIRED_VARS GIT_EXECUTABLE
  64. VERSION_VAR GIT_VERSION_STRING)