diff options
author | Sam James <sam@gentoo.org> | 2022-10-19 19:50:20 +0100 |
---|---|---|
committer | Tom Stellard <tstellar@redhat.com> | 2022-11-10 17:04:34 -0800 |
commit | d75ae21044ad893572855cefb0c0898a771b2094 (patch) | |
tree | 4aaba37bf52f02de40912d92ea72faf0e16c51a2 | |
parent | 6750e341b076d10a336c662ed6916500fdb20105 (diff) | |
download | llvm-d75ae21044ad893572855cefb0c0898a771b2094.zip llvm-d75ae21044ad893572855cefb0c0898a771b2094.tar.gz llvm-d75ae21044ad893572855cefb0c0898a771b2094.tar.bz2 |
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 <Arfrever@Apache.Org>
Tested-by: erhard_f@mailbox.org <erhard_f@mailbox.org>
Differential Revision: https://reviews.llvm.org/D136280
(cherry picked from commit fa981b541365190ae646d2dce575706cd0626cf7)
-rw-r--r-- | llvm/cmake/modules/AddLLVM.cmake | 1 | ||||
-rw-r--r-- | llvm/cmake/modules/CheckAtomic.cmake | 13 | ||||
-rw-r--r-- | llvm/lib/Support/CMakeLists.txt | 4 | ||||
-rw-r--r-- | llvm/tools/dsymutil/CMakeLists.txt | 4 |
4 files changed, 16 insertions, 6 deletions
diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index 0574312..1f0507f 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -212,6 +212,7 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32) else() if("${stdout}" MATCHES "^mold") set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") + set(LLVM_LINKER_IS_MOLD YES CACHE INTERNAL "") message(STATUS "Linker detection: mold") elseif("${stdout}" MATCHES "GNU gold") set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "") diff --git a/llvm/cmake/modules/CheckAtomic.cmake b/llvm/cmake/modules/CheckAtomic.cmake index 3c5ba72..f11cadf 100644 --- a/llvm/cmake/modules/CheckAtomic.cmake +++ b/llvm/cmake/modules/CheckAtomic.cmake @@ -82,6 +82,19 @@ elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE OR CMAKE_CXX_COMPILER_ID MATCHES "XL") endif() endif() +# Set variable LLVM_ATOMIC_LIB specifying flags for linking against libatomic. +if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) + # Use options --push-state, --as-needed and --pop-state if linker is known to support them. + # Use single option -Wl of compiler driver to avoid incorrect re-ordering of options by CMake. + if(LLVM_LINKER_IS_GNULD OR LLVM_LINKER_IS_GOLD OR LLVM_LINKER_IS_LLD OR LLVM_LINKER_IS_MOLD) + set(LLVM_ATOMIC_LIB "-Wl,--push-state,--as-needed,-latomic,--pop-state") + else() + set(LLVM_ATOMIC_LIB "-latomic") + endif() +else() + set(LLVM_ATOMIC_LIB) +endif() + ## TODO: This define is only used for the legacy atomic operations in ## llvm's Atomic.h, which should be replaced. Other code simply ## assumes C++11 <atomic> works. diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index 806cbc8..ff23ec7 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -59,9 +59,7 @@ elseif( CMAKE_HOST_UNIX ) if( LLVM_ENABLE_TERMINFO ) set(imported_libs ${imported_libs} Terminfo::terminfo) endif() - if( LLVM_ENABLE_THREADS AND (HAVE_LIBATOMIC OR HAVE_CXX_LIBATOMICS64) ) - set(system_libs ${system_libs} atomic) - endif() + set(system_libs ${system_libs} ${LLVM_ATOMIC_LIB}) set(system_libs ${system_libs} ${LLVM_PTHREAD_LIB}) if( UNIX AND NOT (BEOS OR HAIKU) ) set(system_libs ${system_libs} m) diff --git a/llvm/tools/dsymutil/CMakeLists.txt b/llvm/tools/dsymutil/CMakeLists.txt index a255c1c..38028cd 100644 --- a/llvm/tools/dsymutil/CMakeLists.txt +++ b/llvm/tools/dsymutil/CMakeLists.txt @@ -40,6 +40,4 @@ if(APPLE) target_link_libraries(dsymutil PRIVATE "-framework CoreFoundation") endif(APPLE) -if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) - target_link_libraries(dsymutil PRIVATE atomic) -endif() +target_link_libraries(dsymutil PRIVATE ${LLVM_ATOMIC_LIB}) |