1include(CheckCSourceCompiles)
2# The begin of the sources (macros and includes)
3set(_source_epilogue "#undef inline")
4
5macro(add_header_include check header)
6  if(${check})
7    set(_source_epilogue "${_source_epilogue}\n#include <${header}>")
8  endif()
9endmacro()
10
11set(signature_call_conv)
12if(HAVE_WINDOWS_H)
13  add_header_include(HAVE_WINSOCK2_H "winsock2.h")
14  add_header_include(HAVE_WINDOWS_H "windows.h")
15  add_header_include(HAVE_WINSOCK_H "winsock.h")
16  set(_source_epilogue
17      "${_source_epilogue}\n#ifndef WIN32_LEAN_AND_MEAN\n#define WIN32_LEAN_AND_MEAN\n#endif")
18  set(signature_call_conv "PASCAL")
19  if(HAVE_LIBWS2_32)
20    set(CMAKE_REQUIRED_LIBRARIES ws2_32)
21  endif()
22else()
23  add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
24  add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
25endif()
26
27check_c_source_compiles("${_source_epilogue}
28int main(void) {
29    recv(0, 0, 0, 0);
30    return 0;
31}" curl_cv_recv)
32if(curl_cv_recv)
33  if(NOT DEFINED curl_cv_func_recv_args OR "${curl_cv_func_recv_args}" STREQUAL "unknown")
34    foreach(recv_retv "int" "ssize_t" )
35      foreach(recv_arg1 "SOCKET" "int" )
36        foreach(recv_arg2 "char *" "void *" )
37          foreach(recv_arg3 "int" "size_t" "socklen_t" "unsigned int")
38            foreach(recv_arg4 "int" "unsigned int")
39              if(NOT curl_cv_func_recv_done)
40                unset(curl_cv_func_recv_test CACHE)
41                check_c_source_compiles("
42                  ${_source_epilogue}
43                  extern ${recv_retv} ${signature_call_conv}
44                  recv(${recv_arg1}, ${recv_arg2}, ${recv_arg3}, ${recv_arg4});
45                  int main(void) {
46                    ${recv_arg1} s=0;
47                    ${recv_arg2} buf=0;
48                    ${recv_arg3} len=0;
49                    ${recv_arg4} flags=0;
50                    ${recv_retv} res = recv(s, buf, len, flags);
51                    (void) res;
52                    return 0;
53                  }"
54                  curl_cv_func_recv_test)
55                message(STATUS
56                  "Tested: ${recv_retv} recv(${recv_arg1}, ${recv_arg2}, ${recv_arg3}, ${recv_arg4})")
57                if(curl_cv_func_recv_test)
58                  set(curl_cv_func_recv_args
59                    "${recv_arg1},${recv_arg2},${recv_arg3},${recv_arg4},${recv_retv}")
60                  set(RECV_TYPE_ARG1 "${recv_arg1}")
61                  set(RECV_TYPE_ARG2 "${recv_arg2}")
62                  set(RECV_TYPE_ARG3 "${recv_arg3}")
63                  set(RECV_TYPE_ARG4 "${recv_arg4}")
64                  set(RECV_TYPE_RETV "${recv_retv}")
65                  set(HAVE_RECV 1)
66                  set(curl_cv_func_recv_done 1)
67                endif()
68              endif()
69            endforeach()
70          endforeach()
71        endforeach()
72      endforeach()
73    endforeach()
74  else()
75    string(REGEX REPLACE "^([^,]*),[^,]*,[^,]*,[^,]*,[^,]*$" "\\1" RECV_TYPE_ARG1 "${curl_cv_func_recv_args}")
76    string(REGEX REPLACE "^[^,]*,([^,]*),[^,]*,[^,]*,[^,]*$" "\\1" RECV_TYPE_ARG2 "${curl_cv_func_recv_args}")
77    string(REGEX REPLACE "^[^,]*,[^,]*,([^,]*),[^,]*,[^,]*$" "\\1" RECV_TYPE_ARG3 "${curl_cv_func_recv_args}")
78    string(REGEX REPLACE "^[^,]*,[^,]*,[^,]*,([^,]*),[^,]*$" "\\1" RECV_TYPE_ARG4 "${curl_cv_func_recv_args}")
79    string(REGEX REPLACE "^[^,]*,[^,]*,[^,]*,[^,]*,([^,]*)$" "\\1" RECV_TYPE_RETV "${curl_cv_func_recv_args}")
80  endif()
81
82  if("${curl_cv_func_recv_args}" STREQUAL "unknown")
83    message(FATAL_ERROR "Cannot find proper types to use for recv args")
84  endif()
85else()
86  message(FATAL_ERROR "Unable to link function recv")
87endif()
88set(curl_cv_func_recv_args "${curl_cv_func_recv_args}" CACHE INTERNAL "Arguments for recv")
89set(HAVE_RECV 1)
90
91check_c_source_compiles("${_source_epilogue}
92int main(void) {
93    send(0, 0, 0, 0);
94    return 0;
95}" curl_cv_send)
96if(curl_cv_send)
97  if(NOT DEFINED curl_cv_func_send_args OR "${curl_cv_func_send_args}" STREQUAL "unknown")
98    foreach(send_retv "int" "ssize_t" )
99      foreach(send_arg1 "SOCKET" "int" "ssize_t" )
100        foreach(send_arg2 "const char *" "const void *" "void *" "char *")
101          foreach(send_arg3 "int" "size_t" "socklen_t" "unsigned int")
102            foreach(send_arg4 "int" "unsigned int")
103              if(NOT curl_cv_func_send_done)
104                unset(curl_cv_func_send_test CACHE)
105                check_c_source_compiles("
106                  ${_source_epilogue}
107                  extern ${send_retv} ${signature_call_conv}
108                  send(${send_arg1}, ${send_arg2}, ${send_arg3}, ${send_arg4});
109                  int main(void) {
110                    ${send_arg1} s=0;
111                    ${send_arg2} buf=0;
112                    ${send_arg3} len=0;
113                    ${send_arg4} flags=0;
114                    ${send_retv} res = send(s, buf, len, flags);
115                    (void) res;
116                    return 0;
117                  }"
118                  curl_cv_func_send_test)
119                message(STATUS
120                  "Tested: ${send_retv} send(${send_arg1}, ${send_arg2}, ${send_arg3}, ${send_arg4})")
121                if(curl_cv_func_send_test)
122                  string(REGEX REPLACE "(const) .*" "\\1" send_qual_arg2 "${send_arg2}")
123                  string(REGEX REPLACE "const (.*)" "\\1" send_arg2 "${send_arg2}")
124                  set(curl_cv_func_send_args
125                    "${send_arg1},${send_arg2},${send_arg3},${send_arg4},${send_retv},${send_qual_arg2}")
126                  set(SEND_TYPE_ARG1 "${send_arg1}")
127                  set(SEND_TYPE_ARG2 "${send_arg2}")
128                  set(SEND_TYPE_ARG3 "${send_arg3}")
129                  set(SEND_TYPE_ARG4 "${send_arg4}")
130                  set(SEND_TYPE_RETV "${send_retv}")
131                  set(HAVE_SEND 1)
132                  set(curl_cv_func_send_done 1)
133                endif()
134              endif()
135            endforeach()
136          endforeach()
137        endforeach()
138      endforeach()
139    endforeach()
140  else()
141    string(REGEX REPLACE "^([^,]*),[^,]*,[^,]*,[^,]*,[^,]*,[^,]*$" "\\1" SEND_TYPE_ARG1 "${curl_cv_func_send_args}")
142    string(REGEX REPLACE "^[^,]*,([^,]*),[^,]*,[^,]*,[^,]*,[^,]*$" "\\1" SEND_TYPE_ARG2 "${curl_cv_func_send_args}")
143    string(REGEX REPLACE "^[^,]*,[^,]*,([^,]*),[^,]*,[^,]*,[^,]*$" "\\1" SEND_TYPE_ARG3 "${curl_cv_func_send_args}")
144    string(REGEX REPLACE "^[^,]*,[^,]*,[^,]*,([^,]*),[^,]*,[^,]*$" "\\1" SEND_TYPE_ARG4 "${curl_cv_func_send_args}")
145    string(REGEX REPLACE "^[^,]*,[^,]*,[^,]*,[^,]*,([^,]*),[^,]*$" "\\1" SEND_TYPE_RETV "${curl_cv_func_send_args}")
146    string(REGEX REPLACE "^[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,([^,]*)$" "\\1" SEND_QUAL_ARG2 "${curl_cv_func_send_args}")
147  endif()
148
149  if("${curl_cv_func_send_args}" STREQUAL "unknown")
150    message(FATAL_ERROR "Cannot find proper types to use for send args")
151  endif()
152  set(SEND_QUAL_ARG2 "const")
153else()
154  message(FATAL_ERROR "Unable to link function send")
155endif()
156set(curl_cv_func_send_args "${curl_cv_func_send_args}" CACHE INTERNAL "Arguments for send")
157set(HAVE_SEND 1)
158
159check_c_source_compiles("${_source_epilogue}
160  int main(void) {
161    int flag = MSG_NOSIGNAL;
162    (void)flag;
163    return 0;
164  }" HAVE_MSG_NOSIGNAL)
165
166if(NOT HAVE_WINDOWS_H)
167  add_header_include(HAVE_SYS_TIME_H "sys/time.h")
168  add_header_include(TIME_WITH_SYS_TIME "time.h")
169  add_header_include(HAVE_TIME_H "time.h")
170endif()
171check_c_source_compiles("${_source_epilogue}
172int main(void) {
173  struct timeval ts;
174  ts.tv_sec  = 0;
175  ts.tv_usec = 0;
176  (void)ts;
177  return 0;
178}" HAVE_STRUCT_TIMEVAL)
179
180if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
181  # if not cross-compilation...
182  include(CheckCSourceRuns)
183  set(CMAKE_REQUIRED_FLAGS "")
184  if(HAVE_SYS_POLL_H)
185    set(CMAKE_REQUIRED_FLAGS "-DHAVE_SYS_POLL_H")
186  elseif(HAVE_POLL_H)
187    set(CMAKE_REQUIRED_FLAGS "-DHAVE_POLL_H")
188  endif()
189  check_c_source_runs("
190    #include <stdlib.h>
191    #include <sys/time.h>
192
193    #ifdef HAVE_SYS_POLL_H
194    #  include <sys/poll.h>
195    #elif  HAVE_POLL_H
196    #  include <poll.h>
197    #endif
198
199    int main(void)
200    {
201        if(0 != poll(0, 0, 10)) {
202          return 1; /* fail */
203        }
204        else {
205          /* detect the 10.12 poll() breakage */
206          struct timeval before, after;
207          int rc;
208          size_t us;
209
210          gettimeofday(&before, NULL);
211          rc = poll(NULL, 0, 500);
212          gettimeofday(&after, NULL);
213
214          us = (after.tv_sec - before.tv_sec) * 1000000 +
215            (after.tv_usec - before.tv_usec);
216
217          if(us < 400000) {
218            return 1;
219          }
220        }
221        return 0;
222    }" HAVE_POLL_FINE)
223endif()
224
225set(HAVE_SIG_ATOMIC_T 1)
226set(CMAKE_REQUIRED_FLAGS)
227if(HAVE_SIGNAL_H)
228  set(CMAKE_REQUIRED_FLAGS "-DHAVE_SIGNAL_H")
229  set(CMAKE_EXTRA_INCLUDE_FILES "signal.h")
230endif()
231check_type_size("sig_atomic_t" SIZEOF_SIG_ATOMIC_T)
232if(HAVE_SIZEOF_SIG_ATOMIC_T)
233  check_c_source_compiles("
234    #ifdef HAVE_SIGNAL_H
235    #  include <signal.h>
236    #endif
237    int main(void) {
238      static volatile sig_atomic_t dummy = 0;
239      (void)dummy;
240      return 0;
241    }" HAVE_SIG_ATOMIC_T_NOT_VOLATILE)
242  if(NOT HAVE_SIG_ATOMIC_T_NOT_VOLATILE)
243    set(HAVE_SIG_ATOMIC_T_VOLATILE 1)
244  endif()
245endif()
246
247if(HAVE_WINDOWS_H)
248  set(CMAKE_EXTRA_INCLUDE_FILES winsock2.h)
249else()
250  set(CMAKE_EXTRA_INCLUDE_FILES)
251  if(HAVE_SYS_SOCKET_H)
252    set(CMAKE_EXTRA_INCLUDE_FILES sys/socket.h)
253  endif()
254endif()
255
256check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE)
257if(HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE)
258  set(HAVE_STRUCT_SOCKADDR_STORAGE 1)
259endif()
260