Skip to content

Commit

Permalink
Simplify libatomic detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwalisch committed Jan 2, 2024
1 parent 66433f3 commit 84e7da6
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions cmake/libatomic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,25 @@ if(NOT atomic64)
# instead of lib/libatomic.dylib. CMake's find_library() cannot easily
# be used to recursively find libraries. Therefore we use this workaround
# here (try adding -latomic to linker options) for this use case.
set(CMAKE_REQUIRED_LINK_OPTIONS "-latomic")
set(LIBATOMIC "-latomic")
message(STATUS "Add linker flag: ${LIBATOMIC}")
endif()

check_cxx_source_compiles("
#include <atomic>
#include <stdint.h>
int main() {
std::atomic<int64_t> x;
x = 1;
x--;
return (int) x;
}"
atomic_linker_flag)
set(CMAKE_REQUIRED_LIBRARIES "${LIB_ATOMIC}")

if (atomic_linker_flag)
set(LIBATOMIC "-latomic")
message(STATUS "Add linker flag: ${LIBATOMIC}")
else()
# This code block is used to print a nice error message to the
# user. If atomic32 compiles and atomic64 does not compile then
# this is almost likely due to the missing libatomic library.
check_cxx_source_compiles("
#include <atomic>
#include <stdint.h>
int main() {
std::atomic<int32_t> x;
x = 1;
x--;
return (int) x;
}"
atomic32)
check_cxx_source_compiles("
#include <atomic>
#include <stdint.h>
int main() {
std::atomic<int64_t> x;
x = 1;
x--;
return (int) x;
}"
atomic64_with_libatomic)

if(atomic32)
message(FATAL_ERROR "Failed to find libatomic!")
endif()
endif()
if (NOT atomic64_with_libatomic)
message(FATAL "Failed to compile std::atomic, libatomic likely not found!")
endif()
endif()

Expand Down

0 comments on commit 84e7da6

Please sign in to comment.