xref: /curl/CMake/OtherTests.cmake (revision 825a800e)
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  cmake_push_check_state()
39  unset(CMAKE_EXTRA_INCLUDE_FILES)
40  if(WIN32)
41    set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
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})
48  cmake_pop_check_state()
49endif()
50
51if(NOT WIN32)
52  set(_source_epilogue "#undef inline")
53  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
54  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
55  check_c_source_compiles("${_source_epilogue}
56    int main(void)
57    {
58      int flag = MSG_NOSIGNAL;
59      (void)flag;
60      return 0;
61    }" HAVE_MSG_NOSIGNAL)
62endif()
63
64set(_source_epilogue "#undef inline")
65add_header_include(HAVE_SYS_TIME_H "sys/time.h")
66check_c_source_compiles("${_source_epilogue}
67  #include <time.h>
68  int main(void)
69  {
70    struct timeval ts;
71    ts.tv_sec  = 0;
72    ts.tv_usec = 0;
73    (void)ts;
74    return 0;
75  }" HAVE_STRUCT_TIMEVAL)
76
77unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
78
79# Detect HAVE_GETADDRINFO_THREADSAFE
80
81if(WIN32)
82  set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO})
83elseif(NOT HAVE_GETADDRINFO)
84  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
85elseif(APPLE OR
86       CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
87       CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
88       CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR
89       CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR
90       CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR
91       CMAKE_SYSTEM_NAME STREQUAL "SunOS")
92  set(HAVE_GETADDRINFO_THREADSAFE TRUE)
93elseif(BSD OR CMAKE_SYSTEM_NAME MATCHES "BSD")
94  set(HAVE_GETADDRINFO_THREADSAFE FALSE)
95endif()
96
97if(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE)
98  set(_source_epilogue "#undef inline")
99  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
100  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
101  add_header_include(HAVE_NETDB_H "netdb.h")
102  check_c_source_compiles("${_source_epilogue}
103    int main(void)
104    {
105    #ifdef h_errno
106      return 0;
107    #else
108      #error force compilation error
109    #endif
110    }" HAVE_H_ERRNO)
111
112  if(NOT HAVE_H_ERRNO)
113    check_c_source_compiles("${_source_epilogue}
114      int main(void)
115      {
116        h_errno = 2;
117        return h_errno != 0 ? 1 : 0;
118      }" HAVE_H_ERRNO_ASSIGNABLE)
119
120    if(NOT HAVE_H_ERRNO_ASSIGNABLE)
121      check_c_source_compiles("${_source_epilogue}
122        int main(void)
123        {
124        #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
125          return 0;
126        #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700)
127          return 0;
128        #else
129          #error force compilation error
130        #endif
131        }" HAVE_H_ERRNO_SBS_ISSUE_7)
132    endif()
133  endif()
134
135  if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7)
136    set(HAVE_GETADDRINFO_THREADSAFE TRUE)
137  endif()
138endif()
139
140if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
141  set(_source_epilogue "#undef inline")
142  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
143  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
144  check_c_source_compiles("${_source_epilogue}
145    #include <time.h>
146    int main(void)
147    {
148      struct timespec ts;
149      (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
150      return 0;
151    }" HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
152endif()
153