OtherTests.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) 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. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. include(CheckCSourceCompiles)
  25. include(CheckCSourceRuns)
  26. include(CheckTypeSize)
  27. macro(add_header_include check header)
  28. if(${check})
  29. set(_source_epilogue "${_source_epilogue}
  30. #include <${header}>")
  31. endif()
  32. endmacro()
  33. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  34. if(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE)
  35. set(CMAKE_EXTRA_INCLUDE_FILES)
  36. if(WIN32)
  37. set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
  38. set(CMAKE_REQUIRED_DEFINITIONS "-DWIN32_LEAN_AND_MEAN")
  39. set(CMAKE_REQUIRED_LIBRARIES "ws2_32")
  40. elseif(HAVE_SYS_SOCKET_H)
  41. set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h")
  42. endif()
  43. check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE)
  44. set(HAVE_STRUCT_SOCKADDR_STORAGE ${HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE})
  45. endif()
  46. if(NOT WIN32)
  47. set(_source_epilogue "#undef inline")
  48. add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
  49. add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
  50. check_c_source_compiles("${_source_epilogue}
  51. int main(void)
  52. {
  53. int flag = MSG_NOSIGNAL;
  54. (void)flag;
  55. return 0;
  56. }" HAVE_MSG_NOSIGNAL)
  57. endif()
  58. set(_source_epilogue "#undef inline")
  59. add_header_include(HAVE_SYS_TIME_H "sys/time.h")
  60. check_c_source_compiles("${_source_epilogue}
  61. #include <time.h>
  62. int main(void)
  63. {
  64. struct timeval ts;
  65. ts.tv_sec = 0;
  66. ts.tv_usec = 0;
  67. (void)ts;
  68. return 0;
  69. }" HAVE_STRUCT_TIMEVAL)
  70. unset(CMAKE_TRY_COMPILE_TARGET_TYPE)
  71. if(NOT CMAKE_CROSSCOMPILING AND NOT APPLE)
  72. set(_source_epilogue "#undef inline")
  73. add_header_include(HAVE_SYS_POLL_H "sys/poll.h")
  74. add_header_include(HAVE_POLL_H "poll.h")
  75. check_c_source_runs("${_source_epilogue}
  76. #include <stdlib.h>
  77. #include <sys/time.h>
  78. int main(void)
  79. {
  80. if(0 != poll(0, 0, 10)) {
  81. return 1; /* fail */
  82. }
  83. else {
  84. /* detect the 10.12 poll() breakage */
  85. struct timeval before, after;
  86. int rc;
  87. size_t us;
  88. gettimeofday(&before, NULL);
  89. rc = poll(NULL, 0, 500);
  90. gettimeofday(&after, NULL);
  91. us = (after.tv_sec - before.tv_sec) * 1000000 +
  92. (after.tv_usec - before.tv_usec);
  93. if(us < 400000) {
  94. return 1;
  95. }
  96. }
  97. return 0;
  98. }" HAVE_POLL_FINE)
  99. endif()
  100. # Detect HAVE_GETADDRINFO_THREADSAFE
  101. if(WIN32)
  102. set(HAVE_GETADDRINFO_THREADSAFE ${HAVE_GETADDRINFO})
  103. elseif(NOT HAVE_GETADDRINFO)
  104. set(HAVE_GETADDRINFO_THREADSAFE FALSE)
  105. elseif(APPLE OR
  106. CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  107. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
  108. CMAKE_SYSTEM_NAME STREQUAL "HP-UX" OR
  109. CMAKE_SYSTEM_NAME STREQUAL "MidnightBSD" OR
  110. CMAKE_SYSTEM_NAME STREQUAL "NetBSD" OR
  111. CMAKE_SYSTEM_NAME STREQUAL "SunOS")
  112. set(HAVE_GETADDRINFO_THREADSAFE TRUE)
  113. elseif(CMAKE_SYSTEM_NAME MATCHES "BSD")
  114. set(HAVE_GETADDRINFO_THREADSAFE FALSE)
  115. endif()
  116. if(NOT DEFINED HAVE_GETADDRINFO_THREADSAFE)
  117. set(_source_epilogue "#undef inline")
  118. add_header_include(HAVE_SYS_SOCKET_H "sys/socket.h")
  119. add_header_include(HAVE_SYS_TIME_H "sys/time.h")
  120. add_header_include(HAVE_NETDB_H "netdb.h")
  121. check_c_source_compiles("${_source_epilogue}
  122. int main(void)
  123. {
  124. #ifdef h_errno
  125. return 0;
  126. #else
  127. force compilation error
  128. #endif
  129. }" HAVE_H_ERRNO)
  130. if(NOT HAVE_H_ERRNO)
  131. check_c_source_compiles("${_source_epilogue}
  132. int main(void)
  133. {
  134. h_errno = 2;
  135. return h_errno != 0 ? 1 : 0;
  136. }" HAVE_H_ERRNO_ASSIGNABLE)
  137. if(NOT HAVE_H_ERRNO_ASSIGNABLE)
  138. check_c_source_compiles("${_source_epilogue}
  139. int main(void)
  140. {
  141. #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
  142. return 0;
  143. #elif defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 700)
  144. return 0;
  145. #else
  146. force compilation error
  147. #endif
  148. }" HAVE_H_ERRNO_SBS_ISSUE_7)
  149. endif()
  150. endif()
  151. if(HAVE_H_ERRNO OR HAVE_H_ERRNO_ASSIGNABLE OR HAVE_H_ERRNO_SBS_ISSUE_7)
  152. set(HAVE_GETADDRINFO_THREADSAFE TRUE)
  153. endif()
  154. endif()
  155. if(NOT WIN32 AND NOT DEFINED HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
  156. set(_source_epilogue "#undef inline")
  157. add_header_include(HAVE_SYS_TYPES_H "sys/types.h")
  158. add_header_include(HAVE_SYS_TIME_H "sys/time.h")
  159. check_c_source_compiles("${_source_epilogue}
  160. #include <time.h>
  161. int main(void)
  162. {
  163. struct timespec ts;
  164. (void)clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  165. return 0;
  166. }" HAVE_CLOCK_GETTIME_MONOTONIC_RAW)
  167. endif()