[CMake][compiler-rt] Support for using compiler-rt atomic library (#106603)

Not every toolchain provides and want to use libatomic which is a part
of GCC, some toolchains may opt into using compiler-rt atomic library.
This commit is contained in:
Petr Hosek 2024-09-03 10:28:49 -07:00 committed by GitHub
parent 4640736616
commit 26a4edf655
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 8 deletions

View File

@ -155,6 +155,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
set(BUILTINS_${target}_CMAKE_MODULE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "") set(BUILTINS_${target}_CMAKE_MODULE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
set(BUILTINS_${target}_CMAKE_EXE_LINKER_FLAG "-fuse-ld=lld" CACHE STRING "") set(BUILTINS_${target}_CMAKE_EXE_LINKER_FLAG "-fuse-ld=lld" CACHE STRING "")
set(BUILTINS_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "") set(BUILTINS_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "")
set(BUILTINS_${target}_COMPILER_RT_LIBATOMIC_USE_PTHREAD ON CACHE BOOL "")
# Set the per-target runtimes options. # Set the per-target runtimes options.
list(APPEND RUNTIME_TARGETS "${target}") list(APPEND RUNTIME_TARGETS "${target}")
@ -169,6 +170,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
set(RUNTIMES_${target}_CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "") set(RUNTIMES_${target}_CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
set(RUNTIMES_${target}_COMPILER_RT_CXX_LIBRARY "libcxx" CACHE STRING "") set(RUNTIMES_${target}_COMPILER_RT_CXX_LIBRARY "libcxx" CACHE STRING "")
set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "") set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_USE_ATOMIC_LIBRARY ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_USE_LLVM_UNWINDER ON CACHE BOOL "") set(RUNTIMES_${target}_COMPILER_RT_USE_LLVM_UNWINDER ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "") set(RUNTIMES_${target}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "")
set(RUNTIMES_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "") set(RUNTIMES_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "")

View File

@ -51,7 +51,7 @@ endfunction()
# This calls cache_compiler_rt_library that caches the path to speed up # This calls cache_compiler_rt_library that caches the path to speed up
# repeated invocations with the same `name` and `target`. # repeated invocations with the same `name` and `target`.
function(find_compiler_rt_library name variable) function(find_compiler_rt_library name variable)
cmake_parse_arguments(ARG "" "TARGET;FLAGS" "" ${ARGN}) cmake_parse_arguments(ARG "SHARED" "TARGET;FLAGS" "" ${ARGN})
# While we can use compiler-rt runtimes with other compilers, we need to # While we can use compiler-rt runtimes with other compilers, we need to
# query the compiler for runtime location and thus we require Clang. # query the compiler for runtime location and thus we require Clang.
if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang) if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
@ -112,12 +112,16 @@ function(find_compiler_rt_library name variable)
# path and then checking if the resultant path exists. The result of # path and then checking if the resultant path exists. The result of
# this check is also cached by cache_compiler_rt_library. # this check is also cached by cache_compiler_rt_library.
set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}") set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")
if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)") get_component_name("builtins" from_name)
set(from_name ${CMAKE_MATCH_0}) get_component_name(${name} to_name)
get_component_name(${name} to_name) get_filename_component(basename ${library_file} NAME)
string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}") string(REPLACE "${from_name}" "${to_name}" basename "${basename}")
cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}") if (ARG_SHARED)
string(REGEX REPLACE "\.(a|lib)$" ".so" basename "${basename}")
endif() endif()
get_filename_component(dirname ${library_file} DIRECTORY)
set(library_file "${dirname}/${basename}")
cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
endif() endif()
set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE) set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)
endfunction() endfunction()

View File

@ -284,6 +284,8 @@ endif()
option(COMPILER_RT_USE_BUILTINS_LIBRARY option(COMPILER_RT_USE_BUILTINS_LIBRARY
"Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY}) "Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY})
option(COMPILER_RT_USE_ATOMIC_LIBRARY "Use compiler-rt atomic instead of libatomic" OFF)
include(config-ix) include(config-ix)
#================================ #================================

View File

@ -74,6 +74,14 @@ if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
endif() endif()
endif () endif ()
if (COMPILER_RT_USE_ATOMIC_LIBRARY)
include(HandleCompilerRT)
find_compiler_rt_library(atomic COMPILER_RT_ATOMIC_LIBRARY SHARED
FLAGS ${SANITIZER_COMMON_FLAGS})
else()
check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
endif()
# CodeGen options. # CodeGen options.
check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG) check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG)
check_c_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG) check_c_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG)
@ -175,7 +183,6 @@ check_cxx_compiler_flag(-nostdlib++ COMPILER_RT_HAS_NOSTDLIBXX_FLAG)
check_include_files("sys/auxv.h" COMPILER_RT_HAS_AUXV) check_include_files("sys/auxv.h" COMPILER_RT_HAS_AUXV)
# Libraries. # Libraries.
check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL) check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL)
check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT) check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT)
check_library_exists(m pow "" COMPILER_RT_HAS_LIBM) check_library_exists(m pow "" COMPILER_RT_HAS_LIBM)

View File

@ -36,7 +36,15 @@ set(RTSAN_UNITTEST_LINK_FLAGS
${SANITIZER_TEST_CXX_LIBRARIES} ${SANITIZER_TEST_CXX_LIBRARIES}
-no-pie) -no-pie)
append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS) if (COMPILER_RT_USE_ATOMIC_LIBRARY)
list(APPEND RTSAN_UNITTEST_LINK_FLAGS ${COMPILER_RT_ATOMIC_LIBRARY})
if (NOT WIN32)
get_filename_component(atomic_dir ${COMPILER_RT_ATOMIC_LIBRARY} DIRECTORY)
list(APPEND RTSAN_UNITTEST_LINK_FLAGS "-Wl,-rpath,${atomic_dir}")
endif()
else()
append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS)
endif()
append_list_if(COMPILER_RT_HAS_LIBDL -ldl RTSAN_UNITTEST_LINK_FLAGS) append_list_if(COMPILER_RT_HAS_LIBDL -ldl RTSAN_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS) append_list_if(COMPILER_RT_HAS_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS) append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS)