swrap: Provide a fcntl64()
[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(openat64 HAVE_OPENAT64)
74 check_function_exists(fopen64 HAVE_FOPEN64)
75 check_function_exists(getprogname HAVE_GETPROGNAME)
76 check_function_exists(getexecname HAVE_GETEXECNAME)
77 check_function_exists(pledge HAVE_PLEDGE)
78 check_function_exists(_socket HAVE__SOCKET)
79 check_function_exists(_close HAVE__CLOSE)
80 check_function_exists(__close_nocancel HAVE___CLOSE_NOCANCEL)
81 check_function_exists(recvmmsg HAVE_RECVMMSG)
82 check_function_exists(sendmmsg HAVE_SENDMMSG)
83 check_function_exists(syscall HAVE_SYSCALL)
84 check_function_exists(fcntl64 HAVE_FCNTL64)
85
86 if (UNIX)
87     find_library(DLFCN_LIBRARY dl)
88     if (DLFCN_LIBRARY)
89         list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
90     else()
91         check_function_exists(dlopen HAVE_DLOPEN)
92         if (NOT HAVE_DLOPEN)
93             message(FATAL_ERROR "FATAL: No dlopen() function detected")
94         endif()
95     endif()
96
97     if (NOT LINUX)
98         # libsocket (Solaris)
99         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
100         if (HAVE_LIBSOCKET)
101             list(APPEND _REQUIRED_LIBRARIES socket)
102         endif (HAVE_LIBSOCKET)
103
104         # libnsl/inet_pton (Solaris)
105         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
106         if (HAVE_LIBNSL)
107             list(APPEND _REQUIRED_LIBRARIES nsl)
108         endif (HAVE_LIBNSL)
109     endif (NOT LINUX)
110
111     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
112 endif (UNIX)
113
114 # STRUCTS
115 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
116 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
117 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
118 unset(CMAKE_REQUIRED_FLAGS)
119
120 # STRUCT MEMBERS
121 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
122 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
123
124 # PROTOTYPES
125 check_prototype_definition(gettimeofday
126     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
127     "-1"
128     "sys/time.h"
129     HAVE_GETTIMEOFDAY_TZ)
130
131 check_prototype_definition(gettimeofday
132     "int gettimeofday(struct timeval *tv, void *tzp)"
133     "-1"
134     "sys/time.h"
135     HAVE_GETTIMEOFDAY_TZ_VOID)
136
137 check_prototype_definition(accept
138     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
139     "-1"
140     "sys/types.h;sys/socket.h"
141     HAVE_ACCEPT_PSOCKLEN_T)
142
143 check_prototype_definition(ioctl
144     "int ioctl(int s, int r, ...)"
145     "-1"
146     "unistd.h;sys/ioctl.h"
147     HAVE_IOCTL_INT)
148
149 if (HAVE_EVENTFD)
150     check_prototype_definition(eventfd
151         "int eventfd(unsigned int count, int flags)"
152         "-1"
153         "sys/eventfd.h"
154         HAVE_EVENTFD_UNSIGNED_INT)
155 endif (HAVE_EVENTFD)
156
157 if (HAVE_SYSCALL)
158     set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
159
160     check_prototype_definition(syscall
161         "int syscall(int sysno, ...)"
162         "-1"
163         "unistd.h;sys/syscall.h"
164         HAVE_SYSCALL_INT)
165     set(CMAKE_REQUIRED_DEFINITIONS)
166 endif (HAVE_SYSCALL)
167
168 if (HAVE_RECVMMSG)
169     # Linux legacy glibc < 2.21
170     set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
171     check_prototype_definition(recvmmsg
172         "int recvmmsg(int __fd, struct mmsghdr *__vmessages, unsigned int __vlen, int __flags, const struct timespec *__tmo)"
173         "-1"
174         "sys/types.h;sys/socket.h"
175         HAVE_RECVMMSG_CONST_TIMEOUT)
176     set(CMAKE_REQUIRED_DEFINITIONS)
177
178     # FreeBSD
179     check_prototype_definition(recvmmsg
180         "ssize_t recvmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags, const struct timespec * __restrict __tmo)"
181         "-1"
182         "sys/types.h;sys/socket.h"
183         HAVE_RECVMMSG_SSIZE_T_CONST_TIMEOUT)
184 endif (HAVE_RECVMMSG)
185
186 if (HAVE_SENDMMSG)
187     # FreeBSD
188     check_prototype_definition(sendmmsg
189         "ssize_t sendmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags)"
190         "-1"
191         "sys/types.h;sys/socket.h"
192         HAVE_SENDMMSG_SSIZE_T)
193 endif (HAVE_SENDMMSG)
194
195 # IPV6
196 check_c_source_compiles("
197     #include <stdlib.h>
198     #include <sys/socket.h>
199     #include <netdb.h>
200     #include <netinet/in.h>
201     #include <net/if.h>
202
203 int main(void) {
204     struct sockaddr_storage sa_store;
205     struct addrinfo *ai = NULL;
206     struct in6_addr in6addr;
207     int idx = if_nametoindex(\"iface1\");
208     int s = socket(AF_INET6, SOCK_STREAM, 0);
209     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
210     if (ret != 0) {
211         const char *es = gai_strerror(ret);
212     }
213
214     freeaddrinfo(ai);
215     {
216         int val = 1;
217 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
218 #define IPV6_V6ONLY 26
219 #endif
220         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
221                          (const void *)&val, sizeof(val));
222     }
223
224     return 0;
225 }" HAVE_IPV6)
226
227 check_c_source_compiles("
228 #include <sys/socket.h>
229
230 int main(void) {
231     struct sockaddr_storage s;
232
233     return 0;
234 }" HAVE_SOCKADDR_STORAGE)
235
236 ###########################################################
237 # For detecting attributes we need to treat warnings as
238 # errors
239 set(CMAKE_REQUIRED_FLAGS "-Werror")
240
241 check_c_source_compiles("
242 void test_constructor_attribute(void) __attribute__ ((constructor));
243
244 void test_constructor_attribute(void)
245 {
246     return;
247 }
248
249 int main(void) {
250     return 0;
251 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
252
253 check_c_source_compiles("
254 void test_destructor_attribute(void) __attribute__ ((destructor));
255
256 void test_destructor_attribute(void)
257 {
258     return;
259 }
260
261 int main(void) {
262     return 0;
263 }" HAVE_DESTRUCTOR_ATTRIBUTE)
264
265 check_c_source_compiles("
266 #pragma init (test_constructor)
267 void test_constructor(void);
268
269 void test_constructor(void)
270 {
271     return;
272 }
273
274 int main(void) {
275     return 0;
276 }" HAVE_PRAGMA_INIT)
277
278 check_c_source_compiles("
279 #pragma fini (test_destructor)
280 void test_destructor(void);
281
282 void test_destructor(void)
283 {
284     return;
285 }
286
287 int main(void) {
288     return 0;
289 }" HAVE_PRAGMA_FINI)
290
291 check_c_source_compiles("
292 #define FALL_THROUGH __attribute__((fallthrough))
293
294 int main(void) {
295     int i = 2;
296
297     switch (i) {
298     case 0:
299         FALL_THROUGH;
300     case 1:
301         break;
302     default:
303         break;
304     }
305
306     return 0;
307 }" HAVE_FALLTHROUGH_ATTRIBUTE)
308
309 check_c_source_compiles("
310 __thread int tls;
311
312 int main(void) {
313     return 0;
314 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
315
316 check_c_source_compiles("
317 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
318
319 int main(void) {
320     return 0;
321 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
322
323 check_c_source_compiles("
324 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
325
326 void test_address_sanitizer_attribute(void)
327 {
328     return;
329 }
330
331 int main(void) {
332     return 0;
333 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
334
335 # Stop treating wanrings as errors
336 unset(CMAKE_REQUIRED_FLAGS)
337 ###########################################################
338
339 if (OSX)
340     set(HAVE_APPLE 1)
341 endif (OSX)
342
343 # ENDIAN
344 if (NOT WIN32)
345     test_big_endian(WORDS_BIGENDIAN)
346 endif (NOT WIN32)
347
348 check_type_size(pid_t SIZEOF_PID_T)
349
350 set(SWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")