Skip to content

Commit 69908ae

Browse files
committed
Rewrite FindAtomics.cmake.
1 parent 3967634 commit 69908ae

2 files changed

Lines changed: 33 additions & 30 deletions

File tree

‎CMake/FindAtomics.cmake‎

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,53 @@
77
# riscv64 specific:
88
# * https://lists.debian.org/debian-riscv/2022/01/msg00009.html
99
#
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()
1316

1417
include(CheckCXXSourceCompiles)
1518

1619
# RISC-V only has 32-bit and 64-bit atomic instructions. GCC is supposed
1720
# to convert smaller atomics to those larger ones via masking and
1821
# shifting like LLVM, but it’s a known bug that it does not. This means
1922
# 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
2225
"
2326
#include <atomic>
2427
#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
2730
int main() {
2831
++n8;
2932
++n64;
30-
return 0;
3133
}")
3234

33-
check_cxx_source_compiles("${atomic_code}" ATOMICS_LOCK_FREE_INSTRUCTIONS)
35+
set(ATOMICS_LIBS " " "-latomic")
3436

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)
5349
endif()
50+
break()
5451
endif()
52+
endforeach()
53+
if (Atomics_FIND_REQUIRED AND NOT ATOMICS_FOUND)
54+
message(FATAL_ERROR "Atomic operations library isn't found.")
5555
endif()
56-
unset(atomic_code)
56+
57+
unset(ATOMICS_LIBRARY)
58+
unset(ATOMICS_LIBS)
59+
unset(ATOMIC_CODE)

‎CMakeLists.txt‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ include(TdSetUpCompiler)
133133
td_set_up_compiler()
134134

135135
find_package(Atomics REQUIRED)
136-
if (ATOMICS_LIBRARY_FLAGS)
137-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ATOMICS_LIBRARY_FLAGS}")
136+
if (ATOMICS_LIBRARIES)
137+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ATOMICS_LIBRARIES}")
138138
endif()
139139

140140
if (MSVC)

0 commit comments

Comments
 (0)