1 From 71830c804be76cf6abe913ac2fe584947b7a91ea Mon Sep 17 00:00:00 2001
2 From: Samuel Martin <s.martin49@gmail.com>
3 Date: Tue, 24 May 2016 23:08:40 +0200
4 Subject: [PATCH] cmake: add check for explicit linking against libatomic
6 To use atomics functions, some toolchains requires to explicitly add
7 -latomic to the linker flags (because they are not provided by libc,
10 This change adds a helper function trying to build/link a test program
11 using atomics, then calls it to:
12 * first check if atomics are directly available in the libc;
13 * if not and libatomic has been found, then run the same test with
14 "-latomic" added to the linker flags.
15 The pulseview link library list is updated according to the results of
18 This issue was triggered by the Buildroot farms:
19 http://autobuild.buildroot.org/results/1e3/1e3101261252d5f30fdf842cc99604e4f4c25eef/build-end.log
22 1- CMAKE_REQUIRED_* variables are only used in check functions. They
23 are not automatically forwarded to/handled by the target commands
24 (such as target_link_library), because the check functions are
25 implemented as macro in CMake code, whereas many target commands
27 2- Because of note #1, CMAKE_REQUIRED_LIBRARIES (or its value) must be
28 explicitly passed to the target_link_library command when this is
30 3- In this implementation, LIBATOMIC_LIBRARY is only set when it is
31 needed; so, unconditionally appending it to PULSEVIEW_LINK_LIBS
32 will produce the expected behavior.
34 Signed-off-by: Samuel Martin <s.martin49@gmail.com>
38 - use std::atomic_fetch_add_explicit function instead of
40 - rework code using cmake_*_check_state and find_library helpers;
41 - quiet-ize checks and clean outputs
42 - extend the commit log
44 CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
45 1 file changed, 50 insertions(+)
47 diff --git a/CMakeLists.txt b/CMakeLists.txt
48 index 9dac69f..44f810e 100644
51 @@ -107,6 +107,55 @@ endif()
52 # This will set ${CMAKE_THREAD_LIBS_INIT} to the correct, OS-specific value.
53 find_package(Threads REQUIRED)
56 +# Check for explicit link against libatomic
58 +# Depending on the toolchain, linking a program using atomic functions may need
59 +# "-latomic" explicitly passed to the linker
61 +# This check first tests if atomics are available in the C-library, if not and
62 +# libatomic exists, then it runs the same test with -latomic added to the
65 +# Helper for checking for atomics
66 +function(check_working_cxx_atomics varname additional_lib)
67 + include(CheckCXXSourceCompiles)
68 + include(CMakePushCheckState)
69 + cmake_push_check_state()
70 + set(CMAKE_REQUIRED_FLAGS "-std=c++11")
71 + set(CMAKE_REQUIRED_LIBRARIES "${additional_lib}")
72 + set(CMAKE_REQUIRED_QUIET 1)
73 + CHECK_CXX_SOURCE_COMPILES("
77 + return std::atomic_fetch_add_explicit(&x, 1, std::memory_order_seq_cst);
80 + cmake_pop_check_state()
81 +endfunction(check_working_cxx_atomics)
83 +# First check if atomics work without the library.
84 +# If not, check if the library exists, and atomics work with it.
85 +check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB "")
86 +if(HAVE_CXX_ATOMICS_WITHOUT_LIB)
87 + message(STATUS "Atomics provided by the C-library - yes")
89 + message(STATUS "Atomics provided by the C-library - no")
90 + find_library(LIBATOMIC_LIBRARY NAMES atomic PATH_SUFFIXES lib)
91 + if(LIBATOMIC_LIBRARY)
92 + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB "${LIBATOMIC_LIBRARY}")
93 + if (HAVE_CXX_ATOMICS_WITH_LIB)
94 + message(STATUS "Atomics provided by libatomic - yes")
96 + message(STATUS "Atomics provided by libatomic - no")
97 + message(FATAL_ERROR "Compiler must support std::atomic!")
100 + message(FATAL_ERROR "Compiler appears to require libatomic, but cannot find it.")
104 #===============================================================================
105 #= System Introspection
106 #-------------------------------------------------------------------------------
107 @@ -387,6 +436,7 @@ set(PULSEVIEW_LINK_LIBS
110 ${CMAKE_THREAD_LIBS_INIT}
111 + ${LIBATOMIC_LIBRARY}
114 if(STATIC_PKGDEPS_LIBS)