Skip to content

Commit a5f1574

Browse files
committed
Add script to detect whether we need -latomic
(Mostly) copied from lokinet.
1 parent ff0e515 commit a5f1574

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ project(liboxenmq
2222

2323
include(GNUInstallDirs)
2424

25+
include(cmake/libatomic.cmake)
26+
2527
message(STATUS "oxenmq v${PROJECT_VERSION}")
2628

2729
set(OXENMQ_LIBVERSION 0)

cmake/libatomic.cmake

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
include(CheckCXXSourceCompiles)
2+
3+
function(check_working_cxx_atomics64 varname)
4+
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
5+
if (EMBEDDED_CFG)
6+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -m32 -march=i486")
7+
elseif(MSVC OR MSVC_VERSION)
8+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -arch:IA32 -std:c++14")
9+
else()
10+
# CMAKE_CXX_STANDARD does not propagate to cmake compile tests
11+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++14")
12+
endif()
13+
check_cxx_source_compiles("
14+
#include <atomic>
15+
#include <cstdint>
16+
std::atomic<uint64_t> x (0);
17+
int main() {
18+
uint64_t i = x.load(std::memory_order_relaxed);
19+
return 0;
20+
}
21+
" ${varname})
22+
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
23+
endfunction()
24+
25+
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
26+
27+
if(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
28+
message(STATUS "Have working 64bit atomics")
29+
return()
30+
endif()
31+
32+
if (NOT MSVC AND NOT MSVC_VERSION)
33+
check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
34+
if (HAVE_CXX_LIBATOMICS64)
35+
message(STATUS "Have 64bit atomics via library")
36+
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
37+
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
38+
if (HAVE_CXX_ATOMICS64_WITH_LIB)
39+
message(STATUS "Can link with libatomic")
40+
link_libraries(-latomic)
41+
return()
42+
endif()
43+
endif()
44+
endif()
45+
if (MSVC OR MSVC_VERSION)
46+
message(FATAL_ERROR "Host compiler must support 64-bit std::atomic! (What does MSVC do to inline atomics?)")
47+
else()
48+
message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
49+
endif()

0 commit comments

Comments
 (0)