FindGit.cmake 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #.rst:
  2. # FindGit
  3. # -------
  4. #
  5. # The module defines the following variables:
  6. #
  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. #
  14. # Example usage:
  15. #
  16. # .. code-block:: cmake
  17. #
  18. # find_package(Git)
  19. # if(Git_FOUND)
  20. # message("Git found: ${GIT_EXECUTABLE}")
  21. # endif()
  22. #=============================================================================
  23. # Copyright 2010-2016 Kitware, Inc.
  24. # Copyright 2012 Rolf Eike Beer <[email protected]>
  25. #
  26. # Distributed under the OSI-approved BSD License (the "License");
  27. # see accompanying file Copyright.txt for details.
  28. #
  29. # This software is distributed WITHOUT ANY WARRANTY; without even the
  30. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  31. # See the License for more information.
  32. #=============================================================================
  33. # (To distribute this file outside of CMake, substitute the full
  34. # License text for the above reference.)
  35. # Look for 'git' or 'eg' (easy git)
  36. #
  37. set(git_names git eg)
  38. # Prefer .cmd variants on Windows unless running in a Makefile
  39. # in the MSYS shell.
  40. #
  41. if(WIN32)
  42. if(NOT CMAKE_GENERATOR MATCHES "MSYS")
  43. set(git_names git.cmd git eg.cmd eg)
  44. # GitHub search path for Windows
  45. set(github_path "$ENV{LOCALAPPDATA}/Github/PortableGit*/bin")
  46. file(GLOB github_path "${github_path}")
  47. # SourceTree search path for Windows
  48. set(_git_sourcetree_path "$ENV{LOCALAPPDATA}/Atlassian/SourceTree/git_local/bin")
  49. endif()
  50. endif()
  51. find_program(GIT_EXECUTABLE
  52. NAMES ${git_names}
  53. PATHS ${github_path} ${_git_sourcetree_path}
  54. PATH_SUFFIXES Git/cmd Git/bin
  55. DOC "Git command line client"
  56. )
  57. mark_as_advanced(GIT_EXECUTABLE)
  58. unset(git_names)
  59. unset(_git_sourcetree_path)
  60. if(GIT_EXECUTABLE)
  61. execute_process(COMMAND ${GIT_EXECUTABLE} --version
  62. OUTPUT_VARIABLE git_version
  63. ERROR_QUIET
  64. OUTPUT_STRIP_TRAILING_WHITESPACE)
  65. if (git_version MATCHES "^git version [0-9]")
  66. string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}")
  67. endif()
  68. unset(git_version)
  69. endif()
  70. # Handle the QUIETLY and REQUIRED arguments and set Git_FOUND to TRUE if
  71. # all listed variables are TRUE
  72. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  73. find_package_handle_standard_args(Git
  74. REQUIRED_VARS GIT_EXECUTABLE
  75. VERSION_VAR GIT_VERSION_STRING)