swrap: Also log the fd in swrap_socket()
[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(PACKAGE ${PROJECT_NAME})
11 set(VERSION ${PROJECT_VERSION})
12 set(DATADIR ${DATA_INSTALL_DIR})
13 set(LIBDIR ${LIB_INSTALL_DIR})
14 set(PLUGINDIR "${PLUGIN_INSTALL_DIR}-${LIBRARY_SOVERSION}")
15 set(SYSCONFDIR ${SYSCONF_INSTALL_DIR})
16
17 set(BINARYDIR ${CMAKE_BINARY_DIR})
18 set(SOURCEDIR ${CMAKE_SOURCE_DIR})
19
20 function(COMPILER_DUMPVERSION _OUTPUT_VERSION)
21     # Remove whitespaces from the argument.
22     # This is needed for CC="ccache gcc" cmake ..
23     string(REPLACE " " "" _C_COMPILER_ARG "${CMAKE_C_COMPILER_ARG1}")
24
25     execute_process(
26         COMMAND
27             ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
28         OUTPUT_VARIABLE _COMPILER_VERSION
29     )
30
31     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
32            _COMPILER_VERSION "${_COMPILER_VERSION}")
33
34     set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
35 endfunction()
36
37 if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
38     compiler_dumpversion(GNUCC_VERSION)
39     if (NOT GNUCC_VERSION EQUAL 34)
40         set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
41         check_c_source_compiles(
42 "void __attribute__((visibility(\"default\"))) test() {}
43 int main(void){ return 0; }
44 " WITH_VISIBILITY_HIDDEN)
45         unset(CMAKE_REQUIRED_FLAGS)
46     endif (NOT GNUCC_VERSION EQUAL 34)
47 endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
48
49 # HEADERS
50 check_include_file(sys/filio.h HAVE_SYS_FILIO_H)
51 check_include_file(sys/signalfd.h HAVE_SYS_SIGNALFD_H)
52 check_include_file(sys/eventfd.h HAVE_SYS_EVENTFD_H)
53 check_include_file(sys/timerfd.h HAVE_SYS_TIMERFD_H)
54 check_include_file(gnu/lib-names.h HAVE_GNU_LIB_NAMES_H)
55 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
56
57 # SYMBOLS
58 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
59 check_symbol_exists(program_invocation_short_name
60                     "errno.h"
61                     HAVE_PROGRAM_INVOCATION_SHORT_NAME)
62 unset(CMAKE_REQUIRED_FLAGS)
63
64 # FUNCTIONS
65 check_function_exists(strncpy HAVE_STRNCPY)
66 check_function_exists(vsnprintf HAVE_VSNPRINTF)
67 check_function_exists(snprintf HAVE_SNPRINTF)
68 check_function_exists(signalfd HAVE_SIGNALFD)
69 check_function_exists(eventfd HAVE_EVENTFD)
70 check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
71 check_function_exists(bindresvport HAVE_BINDRESVPORT)
72 check_function_exists(accept4 HAVE_ACCEPT4)
73 check_function_exists(open64 HAVE_OPEN64)
74 check_function_exists(fopen64 HAVE_FOPEN64)
75 check_function_exists(getprogname HAVE_GETPROGNAME)
76 check_function_exists(getexecname HAVE_GETEXECNAME)
77
78 check_function_exists(pledge HAVE_PLEDGE)
79
80
81 if (UNIX)
82     find_library(DLFCN_LIBRARY dl)
83     if (DLFCN_LIBRARY)
84         list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
85     else()
86         check_function_exists(dlopen HAVE_DLOPEN)
87         if (NOT HAVE_DLOPEN)
88             message(FATAL_ERROR "FATAL: No dlopen() function detected")
89         endif()
90     endif()
91
92     if (NOT LINUX)
93         # libsocket (Solaris)
94         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
95         if (HAVE_LIBSOCKET)
96             list(APPEND _REQUIRED_LIBRARIES socket)
97         endif (HAVE_LIBSOCKET)
98
99         # libnsl/inet_pton (Solaris)
100         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
101         if (HAVE_LIBNSL)
102             list(APPEND _REQUIRED_LIBRARIES nsl)
103         endif (HAVE_LIBNSL)
104     endif (NOT LINUX)
105
106     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
107 endif (UNIX)
108
109 # STRUCTS
110 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
111 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
112 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
113 unset(CMAKE_REQUIRED_FLAGS)
114
115 # STRUCT MEMBERS
116 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
117 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
118
119 # PROTOTYPES
120 check_prototype_definition(gettimeofday
121     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
122     "-1"
123     "sys/time.h"
124     HAVE_GETTIMEOFDAY_TZ)
125
126 check_prototype_definition(gettimeofday
127     "int gettimeofday(struct timeval *tv, void *tzp)"
128     "-1"
129     "sys/time.h"
130     HAVE_GETTIMEOFDAY_TZ_VOID)
131
132 check_prototype_definition(accept
133     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
134     "-1"
135     "sys/types.h;sys/socket.h"
136     HAVE_ACCEPT_PSOCKLEN_T)
137
138 check_prototype_definition(ioctl
139     "int ioctl(int s, int r, ...)"
140     "-1"
141     "unistd.h;sys/ioctl.h"
142     HAVE_IOCTL_INT)
143
144 if (HAVE_EVENTFD)
145     check_prototype_definition(eventfd
146         "int eventfd(unsigned int count, int flags)"
147         "-1"
148         "sys/eventfd.h"
149         HAVE_EVENTFD_UNSIGNED_INT)
150 endif (HAVE_EVENTFD)
151
152 # IPV6
153 check_c_source_compiles("
154     #include <stdlib.h>
155     #include <sys/socket.h>
156     #include <netdb.h>
157     #include <netinet/in.h>
158     #include <net/if.h>
159
160 int main(void) {
161     struct sockaddr_storage sa_store;
162     struct addrinfo *ai = NULL;
163     struct in6_addr in6addr;
164     int idx = if_nametoindex(\"iface1\");
165     int s = socket(AF_INET6, SOCK_STREAM, 0);
166     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
167     if (ret != 0) {
168         const char *es = gai_strerror(ret);
169     }
170
171     freeaddrinfo(ai);
172     {
173         int val = 1;
174 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
175 #define IPV6_V6ONLY 26
176 #endif
177         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
178                          (const void *)&val, sizeof(val));
179     }
180
181     return 0;
182 }" HAVE_IPV6)
183
184 check_c_source_compiles("
185 #include <sys/socket.h>
186
187 int main(void) {
188     struct sockaddr_storage s;
189
190     return 0;
191 }" HAVE_SOCKADDR_STORAGE)
192
193 ###########################################################
194 # For detecting attributes we need to treat warnings as
195 # errors
196 set(CMAKE_REQUIRED_FLAGS "-Werror")
197
198 check_c_source_compiles("
199 void test_constructor_attribute(void) __attribute__ ((constructor));
200
201 void test_constructor_attribute(void)
202 {
203     return;
204 }
205
206 int main(void) {
207     return 0;
208 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
209
210 check_c_source_compiles("
211 void test_destructor_attribute(void) __attribute__ ((destructor));
212
213 void test_destructor_attribute(void)
214 {
215     return;
216 }
217
218 int main(void) {
219     return 0;
220 }" HAVE_DESTRUCTOR_ATTRIBUTE)
221
222 check_c_source_compiles("
223 #define FALL_THROUGH __attribute__((fallthrough))
224
225 int main(void) {
226     int i = 2;
227
228     switch (i) {
229     case 0:
230         FALL_THROUGH;
231     case 1:
232         break;
233     default:
234         break;
235     }
236
237     return 0;
238 }" HAVE_FALLTHROUGH_ATTRIBUTE)
239
240 check_c_source_compiles("
241 __thread int tls;
242
243 int main(void) {
244     return 0;
245 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
246
247 check_c_source_compiles("
248 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
249
250 int main(void) {
251     return 0;
252 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
253
254 check_c_source_compiles("
255 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
256
257 void test_address_sanitizer_attribute(void)
258 {
259     return;
260 }
261
262 int main(void) {
263     return 0;
264 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
265
266 # Stop treating wanrings as errors
267 unset(CMAKE_REQUIRED_FLAGS)
268 ###########################################################
269
270 if (OSX)
271     set(HAVE_APPLE 1)
272 endif (OSX)
273
274 # ENDIAN
275 if (NOT WIN32)
276     test_big_endian(WORDS_BIGENDIAN)
277 endif (NOT WIN32)
278
279 check_type_size(pid_t SIZEOF_PID_T)
280
281 set(SWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")