aboutsummaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
Diffstat (limited to 'host')
-rw-r--r--host/cmake/Modules/UHDAtomics.cmake80
-rw-r--r--host/lib/utils/CMakeLists.txt15
2 files changed, 95 insertions, 0 deletions
diff --git a/host/cmake/Modules/UHDAtomics.cmake b/host/cmake/Modules/UHDAtomics.cmake
new file mode 100644
index 000000000..4d53a2d0b
--- /dev/null
+++ b/host/cmake/Modules/UHDAtomics.cmake
@@ -0,0 +1,80 @@
+#
+# This file is mostly inspired by
+# https://github.com/llvm-mirror/llvm/blob/master/cmake/modules/CheckAtomic.cmake
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+
+include(CheckCXXSourceCompiles)
+include(CheckLibraryExists)
+
+# Sometimes linking against libatomic is required for atomic ops, if
+# the platform doesn't support lock-free atomics.
+
+function(CHECK_WORKING_CXX_ATOMICS varname)
+ CHECK_CXX_SOURCE_COMPILES("
+ #include <atomic>
+ std::atomic<int> x;
+ int main() {
+ return x;
+ }
+ " ${varname})
+endfunction(CHECK_WORKING_CXX_ATOMICS)
+
+function(CHECK_WORKING_CXX_ATOMICS64 varname)
+ CHECK_CXX_SOURCE_COMPILES("
+ #include <atomic>
+ #include <cstdint>
+ std::atomic<uint64_t> x (0);
+ int main() {
+ uint64_t i = x.load(std::memory_order_relaxed);
+ return 0;
+ }
+ " ${varname})
+endfunction(CHECK_WORKING_CXX_ATOMICS64)
+
+macro(CHECK_ATOMICS_LIB_REQUIRED required_var)
+ set(${required_var} FALSE)
+ if(MSVC)
+ set(${required_var} FALSE)
+ else()
+ # First check if atomics work without the library.
+ CHECK_WORKING_CXX_ATOMICS(HAVE_CXX_ATOMICS_WITHOUT_LIB)
+ # If not, check if the library exists, and atomics work with it.
+ if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
+ check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
+ if(HAVE_LIBATOMIC)
+ set(${required_var} TRUE)
+ set(CMAKE_REQUIRED_LIBRARIES "atomic")
+ CHECK_WORKING_CXX_ATOMICS(HAVE_CXX_ATOMICS_WITH_LIB)
+ if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
+ message(FATAL_ERROR "Host compiler must support std::atomic!")
+ endif()
+ unset(CMAKE_REQUIRED_LIBRARIES)
+ else()
+ message(
+ FATAL_ERROR
+ "Host compiler appears to require libatomic, but cannot find it.")
+ endif()
+ endif()
+ # Same check, but for 64-bit atomics.
+ CHECK_WORKING_CXX_ATOMICS64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
+ # If not, check if the library exists, and atomics work with it.
+ if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
+ check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
+ if(HAVE_CXX_LIBATOMICS64)
+ set(${required_var} TRUE)
+ set(CMAKE_REQUIRED_LIBRARIES "atomic")
+ CHECK_WORKING_CXX_ATOMICS64(HAVE_CXX_ATOMICS64_WITH_LIB)
+ unset(CMAKE_REQUIRED_LIBRARIES)
+ if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
+ message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
+ endif()
+ else()
+ message(
+ FATAL_ERROR
+ "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
+ endif()
+ endif()
+ endif()
+endmacro(CHECK_ATOMICS_LIB_REQUIRED)
diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt
index 0d18945ca..639b3e94e 100644
--- a/host/lib/utils/CMakeLists.txt
+++ b/host/lib/utils/CMakeLists.txt
@@ -178,6 +178,21 @@ set_source_files_properties(
)
########################################################################
+# Check if we dynamically need to link against libatomic
+# "Guerney, deploy the family atomics!"
+########################################################################
+message(STATUS "")
+message(STATUS "Configuring atomics support...")
+include(UHDAtomics)
+CHECK_ATOMICS_LIB_REQUIRED(NEED_LIBATOMIC)
+if(NEED_LIBATOMIC)
+ message(STATUS " Atomics support is provided by separate libatomic.")
+ LIBUHD_APPEND_LIBS("atomic")
+else()
+ message(STATUS " Atomics support is built-in, no linking required.")
+endif()
+
+########################################################################
# Define UHD_PKG_DATA_PATH for paths.cpp
########################################################################
file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" UHD_PKG_PATH)