Vasileios Kalintiris | d5ff320 | 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}) |
Don Hinton | 8f56959 | 2018-01-23 03:30:23 +0000 | [diff] [blame] | 12 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs -std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include") |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 13 | if (${LIBCXX_GCC_TOOLCHAIN}) |
Eric Fiselier | 92a984e | 2016-02-20 00:24:43 +0000 | [diff] [blame] | 14 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}") |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 15 | endif() |
Saleem Abdulrasool | bd21dad | 2016-08-29 21:33:37 +0000 | [diff] [blame] | 16 | if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) |
| 17 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") |
| 18 | endif() |
Ivan Krasin | 575076d | 2016-09-01 01:38:32 +0000 | [diff] [blame] | 19 | if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) |
| 20 | set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") |
| 21 | endif() |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 22 | check_cxx_source_compiles(" |
| 23 | #include <cstdint> |
| 24 | #include <atomic> |
| 25 | std::atomic<uintptr_t> x; |
| 26 | std::atomic<uintmax_t> y; |
JF Bastien | b7b53ca | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 27 | int main(int, char**) { |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 28 | return x + y; |
| 29 | } |
| 30 | " ${varname}) |
| 31 | set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 32 | endfunction(check_cxx_atomics) |
| 33 | |
Simon Dardis | f856d12 | 2018-04-04 11:05:03 +0000 | [diff] [blame] | 34 | # Perform the check for 64bit atomics without libatomic. It may have been |
| 35 | # added to the required libraries during in the configuration of LLVM, which |
| 36 | # would cause the check for CXX atomics without libatomic to incorrectly pass. |
Brian Cain | 5977ece | 2019-05-02 03:49:50 +0000 | [diff] [blame] | 37 | if (CMAKE_REQUIRED_LIBRARIES) |
| 38 | set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) |
| 39 | list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "atomic") |
| 40 | check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 41 | set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) |
| 42 | endif() |
Simon Dardis | f856d12 | 2018-04-04 11:05:03 +0000 | [diff] [blame] | 43 | |
Eric Fiselier | 9ed71b3 | 2016-07-18 06:01:50 +0000 | [diff] [blame] | 44 | check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 45 | # If not, check if the library exists, and atomics work with it. |
Eric Fiselier | 76498d9 | 2016-02-11 15:05:56 +0000 | [diff] [blame] | 46 | if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) |
Eric Fiselier | 76498d9 | 2016-02-11 15:05:56 +0000 | [diff] [blame] | 47 | if(LIBCXX_HAS_ATOMIC_LIB) |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 48 | list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
Eric Fiselier | 56c1c1e | 2016-02-11 15:52:52 +0000 | [diff] [blame] | 49 | check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) |
| 50 | if (NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) |
Vasileios Kalintiris | 38023b7 | 2016-02-16 14:15:27 +0000 | [diff] [blame] | 51 | message(WARNING "Host compiler must support std::atomic!") |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 52 | endif() |
| 53 | else() |
Vasileios Kalintiris | 38023b7 | 2016-02-16 14:15:27 +0000 | [diff] [blame] | 54 | message(WARNING "Host compiler appears to require libatomic, but cannot find it.") |
Vasileios Kalintiris | d5ff320 | 2016-02-11 12:43:04 +0000 | [diff] [blame] | 55 | endif() |
| 56 | endif() |