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

via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 29 11:23:15 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Petr Hosek (petrhosek)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/106603.diff


5 Files Affected:

- (modified) clang/cmake/caches/Fuchsia-stage2.cmake (+1) 
- (modified) cmake/Modules/HandleCompilerRT.cmake (+10-6) 
- (modified) compiler-rt/CMakeLists.txt (+2) 
- (modified) compiler-rt/cmake/config-ix.cmake (+8-1) 
- (modified) compiler-rt/lib/rtsan/tests/CMakeLists.txt (+5-1) 


``````````diff
diff --git a/clang/cmake/caches/Fuchsia-stage2.cmake b/clang/cmake/caches/Fuchsia-stage2.cmake
index 4ef37a5fad67f5..a6d619c5fe714b 100644
--- a/clang/cmake/caches/Fuchsia-stage2.cmake
+++ b/clang/cmake/caches/Fuchsia-stage2.cmake
@@ -169,6 +169,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}_COMPILER_RT_CXX_LIBRARY "libcxx" CACHE STRING "")
     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_CAN_EXECUTE_TESTS ON CACHE BOOL "")
     set(RUNTIMES_${target}_COMPILER_RT_BUILD_STANDALONE_LIBATOMIC ON CACHE BOOL "")
diff --git a/cmake/Modules/HandleCompilerRT.cmake b/cmake/Modules/HandleCompilerRT.cmake
index 6865f45175ed79..0a7c56bc630934 100644
--- a/cmake/Modules/HandleCompilerRT.cmake
+++ b/cmake/Modules/HandleCompilerRT.cmake
@@ -51,7 +51,7 @@ endfunction()
 # This calls cache_compiler_rt_library that caches the path to speed up
 # repeated invocations with the same `name` and `target`.
 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
   # query the compiler for runtime location and thus we require 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
     # this check is also cached by cache_compiler_rt_library.
     set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")
-    if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")
-      set(from_name ${CMAKE_MATCH_0})
-      get_component_name(${name} to_name)
-      string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}")
-      cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
+    get_component_name("builtins" from_name)
+    get_component_name(${name} to_name)
+    get_filename_component(basename ${library_file} NAME)
+    string(REPLACE "${from_name}" "${to_name}" basename "${basename}")
+    if (ARG_SHARED)
+      string(REGEX REPLACE "\.(a|lib)$" ".so" basename "${basename}")
     endif()
+    get_filename_component(dirname ${library_file} DIRECTORY)
+    set(library_file "${dirname}/${basename}")
+    cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
   endif()
   set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)
 endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index 2207555b03a03f..57914c3175e815 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -284,6 +284,8 @@ endif()
 option(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)
 
 #================================
diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index 97204177cde9a4..a93a88a9205001 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -74,6 +74,14 @@ if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
   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.
 check_c_compiler_flag(-ffreestanding         COMPILER_RT_HAS_FFREESTANDING_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)
 
 # Libraries.
-check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
 check_library_exists(dl dlopen "" COMPILER_RT_HAS_LIBDL)
 check_library_exists(rt shm_open "" COMPILER_RT_HAS_LIBRT)
 check_library_exists(m pow "" COMPILER_RT_HAS_LIBM)
diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 0320bbad592186..1681f1062e1ba1 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -36,7 +36,11 @@ set(RTSAN_UNITTEST_LINK_FLAGS
   ${SANITIZER_TEST_CXX_LIBRARIES}
   -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})
+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_LIBRT -lrt RTSAN_UNITTEST_LINK_FLAGS)
 append_list_if(COMPILER_RT_HAS_LIBM -lm RTSAN_UNITTEST_LINK_FLAGS)

``````````

</details>


https://github.com/llvm/llvm-project/pull/106603


More information about the cfe-commits mailing list