Skip to content

Commit fa981b5

Browse files
thesamesammgorny
authored andcommitted
Set LLVM_ATOMIC_LIB variable for convenient linking against libatomic
* Set LLVM_ATOMIC_LIB to keep track of when we need to link against libatomic. * Add detection of mold linker which is required for this. * Use --as-needed when linking against libatomic as a bonus. On some platforms, libatomic may be required only sometimes. Bug: https://bugs.gentoo.org/832675 Thanks-to: Arfrever Frehtes Taifersar Arahesis <[email protected]> Tested-by: [email protected] <[email protected]> Differential Revision: https://reviews.llvm.org/D136280
1 parent f62d8a1 commit fa981b5

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
219219
else()
220220
if("${stdout}" MATCHES "^mold")
221221
set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
222+
set(LLVM_LINKER_IS_MOLD YES CACHE INTERNAL "")
222223
message(STATUS "Linker detection: mold")
223224
elseif("${stdout}" MATCHES "GNU gold")
224225
set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")

llvm/cmake/modules/CheckAtomic.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL")
8282
endif()
8383
endif()
8484

85+
# Set variable LLVM_ATOMIC_LIB specifying flags for linking against libatomic.
86+
if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
87+
# Use options --push-state, --as-needed and --pop-state if linker is known to support them.
88+
# Use single option -Wl of compiler driver to avoid incorrect re-ordering of options by CMake.
89+
if(LLVM_LINKER_IS_GNULD OR LLVM_LINKER_IS_GOLD OR LLVM_LINKER_IS_LLD OR LLVM_LINKER_IS_MOLD)
90+
set(LLVM_ATOMIC_LIB "-Wl,--push-state,--as-needed,-latomic,--pop-state")
91+
else()
92+
set(LLVM_ATOMIC_LIB "-latomic")
93+
endif()
94+
else()
95+
set(LLVM_ATOMIC_LIB)
96+
endif()
97+
8598
## TODO: This define is only used for the legacy atomic operations in
8699
## llvm's Atomic.h, which should be replaced. Other code simply
87100
## assumes C++11 <atomic> works.

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ elseif( CMAKE_HOST_UNIX )
5959
if( LLVM_ENABLE_TERMINFO )
6060
set(imported_libs ${imported_libs} Terminfo::terminfo)
6161
endif()
62-
if( LLVM_ENABLE_THREADS AND (HAVE_LIBATOMIC OR HAVE_CXX_LIBATOMICS64) )
63-
set(system_libs ${system_libs} atomic)
64-
endif()
62+
set(system_libs ${system_libs} ${LLVM_ATOMIC_LIB})
6563
set(system_libs ${system_libs} ${LLVM_PTHREAD_LIB})
6664
if( UNIX AND NOT (BEOS OR HAIKU) )
6765
set(system_libs ${system_libs} m)

llvm/tools/dsymutil/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,4 @@ if(APPLE)
4040
target_link_libraries(dsymutil PRIVATE "-framework CoreFoundation")
4141
endif(APPLE)
4242

43-
if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
44-
target_link_libraries(dsymutil PRIVATE atomic)
45-
endif()
43+
target_link_libraries(dsymutil PRIVATE ${LLVM_ATOMIC_LIB})

0 commit comments

Comments
 (0)