Skip to content

Commit 7df9b88

Browse files
committed
Check if we need to link against libatomic
On some 32-bit platforms, explicit linking against libatomic is required for atomics to be utilized. Test that at the CMake level, adding an import target as we did for `std::filesystem` that can be used. Based on the suggestion from: #81
1 parent 39cd4b0 commit 7df9b88

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ endif()
4141
# Our custom Filesystem module creates the std::filesystem library target for
4242
# any special dependencies needed for using std::filesystem.
4343
find_package( Filesystem REQUIRED )
44+
# Similar setup for libatomic; required only on 32-bit systems
45+
find_package( Atomic REQUIRED )
4446

4547
if( NOT XROOTD_EXTERNAL_TINYXML2 )
4648
include(FetchContent)
@@ -77,7 +79,7 @@ add_definitions( -D_FILE_OFFSET_BITS=64 )
7779
add_library(XrdS3Obj OBJECT src/CurlUtil.cc src/S3File.cc src/S3Directory.cc src/S3AccessInfo.cc src/S3FileSystem.cc src/AWSv4-impl.cc src/S3Commands.cc src/HTTPCommands.cc src/TokenFile.cc src/stl_string_utils.cc src/shortfile.cc src/logging.cc)
7880
set_target_properties(XrdS3Obj PROPERTIES POSITION_INDEPENDENT_CODE ON)
7981
target_include_directories(XrdS3Obj PRIVATE ${XRootD_INCLUDE_DIRS})
80-
target_link_libraries(XrdS3Obj ${XRootD_UTILS_LIBRARIES} ${XRootD_SERVER_LIBRARIES} CURL::libcurl OpenSSL::Crypto tinyxml2::tinyxml2 Threads::Threads std::filesystem)
82+
target_link_libraries( XrdS3Obj ${XRootD_UTILS_LIBRARIES} ${XRootD_SERVER_LIBRARIES} CURL::libcurl OpenSSL::Crypto tinyxml2::tinyxml2 Threads::Threads std::filesystem std::atomic )
8183

8284
add_library(XrdS3 MODULE "$<TARGET_OBJECTS:XrdS3Obj>")
8385
target_link_libraries(XrdS3 XrdS3Obj)

cmake/FindAtomic.cmake

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
# Ideas come from
3+
#
4+
# https://gitlab.kitware.com/cmake/cmake/-/issues/17834
5+
#
6+
# But applied to the use of libatomic instead of libstdc++fs.
7+
# The need to do this was highlighted as part of:
8+
# https://github.com/PelicanPlatform/xrootd-s3-http/pull/81
9+
# The original driving use case was 32-bit builds; if we
10+
# decide to drop those, we can rip out the target
11+
12+
include(CheckSourceCompiles)
13+
14+
function( check_working_cxx_atomics varname )
15+
CHECK_SOURCE_COMPILES( CXX "
16+
#include <cstdlib>
17+
#include <atomic>
18+
#include <cstdint>
19+
20+
int main() {
21+
std::atomic<uint8_t> a1;
22+
std::atomic<uint16_t> a2;
23+
std::atomic<uint32_t> a3;
24+
std::atomic<uint64_t> a4;
25+
return a1++ + a2++ + a3++ + a4++;
26+
}" ${varname}
27+
)
28+
endfunction( check_working_cxx_atomics varname )
29+
30+
31+
check_working_cxx_atomics( CXX_ATOMIC_NO_LINK_NEEDED )
32+
33+
set( _found FALSE )
34+
if( CXX_ATOMIC_NO_LINK_NEEDED )
35+
set( _found TRUE )
36+
else()
37+
check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
38+
if (HAVE_LIBATOMIC)
39+
set( OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} )
40+
list( APPEND CMAKE_REQUIRED_LIBRARIES "atomic" )
41+
check_working_cxx_atomics( HAVE_CXX_ATOMICS_WITH_LIB )
42+
set( CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES} )
43+
set( HAVE_CXX_ATOMICS_WITH_LIB TRUE )
44+
set( _found TRUE )
45+
endif()
46+
endif()
47+
48+
add_library( std::atomic INTERFACE IMPORTED )
49+
50+
if( HAVE_CXX_ATOMICS_WITH_LIB )
51+
set_property( TARGET std::atomic APPEND PROPERTY INTERFACE_LINK_LIBRARIES atomic )
52+
endif()
53+
54+
set( Atomic_FOUND ${_found} CACHE BOOL "TRUE if we can run a program using std::atomic" FORCE )
55+
if( Atomic_FIND_REQUIRED AND NOT Atomic_FOUND )
56+
message( FATAL_ERROR "Cannot run simple program using std::atomic" )
57+
endif()

0 commit comments

Comments
 (0)