OtherTests.cmake 10 KB

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