Skip to content

Commit 26a4edf

Browse files
authored
[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.
1 parent 4640736 commit 26a4edf

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

clang/cmake/caches/Fuchsia-stage2.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
155155
set(BUILTINS_${target}_CMAKE_MODULE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
156156
set(BUILTINS_${target}_CMAKE_EXE_LINKER_FLAG "-fuse-ld=lld" CACHE STRING "")
157157
set(BUILTINS_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "")
158+
set(BUILTINS_${target}_COMPILER_RT_LIBATOMIC_USE_PTHREAD ON CACHE BOOL "")
158159

159160
# Set the per-target runtimes options.
160161
list(APPEND RUNTIME_TARGETS "${target}")
@@ -169,6 +170,7 @@ foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unkn
169170
set(RUNTIMES_${target}_CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "")
170171
set(RUNTIMES_${target}_COMPILER_RT_CXX_LIBRARY "libcxx" CACHE STRING "")
171172
set(RUNTIMES_${target}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "")
173+
set(RUNTIMES_${target}_COMPILER_RT_USE_ATOMIC_LIBRARY ON CACHE BOOL "")
172174
set(RUNTIMES_${target}_COMPILER_RT_USE_LLVM_UNWINDER ON CACHE BOOL "")
173175
set(RUNTIMES_${target}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "")
174176
set(RUNTIMES_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "")

cmake/Modules/HandleCompilerRT.cmake

+10-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ endfunction()
5151
# This calls cache_compiler_rt_library that caches the path to speed up
5252
# repeated invocations with the same `name` and `target`.
5353
function(find_compiler_rt_library name variable)
54-
cmake_parse_arguments(ARG "" "TARGET;FLAGS" "" ${ARGN})
54+
cmake_parse_arguments(ARG "SHARED" "TARGET;FLAGS" "" ${ARGN})
5555
# While we can use compiler-rt runtimes with other compilers, we need to
5656
# query the compiler for runtime location and thus we require Clang.
5757
if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
@@ -112,12 +112,16 @@ function(find_compiler_rt_library name variable)
112112
# path and then checking if the resultant path exists. The result of
113113
# this check is also cached by cache_compiler_rt_library.
114114
set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")
115-
if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")
116-
set(from_name ${CMAKE_MATCH_0})
117-
get_component_name(${name} to_name)
118-
string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}")
119-
cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
115+
get_component_name("builtins" from_name)
116+
get_component_name(${name} to_name)
117+
get_filename_component(basename ${library_file} NAME)
118+
string(REPLACE "${from_name}" "${to_name}" basename "${basename}")
119+
if (ARG_SHARED)
120+
string(REGEX REPLACE "\.(a|lib)$" ".so" basename "${basename}")
120121
endif()
122+
get_filename_component(dirname ${library_file} DIRECTORY)
123+
set(library_file "${dirname}/${basename}")
124+
cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
121125
endif()
122126
set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)
123127
endfunction()

compiler-rt/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ endif()
284284
option(COMPILER_RT_USE_BUILTINS_LIBRARY
285285
"Use compiler-rt builtins instead of libgcc" ${DEFAULT_COMPILER_RT_USE_BUILTINS_LIBRARY})
286286

287+
option(COMPILER_RT_USE_ATOMIC_LIBRARY "Use compiler-rt atomic instead of libatomic" OFF)
288+
287289
include(config-ix)
288290

289291
#================================

compiler-rt/cmake/config-ix.cmake

+8-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
7474
endif()
7575
endif ()
7676

77+
if (COMPILER_RT_USE_ATOMIC_LIBRARY)
78+
include(HandleCompilerRT)
79+
find_compiler_rt_library(atomic COMPILER_RT_ATOMIC_LIBRARY SHARED
80+
FLAGS ${SANITIZER_COMMON_FLAGS})
81+
else()
82+
check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
83+
endif()
84+
7785
# CodeGen options.
7886
check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG)
7987
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)
175183
check_include_files("sys/auxv.h" COMPILER_RT_HAS_AUXV)
176184

177185
# Libraries.
178-
check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
179186
check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL)
180187
check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT)
181188
check_library_exists(m pow "" COMPILER_RT_HAS_LIBM)

compiler-rt/lib/rtsan/tests/CMakeLists.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ set(RTSAN_UNITTEST_LINK_FLAGS
3636
${SANITIZER_TEST_CXX_LIBRARIES}
3737
-no-pie)
3838

39-
append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS)
39+
if (COMPILER_RT_USE_ATOMIC_LIBRARY)
40+
list(APPEND RTSAN_UNITTEST_LINK_FLAGS ${COMPILER_RT_ATOMIC_LIBRARY})
41+
if (NOT WIN32)
42+
get_filename_component(atomic_dir ${COMPILER_RT_ATOMIC_LIBRARY} DIRECTORY)
43+
list(APPEND RTSAN_UNITTEST_LINK_FLAGS "-Wl,-rpath,${atomic_dir}")
44+
endif()
45+
else()
46+
append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RTSAN_UNITTEST_LINK_FLAGS)
47+
endif()
4048
append_list_if(COMPILER_RT_HAS_LIBDL -ldl RTSAN_UNITTEST_LINK_FLAGS)
4149
append_list_if(COMPILER_RT_HAS_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS)
4250
append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS)

0 commit comments

Comments
 (0)