2
0

OtherTests.cmake 10.0 KB

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