Skip to content

Commit

Permalink
fixup! cmake: Check system symbols
Browse files Browse the repository at this point in the history
Add new test_append_atomic_library() function.
  • Loading branch information
hebasto committed Nov 12, 2023
1 parent 46256e0 commit 6d752ea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmake/introspection.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ if(HAVE_SYS_TYPES_H AND HAVE_IFADDRS_H)
endif()
endif()

include(TestAppendRequiredLibraries)
test_append_atomic_library(core)

# Check for gmtime_r(), fallback to gmtime_s() if that is unavailable.
# Fail if neither are available.
check_cxx_source_compiles("
Expand Down
43 changes: 43 additions & 0 deletions cmake/module/TestAppendRequiredLibraries.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,46 @@ function(test_append_socket_library target)
message(FATAL_ERROR "Cannot figure out how to use getifaddrs/freeifaddrs.")
endif()
endfunction()

# Clang prior to version 15, when building for 32-bit,
# and linking against libstdc++, requires linking with
# -latomic if using the C++ atomic library.
# Can be tested with: clang++ test.cpp -m32
#
# Sourced from http://bugs.debian.org/797228
function(test_append_atomic_library target)
set(check_atomic_source "
#include <atomic>
#include <cstdint>
#include <chrono>
using namespace std::chrono_literals;
int main() {
std::atomic<bool> lock{true};
lock.exchange(false);
std::atomic<std::chrono::seconds> t{0s};
t.store(2s);
std::atomic<int64_t> a{};
int64_t v = 5;
int64_t r = a.fetch_add(v);
return static_cast<int>(r);
}
")

include(CheckSourceCompilesAndLinks)
check_cxx_source_links("${check_atomic_source}" STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
if(STD_ATOMIC_LINKS_WITHOUT_LIBATOMIC)
return()
endif()

check_cxx_source_links_with_libs(atomic "${check_atomic_source}" STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC)
if(STD_ATOMIC_NEEDS_LINK_TO_LIBATOMIC)
target_link_libraries(${target} INTERFACE atomic)
else()
message(FATAL_ERROR "Cannot figure out how to use std::atomic.")
endif()
endfunction()

0 comments on commit 6d752ea

Please sign in to comment.