OtherTests.cmake 9.3 KB

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