Skip to content

Commit

Permalink
build with -latomic on platforms that need it
Browse files Browse the repository at this point in the history
  • Loading branch information
niol committed May 22, 2024
1 parent 6c1cee5 commit 24b96cb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions cmake/CheckAtomic.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# - Try to find if 64-bits atomics need -latomic linking
# Once done this will define
# HAVE_CXX_ATOMICS_WITHOUT_LIB - Whether atomic types work without -latomic

INCLUDE(CheckCXXSourceCompiles)
INCLUDE(CheckLibraryExists)

# Sometimes linking against libatomic is required for atomic ops, if
# the platform doesn't support lock-free atomics.

function(check_working_cxx_atomics varname)
CHECK_CXX_SOURCE_COMPILES("
#include <atomic>
int main() {
std::atomic<long long> x;
return std::atomic_is_lock_free(&x);
}
" ${varname})
endfunction(check_working_cxx_atomics)

# Check for atomic operations.
if(MSVC)
# This isn't necessary on MSVC.
set(HAVE_CXX_ATOMICS_WITHOUT_LIB True)
else()
# First check if atomics work without the library.
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
endif()

# If not, check if the library exists, and atomics work with it.
if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
check_library_exists(atomic __atomic_load_8 "" HAVE_LIBATOMIC)
if(NOT HAVE_LIBATOMIC)
message(STATUS "Host compiler appears to require libatomic, but cannot locate it.")
endif()
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
message(FATAL_ERROR "Host compiler must support std::atomic!")
endif()
endif()
5 changes: 5 additions & 0 deletions libtransmission/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include(CheckLibraryExists)
include(CheckSymbolExists)
include(CheckAtomic)

if(ENABLE_NLS)
check_library_exists(intl libintl_gettext "" HAVE_LIBINTL)
Expand Down Expand Up @@ -305,6 +306,10 @@ target_link_libraries(${TR_NAME}
small::small
libevent::event)

if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
target_link_libraries(${TR_NAME} PRIVATE atomic)
endif()

if(INSTALL_LIB)
install(
TARGETS ${TR_NAME}
Expand Down

0 comments on commit 24b96cb

Please sign in to comment.