Skip to content

Commit 6810583

Browse files
authored
[UR][CMake] Support preinstalled GTest (#19720)
Don't fetch and build it if a compatible version is installed on the system. GMock is considered part of GTest to CMake (and not even a component) but in some Linux distros its a separate package, so we need to handle the case where one is installed but not the other. Issue: #19635 Signed-off-by: Sarnie, Nick <[email protected]>
1 parent 284e9f0 commit 6810583

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

unified-runtime/test/CMakeLists.txt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
# See LICENSE.TXT
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6-
include(FetchContent)
7-
FetchContent_Declare(
8-
googletest
9-
GIT_REPOSITORY https://github.com/google/googletest.git
10-
GIT_TAG v1.13.0
11-
)
6+
set(GTEST_VER 1.13.0)
7+
8+
find_package(GTest ${GTEST_VER} QUIET)
9+
10+
if(GTest_FOUND AND NOT TARGET GTest::gmock)
11+
message(WARNING "Found system install of GTest but not GMock. Building GTest and GMock from source")
12+
set(GTest_FOUND FALSE)
13+
endif()
14+
15+
if(NOT GTest_FOUND)
16+
include(FetchContent)
17+
FetchContent_Declare(
18+
googletest
19+
GIT_REPOSITORY https://github.com/google/googletest.git
20+
GIT_TAG v${GTEST_VER}
21+
)
22+
endif()
1223

1324
include(FindLit)
1425

@@ -22,8 +33,10 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND UR_DPCXX AND UR_TEST_FUZZTESTS)
2233
set(UR_FUZZTESTING_ENABLED ON)
2334
endif()
2435

25-
set(INSTALL_GTEST OFF)
26-
FetchContent_MakeAvailable(googletest)
36+
if(NOT GTest_FOUND)
37+
set(INSTALL_GTEST OFF)
38+
FetchContent_MakeAvailable(googletest)
39+
endif()
2740
enable_testing()
2841

2942
# At the time of writing this comment, this is only used for level_zero adapter testing.
@@ -112,7 +125,7 @@ function(add_gtest_test name)
112125
add_testing_binary(${TEST_TARGET_NAME} ${ARGN})
113126
target_link_libraries(${TEST_TARGET_NAME}
114127
PRIVATE
115-
gmock
128+
GTest::gmock
116129
GTest::gtest_main)
117130
endfunction()
118131

0 commit comments

Comments
 (0)