[flang-commits] [flang] 16f1179 - [flang][cmake] Set the usual linker flags for non-gtest unit tests (#165256)
via flang-commits
flang-commits at lists.llvm.org
Wed Oct 29 04:27:15 PDT 2025
Author: Nikita Popov
Date: 2025-10-29T12:27:11+01:00
New Revision: 16f11794216f9a5533a1495d6ddbb3ad821d9629
URL: https://github.com/llvm/llvm-project/commit/16f11794216f9a5533a1495d6ddbb3ad821d9629
DIFF: https://github.com/llvm/llvm-project/commit/16f11794216f9a5533a1495d6ddbb3ad821d9629.diff
LOG: [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.
Added:
Modified:
flang/unittests/CMakeLists.txt
llvm/cmake/modules/AddLLVM.cmake
Removed:
################################################################################
diff --git a/flang/unittests/CMakeLists.txt b/flang/unittests/CMakeLists.txt
index db04923e2943a..2d612e58dae24 100644
--- a/flang/unittests/CMakeLists.txt
+++ b/flang/unittests/CMakeLists.txt
@@ -48,6 +48,7 @@ function(add_flang_nongtest_unittest test_name)
llvm_map_components_to_libnames(llvm_libs Support)
endif()
target_link_libraries(${test_name}${suffix} ${llvm_libs} ${ARG_UNPARSED_ARGUMENTS})
+ set_unittest_link_flags(${test_name}${suffix})
if(NOT ARG_SLOW_TEST)
add_dependencies(FlangUnitTests ${test_name}${suffix})
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 80e59a4df2433..7d40d309d538e 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -1747,6 +1747,31 @@ function(add_llvm_implicit_projects)
llvm_add_implicit_projects(LLVM)
endfunction(add_llvm_implicit_projects)
+function(set_unittest_link_flags target_name)
+ # The runtime benefits of LTO don't outweight the compile time costs for
+ # tests.
+ if(LLVM_ENABLE_LTO)
+ if((UNIX OR MINGW) AND LINKER_IS_LLD)
+ if(LLVM_ENABLE_FATLTO AND NOT APPLE)
+ # When using FatLTO, just use relocatable linking.
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,--no-fat-lto-objects")
+ else()
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,--lto-O0")
+ endif()
+ elseif(LINKER_IS_LLD_LINK)
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " /opt:lldlto=0")
+ elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
+ set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+ LINK_FLAGS " -Wl,-mllvm,-O0")
+ endif()
+ endif()
+
+ target_link_options(${target_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
+endfunction(set_unittest_link_flags)
+
# Generic support for adding a unittest.
function(add_unittest test_suite test_name)
if( NOT LLVM_BUILD_TESTS )
@@ -1770,27 +1795,7 @@ function(add_unittest test_suite test_name)
get_subproject_title(subproject_title)
set_target_properties(${test_name} PROPERTIES FOLDER "${subproject_title}/Tests/Unit")
- # The runtime benefits of LTO don't outweight the compile time costs for tests.
- if(LLVM_ENABLE_LTO)
- if((UNIX OR MINGW) AND LINKER_IS_LLD)
- if(LLVM_ENABLE_FATLTO AND NOT APPLE)
- # When using FatLTO, just use relocatable linking.
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,--no-fat-lto-objects")
- else()
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,--lto-O0")
- endif()
- elseif(LINKER_IS_LLD_LINK)
- set_property(TARGET ${test_name} APPEND_STRING PROPERTY
- LINK_FLAGS " /opt:lldlto=0")
- elseif(APPLE AND NOT uppercase_LLVM_ENABLE_LTO STREQUAL "THIN")
- set_property(TARGET ${target_name} APPEND_STRING PROPERTY
- LINK_FLAGS " -Wl,-mllvm,-O0")
- endif()
- endif()
-
- target_link_options(${test_name} PRIVATE "${LLVM_UNITTEST_LINK_FLAGS}")
+ set_unittest_link_flags(${test_name})
set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir})
More information about the flang-commits
mailing list