Skip to content

Commit

Permalink
fixup! cmake: Check system symbols
Browse files Browse the repository at this point in the history
Refactor out the `TestAppendRequiredLibraries` module.
  • Loading branch information
hebasto committed Nov 12, 2023
1 parent d48e3ba commit 46256e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
24 changes: 2 additions & 22 deletions cmake/introspection.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,9 @@ check_include_file_cxx(ifaddrs.h HAVE_IFADDRS_H)
if(HAVE_SYS_TYPES_H AND HAVE_IFADDRS_H)
check_cxx_symbol_exists(freeifaddrs "sys/types.h;ifaddrs.h" HAVE_DECL_FREEIFADDRS)
check_cxx_symbol_exists(getifaddrs "sys/types.h;ifaddrs.h" HAVE_DECL_GETIFADDRS)
# Illumos/SmartOS requires linking with -lsocket if
# using getifaddrs & freeifaddrs.
# See: https://github.com/bitcoin/bitcoin/pull/21486
if(HAVE_DECL_GETIFADDRS AND HAVE_DECL_FREEIFADDRS)
set(check_socket_source "
#include <sys/types.h>
#include <ifaddrs.h>
int main() {
struct ifaddrs* ifaddr;
getifaddrs(&ifaddr);
freeifaddrs(ifaddr);
}
")
check_cxx_source_links("${check_socket_source}" IFADDR_LINKS_WITHOUT_LIBSOCKET)
if(NOT IFADDR_LINKS_WITHOUT_LIBSOCKET)
check_cxx_source_links_with_libs(socket "${check_socket_source}" IFADDR_NEEDS_LINK_TO_LIBSOCKET)
if(IFADDR_NEEDS_LINK_TO_LIBSOCKET)
link_libraries(socket)
else()
message(FATAL_ERROR "Cannot figure out how to use getifaddrs/freeifaddrs.")
endif()
endif()
include(TestAppendRequiredLibraries)
test_append_socket_library(core)
endif()
endif()

Expand Down
34 changes: 34 additions & 0 deletions cmake/module/TestAppendRequiredLibraries.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) 2023-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

include_guard(GLOBAL)

# Illumos/SmartOS requires linking with -lsocket if
# using getifaddrs & freeifaddrs.
# See: https://github.com/bitcoin/bitcoin/pull/21486
function(test_append_socket_library target)
set(check_socket_source "
#include <sys/types.h>
#include <ifaddrs.h>
int main() {
struct ifaddrs* ifaddr;
getifaddrs(&ifaddr);
freeifaddrs(ifaddr);
}
")

include(CheckSourceCompilesAndLinks)
check_cxx_source_links("${check_socket_source}" IFADDR_LINKS_WITHOUT_LIBSOCKET)
if(IFADDR_LINKS_WITHOUT_LIBSOCKET)
return()
endif()

check_cxx_source_links_with_libs(socket "${check_socket_source}" IFADDR_NEEDS_LINK_TO_LIBSOCKET)
if(IFADDR_NEEDS_LINK_TO_LIBSOCKET)
target_link_libraries(${target} INTERFACE socket)
else()
message(FATAL_ERROR "Cannot figure out how to use getifaddrs/freeifaddrs.")
endif()
endfunction()

0 comments on commit 46256e0

Please sign in to comment.