xref: /curl/CMake/OtherTests.cmake (revision 68d80a8f)
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 STATIC_LIBRARY)
36
37if(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE)
38  set(CMAKE_EXTRA_INCLUDE_FILES)
39  if(WIN32)
40    set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
41    set(CMAKE_REQUIRED_DEFINITIONS "-DWIN32_LEAN_AND_MEAN")
42    set(CMAKE_REQUIRED_LIBRARIES "ws2_32")
43  elseif(HAVE_SYS_SOCKET_H)
44    set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h")
45  endif()
46  check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE)
47  set(HAVE_STRUCT_SOCKADDR_STORAGE ${HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE})
48endif()
49
50if(NOT WIN32)
51  set(_source_epilogue "#undef inline")
52  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
53  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
54  check_c_source_compiles("${_source_epilogue}
55    int main(void)
56    {
57      int flag = MSG_NOSIGNAL;
58      (void)flag;
59      return 0;
60    }" HAVE_MSG_NOSIGNAL)
61endif()
62
63set(_source_epilogue "#undef inline")
64add_header_include(HAVE_SYS_TIME_H "sys/time.h")
65check_c_source_compiles("${_source_epilogue}
66  #include <time.h>
67  int main(void)
68  {
69    struct timeval ts;
70    ts.tv_sec  = 0;
71    ts.tv_usec = 0;
72    (void)ts;
73    return 0;
74  }" HAVE_STRUCT_TIMEVAL)
75
76unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
77
78if(NOT CMAKE_CROSSCOMPILING AND NOT APPLE)
79  set(_source_epilogue "#undef inline")
80  add_header_include(HAVE_SYS_POLL_H "sys/poll.h")
81  add_header_include(HAVE_POLL_H "poll.h")
82  check_c_source_runs("${_source_epilogue}
83    #include <stdlib.h>
84    #include <sys/time.h>
85    int main(void)
86    {
87      if(0 != poll(0, 0, 10)) {
88        return 1; /* fail */
89      }
90      else {
91        /* detect the 10.12 poll() breakage */
92        struct timeval before, after;
93        int rc;
94        size_t us;
95
96        gettimeofday(&before, NULL);
97        rc = poll(NULL, 0, 500);
98        gettimeofday(&after, NULL);
99
100        us = (after.tv_sec - before.tv_sec) * 1000000 +
101          (after.tv_usec - before.tv_usec);
102
103        if(us < 400000) {
104          return 1;
105        }
106      }
107      return 0;
108    }" HAVE_POLL_FINE)
109endif()
110
111# Detect HAVE_GETADDRINFO_THREADSAFE
112
113if(WIN32)
114  set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO})
115elseif(NOT HAVE_GETADDRINFO)
116  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
117elseif(APPLE OR
118       CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
119       CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
120       CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR
121       CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR
122       CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR
123       CMAKE_SYSTEM_NAME STREQUAL "SunOS")
124  set(HAVE_GETADDRINFO_THREADSAFE TRUE)
125elseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
126  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
127endif()
128
129if(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE)
130  set(_source_epilogue "#undef inline")
131  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
132  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
133  add_header_include(HAVE_NETDB_H "netdb.h")
134  check_c_source_compiles("${_source_epilogue}
135    int main(void)
136    {
137    #ifdef h_errno
138      return 0;
139    #else
140      force compilation error
141    #endif
142    }" HAVE_H_ERRNO)
143
144  if(NOT HAVE_H_ERRNO)
145    check_c_source_compiles("${_source_epilogue}
146      int main(void)
147      {
148        h_errno = 2;
149        return h_errno != 0 ? 1 : 0;
150      }" HAVE_H_ERRNO_ASSIGNABLE)
151
152    if(NOT HAVE_H_ERRNO_ASSIGNABLE)
153      check_c_source_compiles("${_source_epilogue}
154        int main(void)
155        {
156        #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
157          return 0;
158        #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700)
159          return 0;
160        #else
161          force compilation error
162        #endif
163        }" HAVE_H_ERRNO_SBS_ISSUE_7)
164    endif()
165  endif()
166
167  if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7)
168    set(HAVE_GETADDRINFO_THREADSAFE TRUE)
169  endif()
170endif()
171
172if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
173  set(_source_epilogue "#undef inline")
174  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
175  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
176  check_c_source_compiles("${_source_epilogue}
177    #include <time.h>
178    int main(void)
179    {
180      struct timespec ts;
181      (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
182      return 0;
183    }" HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
184endif()
185