|
| 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