FindGit.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ``IMPORTED`` targets (when
  7. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  8. ``Git::Git``
  9. Executable of the Git command-line client.
  10. The module defines the following variables:
  11. ``GIT_EXECUTABLE``
  12. Path to Git command-line client.
  13. ``Git_FOUND``, ``GIT_FOUND``
  14. True if the Git command-line client was found.
  15. ``GIT_VERSION_STRING``
  16. The version of Git found.
  17. Example usage:
  18. .. code-block:: cmake
  19. find_package(Git)
  20. if(Git_FOUND)
  21. message("Git found: ${GIT_EXECUTABLE}")
  22. endif()
  23. #]=======================================================================]
  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. get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE)
  73. if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git)
  74. add_executable(Git::Git IMPORTED)
  75. set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}")
  76. endif()
  77. endif()
  78. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  79. find_package_handle_standard_args(Git
  80. REQUIRED_VARS GIT_EXECUTABLE
  81. VERSION_VAR GIT_VERSION_STRING)