summaryrefslogtreecommitdiffstats
path: root/cmake/FindWrapAtomic.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'cmake/FindWrapAtomic.cmake')
-rw-r--r--cmake/FindWrapAtomic.cmake41
1 files changed, 24 insertions, 17 deletions
diff --git a/cmake/FindWrapAtomic.cmake b/cmake/FindWrapAtomic.cmake
index 56aa7323bf..04768e6490 100644
--- a/cmake/FindWrapAtomic.cmake
+++ b/cmake/FindWrapAtomic.cmake
@@ -1,3 +1,6 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
# We can't create the same interface imported target multiple times, CMake will complain if we do
# that. This can happen if the find_package call is done in multiple different subdirectories.
if(TARGET WrapAtomic::WrapAtomic)
@@ -10,35 +13,39 @@ include(CheckCXXSourceCompiles)
set (atomic_test_sources "#include <atomic>
#include <cstdint>
-void test(volatile std::atomic<std::int64_t> &a)
-{
- std::int64_t v = a.load(std::memory_order_acquire);
- while (!a.compare_exchange_strong(v, v + 1,
- std::memory_order_acq_rel,
- std::memory_order_acquire)) {
- v = a.exchange(v - 1);
- }
- a.store(v + 1, std::memory_order_release);
-}
-
int main(int, char **)
{
- void *ptr = (void*)0xffffffc0; // any random pointer
- test(*reinterpret_cast<std::atomic<std::int64_t> *>(ptr));
+ volatile std::atomic<char> size_1;
+ volatile std::atomic<short> size_2;
+ volatile std::atomic<int> size_4;
+ volatile std::atomic<int64_t> size_8;
+
+ ++size_1;
+ ++size_2;
+ ++size_4;
+ ++size_8;
+
+ (void)size_1.load(std::memory_order_relaxed);
+ (void)size_2.load(std::memory_order_relaxed);
+ (void)size_4.load(std::memory_order_relaxed);
+ (void)size_8.load(std::memory_order_relaxed);
+
return 0;
}")
check_cxx_source_compiles("${atomic_test_sources}" HAVE_STDATOMIC)
if(NOT HAVE_STDATOMIC)
- set(_req_libraries "${CMAKE_REQUIRE_LIBRARIES}")
- set(CMAKE_REQUIRE_LIBRARIES "atomic")
+ set(_req_libraries "${CMAKE_REQUIRED_LIBRARIES}")
+ set(atomic_LIB "-latomic")
+ set(CMAKE_REQUIRED_LIBRARIES ${atomic_LIB})
check_cxx_source_compiles("${atomic_test_sources}" HAVE_STDATOMIC_WITH_LIB)
- set(CMAKE_REQUIRE_LIBRARIES "${_req_libraries}")
+ set(CMAKE_REQUIRED_LIBRARIES "${_req_libraries}")
endif()
add_library(WrapAtomic::WrapAtomic INTERFACE IMPORTED)
if(HAVE_STDATOMIC_WITH_LIB)
- target_link_libraries(WrapAtomic::WrapAtomic INTERFACE atomic)
+ # atomic_LIB is already found above.
+ target_link_libraries(WrapAtomic::WrapAtomic INTERFACE ${atomic_LIB})
endif()
set(WrapAtomic_FOUND 1)