|
7 | 7 | # riscv64 specific: |
8 | 8 | # * https://lists.debian.org/debian-riscv/2022/01/msg00009.html |
9 | 9 | # |
10 | | -# ATOMICS_FOUND - system has c++ atomics |
11 | | -# ATOMICS_LIBRARIES - libraries needed to use c++ atomics |
12 | | -# ATOMICS_LIBRARY_FLAGS - flags required to link with c++ atomics library |
| 10 | +# ATOMICS_FOUND - system has C++ atomics |
| 11 | +# ATOMICS_LIBRARIES - libraries needed to use C++ atomics |
| 12 | + |
| 13 | +if (ATOMICS_FOUND) |
| 14 | + return() |
| 15 | +endif() |
13 | 16 |
|
14 | 17 | include(CheckCXXSourceCompiles) |
15 | 18 |
|
16 | 19 | # RISC-V only has 32-bit and 64-bit atomic instructions. GCC is supposed |
17 | 20 | # to convert smaller atomics to those larger ones via masking and |
18 | 21 | # shifting like LLVM, but it’s a known bug that it does not. This means |
19 | 22 | # anything that wants to use atomics on 1-byte or 2-byte types needs |
20 | | -# -latomic, but not 4-byte or 8-byte (though it does no harm). |
21 | | -set(atomic_code |
| 23 | +# to link atomic library, but not 4-byte or 8-byte (though it does no harm). |
| 24 | +set(ATOMIC_CODE |
22 | 25 | " |
23 | 26 | #include <atomic> |
24 | 27 | #include <cstdint> |
25 | | - std::atomic<uint8_t> n8 (0); // riscv64 |
26 | | - std::atomic<uint64_t> n64 (0); // armel, mipsel, powerpc |
| 28 | + std::atomic<std::uint8_t> n8(0); // riscv64 |
| 29 | + std::atomic<std::uint64_t> n64(0); // armel, mipsel, powerpc |
27 | 30 | int main() { |
28 | 31 | ++n8; |
29 | 32 | ++n64; |
30 | | - return 0; |
31 | 33 | }") |
32 | 34 |
|
33 | | -check_cxx_source_compiles("${atomic_code}" ATOMICS_LOCK_FREE_INSTRUCTIONS) |
| 35 | +set(ATOMICS_LIBS " " "-latomic") |
34 | 36 |
|
35 | | -if (ATOMICS_LOCK_FREE_INSTRUCTIONS) |
36 | | - set(ATOMICS_FOUND TRUE) |
37 | | - set(ATOMICS_LIBRARIES) |
38 | | - set(ATOMICS_LIBRARY_FLAGS) |
39 | | -else() |
40 | | - set(CMAKE_REQUIRED_LIBRARIES "-latomic") |
41 | | - check_cxx_source_compiles("${atomic_code}" ATOMICS_IN_LIBRARY) |
42 | | - set(CMAKE_REQUIRED_LIBRARIES) |
43 | | - if (ATOMICS_IN_LIBRARY) |
44 | | - set(ATOMICS_LIBRARY atomic) |
45 | | - include(FindPackageHandleStandardArgs) |
46 | | - find_package_handle_standard_args(Atomics DEFAULT_MSG ATOMICS_LIBRARY) |
47 | | - set(ATOMICS_LIBRARIES ${ATOMICS_LIBRARY}) |
48 | | - set(ATOMICS_LIBRARY_FLAGS "-latomic") |
49 | | - unset(ATOMICS_LIBRARY) |
50 | | - else() |
51 | | - if (Atomics_FIND_REQUIRED) |
52 | | - message(FATAL_ERROR "Neither lock free instructions nor -latomic found.") |
| 37 | +foreach (ATOMICS_LIBRARY ${ATOMICS_LIBS}) |
| 38 | + unset(ATOMICS_FOUND CACHE) |
| 39 | + set(CMAKE_REQUIRED_LIBRARIES "${ATOMICS_LIBRARY}") |
| 40 | + check_cxx_source_compiles("${ATOMIC_CODE}" ATOMICS_FOUND) |
| 41 | + unset(CMAKE_REQUIRED_LIBRARIES) |
| 42 | + if (ATOMICS_FOUND) |
| 43 | + if (NOT ATOMICS_LIBRARY STREQUAL " ") |
| 44 | + include(FindPackageHandleStandardArgs) |
| 45 | + find_package_handle_standard_args(Atomics DEFAULT_MSG ATOMICS_LIBRARY) |
| 46 | + set(ATOMICS_LIBRARIES "${ATOMICS_LIBRARY}" CACHE STRING "Atomic library" FORCE) |
| 47 | + else() |
| 48 | + set(ATOMICS_LIBRARIES "" CACHE STRING "Atomic operations library" FORCE) |
53 | 49 | endif() |
| 50 | + break() |
54 | 51 | endif() |
| 52 | +endforeach() |
| 53 | +if (Atomics_FIND_REQUIRED AND NOT ATOMICS_FOUND) |
| 54 | + message(FATAL_ERROR "Atomic operations library isn't found.") |
55 | 55 | endif() |
56 | | -unset(atomic_code) |
| 56 | + |
| 57 | +unset(ATOMICS_LIBRARY) |
| 58 | +unset(ATOMICS_LIBS) |
| 59 | +unset(ATOMIC_CODE) |
0 commit comments