Skip to content

Commit 16f1179

Browse files
authored
[flang][cmake] Set the usual linker flags for non-gtest unit tests (#165256)
Flang also uses non-gtest based unittests, which don't go through the usual add_unittest() helper. These currently do not use the usual linker flags for unit tests. This means that in LTO builds, they do not disable LTO when building unit tests, which increases the build time.
1 parent 334d438 commit 16f1179

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

flang/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function(add_flang_nongtest_unittest test_name)
4848
llvm_map_components_to_libnames(llvm_libs Support)
4949
endif()
5050
target_link_libraries(${test_name}${suffix} ${llvm_libs} ${ARG_UNPARSED_ARGUMENTS})
51+
set_unittest_link_flags(${test_name}${suffix})
5152

5253
if(NOT ARG_SLOW_TEST)
5354
add_dependencies(FlangUnitTests ${test_name}${suffix})

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,31 @@ function(add_llvm_implicit_projects)
17471747
llvm_add_implicit_projects(LLVM)
17481748
endfunction(add_llvm_implicit_projects)
17491749

1750+
function(set_unittest_link_flags target_name)
1751+
# The runtime benefits of LTO don't outweight the compile time costs for
1752+
# tests.
1753+
if(LLVM_ENABLE_LTO)
1754+
if((UNIX OR MINGW) AND LINKER_IS_LLD)
1755+
if(LLVM_ENABLE_FATLTO AND NOT APPLE)
1756+
# When using FatLTO, just use relocatable linking.
1757+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1758+
LINK_FLAGS " -Wl,--no-fat-lto-objects")
1759+
else()
1760+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1761+
LINK_FLAGS " -Wl,--lto-O0")
1762+
endif()
1763+
elseif(LINKER_IS_LLD_LINK)
1764+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1765+
LINK_FLAGS " /opt:lldlto=0")
1766+
elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
1767+
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1768+
LINK_FLAGS " -Wl,-mllvm,-O0")
1769+
endif()
1770+
endif()
1771+
1772+
target_link_options(${target_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
1773+
endfunction(set_unittest_link_flags)
1774+
17501775
# Generic support for adding a unittest.
17511776
function(add_unittest test_suite test_name)
17521777
if( NOT LLVM_BUILD_TESTS )
@@ -1770,27 +1795,7 @@ function(add_unittest test_suite test_name)
17701795
get_subproject_title(subproject_title)
17711796
set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit")
17721797

1773-
# The runtime benefits of LTO don't outweight the compile time costs for tests.
1774-
if(LLVM_ENABLE_LTO)
1775-
if((UNIX OR MINGW) AND LINKER_IS_LLD)
1776-
if(LLVM_ENABLE_FATLTO AND NOT APPLE)
1777-
# When using FatLTO, just use relocatable linking.
1778-
set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1779-
LINK_FLAGS " -Wl,--no-fat-lto-objects")
1780-
else()
1781-
set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1782-
LINK_FLAGS " -Wl,--lto-O0")
1783-
endif()
1784-
elseif(LINKER_IS_LLD_LINK)
1785-
set_property(TARGET ${test_name} APPEND_STRING PROPERTY
1786-
LINK_FLAGS " /opt:lldlto=0")
1787-
elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
1788-
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
1789-
LINK_FLAGS " -Wl,-mllvm,-O0")
1790-
endif()
1791-
endif()
1792-
1793-
target_link_options(${test_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
1798+
set_unittest_link_flags(${test_name})
17941799

17951800
set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
17961801
set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})

0 commit comments

Comments
 (0)