|
| 1 | +# Check if _Atomic needs -latomic |
| 2 | + |
| 3 | +set(LIBATOMIC_STATIC_PATH "" CACHE PATH "Directory containing static libatomic.a") |
| 4 | + |
| 5 | +include(CheckCSourceCompiles) |
| 6 | + |
| 7 | +set( |
| 8 | + check_std_atomic_source_code |
| 9 | + [=[ |
| 10 | + #include <stdatomic.h> |
| 11 | + _Atomic long long x = 0; |
| 12 | + atomic_uint y = 0; |
| 13 | + void test(_Atomic long long *x, long long v) { |
| 14 | + atomic_store(x, v); |
| 15 | + y = v + 1; |
| 16 | + } |
| 17 | + int main(int argc, char **argv) { |
| 18 | + test(&x, argc); |
| 19 | + return 0; |
| 20 | + } |
| 21 | + ]=]) |
| 22 | + |
| 23 | +check_c_source_compiles("${check_std_atomic_source_code}" std_atomic_without_libatomic) |
| 24 | + |
| 25 | +if(NOT std_atomic_without_libatomic) |
| 26 | + set(CMAKE_REQUIRED_LIBRARIES atomic) |
| 27 | + check_c_source_compiles("${check_std_atomic_source_code}" std_atomic_with_libatomic) |
| 28 | + set(CMAKE_REQUIRED_LIBRARIES) |
| 29 | + if(NOT std_atomic_with_libatomic) |
| 30 | + message(FATAL_ERROR "Toolchain doesn't support C11 _Atomic with nor without -latomic") |
| 31 | + else() |
| 32 | + find_library(ATOMIC_STATIC NAMES libatomic.a PATHS /usr/lib /usr/local/lib ${LIBATOMIC_STATIC_PATH} NO_DEFAULT_PATH) |
| 33 | + if(ATOMIC_STATIC) |
| 34 | + get_filename_component(ATOMIC_STATIC_DIR "${ATOMIC_STATIC}" DIRECTORY) |
| 35 | + get_filename_component(ATOMIC_STATIC_NAME "${ATOMIC_STATIC}" NAME) |
| 36 | + message(STATUS "Linking static libatomic: -L${ATOMIC_STATIC_DIR} -l:${ATOMIC_STATIC_NAME}") |
| 37 | + set(EXTRA_PRIVATE_LIBS "-L${ATOMIC_STATIC_DIR} -l:${ATOMIC_STATIC_NAME}") |
| 38 | + if(ENABLE_SHARED) |
| 39 | + target_link_directories(kqueue PRIVATE "${ATOMIC_STATIC_DIR}") |
| 40 | + target_link_libraries(kqueue PRIVATE "-l:${ATOMIC_STATIC_NAME}") |
| 41 | + endif() |
| 42 | + else() |
| 43 | + message(WARNING "static libatomic not found; falling back to -latomic") |
| 44 | + set(EXTRA_PRIVATE_LIBS "-latomic") |
| 45 | + if(ENABLE_SHARED) |
| 46 | + target_link_libraries(kqueue PRIVATE atomic) |
| 47 | + endif() |
| 48 | + endif() |
| 49 | + endif() |
| 50 | +endif() |
0 commit comments