Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 1 | INCLUDE(CheckCXXSourceCompiles) |
| 2 | |
| 3 | # Sometimes linking against libatomic is required for atomic ops, if |
| 4 | # the platform doesn't support lock-free atomics. |
| 5 | # |
| 6 | # We could modify LLVM's CheckAtomic module and have it check for 64-bit |
| 7 | # atomics instead. However, we would like to avoid careless uses of 64-bit |
| 8 | # atomics inside LLVM over time on 32-bit platforms. |
| 9 | |
| 10 | function(check_cxx_atomics varname) |
| 11 | set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
Eric Fiselier | 100ce64 | 2016-02-20 00:24:43 +0000 | [diff] [blame] | 12 | set(CMAKE_REQUIRED_FLAGS "-std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include") |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 13 | if (${LIBCXX_GCC_TOOLCHAIN}) |
Eric Fiselier | 100ce64 | 2016-02-20 00:24:43 +0000 | [diff] [blame] | 14 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}") |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 15 | endif() |
| 16 | check_cxx_source_compiles(" |
| 17 | #include <cstdint> |
| 18 | #include <atomic> |
| 19 | std::atomic<uintptr_t> x; |
| 20 | std::atomic<uintmax_t> y; |
| 21 | int main() { |
| 22 | return x + y; |
| 23 | } |
| 24 | " ${varname}) |
| 25 | set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 26 | endfunction(check_cxx_atomics) |
| 27 | |
Eric Fiselier | df69432 | 2016-02-11 15:05:56 +0000 | [diff] [blame] | 28 | check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 29 | # If not, check if the library exists, and atomics work with it. |
Eric Fiselier | df69432 | 2016-02-11 15:05:56 +0000 | [diff] [blame] | 30 | if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 31 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
| 32 | if(LIBCXX_HAS_ATOMIC_LIB) |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 33 | list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
Eric Fiselier | 629135a | 2016-02-11 15:52:52 +0000 | [diff] [blame] | 34 | check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) |
| 35 | if (NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) |
Vasileios Kalintiris | fea3b21 | 2016-02-16 14:15:27 +0000 | [diff] [blame] | 36 | message(WARNING "Host compiler must support std::atomic!") |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 37 | endif() |
| 38 | else() |
Vasileios Kalintiris | fea3b21 | 2016-02-16 14:15:27 +0000 | [diff] [blame] | 39 | message(WARNING "Host compiler appears to require libatomic, but cannot find it.") |
Vasileios Kalintiris | cb6641a | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 40 | endif() |
| 41 | endif() |