Skip to content

Commit 96e15eb

Browse files
DavidTrubyllvmbot
authored andcommitted
[flang-rt] Remove hard-coded dependency on compiler-rt path on Windows (#150244)
This fixes an issue where if the build folder is no longer present flang cannot link anything on Windows because the path to compiler-rt in the binary is hard-coded. Flang already links compiler-rt on Windows so it isn't necessary for flang-rt to specify that it depends on compiler-rt at all, other than for the unit tests, so instead we can move that logic into the unit test compile lines. (cherry picked from commit c20a95a)
1 parent 3db29aa commit 96e15eb

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

flang-rt/cmake/modules/AddFlangRT.cmake

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,27 +286,6 @@ function (add_flangrt_library name)
286286
target_compile_options(${tgtname} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS)
287287
endif ()
288288

289-
# Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
290-
# should only depend on msvcrt/ucrt. LLVM still emits libgcc/compiler-rt
291-
# functions in some cases like 128-bit integer math (__udivti3, __modti3,
292-
# __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a
293-
# dependency to Compiler-RT's builtin library where these are implemented.
294-
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
295-
if (FLANG_RT_BUILTINS_LIBRARY)
296-
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
297-
endif ()
298-
endif ()
299-
if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
300-
if (FLANG_RT_BUILTINS_LIBRARY)
301-
target_compile_options(${tgtname} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
302-
else ()
303-
message(WARNING "Did not find libclang_rt.builtins.lib.
304-
LLVM may emit builtins that are not implemented in msvcrt/ucrt and
305-
instead falls back to builtins from Compiler-RT. Linking with ${tgtname}
306-
may result in a linker error.")
307-
endif ()
308-
endif ()
309-
310289
# Non-GTest unittests depend on LLVMSupport
311290
if (ARG_LINK_TO_LLVM)
312291
if (LLVM_LINK_LLVM_DYLIB)

flang-rt/unittests/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ function(add_flangrt_unittest_offload_properties target)
6060
endif()
6161
endfunction()
6262

63+
# flang-rt on Windows requires compiler-rt for some symbols. For binaries built
64+
# with flang this dependency is added by the flang driver, but since the unit
65+
# tests are built with clang we need to add the dependency manually.
66+
function(add_flangrt_dependent_libs target)
67+
if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
68+
if (FLANG_RT_BUILTINS_LIBRARY)
69+
target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
70+
endif ()
71+
endif ()
72+
if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
73+
if (FLANG_RT_BUILTINS_LIBRARY)
74+
target_compile_options(${target} PRIVATE "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>")
75+
else ()
76+
message(WARNING "Did not find libclang_rt.builtins.lib.
77+
LLVM may emit builtins that are not implemented in msvcrt/ucrt and
78+
instead falls back to builtins from Compiler-RT. Linking with ${tgtname}
79+
may result in a linker error.")
80+
endif ()
81+
endif ()
82+
endfunction()
83+
6384

6485
function(add_flangrt_unittest test_dirname)
6586
cmake_parse_arguments(ARG
@@ -72,6 +93,7 @@ function(add_flangrt_unittest test_dirname)
7293

7394
target_link_libraries(${test_dirname} PRIVATE ${ARG_LINK_LIBS})
7495
add_flangrt_unittest_offload_properties(${test_dirname})
96+
add_flangrt_dependent_libs(${test_dirname})
7597

7698
# Required because LLVMSupport is compiled with this option.
7799
# FIXME: According to CMake documentation, this is the default. Why is it
@@ -99,6 +121,7 @@ function(add_flangrt_nongtest_unittest test_name)
99121
set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang-RT/Tests/Unit")
100122

101123
target_link_libraries(${test_name}${suffix} PRIVATE NonGTestTesting ${ARG_LINK_LIBS})
124+
add_flangrt_dependent_libs(${test_name}${suffix})
102125

103126
if(NOT ARG_SLOW_TEST)
104127
add_dependencies(FlangRTUnitTests ${test_name}${suffix})

0 commit comments

Comments
 (0)