Skip to content

Commit ace8ff2

Browse files
build: fix linking failure on RPi 32bit
this fixes a linking problem with RPi 3 (and probably others) running with Raspbian (new Raspberry Pi OS) that can't use the inline atomic functions but instead require linking against the lib -latomic. The CMake code is based on SoapyRTLSdr file (licensed under MIT) https://github.com/pothosware/SoapyRTLSDR/blob/master/CheckAtomic.cmake
1 parent 72bff83 commit ace8ff2

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ if (STOP_ON_WARNING)
130130
add_definitions(-DSTOP_ON_WARNING)
131131
endif()
132132

133+
# Test for Atomics
134+
include(CheckAtomic)
135+
if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB OR NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
136+
set(ATOMIC_LIBS "atomic")
137+
endif()
138+
133139
########################################################################
134140
# Find dependencies
135141
########################################################################

COPYRIGHT

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Files: lib/include/srsran/common/backward.hpp
5151
Copyright: 2013, Google Inc.
5252
License: MIT
5353

54+
Files: cmake/modules/CheckAtomic.cmake
55+
Copyright: 2015, Charles J. Cliffe
56+
License: MIT
57+
5458
License: AGPL-3+
5559
This program is free software: you can redistribute it and/or modify
5660
it under the terms of the GNU Affero General Public License as

cmake/modules/CheckAtomic.cmake

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#
2+
# Copyright 2013-2021 Software Radio Systems Limited
3+
#
4+
# By using this file, you agree to the terms and conditions set
5+
# forth in the LICENSE file which can be found at the top level of
6+
# the distribution.
7+
#
8+
9+
# Adopted from https://github.com/pothosware/SoapyRTLSDR
10+
# Copyright: 2015, Charles J. Cliffe
11+
# License: MIT
12+
13+
# - Try to find if atomics need -latomic linking
14+
# Once done this will define
15+
# HAVE_CXX_ATOMICS_WITHOUT_LIB - Wether atomic types work without -latomic
16+
# HAVE_CXX_ATOMICS64_WITHOUT_LIB - Wether 64 bit atomic types work without -latomic
17+
18+
INCLUDE(CheckCXXSourceCompiles)
19+
INCLUDE(CheckLibraryExists)
20+
21+
# Sometimes linking against libatomic is required for atomic ops, if
22+
# the platform doesn't support lock-free atomics.
23+
24+
function(check_working_cxx_atomics varname)
25+
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
26+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
27+
CHECK_CXX_SOURCE_COMPILES("
28+
#include <atomic>
29+
std::atomic<int> x;
30+
int main() {
31+
return std::atomic_is_lock_free(&x);
32+
}
33+
" ${varname})
34+
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
35+
endfunction(check_working_cxx_atomics)
36+
37+
function(check_working_cxx_atomics64 varname)
38+
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
39+
set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
40+
CHECK_CXX_SOURCE_COMPILES("
41+
#include <atomic>
42+
#include <cstdint>
43+
std::atomic<uint64_t> x (0);
44+
int main() {
45+
uint64_t i = x.load(std::memory_order_relaxed);
46+
return std::atomic_is_lock_free(&x);
47+
}
48+
" ${varname})
49+
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
50+
endfunction(check_working_cxx_atomics64)
51+
52+
# Check for atomic operations.
53+
if(MSVC)
54+
# This isn't necessary on MSVC.
55+
set(HAVE_CXX_ATOMICS_WITHOUT_LIB True)
56+
else()
57+
# First check if atomics work without the library.
58+
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
59+
endif()
60+
61+
# If not, check if the library exists, and atomics work with it.
62+
if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
63+
check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
64+
if(NOT HAVE_LIBATOMIC)
65+
message(STATUS "Host compiler appears to require libatomic, but cannot locate it.")
66+
endif()
67+
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
68+
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
69+
if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
70+
message(FATAL_ERROR "Host compiler must support std::atomic!")
71+
endif()
72+
endif()
73+
74+
# Check for 64 bit atomic operations.
75+
if(MSVC)
76+
set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
77+
else()
78+
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
79+
endif()
80+
81+
# If not, check if the library exists, and atomics work with it.
82+
if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
83+
check_library_exists(atomic __atomic_load_8 "" HAVE_LIBATOMIC64)
84+
if(NOT HAVE_LIBATOMIC64)
85+
message(STATUS "Host compiler appears to require libatomic, but cannot locate it.")
86+
endif()
87+
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
88+
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
89+
if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
90+
message(FATAL_ERROR "Host compiler must support std::atomic!")
91+
endif()
92+
endif()

debian/copyright

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Files: lib/include/srsran/common/backward.hpp
5858
Copyright: 2013, Google Inc.
5959
License: MIT
6060

61+
Files: cmake/modules/CheckAtomic.cmake
62+
Copyright: 2015, Charles J. Cliffe
63+
License: MIT
6164

6265
License: AGPL-3+
6366
This program is free software: you can redistribute it and/or modify

srsue/src/stack/mac/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020

2121
set(SOURCES demux.cc dl_harq.cc mac.cc mux.cc proc_bsr.cc proc_phr.cc proc_ra.cc proc_sr.cc ul_harq.cc)
2222
add_library(srsue_mac STATIC ${SOURCES})
23-
target_link_libraries(srsue_mac srsue_mac_common)
23+
target_link_libraries(srsue_mac srsue_mac_common ${ATOMIC_LIBS})

0 commit comments

Comments
 (0)