1 include(CheckIncludeFile)
2 include(CheckSymbolExists)
3 include(CheckFunctionExists)
4 include(CheckLibraryExists)
6 include(CheckStructHasMember)
7 include(CheckPrototypeDefinition)
10 set(SOCKET_WRAPPER_PACKAGE ${PROJECT_NAME})
11 set(SOCKET_WRAPPER_VERSION ${PROJECT_VERSION})
13 set(BINARYDIR ${CMAKE_BINARY_DIR})
14 set(SOURCEDIR ${CMAKE_SOURCE_DIR})
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}")
23 ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
24 OUTPUT_VARIABLE _COMPILER_VERSION
27 string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
28 _COMPILER_VERSION "${_COMPILER_VERSION}")
30 set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
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)
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)
57 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
58 check_symbol_exists(program_invocation_short_name
60 HAVE_PROGRAM_INVOCATION_SHORT_NAME)
61 unset(CMAKE_REQUIRED_FLAGS)
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 check_function_exists(fcntl64 HAVE_FCNTL64)
86 find_library(DLFCN_LIBRARY dl)
88 list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
90 check_function_exists(dlopen HAVE_DLOPEN)
92 message(FATAL_ERROR "FATAL: No dlopen() function detected")
98 check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
100 list(APPEND _REQUIRED_LIBRARIES socket)
101 endif (HAVE_LIBSOCKET)
103 # libnsl/inet_pton (Solaris)
104 check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
106 list(APPEND _REQUIRED_LIBRARIES nsl)
110 check_function_exists(getaddrinfo HAVE_GETADDRINFO)
114 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
115 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
116 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
117 unset(CMAKE_REQUIRED_FLAGS)
120 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
121 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
124 check_prototype_definition(gettimeofday
125 "int gettimeofday(struct timeval *tv, struct timezone *tz)"
128 HAVE_GETTIMEOFDAY_TZ)
130 check_prototype_definition(gettimeofday
131 "int gettimeofday(struct timeval *tv, void *tzp)"
134 HAVE_GETTIMEOFDAY_TZ_VOID)
136 check_prototype_definition(accept
137 "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
139 "sys/types.h;sys/socket.h"
140 HAVE_ACCEPT_PSOCKLEN_T)
142 check_prototype_definition(ioctl
143 "int ioctl(int s, int r, ...)"
145 "unistd.h;sys/ioctl.h"
149 check_prototype_definition(eventfd
150 "int eventfd(unsigned int count, int flags)"
153 HAVE_EVENTFD_UNSIGNED_INT)
157 set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
159 check_prototype_definition(syscall
160 "int syscall(int sysno, ...)"
162 "unistd.h;sys/syscall.h"
164 set(CMAKE_REQUIRED_DEFINITIONS)
168 # Linux legacy glibc < 2.21
169 set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
170 check_prototype_definition(recvmmsg
171 "int recvmmsg(int __fd, struct mmsghdr *__vmessages, unsigned int __vlen, int __flags, const struct timespec *__tmo)"
173 "sys/types.h;sys/socket.h"
174 HAVE_RECVMMSG_CONST_TIMEOUT)
175 set(CMAKE_REQUIRED_DEFINITIONS)
178 check_prototype_definition(recvmmsg
179 "ssize_t recvmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags, const struct timespec * __restrict __tmo)"
181 "sys/types.h;sys/socket.h"
182 HAVE_RECVMMSG_SSIZE_T_CONST_TIMEOUT)
183 endif (HAVE_RECVMMSG)
187 check_prototype_definition(sendmmsg
188 "ssize_t sendmmsg(int __fd, struct mmsghdr * __restrict __vmessages, size_t __vlen, int __flags)"
190 "sys/types.h;sys/socket.h"
191 HAVE_SENDMMSG_SSIZE_T)
192 endif (HAVE_SENDMMSG)
195 check_c_source_compiles("
197 #include <sys/socket.h>
199 #include <netinet/in.h>
203 struct sockaddr_storage sa_store;
204 struct addrinfo *ai = NULL;
205 struct in6_addr in6addr;
206 int idx = if_nametoindex(\"iface1\");
207 int s = socket(AF_INET6, SOCK_STREAM, 0);
208 int ret = getaddrinfo(NULL, NULL, NULL, &ai);
210 const char *es = gai_strerror(ret);
216 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
217 #define IPV6_V6ONLY 26
219 ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
220 (const void *)&val, sizeof(val));
226 check_c_source_compiles("
227 #include <sys/socket.h>
230 struct sockaddr_storage s;
233 }" HAVE_SOCKADDR_STORAGE)
235 ###########################################################
236 # For detecting attributes we need to treat warnings as
238 set(CMAKE_REQUIRED_FLAGS "-Werror")
240 check_c_source_compiles("
241 void test_constructor_attribute(void) __attribute__ ((constructor));
243 void test_constructor_attribute(void)
250 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
252 check_c_source_compiles("
253 void test_destructor_attribute(void) __attribute__ ((destructor));
255 void test_destructor_attribute(void)
262 }" HAVE_DESTRUCTOR_ATTRIBUTE)
264 check_c_source_compiles("
265 #pragma init (test_constructor)
266 void test_constructor(void);
268 void test_constructor(void)
277 check_c_source_compiles("
278 #pragma fini (test_destructor)
279 void test_destructor(void);
281 void test_destructor(void)
290 check_c_source_compiles("
291 #define FALL_THROUGH __attribute__((fallthrough))
306 }" HAVE_FALLTHROUGH_ATTRIBUTE)
308 check_c_source_compiles("
313 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
315 check_c_source_compiles("
316 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
320 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
322 check_c_source_compiles("
323 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
325 void test_address_sanitizer_attribute(void)
332 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
334 # Stop treating wanrings as errors
335 unset(CMAKE_REQUIRED_FLAGS)
336 ###########################################################
344 test_big_endian(WORDS_BIGENDIAN)
347 check_type_size(pid_t SIZEOF_PID_T)
349 set(SWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")