Rewrite FindAtomics.cmake.

This commit is contained in:
levlam 2024-02-21 18:29:35 +03:00
parent 3967634933
commit 69908ae3c4
2 changed files with 33 additions and 30 deletions

View File

@ -7,9 +7,12 @@
# riscv64 specific: # riscv64 specific:
# * https://lists.debian.org/debian-riscv/2022/01/msg00009.html # * https://lists.debian.org/debian-riscv/2022/01/msg00009.html
# #
# ATOMICS_FOUND - system has c++ atomics # ATOMICS_FOUND - system has C++ atomics
# ATOMICS_LIBRARIES - libraries needed to use c++ atomics # ATOMICS_LIBRARIES - libraries needed to use C++ atomics
# ATOMICS_LIBRARY_FLAGS - flags required to link with c++ atomics library
if (ATOMICS_FOUND)
return()
endif()
include(CheckCXXSourceCompiles) include(CheckCXXSourceCompiles)
@ -17,40 +20,40 @@ include(CheckCXXSourceCompiles)
# to convert smaller atomics to those larger ones via masking and # to convert smaller atomics to those larger ones via masking and
# shifting like LLVM, but its a known bug that it does not. This means # shifting like LLVM, but its a known bug that it does not. This means
# anything that wants to use atomics on 1-byte or 2-byte types needs # anything that wants to use atomics on 1-byte or 2-byte types needs
# -latomic, but not 4-byte or 8-byte (though it does no harm). # to link atomic library, but not 4-byte or 8-byte (though it does no harm).
set(atomic_code set(ATOMIC_CODE
" "
#include <atomic> #include <atomic>
#include <cstdint> #include <cstdint>
std::atomic<uint8_t> n8 (0); // riscv64 std::atomic<std::uint8_t> n8(0); // riscv64
std::atomic<uint64_t> n64 (0); // armel, mipsel, powerpc std::atomic<std::uint64_t> n64(0); // armel, mipsel, powerpc
int main() { int main() {
++n8; ++n8;
++n64; ++n64;
return 0;
}") }")
check_cxx_source_compiles("${atomic_code}" ATOMICS_LOCK_FREE_INSTRUCTIONS) set(ATOMICS_LIBS " " "-latomic")
if (ATOMICS_LOCK_FREE_INSTRUCTIONS) foreach (ATOMICS_LIBRARY ${ATOMICS_LIBS})
set(ATOMICS_FOUND TRUE) unset(ATOMICS_FOUND CACHE)
set(ATOMICS_LIBRARIES) set(CMAKE_REQUIRED_LIBRARIES "${ATOMICS_LIBRARY}")
set(ATOMICS_LIBRARY_FLAGS) check_cxx_source_compiles("${ATOMIC_CODE}" ATOMICS_FOUND)
else() unset(CMAKE_REQUIRED_LIBRARIES)
set(CMAKE_REQUIRED_LIBRARIES "-latomic") if (ATOMICS_FOUND)
check_cxx_source_compiles("${atomic_code}" ATOMICS_IN_LIBRARY) if (NOT ATOMICS_LIBRARY STREQUAL " ")
set(CMAKE_REQUIRED_LIBRARIES) include(FindPackageHandleStandardArgs)
if (ATOMICS_IN_LIBRARY) find_package_handle_standard_args(Atomics DEFAULT_MSG ATOMICS_LIBRARY)
set(ATOMICS_LIBRARY atomic) set(ATOMICS_LIBRARIES "${ATOMICS_LIBRARY}" CACHE STRING "Atomic library" FORCE)
include(FindPackageHandleStandardArgs) else()
find_package_handle_standard_args(Atomics DEFAULT_MSG ATOMICS_LIBRARY) set(ATOMICS_LIBRARIES "" CACHE STRING "Atomic operations library" FORCE)
set(ATOMICS_LIBRARIES ${ATOMICS_LIBRARY})
set(ATOMICS_LIBRARY_FLAGS "-latomic")
unset(ATOMICS_LIBRARY)
else()
if (Atomics_FIND_REQUIRED)
message(FATAL_ERROR "Neither lock free instructions nor -latomic found.")
endif() endif()
break()
endif() endif()
endforeach()
if (Atomics_FIND_REQUIRED AND NOT ATOMICS_FOUND)
message(FATAL_ERROR "Atomic operations library isn't found.")
endif() endif()
unset(atomic_code)
unset(ATOMICS_LIBRARY)
unset(ATOMICS_LIBS)
unset(ATOMIC_CODE)

View File

@ -133,8 +133,8 @@ include(TdSetUpCompiler)
td_set_up_compiler() td_set_up_compiler()
find_package(Atomics REQUIRED) find_package(Atomics REQUIRED)
if (ATOMICS_LIBRARY_FLAGS) if (ATOMICS_LIBRARIES)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ATOMICS_LIBRARY_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ATOMICS_LIBRARIES}")
endif() endif()
if (MSVC) if (MSVC)