diff --git a/flang-rt/cmake/modules/AddFlangRT.cmake b/flang-rt/cmake/modules/AddFlangRT.cmake index e51590fdae3d..58541609829c 100644 --- a/flang-rt/cmake/modules/AddFlangRT.cmake +++ b/flang-rt/cmake/modules/AddFlangRT.cmake @@ -286,27 +286,6 @@ function (add_flangrt_library name) target_compile_options(${tgtname} PUBLIC -U_LIBCPP_ENABLE_ASSERTIONS) endif () - # Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI - # should only depend on msvcrt/ucrt. LLVM still emits libgcc/compiler-rt - # functions in some cases like 128-bit integer math (__udivti3, __modti3, - # __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a - # dependency to Compiler-RT's builtin library where these are implemented. - if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") - if (FLANG_RT_BUILTINS_LIBRARY) - target_compile_options(${tgtname} PRIVATE "$<$:-Xclang>" "$<$:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>") - endif () - endif () - if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang") - if (FLANG_RT_BUILTINS_LIBRARY) - target_compile_options(${tgtname} PRIVATE "$<$:-Xflang>" "$<$:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>") - else () - message(WARNING "Did not find libclang_rt.builtins.lib. - LLVM may emit builtins that are not implemented in msvcrt/ucrt and - instead falls back to builtins from Compiler-RT. Linking with ${tgtname} - may result in a linker error.") - endif () - endif () - # Non-GTest unittests depend on LLVMSupport if (ARG_LINK_TO_LLVM) if (LLVM_LINK_LLVM_DYLIB) diff --git a/flang-rt/unittests/CMakeLists.txt b/flang-rt/unittests/CMakeLists.txt index 528219617413..831bc8a4c290 100644 --- a/flang-rt/unittests/CMakeLists.txt +++ b/flang-rt/unittests/CMakeLists.txt @@ -60,6 +60,27 @@ function(add_flangrt_unittest_offload_properties target) endif() endfunction() +# flang-rt on Windows requires compiler-rt for some symbols. For binaries built +# with flang this dependency is added by the flang driver, but since the unit +# tests are built with clang we need to add the dependency manually. +function(add_flangrt_dependent_libs target) + if (MSVC AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") + if (FLANG_RT_BUILTINS_LIBRARY) + target_compile_options(${target} PRIVATE "$<$:-Xclang>" "$<$:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>") + endif () + endif () + if (MSVC AND CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang") + if (FLANG_RT_BUILTINS_LIBRARY) + target_compile_options(${target} PRIVATE "$<$:-Xflang>" "$<$:--dependent-lib=${FLANG_RT_BUILTINS_LIBRARY}>") + else () + message(WARNING "Did not find libclang_rt.builtins.lib. + LLVM may emit builtins that are not implemented in msvcrt/ucrt and + instead falls back to builtins from Compiler-RT. Linking with ${tgtname} + may result in a linker error.") + endif () + endif () +endfunction() + function(add_flangrt_unittest test_dirname) cmake_parse_arguments(ARG @@ -72,6 +93,7 @@ function(add_flangrt_unittest test_dirname) target_link_libraries(${test_dirname} PRIVATE ${ARG_LINK_LIBS}) add_flangrt_unittest_offload_properties(${test_dirname}) + add_flangrt_dependent_libs(${test_dirname}) # Required because LLVMSupport is compiled with this option. # FIXME: According to CMake documentation, this is the default. Why is it @@ -99,6 +121,7 @@ function(add_flangrt_nongtest_unittest test_name) set_target_properties(${test_name}${suffix} PROPERTIES FOLDER "Flang-RT/Tests/Unit") target_link_libraries(${test_name}${suffix} PRIVATE NonGTestTesting ${ARG_LINK_LIBS}) + add_flangrt_dependent_libs(${test_name}${suffix}) if(NOT ARG_SLOW_TEST) add_dependencies(FlangRTUnitTests ${test_name}${suffix})