Skip to content

[flang-rt] Remove hard-coded dependency on compiler-rt path on Windows #150244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions flang-rt/cmake/modules/AddFlangRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--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 "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--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)
Expand Down
23 changes: 23 additions & 0 deletions flang-rt/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<$<COMPILE_LANGUAGE:CXX,C>:-Xclang>" "$<$<COMPILE_LANGUAGE:CXX,C>:--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 "$<$<COMPILE_LANGUAGE:Fortran>:-Xflang>" "$<$<COMPILE_LANGUAGE:Fortran>:--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
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down
Loading