Skip to content

Commit 4be3e95

Browse files
authored
[Flang-RT][Offload] Always use LLVM-built GTest (#143682)
The Offload and Flang-RT had the ability to compile GTest themselves. But in bootstrapping builds, LLVM_LIBRARY_OUTPUT_INTDIR points to the same location as the stage1 build. If both are building GTest, they everwrite each others `libllvm_gtest.a` and `libllvm_test_main.a` which causes #143134. This PR removes the ability for the Offload/Flang-RT runtimes to build their own GTest and instead relies on the stage1 build of GTest. This was already the case with LLVM_INSTALL_GTEST=ON configurations. For LLVM_INSTALL_GTEST=OFF configurations, we now also export gtest into the buildtree configuration. Ultimately, this reduces combinatorial explosion of configurations in which unittests could be built (LLVM_INSTALL_GTEST=ON, GTest built by Offload, GTest built by Flang-RT, GTest built by Offload and also used by Flang-RT). GTest and therefore Offload/Runtime unittests will not be available if the runtimes are configured against an LLVM install tree. Since llvm-lit isn't available in the install tree either, it doesn't matter. Note that compiler-rt and libc also use GTest in non-default configrations. libc also depends on LLVM's GTest build (and would error-out if unavailable), but compiler-rt builds it completely different. Fixes #143134
1 parent fbb2fa9 commit 4be3e95

File tree

6 files changed

+66
-50
lines changed

6 files changed

+66
-50
lines changed

flang-rt/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ if (LLVM_INCLUDE_EXAMPLES)
334334
endif ()
335335

336336
if (FLANG_RT_INCLUDE_TESTS)
337-
add_subdirectory(unittests)
338337
add_subdirectory(test)
338+
add_subdirectory(unittests)
339339
else ()
340340
add_custom_target(check-flang-rt)
341341
endif()

flang-rt/test/CMakeLists.txt

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,24 @@ configure_lit_site_cfg(
2323
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
2424
)
2525

26-
if (TARGET FlangRTUnitTests)
27-
configure_lit_site_cfg(
28-
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
29-
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
30-
MAIN_CONFIG
31-
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
32-
)
26+
configure_lit_site_cfg(
27+
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in
28+
${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py
29+
MAIN_CONFIG
30+
${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py
31+
)
3332

34-
configure_lit_site_cfg(
35-
${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.site.cfg.py.in
36-
${CMAKE_CURRENT_BINARY_DIR}/NonGtestUnit/lit.site.cfg.py
37-
MAIN_CONFIG
38-
${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.cfg.py
39-
)
40-
endif ()
33+
configure_lit_site_cfg(
34+
${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.site.cfg.py.in
35+
${CMAKE_CURRENT_BINARY_DIR}/NonGtestUnit/lit.site.cfg.py
36+
MAIN_CONFIG
37+
${CMAKE_CURRENT_SOURCE_DIR}/NonGtestUnit/lit.cfg.py
38+
)
4139

4240

4341
add_custom_target(flang-rt-test-depends)
4442
set_target_properties(flang-rt-test-depends PROPERTIES FOLDER "Flang-RT/Meta")
4543
add_dependencies(flang-rt-test-depends
46-
FlangRTUnitTests
47-
flang_rt.runtime.unittest
4844
flang_rt.runtime
4945
)
5046

flang-rt/unittests/CMakeLists.txt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,39 @@
66
#
77
#===------------------------------------------------------------------------===#
88

9+
# Target that depends on all unittests
10+
add_custom_target(FlangRTUnitTests)
11+
set_target_properties(FlangRTUnitTests PROPERTIES FOLDER "Flang-RT/Meta")
12+
913
# LLVM uses a modified version of GTest that uses LLVMSupport for console
10-
# output. Therefore it also needs to include files from LLVM. Unfortunately,
11-
# LLVM/GTest doesn't add the include search path itself. Limiting the scope
12-
# using target_include_directories does not work because with
13-
# LLVM_INSTALL_GTEST=ON, as llvm_gtest is an IMPORT library.
14-
include_directories("${LLVM_INCLUDE_DIR}" "${LLVM_MAIN_INCLUDE_DIR}")
15-
16-
# Add GTest if not already present.
17-
# Using a function so LLVM_SUBPROJECT_TITLE does not propagate.
18-
function (build_gtest)
19-
set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")
20-
add_subdirectory("${LLVM_THIRD_PARTY_DIR}/unittest" "${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest")
21-
endfunction ()
14+
# output. We are using the pre-compiled GTest library from the LLVM build,
15+
# if available. Otherwise, do nothing.
16+
17+
if (CMAKE_CROSSCOMPILING)
18+
# TODO: It is possible that LLVM_GTEST_RUN_UNDER defines an emulator or
19+
# ssh remote command invocation; for this case provide an option to
20+
# enable unittests.
21+
message(STATUS "Flang-RT unittests disabled because we are cross-compiling")
22+
return ()
23+
endif ()
24+
2225
if (NOT TARGET llvm_gtest)
23-
build_gtest()
26+
message(WARNING "Flang-RT unittests disabled due to GTest being unavailable; "
27+
"Try LLVM_INSTALL_GTEST=ON for the LLVM build")
28+
return ()
2429
endif ()
2530

31+
32+
add_dependencies(flang-rt-test-depends
33+
FlangRTUnitTests
34+
flang_rt.runtime.unittest
35+
)
36+
2637
if (CXX_SUPPORTS_SUGGEST_OVERRIDE_FLAG)
2738
add_compile_options("-Wno-suggest-override")
2839
endif()
2940

3041

31-
# Target that depends on all unittests
32-
add_custom_target(FlangRTUnitTests)
33-
set_target_properties(FlangRTUnitTests PROPERTIES FOLDER "Flang-RT/Meta")
34-
35-
3642
function(add_flangrt_unittest_offload_properties target)
3743
# Set CUDA_RESOLVE_DEVICE_SYMBOLS.
3844
if (FLANG_RT_EXPERIMENTAL_OFFLOAD_SUPPORT STREQUAL "CUDA")

offload/CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,5 @@ add_subdirectory(liboffload)
380380
# Add tests.
381381
if(OFFLOAD_INCLUDE_TESTS)
382382
add_subdirectory(test)
383-
384-
# Add unit tests if GMock/GTest is present
385-
if(NOT LLVM_THIRD_PARTY_DIR)
386-
set(LLVM_THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../third-party")
387-
endif()
388-
if(EXISTS ${LLVM_THIRD_PARTY_DIR}/unittest AND NOT TARGET llvm_gtest)
389-
add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest ${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest)
390-
endif()
391-
if(TARGET llvm_gtest)
392-
add_subdirectory(unittests)
393-
endif()
383+
add_subdirectory(unittests)
394384
endif()

offload/unittests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
add_custom_target(OffloadUnitTests)
22
set_target_properties(OffloadUnitTests PROPERTIES FOLDER "Tests/UnitTests")
33

4+
if (CMAKE_CROSSCOMPILING)
5+
# TODO: It is possible that LLVM_GTEST_RUN_UNDER defines an emulator or
6+
# ssh remote command invocation; for this case provide an option to
7+
# enable unittests.
8+
message(STATUS "Offload unittests disabled because we are cross-compiling")
9+
return ()
10+
endif ()
11+
12+
if (NOT TARGET llvm_gtest)
13+
message(WARNING "Offload unittests disabled due to GTest being unavailable; "
14+
"Try LLVM_INSTALL_GTEST=ON for the LLVM build")
15+
return ()
16+
endif ()
17+
418
function(add_offload_test_device_code test_filename test_name)
519
set(SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${test_filename})
620
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

third-party/unittest/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ if (HAVE_LIBPTHREAD)
3838
list(APPEND LIBS pthread)
3939
endif()
4040

41-
# Do not build unittest libraries automatically, they will be pulled in
42-
# by unittests if these are built.
41+
# Make available for runtimes using the LLVM buildtree
42+
# (required for unittests in bootstrapping builds)
43+
set(EXCLUDE_FROM_ALL OFF)
4344

45+
# Install GTest only if requested.
4446
set(BUILDTREE_ONLY BUILDTREE_ONLY)
45-
set(EXCLUDE_FROM_ALL ON)
4647
if (LLVM_INSTALL_GTEST)
47-
set(EXCLUDE_FROM_ALL OFF)
4848
set(BUILDTREE_ONLY "")
4949
endif ()
5050

@@ -82,6 +82,16 @@ target_include_directories(llvm_gtest
8282
PRIVATE googletest googlemock
8383
)
8484

85+
# When used from the buildtree, also force use of buildtree LLVM headers,
86+
# (instead locally installed version)
87+
# FIXME: Shouldn't this be done for all LLVM libraries? Currently, LLVM uses a
88+
# big giant `include_directories( ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})`
89+
# which CMake does not add to the import library.
90+
target_include_directories(llvm_gtest BEFORE
91+
PUBLIC $<BUILD_INTERFACE:${LLVM_SOURCE_DIR}/include>
92+
$<BUILD_INTERFACE:${LLVM_BINARY_DIR}/include>
93+
)
94+
8595
add_subdirectory(UnitTestMain)
8696

8797
if (LLVM_INSTALL_GTEST)

0 commit comments

Comments
 (0)