From 04a3a377d83fd72c4cf7a96c9cb6d44785e33264 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Fri, 29 Sep 2023 00:38:06 +0000 Subject: [PATCH] cmake: detect `HAVE_GETADDRINFO_THREADSAFE` Based on existing autotools logic. autotools checks for old versions of the allowlisted target OSes and disables this feature when seeing them. In CMake we assume we're running on newer systems and enable regardless of OS version. autotools always runs all 3 probes for non-fast-tracked systems and enables this feature if any one of them was successful. To save configuration time, CMake stops at the first successful check. OpenBSD is not fast-tracked and then gets blocklisted as a generic BSD system. I haven't double-checked if this is correct, but looks odd. Ref: #11964 (effort to sync cmake detections with autotools) Closes #11979 --- CMake/OtherTests.cmake | 70 +++++++++++++++++++++++++++++++++++++++++- CMakeLists.txt | 3 -- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/CMake/OtherTests.cmake b/CMake/OtherTests.cmake index 78d3940455..31bb026b72 100644 --- a/CMake/OtherTests.cmake +++ b/CMake/OtherTests.cmake @@ -22,6 +22,8 @@ # ########################################################################### include(CheckCSourceCompiles) +include(CheckCSourceRuns) + # The begin of the sources (macros and includes) set(_source_epilogue "#undef inline") @@ -89,7 +91,6 @@ if(NOT CMAKE_CROSSCOMPILING) # only try this on non-apple platforms # if not cross-compilation... - include(CheckCSourceRuns) set(CMAKE_REQUIRED_FLAGS "") if(HAVE_SYS_POLL_H) set(CMAKE_REQUIRED_FLAGS "-DHAVE_SYS_POLL_H") @@ -132,3 +133,70 @@ if(NOT CMAKE_CROSSCOMPILING) }" HAVE_POLL_FINE) endif() endif() + +# Detect HAVE_GETADDRINFO_THREADSAFE + +if(WIN32) + set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO}) +elseif(NOT HAVE_GETADDRINFO) + set(HAVE_GETADDRINFO_THREADSAFE FALSE) +elseif(CMAKE_SYSTEM_NAME STREQUAL "AIX" OR + CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR + CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR + CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR + CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR + CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR + CMAKE_SYSTEM_NAME STREQUAL "SunOS") + set(HAVE_GETADDRINFO_THREADSAFE TRUE) +elseif(CMAKE_SYSTEM_NAME MATCHES "BSD") + set(HAVE_GETADDRINFO_THREADSAFE FALSE) +endif() + +if(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE) + + set(_save_epilogue "${_source_epilogue}") + set(_source_epilogue "#undef inline") + + add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h") + add_header_include(HAVE_SYS_TIME_H "sys/time.h") + add_header_include(HAVE_NETDB_H "netdb.h") + + check_c_source_compiles("${_source_epilogue} + int main(void) + { + #ifdef h_errno + return 0; + #else + force compilation error + #endif + }" HAVE_H_ERRNO) + + if(NOT HAVE_H_ERRNO) + check_c_source_runs("${_source_epilogue} + int main(void) + { + h_errno = 2; + return h_errno != 0 ? 1 : 0; + }" HAVE_H_ERRNO_ASSIGNABLE) + + if(NOT HAVE_H_ERRNO_ASSIGNABLE) + check_c_source_compiles("${_source_epilogue} + int main(void) + { + #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) + return 0; + #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700) + return 0; + #else + force compilation error + #endif + }" HAVE_H_ERRNO_SBS_ISSUE_7) + endif() + endif() + + if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7) + set(HAVE_GETADDRINFO_THREADSAFE TRUE) + endif() + + set(_source_epilogue "${_save_epilogue}") +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 36d23c82ea..c08503114a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1141,9 +1141,6 @@ check_symbol_exists(strerror_r "${CURL_INCLUDES};stdlib.h;string.h" HAVE_STR check_symbol_exists(siginterrupt "${CURL_INCLUDES}" HAVE_SIGINTERRUPT) check_symbol_exists(getaddrinfo "${CURL_INCLUDES};stdlib.h;string.h" HAVE_GETADDRINFO) check_symbol_exists(getifaddrs "${CURL_INCLUDES};stdlib.h" HAVE_GETIFADDRS) -if(WIN32) - set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO}) -endif() check_symbol_exists(freeaddrinfo "${CURL_INCLUDES}" HAVE_FREEADDRINFO) check_symbol_exists(pipe "${CURL_INCLUDES}" HAVE_PIPE) check_symbol_exists(ftruncate "${CURL_INCLUDES}" HAVE_FTRUNCATE) -- 2.47.3