tests: New test with poll
[socket_wrapper.git] / ConfigureChecks.cmake
1 include(CheckIncludeFile)
2 include(CheckSymbolExists)
3 include(CheckFunctionExists)
4 include(CheckLibraryExists)
5 include(CheckTypeSize)
6 include(CheckStructHasMember)
7 include(CheckPrototypeDefinition)
8 include(TestBigEndian)
9
10 set(SOCKET_WRAPPER_PACKAGE ${PROJECT_NAME})
11 set(SOCKET_WRAPPER_VERSION ${PROJECT_VERSION})
12
13 set(BINARYDIR ${CMAKE_BINARY_DIR})
14 set(SOURCEDIR ${CMAKE_SOURCE_DIR})
15
16 function(COMPILER_DUMPVERSION _OUTPUT_VERSION)
17     # Remove whitespaces from the argument.
18     # This is needed for CC="ccache gcc" cmake ..
19     string(REPLACE " " "" _C_COMPILER_ARG "${CMAKE_C_COMPILER_ARG1}")
20
21     execute_process(
22         COMMAND
23             ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
24         OUTPUT_VARIABLE _COMPILER_VERSION
25     )
26
27     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
28            _COMPILER_VERSION "${_COMPILER_VERSION}")
29
30     set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
31 endfunction()
32
33 if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
34     compiler_dumpversion(GNUCC_VERSION)
35     if (NOT GNUCC_VERSION EQUAL 34)
36         set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
37         check_c_source_compiles(
38 "void __attribute__((visibility(\"default\"))) test() {}
39 int main(void){ return 0; }
40 " WITH_VISIBILITY_HIDDEN)
41         unset(CMAKE_REQUIRED_FLAGS)
42     endif (NOT GNUCC_VERSION EQUAL 34)
43 endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
44
45 # HEADERS
46 check_include_file(netinet/tcp_fsm.h HAVE_NETINET_TCP_FSM_H)
47 check_include_file(sys/filio.h HAVE_SYS_FILIO_H)
48 check_include_file(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
49 check_include_file(sys/eventfd.h HAVE_SYS_EVENTFD_H)
50 check_include_file(sys/timerfd.h HAVE_SYS_TIMERFD_H)
51 check_include_file(sys/syscall.h HAVE_SYS_SYSCALL_H)
52 check_include_file(gnu/lib-names.h HAVE_GNU_LIB_NAMES_H)
53 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
54 check_include_file(syscall.h HAVE_SYSCALL_H)
55
56 # SYMBOLS
57 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
58 check_symbol_exists(program_invocation_short_name
59                     "errno.h"
60                     HAVE_PROGRAM_INVOCATION_SHORT_NAME)
61 unset(CMAKE_REQUIRED_FLAGS)
62
63 # FUNCTIONS
64 check_function_exists(strncpy HAVE_STRNCPY)
65 check_function_exists(vsnprintf HAVE_VSNPRINTF)
66 check_function_exists(snprintf HAVE_SNPRINTF)
67 check_function_exists(signalfd HAVE_SIGNALFD)
68 check_function_exists(eventfd HAVE_EVENTFD)
69 check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
70 check_function_exists(bindresvport HAVE_BINDRESVPORT)
71 check_function_exists(accept4 HAVE_ACCEPT4)
72 check_function_exists(open64 HAVE_OPEN64)
73 check_function_exists(fopen64 HAVE_FOPEN64)
74 check_function_exists(getprogname HAVE_GETPROGNAME)
75 check_function_exists(getexecname HAVE_GETEXECNAME)
76 check_function_exists(pledge HAVE_PLEDGE)
77 check_function_exists(_socket HAVE__SOCKET)
78 check_function_exists(_close HAVE__CLOSE)
79 check_function_exists(__close_nocancel HAVE___CLOSE_NOCANCEL)
80 check_function_exists(recvmmsg HAVE_RECVMMSG)
81 check_function_exists(sendmmsg HAVE_SENDMMSG)
82 check_function_exists(syscall HAVE_SYSCALL)
83
84 if (UNIX)
85     find_library(DLFCN_LIBRARY dl)
86     if (DLFCN_LIBRARY)
87         list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
88     else()
89         check_function_exists(dlopen HAVE_DLOPEN)
90         if (NOT HAVE_DLOPEN)
91             message(FATAL_ERROR "FATAL: No dlopen() function detected")
92         endif()
93     endif()
94
95     if (NOT LINUX)
96         # libsocket (Solaris)
97         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
98         if (HAVE_LIBSOCKET)
99             list(APPEND _REQUIRED_LIBRARIES socket)
100         endif (HAVE_LIBSOCKET)
101
102         # libnsl/inet_pton (Solaris)
103         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
104         if (HAVE_LIBNSL)
105             list(APPEND _REQUIRED_LIBRARIES nsl)
106         endif (HAVE_LIBNSL)
107     endif (NOT LINUX)
108
109     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
110 endif (UNIX)
111
112 # STRUCTS
113 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
114 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
115 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
116 unset(CMAKE_REQUIRED_FLAGS)
117
118 # STRUCT MEMBERS
119 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
120 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
121
122 # PROTOTYPES
123 check_prototype_definition(gettimeofday
124     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
125     "-1"
126     "sys/time.h"
127     HAVE_GETTIMEOFDAY_TZ)
128
129 check_prototype_definition(gettimeofday
130     "int gettimeofday(struct timeval *tv, void *tzp)"
131     "-1"
132     "sys/time.h"
133     HAVE_GETTIMEOFDAY_TZ_VOID)
134
135 check_prototype_definition(accept
136     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
137     "-1"
138     "sys/types.h;sys/socket.h"
139     HAVE_ACCEPT_PSOCKLEN_T)
140
141 check_prototype_definition(ioctl
142     "int ioctl(int s, int r, ...)"
143     "-1"
144     "unistd.h;sys/ioctl.h"
145     HAVE_IOCTL_INT)
146
147 if (HAVE_EVENTFD)
148     check_prototype_definition(eventfd
149         "int eventfd(unsigned int count, int flags)"
150         "-1"
151         "sys/eventfd.h"
152         HAVE_EVENTFD_UNSIGNED_INT)
153 endif (HAVE_EVENTFD)
154
155 if (HAVE_SYSCALL)
156     set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
157
158     check_prototype_definition(syscall
159         "int syscall(int sysno, ...)"
160         "-1"
161         "unistd.h;sys/syscall.h"
162         HAVE_SYSCALL_INT)
163     set(CMAKE_REQUIRED_DEFINITIONS)
164 endif (HAVE_SYSCALL)
165
166 if (HAVE_RECVMMSG)
167     # Linux legacy glibc < 2.21
168     set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
169     check_prototype_definition(recvmmsg
170         "int recvmmsg(int __fd, struct mmsghdr *__vmessages, unsigned int __vlen, int __flags, const struct timespec *__tmo)"
171         "-1"
172         "sys/types.h;sys/socket.h"
173         HAVE_RECVMMSG_CONST_TIMEOUT)
174     set(CMAKE_REQUIRED_DEFINITIONS)
175
176     # FreeBSD
177     check_prototype_definition(recvmmsg
178         "ssize_t recvmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags, const struct timespec * __restrict __tmo)"
179         "-1"
180         "sys/types.h;sys/socket.h"
181         HAVE_RECVMMSG_SSIZE_T_CONST_TIMEOUT)
182 endif (HAVE_RECVMMSG)
183
184 if (HAVE_SENDMMSG)
185     # FreeBSD
186     check_prototype_definition(sendmmsg
187         "ssize_t sendmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags)"
188         "-1"
189         "sys/types.h;sys/socket.h"
190         HAVE_SENDMMSG_SSIZE_T)
191 endif (HAVE_SENDMMSG)
192
193 # IPV6
194 check_c_source_compiles("
195     #include <stdlib.h>
196     #include <sys/socket.h>
197     #include <netdb.h>
198     #include <netinet/in.h>
199     #include <net/if.h>
200
201 int main(void) {
202     struct sockaddr_storage sa_store;
203     struct addrinfo *ai = NULL;
204     struct in6_addr in6addr;
205     int idx = if_nametoindex(\"iface1\");
206     int s = socket(AF_INET6, SOCK_STREAM, 0);
207     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
208     if (ret != 0) {
209         const char *es = gai_strerror(ret);
210     }
211
212     freeaddrinfo(ai);
213     {
214         int val = 1;
215 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
216 #define IPV6_V6ONLY 26
217 #endif
218         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
219                          (const void *)&val, sizeof(val));
220     }
221
222     return 0;
223 }" HAVE_IPV6)
224
225 check_c_source_compiles("
226 #include <sys/socket.h>
227
228 int main(void) {
229     struct sockaddr_storage s;
230
231     return 0;
232 }" HAVE_SOCKADDR_STORAGE)
233
234 ###########################################################
235 # For detecting attributes we need to treat warnings as
236 # errors
237 set(CMAKE_REQUIRED_FLAGS "-Werror")
238
239 check_c_source_compiles("
240 void test_constructor_attribute(void) __attribute__ ((constructor));
241
242 void test_constructor_attribute(void)
243 {
244     return;
245 }
246
247 int main(void) {
248     return 0;
249 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
250
251 check_c_source_compiles("
252 void test_destructor_attribute(void) __attribute__ ((destructor));
253
254 void test_destructor_attribute(void)
255 {
256     return;
257 }
258
259 int main(void) {
260     return 0;
261 }" HAVE_DESTRUCTOR_ATTRIBUTE)
262
263 check_c_source_compiles("
264 #pragma init (test_constructor)
265 void test_constructor(void);
266
267 void test_constructor(void)
268 {
269     return;
270 }
271
272 int main(void) {
273     return 0;
274 }" HAVE_PRAGMA_INIT)
275
276 check_c_source_compiles("
277 #pragma fini (test_destructor)
278 void test_destructor(void);
279
280 void test_destructor(void)
281 {
282     return;
283 }
284
285 int main(void) {
286     return 0;
287 }" HAVE_PRAGMA_FINI)
288
289 check_c_source_compiles("
290 #define FALL_THROUGH __attribute__((fallthrough))
291
292 int main(void) {
293     int i = 2;
294
295     switch (i) {
296     case 0:
297         FALL_THROUGH;
298     case 1:
299         break;
300     default:
301         break;
302     }
303
304     return 0;
305 }" HAVE_FALLTHROUGH_ATTRIBUTE)
306
307 check_c_source_compiles("
308 __thread int tls;
309
310 int main(void) {
311     return 0;
312 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
313
314 check_c_source_compiles("
315 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
316
317 int main(void) {
318     return 0;
319 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
320
321 check_c_source_compiles("
322 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
323
324 void test_address_sanitizer_attribute(void)
325 {
326     return;
327 }
328
329 int main(void) {
330     return 0;
331 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
332
333 # Stop treating wanrings as errors
334 unset(CMAKE_REQUIRED_FLAGS)
335 ###########################################################
336
337 if (OSX)
338     set(HAVE_APPLE 1)
339 endif (OSX)
340
341 # ENDIAN
342 if (NOT WIN32)
343     test_big_endian(WORDS_BIGENDIAN)
344 endif (NOT WIN32)
345
346 check_type_size(pid_t SIZEOF_PID_T)
347
348 set(SWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")