To use atomics functions, some toolchains requires to explicitly add -latomic to the linker flags (because they are not provided by libc, but libatomic).
This change adds a helper function trying to build/link a test program using atomics, then calls it to: * first check if atomics are directly available in the libc; * if not and libatomic exists, then run the same test with "-latomic" added to the linker flags. The pulseview link library list is updated according to the results of these tests. This issue was triggered by the Buildroot farms: http://autobuild.buildroot.org/results/1e3/1e3101261252d5f30fdf842cc99604e4f4c25eef/build-end.log Signed-off-by: Samuel Martin <[email protected]> --- CMakeLists.txt | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dac69f..385acc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,6 @@ endif() #=============================================================================== #= Dependencies #------------------------------------------------------------------------------- - list(APPEND PKGDEPS libsigrokcxx>=0.4.0) if(ENABLE_DECODE) @@ -107,6 +106,53 @@ endif() # This will set ${CMAKE_THREAD_LIBS_INIT} to the correct, OS-specific value. find_package(Threads REQUIRED) + +# Check for explicit link against libatomic +# +# Depending on the toolchain, linking a program using atomic functions may need +# "-latomic" explicitly passed to the linker +# +# This check first tests if atomics are available in the C-library, if not and +# libatomic exists, then it runs the same test with -latomic added to the +# linker flags. + +include(CheckCXXSourceCompiles) +include(CheckLibraryExists) + +# Helper for checking for atomics +function(check_working_cxx_atomics varname) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "-std=c++11") + CHECK_CXX_SOURCE_COMPILES(" +#include <atomic> +std::atomic<int> x; +int main() { + return (int)__atomic_fetch_add_4(&x, 1, __ATOMIC_SEQ_CST); +} +" ${varname}) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endfunction(check_working_cxx_atomics) + +# First check if atomics work without the library. +check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) +# If not, check if the library exists, and atomics work with it. +if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) + CHECK_LIBRARY_EXISTS(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) + if( HAVE_LIBATOMIC ) + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) + if (NOT HAVE_CXX_ATOMICS_WITH_LIB) + message(FATAL_ERROR "Host compiler must support std::atomic!") + endif() + else() + message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") + endif() +endif() + +if(HAVE_CXX_ATOMICS_WITH_LIB) + set(ATOMIC_LIBRARIES "-latomic") +endif() + #=============================================================================== #= System Introspection #------------------------------------------------------------------------------- @@ -387,6 +433,7 @@ set(PULSEVIEW_LINK_LIBS ${Boost_LIBRARIES} ${QT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} + ${ATOMIC_LIBRARIES} ) if(STATIC_PKGDEPS_LIBS) -- 2.8.3 ------------------------------------------------------------------------------ Mobile security can be enabling, not merely restricting. Employees who bring their own devices (BYOD) to work are irked by the imposition of MDM restrictions. Mobile Device Manager Plus allows you to control only the apps on BYO-devices by containerizing them, leaving personal data untouched! https://ad.doubleclick.net/ddm/clk/304595813;131938128;j _______________________________________________ sigrok-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/sigrok-devel

