OtherTests.cmake 10.0 KB

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