From 97700e9f5b17b1631f6bfef4fec62dad88dc9319 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Fri, 11 Jan 2019 12:01:10 -0500 Subject: FindGit: Add imported target --- Help/release/dev/findgit-imported-target.rst | 5 +++++ Modules/FindGit.cmake | 11 +++++++++++ Tests/CMakeLists.txt | 4 ++++ Tests/FindGit/CMakeLists.txt | 10 ++++++++++ Tests/FindGit/Test/CMakeLists.txt | 13 +++++++++++++ Tests/FindGit/Test/RunGit.cmake | 20 ++++++++++++++++++++ 6 files changed, 63 insertions(+) create mode 100644 Help/release/dev/findgit-imported-target.rst create mode 100644 Tests/FindGit/CMakeLists.txt create mode 100644 Tests/FindGit/Test/CMakeLists.txt create mode 100644 Tests/FindGit/Test/RunGit.cmake diff --git a/Help/release/dev/findgit-imported-target.rst b/Help/release/dev/findgit-imported-target.rst new file mode 100644 index 0000000..cabbae5 --- /dev/null +++ b/Help/release/dev/findgit-imported-target.rst @@ -0,0 +1,5 @@ +findgit-imported-target +----------------------- + +* The :module:`FindGit` module now provides an ``IMPORTED`` target for the Git + executable. diff --git a/Modules/FindGit.cmake b/Modules/FindGit.cmake index c447a1a..900e4f5 100644 --- a/Modules/FindGit.cmake +++ b/Modules/FindGit.cmake @@ -5,6 +5,11 @@ FindGit ------- +The module defines the following ``IMPORTED`` targets: + +``Git::Git`` + Executable of the Git command-line client. + The module defines the following variables: ``GIT_EXECUTABLE`` @@ -78,6 +83,12 @@ if(GIT_EXECUTABLE) string(REPLACE "git version " "" GIT_VERSION_STRING "${git_version}") endif() unset(git_version) + + get_property(_findgit_role GLOBAL PROPERTY CMAKE_ROLE) + if(_findgit_role STREQUAL "PROJECT" AND NOT TARGET Git::Git) + add_executable(Git::Git IMPORTED) + set_property(TARGET Git::Git PROPERTY IMPORTED_LOCATION "${GIT_EXECUTABLE}") + endif() endif() include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 86f4c56..c1703cc 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -1437,6 +1437,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release add_subdirectory(FindGIF) endif() + if(CMake_TEST_FindGit) + add_subdirectory(FindGit) + endif() + if(CMake_TEST_FindGSL) add_subdirectory(FindGSL) endif() diff --git a/Tests/FindGit/CMakeLists.txt b/Tests/FindGit/CMakeLists.txt new file mode 100644 index 0000000..5d061f4 --- /dev/null +++ b/Tests/FindGit/CMakeLists.txt @@ -0,0 +1,10 @@ +add_test(NAME FindGit.Test COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindGit/Test" + "${CMake_BINARY_DIR}/Tests/FindGit/Test" + ${build_generator_args} + --build-project TestFindGit + --build-options ${build_options} + --test-command ${CMAKE_CTEST_COMMAND} -V -C $ + ) diff --git a/Tests/FindGit/Test/CMakeLists.txt b/Tests/FindGit/Test/CMakeLists.txt new file mode 100644 index 0000000..26fb372 --- /dev/null +++ b/Tests/FindGit/Test/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.12) +project(TestFindGit NONE) +include(CTest) + +find_package(Git REQUIRED) + +add_test(NAME test_git + COMMAND ${CMAKE_COMMAND} + "-DGIT_EXECUTABLE=${GIT_EXECUTABLE}" + "-DGIT_EXECUTABLE_TARGET=$" + "-DGIT_VERSION_STRING=${GIT_VERSION_STRING}" + -P "${CMAKE_CURRENT_LIST_DIR}/RunGit.cmake" + ) diff --git a/Tests/FindGit/Test/RunGit.cmake b/Tests/FindGit/Test/RunGit.cmake new file mode 100644 index 0000000..f798cd3 --- /dev/null +++ b/Tests/FindGit/Test/RunGit.cmake @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.12) + +function(run_git exe exe_display) + execute_process(COMMAND ${exe} --version + OUTPUT_VARIABLE output + OUTPUT_STRIP_TRAILING_WHITESPACE + RESULT_VARIABLE result + ) + + if(NOT result EQUAL 0) + message(SEND_ERROR "Result of ${exe_display} --version is ${result}, should be 0") + endif() + + if(NOT output STREQUAL "git version ${GIT_VERSION_STRING}") + message(SEND_ERROR "Output of ${exe_display} --version is \"${output}\", should be \"git version ${GIT_VERSION_STRING}\"") + endif() +endfunction() + +run_git("${GIT_EXECUTABLE}" "\${GIT_EXECUTABLE}") +run_git("${GIT_EXECUTABLE_TARGET}" "Git::Git") -- cgit v0.12