From b8163c933bc946ac87fadead3227c229fb573955 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 10 Nov 2020 19:43:14 +0100 Subject: [PATCH] Link with libatomic when needed Linking with libatomic is apparently needed when using functionality with GCC on ARM and PowerPC. --- cmake/StandardSettings.cmake | 2 ++ cmake/StdAtomic.cmake | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 cmake/StdAtomic.cmake diff --git a/cmake/StandardSettings.cmake b/cmake/StandardSettings.cmake index 88d8dff0b3..a0f0189c7d 100644 --- a/cmake/StandardSettings.cmake +++ b/cmake/StandardSettings.cmake @@ -47,6 +47,8 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "^GNU|(Apple)?Clang$") INTERFACE -fsanitize=${SANITIZER}) endforeach() + include(StdAtomic) + elseif(MSVC) target_compile_options(standard_settings INTERFACE /std:c++latest /Zc:preprocessor /Zc:__cplusplus /D_CRT_SECURE_NO_WARNINGS) endif() diff --git a/cmake/StdAtomic.cmake b/cmake/StdAtomic.cmake new file mode 100644 index 0000000000..aa3bb4bff0 --- /dev/null +++ b/cmake/StdAtomic.cmake @@ -0,0 +1,31 @@ +# Check if std::atomic needs -latomic + +include(CheckCXXSourceCompiles) + +set(CMAKE_REQUIRED_FLAGS ${CMAKE_CXX11_STANDARD_COMPILE_OPTION}) +set( + check_std_atomic_source_code + [=[ + #include + int main() + { + std::atomic x; + (void)x.load(); + return 0; + } + ]=]) + +check_cxx_source_compiles("${check_std_atomic_source_code}" std_atomic_without_libatomic) + +if(NOT std_atomic_without_libatomic) + set(CMAKE_REQUIRED_LIBRARIES atomic) + check_cxx_source_compiles("${check_std_atomic_source_code}" std_atomic_with_libatomic) + set(CMAKE_REQUIRED_LIBRARIES) + if(NOT std_atomic_with_libatomic) + message(FATAL_ERROR "Toolchain doesn't support std::atomic with nor without -latomic") + else() + target_link_libraries(standard_settings INTERFACE atomic) + endif() +endif() + +set(CMAKE_REQUIRED_FLAGS)