Skip to content

Commit b9d46c8

Browse files
committed
[CMake] Move to CMake's native FindGTest
- Remove own FindGTest, use the one from CMake. Starting from CMake 3.23, the GTest libraries have canonical names. - Replace uses of legacy targets like "gtest" in ROOT with canonical target names such as GTest::gtest or GTest::gtest_main. - Same as above for roottest. - For CMake between 3.20 and 3.23, create ALIAS targets with the same names as in 3.23 (e.g. GTest::gtest_main). For CMake < 3.23, this will allow using the standard FindGTest with the modern names already in cmake 3.20. - Explicitly list GTest::gmock as a dependency in core.
1 parent 4579a4a commit b9d46c8

File tree

10 files changed

+19
-94
lines changed

10 files changed

+19
-94
lines changed

cmake/modules/FindGTest.cmake

Lines changed: 0 additions & 81 deletions
This file was deleted.

cmake/modules/RootMacros.cmake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,13 +1808,10 @@ function(ROOT_ADD_GTEST test_suite)
18081808
# against. For example, tests in Core should link only against libCore. This could be tricky
18091809
# to implement because some ROOT components create more than one library.
18101810
ROOT_EXECUTABLE(${test_suite} ${source_files} LIBRARIES ${ARG_LIBRARIES})
1811-
target_link_libraries(${test_suite} PRIVATE gtest gtest_main gmock gmock_main)
1811+
target_link_libraries(${test_suite} PRIVATE GTest::gtest GTest::gmock GTest::gtest_main GTest::gmock_main)
18121812
if(TARGET ROOT::TestSupport)
18131813
target_link_libraries(${test_suite} PRIVATE ROOT::TestSupport)
18141814
else()
1815-
# Since we don't inherit the linkage against gtest from ROOT::TestSupport,
1816-
# we need to link against gtest here.
1817-
target_link_libraries(${test_suite} gtest)
18181815
message(WARNING "ROOT_ADD_GTEST(${test_suite} ...): The target ROOT::TestSupport is missing. It looks like the test is declared against a ROOT build that is configured with -Dtesting=OFF.
18191816
If this test sends warning or error messages, this will go unnoticed.")
18201817
endif()

cmake/modules/SearchInstalledSoftware.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,15 @@ if (builtin_gtest)
20222022

20232023
endif()
20242024

2025+
# Starting from cmake 3.23, the GTest targets will have stable names.
2026+
# ROOT was updated to use those, but for older CMake versions, we have to declare the aliases:
2027+
foreach(LIBNAME gtest_main gmock_main gtest gmock)
2028+
if(NOT TARGET GTest::${LIBNAME} AND TARGET ${LIBNAME})
2029+
add_library(GTest::${LIBNAME} ALIAS ${LIBNAME})
2030+
endif()
2031+
endforeach()
2032+
2033+
#------------------------------------------------------------------------------------
20252034
if(webgui AND NOT builtin_openui5)
20262035
ROOT_CHECK_CONNECTION("builtin_openui5=ON")
20272036
if(NO_CONNECTION)

core/foundation/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ROOT_ADD_GTEST(testMake_unique testMake_unique.cxx LIBRARIES Core)
88
ROOT_ADD_GTEST(testTypeTraits testTypeTraits.cxx LIBRARIES Core)
99
ROOT_ADD_GTEST(testNotFn testNotFn.cxx LIBRARIES Core)
1010
ROOT_ADD_GTEST(testClassEdit testClassEdit.cxx LIBRARIES Core RIO COPY_TO_BUILDDIR ${CMAKE_CURRENT_SOURCE_DIR}/file_16199.C)
11-
ROOT_ADD_GTEST(testException testException.cxx LIBRARIES Core)
11+
ROOT_ADD_GTEST(testException testException.cxx LIBRARIES Core GTest::gmock)
1212
ROOT_ADD_GTEST(testLogger testLogger.cxx LIBRARIES Core)
1313
ROOT_ADD_GTEST(testRRangeCast testRRangeCast.cxx LIBRARIES Core)
1414
ROOT_ADD_GTEST(testStringUtils testStringUtils.cxx LIBRARIES Core)

core/metacling/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ROOT_ADD_GTEST(
2626

2727
LIBRARIES
2828
Core
29+
GTest::gmock
2930
)
3031

3132
add_dependencies(TClingTest Cling RIO)

core/testsupport/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ target_include_directories(${libname} PUBLIC
1616
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/inc/>
1717
$<INSTALL_INTERFACE:./>
1818
)
19-
target_link_libraries(${libname} PUBLIC Core gtest)
19+
target_link_libraries(${libname} PRIVATE Core GTest::gtest)
2020

2121
# Installation of header and library:
2222
set_target_properties(${libname} PROPERTIES PUBLIC_HEADER inc/${header_dir}/TestSupport.hxx)

roofit/roofit/test/vectorisedPDFs/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL Intel)
1010
endif()
1111

1212
add_library(VectorisedPDFTests STATIC VectorisedPDFTests.cxx)
13-
target_link_libraries(VectorisedPDFTests PUBLIC gtest ROOT::Gpad ROOT::RooFitCore ROOT::RooFit)
13+
target_link_libraries(VectorisedPDFTests PUBLIC ROOT::Gpad ROOT::RooFitCore ROOT::RooFit GTest::gtest)
1414

1515
ROOT_ADD_GTEST(testCompatMode testCompatMode.cxx
1616
LIBRARIES VectorisedPDFTests)

roottest/cmake/modules/RoottestMacros.cmake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,7 @@ function(ROOTTEST_ADD_UNITTEST_DIR)
11541154
endif()
11551155

11561156
add_executable(${binary} ${unittests_SRC})
1157-
target_include_directories(${binary} PRIVATE ${GTEST_INCLUDE_DIR})
1158-
target_link_libraries(${binary} gtest gtest_main ${libraries})
1157+
target_link_libraries(${binary} PRIVATE GTest::gtest GTest::gtest_main ${libraries})
11591158

11601159
if(MSVC AND DEFINED ROOT_SOURCE_DIR)
11611160
if(TARGET ROOTStaticSanitizerConfig)

roottest/root/ntuple/makeproject/rntuple/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ endif()
5353
ROOTTEST_GENERATE_EXECUTABLE(
5454
read_rntuple
5555
read_rntuple.cxx
56-
LIBRARIES ${ROOT_LIBRARIES} gtest gtest_main gmock gmock_main ${RNTUPLESTLTESTLIB} ROOTNTuple
56+
LIBRARIES ${ROOT_LIBRARIES} GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main ${RNTUPLESTLTESTLIB} ROOTNTuple
5757
FIXTURES_REQUIRED makeproject_rntuple_called
5858
FIXTURES_SETUP read_rntuple_executable
5959
)
60-
target_include_directories(read_rntuple PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${GTEST_INCLUDE_DIR})
60+
target_include_directories(read_rntuple PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
6161
target_link_directories(read_rntuple PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/librntuplestltest)
6262

6363
# Need to also explicitly set the LD_LIBRARY_PATH (at least on MacOS), so that

roottest/root/ntuple/makeproject/ttree/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ endif()
4949
ROOTTEST_GENERATE_EXECUTABLE(
5050
read_ttree
5151
read_ttree.cxx
52-
LIBRARIES ${ROOT_LIBRARIES} gtest gtest_main gmock gmock_main ${TTREESTLTESTLIB}
52+
LIBRARIES ${ROOT_LIBRARIES} GTest::gtest GTest::gtest_main GTest::gmock GTest::gmock_main ${TTREESTLTESTLIB}
5353
FIXTURES_REQUIRED makeproject_ttree_called
5454
FIXTURES_SETUP read_ttree_executable
5555
)
56-
target_include_directories(read_ttree PRIVATE ${CMAKE_CURRENT_BINARY_DIR} ${GTEST_INCLUDE_DIR})
56+
target_include_directories(read_ttree PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
5757
target_link_directories(read_ttree PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/libttreestltest)
5858

5959
# Need to also explicitly set the LD_LIBRARY_PATH (at least on MacOS), so that

0 commit comments

Comments
 (0)