Skip to content

Commit

Permalink
cmake: check atomic for RISC-V architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Mar 28, 2022
1 parent dfd0a09 commit dac69dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 31 additions & 0 deletions cmake/CheckAtomic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dac69dc

Please sign in to comment.