1#*************************************************************************** 2# _ _ ____ _ 3# Project ___| | | | _ \| | 4# / __| | | | |_) | | 5# | (__| |_| | _ <| |___ 6# \___|\___/|_| \_\_____| 7# 8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9# 10# This software is licensed as described in the file COPYING, which 11# you should have received as part of this distribution. The terms 12# are also available at https://curl.se/docs/copyright.html. 13# 14# You may opt to use, copy, modify, merge, publish, distribute and/or sell 15# copies of the Software, and permit persons to whom the Software is 16# furnished to do so, under the terms of the COPYING file. 17# 18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19# KIND, either express or implied. 20# 21# SPDX-License-Identifier: curl 22# 23########################################################################### 24include(CheckCSourceCompiles) 25include(CheckCSourceRuns) 26include(CheckTypeSize) 27 28macro(add_header_include _check _header) 29 if(${_check}) 30 set(_source_epilogue "${_source_epilogue} 31 #include <${_header}>") 32 endif() 33endmacro() 34 35set(_cmake_try_compile_target_type_save ${CMAKE_TRY_COMPILE_TARGET_TYPE}) 36set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") 37 38if(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE) 39 cmake_push_check_state() 40 unset(CMAKE_EXTRA_INCLUDE_FILES) 41 if(WIN32) 42 set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h") 43 set(CMAKE_REQUIRED_LIBRARIES "ws2_32") 44 elseif(HAVE_SYS_SOCKET_H) 45 set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h") 46 endif() 47 check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE) 48 set(HAVE_STRUCT_SOCKADDR_STORAGE ${HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE}) 49 cmake_pop_check_state() 50endif() 51 52if(NOT WIN32) 53 set(_source_epilogue "#undef inline") 54 add_header_include(HAVE_SYS_TYPES_H "sys/types.h") 55 add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h") 56 check_c_source_compiles("${_source_epilogue} 57 int main(void) 58 { 59 int flag = MSG_NOSIGNAL; 60 (void)flag; 61 return 0; 62 }" HAVE_MSG_NOSIGNAL) 63endif() 64 65set(_source_epilogue "#undef inline") 66add_header_include(HAVE_SYS_TIME_H "sys/time.h") 67check_c_source_compiles("${_source_epilogue} 68 #include <time.h> 69 int main(void) 70 { 71 struct timeval ts; 72 ts.tv_sec = 0; 73 ts.tv_usec = 0; 74 (void)ts; 75 return 0; 76 }" HAVE_STRUCT_TIMEVAL) 77 78set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_cmake_try_compile_target_type_save}) 79unset(_cmake_try_compile_target_type_save) 80 81# Detect HAVE_GETADDRINFO_THREADSAFE 82 83if(WIN32) 84 set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO}) 85elseif(NOT HAVE_GETADDRINFO) 86 set(HAVE_GETADDRINFO_THREADSAFE FALSE) 87elseif(APPLE OR 88 CMAKE_SYSTEM_NAME STREQUAL "AIX" OR 89 CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR 90 CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR 91 CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR 92 CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR 93 CMAKE_SYSTEM_NAME STREQUAL "SunOS") 94 set(HAVE_GETADDRINFO_THREADSAFE TRUE) 95elseif(BSD OR CMAKE_SYSTEM_NAME MATCHES "BSD") 96 set(HAVE_GETADDRINFO_THREADSAFE FALSE) 97endif() 98 99if(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE) 100 set(_source_epilogue "#undef inline") 101 add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h") 102 add_header_include(HAVE_SYS_TIME_H "sys/time.h") 103 add_header_include(HAVE_NETDB_H "netdb.h") 104 check_c_source_compiles("${_source_epilogue} 105 int main(void) 106 { 107 #ifdef h_errno 108 return 0; 109 #else 110 #error force compilation error 111 #endif 112 }" HAVE_H_ERRNO) 113 114 if(NOT HAVE_H_ERRNO) 115 check_c_source_compiles("${_source_epilogue} 116 int main(void) 117 { 118 h_errno = 2; 119 return h_errno != 0 ? 1 : 0; 120 }" HAVE_H_ERRNO_ASSIGNABLE) 121 122 if(NOT HAVE_H_ERRNO_ASSIGNABLE) 123 check_c_source_compiles("${_source_epilogue} 124 int main(void) 125 { 126 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) 127 return 0; 128 #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700) 129 return 0; 130 #else 131 #error force compilation error 132 #endif 133 }" HAVE_H_ERRNO_SBS_ISSUE_7) 134 endif() 135 endif() 136 137 if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7) 138 set(HAVE_GETADDRINFO_THREADSAFE TRUE) 139 endif() 140endif() 141 142if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME_MONOTONIC_RAW) 143 set(_source_epilogue "#undef inline") 144 add_header_include(HAVE_SYS_TYPES_H "sys/types.h") 145 add_header_include(HAVE_SYS_TIME_H "sys/time.h") 146 check_c_source_compiles("${_source_epilogue} 147 #include <time.h> 148 int main(void) 149 { 150 struct timespec ts; 151 (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts); 152 return 0; 153 }" HAVE_CLOCK_GETTIME_MONOTONIC_RAW) 154endif() 155 156unset(_source_epilogue) 157