diff --git a/CMakeLists.txt b/CMakeLists.txt index bc971857d1ec3fcc821f7ad207fc247fbb98d397..4289a9c73a3c027bd23f4295774d8c12df2a299b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,6 +320,9 @@ if (OPENDHT_STATIC) PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${GNUTLS_LIBRARIES} ${Nettle_STATIC_LIBRARIES} ${Jsoncpp_STATIC_LIBRARIES} ${FMT_LIBRARY} ${HTTP_PARSER_LIBRARY} ${OPENSSL_STATIC_LIBRARIES}) + if (NOT HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB) + target_link_libraries(opendht-static PUBLIC "atomic") + endif () else () if (OPENDHT_TOOLS) function (add_obj_lib name libfile) @@ -393,6 +396,9 @@ if (OPENDHT_SHARED) PRIVATE ${GNUTLS_LIBRARIES} ${Nettle_LIBRARIES} ${Jsoncpp_LIBRARIES} ${FMT_LIBRARY} ${HTTP_PARSER_LIBRARY} ${argon2_LIBRARIES}) + if (NOT HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB) + target_link_libraries(opendht PUBLIC "atomic") + endif () endif () install (TARGETS opendht DESTINATION ${CMAKE_INSTALL_LIBDIR} EXPORT opendht) @@ -497,6 +503,9 @@ if (OPENDHT_TESTS) ${GNUTLS_LIBRARIES} ${Jsoncpp_LIBRARIES} ) + if (NOT HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB) + target_link_libraries(opendht_unit_tests "atomic") + endif () if (OPENDHT_PROXY_OPENSSL) target_link_libraries(opendht_unit_tests ${OPENSSL_LIBRARIES}) endif() diff --git a/cmake/CheckAtomic.cmake b/cmake/CheckAtomic.cmake index c045e30b22064c9bf4b98f9dd510e2dc52c5103a..10e1d6aabe49a6ba56b4c3799b8c48de47c4dff7 100644 --- a/cmake/CheckAtomic.cmake +++ b/cmake/CheckAtomic.cmake @@ -34,6 +34,21 @@ int main() { set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) endfunction(check_working_cxx_atomics64) +function(check_working_cxx_atomics_for_risc_v varname) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") + CHECK_CXX_SOURCE_COMPILES(" +#include <atomic> +std::atomic<uint8_t> x; +std::atomic<uint16_t> y; +int main() { + x++; + y++; + return 0; +} +" ${varname}) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endfunction(check_working_cxx_atomics_for_risc_v) # This isn't necessary on MSVC, so avoid command-line switch annoyance # by only running on GCC-like hosts. @@ -76,6 +91,22 @@ if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) endif() endif() +# Check for RISC-V atomic operations. +if(MSVC) + set(HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB True) +else() + check_working_cxx_atomics_for_risc_v(HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB) +endif() + +# If not, check if the library exists, and atomics work with it. +if(NOT HAVE_CXX_ATOMICS_RISC_V_WITHOUT_LIB) + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") + check_working_cxx_atomics_for_risc_v(HAVE_CXX_ATOMICS_RISC_V_WITH_LIB) + if (NOT HAVE_CXX_ATOMICS_RISC_V_WITH_LIB) + message(FATAL_ERROR "Host compiler must support RISC-V std::atomic!") + endif() +endif() + ## TODO: This define is only used for the legacy atomic operations in ## llvm's Atomic.h, which should be replaced. Other code simply ## assumes C++11 <atomic> works.