113498266Sopenharmony_ci#***************************************************************************
213498266Sopenharmony_ci#                                  _   _ ____  _
313498266Sopenharmony_ci#  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci#                             / __| | | | |_) | |
513498266Sopenharmony_ci#                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci#                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci#
813498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci#
1013498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci# you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci#
1413498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci#
1813498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci# KIND, either express or implied.
2013498266Sopenharmony_ci#
2113498266Sopenharmony_ci# SPDX-License-Identifier: curl
2213498266Sopenharmony_ci#
2313498266Sopenharmony_ci###########################################################################
2413498266Sopenharmony_ciinclude(CheckCSourceCompiles)
2513498266Sopenharmony_ciinclude(CheckCSourceRuns)
2613498266Sopenharmony_ciinclude(CheckTypeSize)
2713498266Sopenharmony_ci
2813498266Sopenharmony_cimacro(add_header_include check header)
2913498266Sopenharmony_ci  if(${check})
3013498266Sopenharmony_ci    set(_source_epilogue "${_source_epilogue}
3113498266Sopenharmony_ci      #include <${header}>")
3213498266Sopenharmony_ci  endif()
3313498266Sopenharmony_ciendmacro()
3413498266Sopenharmony_ci
3513498266Sopenharmony_ciset(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
3613498266Sopenharmony_ci
3713498266Sopenharmony_ciif(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE)
3813498266Sopenharmony_ci  set(CMAKE_EXTRA_INCLUDE_FILES)
3913498266Sopenharmony_ci  if(WIN32)
4013498266Sopenharmony_ci    set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
4113498266Sopenharmony_ci    set(CMAKE_REQUIRED_DEFINITIONS "-DWIN32_LEAN_AND_MEAN")
4213498266Sopenharmony_ci    set(CMAKE_REQUIRED_LIBRARIES "ws2_32")
4313498266Sopenharmony_ci  elseif(HAVE_SYS_SOCKET_H)
4413498266Sopenharmony_ci    set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h")
4513498266Sopenharmony_ci  endif()
4613498266Sopenharmony_ci  check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE)
4713498266Sopenharmony_ci  set(HAVE_STRUCT_SOCKADDR_STORAGE ${HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE})
4813498266Sopenharmony_ciendif()
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciif(NOT WIN32)
5113498266Sopenharmony_ci  set(_source_epilogue "#undef inline")
5213498266Sopenharmony_ci  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
5313498266Sopenharmony_ci  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
5413498266Sopenharmony_ci  check_c_source_compiles("${_source_epilogue}
5513498266Sopenharmony_ci    int main(void)
5613498266Sopenharmony_ci    {
5713498266Sopenharmony_ci      int flag = MSG_NOSIGNAL;
5813498266Sopenharmony_ci      (void)flag;
5913498266Sopenharmony_ci      return 0;
6013498266Sopenharmony_ci    }" HAVE_MSG_NOSIGNAL)
6113498266Sopenharmony_ciendif()
6213498266Sopenharmony_ci
6313498266Sopenharmony_ciset(_source_epilogue "#undef inline")
6413498266Sopenharmony_ciadd_header_include(HAVE_SYS_TIME_H "sys/time.h")
6513498266Sopenharmony_cicheck_c_source_compiles("${_source_epilogue}
6613498266Sopenharmony_ci  #include <time.h>
6713498266Sopenharmony_ci  int main(void)
6813498266Sopenharmony_ci  {
6913498266Sopenharmony_ci    struct timeval ts;
7013498266Sopenharmony_ci    ts.tv_sec  = 0;
7113498266Sopenharmony_ci    ts.tv_usec = 0;
7213498266Sopenharmony_ci    (void)ts;
7313498266Sopenharmony_ci    return 0;
7413498266Sopenharmony_ci  }" HAVE_STRUCT_TIMEVAL)
7513498266Sopenharmony_ci
7613498266Sopenharmony_ciunset(CMAKE_TRY_COMPILE_TARGET_TYPE)
7713498266Sopenharmony_ci
7813498266Sopenharmony_ciif(NOT CMAKE_CROSSCOMPILING AND NOT APPLE)
7913498266Sopenharmony_ci  set(_source_epilogue "#undef inline")
8013498266Sopenharmony_ci  add_header_include(HAVE_SYS_POLL_H "sys/poll.h")
8113498266Sopenharmony_ci  add_header_include(HAVE_POLL_H "poll.h")
8213498266Sopenharmony_ci  check_c_source_runs("${_source_epilogue}
8313498266Sopenharmony_ci    #include <stdlib.h>
8413498266Sopenharmony_ci    #include <sys/time.h>
8513498266Sopenharmony_ci    int main(void)
8613498266Sopenharmony_ci    {
8713498266Sopenharmony_ci      if(0 != poll(0, 0, 10)) {
8813498266Sopenharmony_ci        return 1; /* fail */
8913498266Sopenharmony_ci      }
9013498266Sopenharmony_ci      else {
9113498266Sopenharmony_ci        /* detect the 10.12 poll() breakage */
9213498266Sopenharmony_ci        struct timeval before, after;
9313498266Sopenharmony_ci        int rc;
9413498266Sopenharmony_ci        size_t us;
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci        gettimeofday(&before, NULL);
9713498266Sopenharmony_ci        rc = poll(NULL, 0, 500);
9813498266Sopenharmony_ci        gettimeofday(&after, NULL);
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci        us = (after.tv_sec - before.tv_sec) * 1000000 +
10113498266Sopenharmony_ci          (after.tv_usec - before.tv_usec);
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci        if(us < 400000) {
10413498266Sopenharmony_ci          return 1;
10513498266Sopenharmony_ci        }
10613498266Sopenharmony_ci      }
10713498266Sopenharmony_ci      return 0;
10813498266Sopenharmony_ci    }" HAVE_POLL_FINE)
10913498266Sopenharmony_ciendif()
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci# Detect HAVE_GETADDRINFO_THREADSAFE
11213498266Sopenharmony_ci
11313498266Sopenharmony_ciif(WIN32)
11413498266Sopenharmony_ci  set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO})
11513498266Sopenharmony_cielseif(NOT HAVE_GETADDRINFO)
11613498266Sopenharmony_ci  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
11713498266Sopenharmony_cielseif(APPLE OR
11813498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
11913498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
12013498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR
12113498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR
12213498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR
12313498266Sopenharmony_ci       CMAKE_SYSTEM_NAME STREQUAL "SunOS")
12413498266Sopenharmony_ci  set(HAVE_GETADDRINFO_THREADSAFE TRUE)
12513498266Sopenharmony_cielseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
12613498266Sopenharmony_ci  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
12713498266Sopenharmony_ciendif()
12813498266Sopenharmony_ci
12913498266Sopenharmony_ciif(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE)
13013498266Sopenharmony_ci  set(_source_epilogue "#undef inline")
13113498266Sopenharmony_ci  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
13213498266Sopenharmony_ci  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
13313498266Sopenharmony_ci  add_header_include(HAVE_NETDB_H "netdb.h")
13413498266Sopenharmony_ci  check_c_source_compiles("${_source_epilogue}
13513498266Sopenharmony_ci    int main(void)
13613498266Sopenharmony_ci    {
13713498266Sopenharmony_ci    #ifdef h_errno
13813498266Sopenharmony_ci      return 0;
13913498266Sopenharmony_ci    #else
14013498266Sopenharmony_ci      force compilation error
14113498266Sopenharmony_ci    #endif
14213498266Sopenharmony_ci    }" HAVE_H_ERRNO)
14313498266Sopenharmony_ci
14413498266Sopenharmony_ci  if(NOT HAVE_H_ERRNO)
14513498266Sopenharmony_ci    check_c_source_compiles("${_source_epilogue}
14613498266Sopenharmony_ci      int main(void)
14713498266Sopenharmony_ci      {
14813498266Sopenharmony_ci        h_errno = 2;
14913498266Sopenharmony_ci        return h_errno != 0 ? 1 : 0;
15013498266Sopenharmony_ci      }" HAVE_H_ERRNO_ASSIGNABLE)
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci    if(NOT HAVE_H_ERRNO_ASSIGNABLE)
15313498266Sopenharmony_ci      check_c_source_compiles("${_source_epilogue}
15413498266Sopenharmony_ci        int main(void)
15513498266Sopenharmony_ci        {
15613498266Sopenharmony_ci        #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
15713498266Sopenharmony_ci          return 0;
15813498266Sopenharmony_ci        #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700)
15913498266Sopenharmony_ci          return 0;
16013498266Sopenharmony_ci        #else
16113498266Sopenharmony_ci          force compilation error
16213498266Sopenharmony_ci        #endif
16313498266Sopenharmony_ci        }" HAVE_H_ERRNO_SBS_ISSUE_7)
16413498266Sopenharmony_ci    endif()
16513498266Sopenharmony_ci  endif()
16613498266Sopenharmony_ci
16713498266Sopenharmony_ci  if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7)
16813498266Sopenharmony_ci    set(HAVE_GETADDRINFO_THREADSAFE TRUE)
16913498266Sopenharmony_ci  endif()
17013498266Sopenharmony_ciendif()
17113498266Sopenharmony_ci
17213498266Sopenharmony_ciif(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
17313498266Sopenharmony_ci  set(_source_epilogue "#undef inline")
17413498266Sopenharmony_ci  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
17513498266Sopenharmony_ci  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
17613498266Sopenharmony_ci  check_c_source_compiles("${_source_epilogue}
17713498266Sopenharmony_ci    #include <time.h>
17813498266Sopenharmony_ci    int main(void)
17913498266Sopenharmony_ci    {
18013498266Sopenharmony_ci      struct timespec ts;
18113498266Sopenharmony_ci      (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
18213498266Sopenharmony_ci      return 0;
18313498266Sopenharmony_ci    }" HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
18413498266Sopenharmony_ciendif()
185