[clang] [compiler-rt] [llvm] [CMake][compiler-rt] Support for using compiler-rt atomic library (PR #106603)
Petr Hosek via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 3 10:28:05 PDT 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/106603
>From 18814eb544e4e8d9274583c41e65b7f16c3bdfcc Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Thu, 29 Aug 2024 18:18:42 +0000
Subject: [PATCH 1/2] [CMake][compiler-rt] Support for using compiler-rt atomic
library
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.
---
clang/cmake/caches/Fuchsia-stage2.cmake | 1 +
cmake/Modules/HandleCompilerRT.cmake | 16 ++++++++++------
compiler-rt/CMakeLists.txt | 2 ++
compiler-rt/cmake/config-ix.cmake | 9 ++++++++-
compiler-rt/lib/rtsan/tests/CMakeLists.txt | 6 +++++-
5 files changed, 26 insertions(+), 8 deletions(-)
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)
>From 7c2a46bfc0bfbaa577c5fad7b377c976238099b7 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 3 Sep 2024 17:27:05 +0000
Subject: [PATCH 2/2] Set runpath for the RTSan tests
---
clang/cmake/caches/Fuchsia-stage2.cmake | 1 +
compiler-rt/lib/rtsan/tests/CMakeLists.txt | 4 ++++
2 files changed, 5 insertions(+)
diff --git a/clang/cmake/caches/Fuchsia-stage2.cmake b/clang/cmake/caches/Fuchsia-stage2.cmake
index a6d619c5fe714b..73334b7320731a 100644
--- a/clang/cmake/caches/Fuchsia-stage2.cmake
+++ b/clang/cmake/caches/Fuchsia-stage2.cmake
@@ -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_EXE_LINKER_FLAG "-fuse-ld=lld" CACHE STRING "")
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.
list(APPEND RUNTIME_TARGETS "${target}")
diff --git a/compiler-rt/lib/rtsan/tests/CMakeLists.txt b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
index 1681f1062e1ba1..5883d33d31895f 100644
--- a/compiler-rt/lib/rtsan/tests/CMakeLists.txt
+++ b/compiler-rt/lib/rtsan/tests/CMakeLists.txt
@@ -38,6 +38,10 @@ set(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()
More information about the cfe-commits
mailing list