swrap: Delay addition of child socket_info_fd into socket_info list
[obnox/cwrap/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 ${APPLICATION_NAME})
11 set(VERSION ${APPLICATION_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         set(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 # FUNCTIONS
58 check_function_exists(strncpy HAVE_STRNCPY)
59 check_function_exists(vsnprintf HAVE_VSNPRINTF)
60 check_function_exists(snprintf HAVE_SNPRINTF)
61 check_function_exists(signalfd HAVE_SIGNALFD)
62 check_function_exists(eventfd HAVE_EVENTFD)
63 check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
64 check_function_exists(bindresvport HAVE_BINDRESVPORT)
65 check_function_exists(accept4 HAVE_ACCEPT4)
66
67 check_function_exists(pledge HAVE_PLEDGE)
68
69
70 if (UNIX)
71     if (NOT LINUX)
72         # libsocket (Solaris)
73         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
74         if (HAVE_LIBSOCKET)
75           set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} socket)
76         endif (HAVE_LIBSOCKET)
77
78         # libnsl/inet_pton (Solaris)
79         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
80         if (HAVE_LIBNSL)
81             set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} nsl)
82         endif (HAVE_LIBNSL)
83     endif (NOT LINUX)
84
85     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
86 endif (UNIX)
87
88 set(SWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")
89
90 # STRUCTS
91 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
92 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
93 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
94 set(CMAKE_REQUIRED_FLAGS)
95
96 # STRUCT MEMBERS
97 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
98 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
99
100 # PROTOTYPES
101 check_prototype_definition(gettimeofday
102     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
103     "-1"
104     "sys/time.h"
105     HAVE_GETTIMEOFDAY_TZ)
106
107 check_prototype_definition(gettimeofday
108     "int gettimeofday(struct timeval *tv, void *tzp)"
109     "-1"
110     "sys/time.h"
111     HAVE_GETTIMEOFDAY_TZ_VOID)
112
113 check_prototype_definition(accept
114     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
115     "-1"
116     "sys/types.h;sys/socket.h"
117     HAVE_ACCEPT_PSOCKLEN_T)
118
119 check_prototype_definition(ioctl
120     "int ioctl(int s, int r, ...)"
121     "-1"
122     "unistd.h;sys/ioctl.h"
123     HAVE_IOCTL_INT)
124
125 if (HAVE_EVENTFD)
126     check_prototype_definition(eventfd
127         "int eventfd(unsigned int count, int flags)"
128         "-1"
129         "sys/eventfd.h"
130         HAVE_EVENTFD_UNSIGNED_INT)
131 endif (HAVE_EVENTFD)
132
133 # IPV6
134 check_c_source_compiles("
135     #include <stdlib.h>
136     #include <sys/socket.h>
137     #include <netdb.h>
138     #include <netinet/in.h>
139     #include <net/if.h>
140
141 int main(void) {
142     struct sockaddr_storage sa_store;
143     struct addrinfo *ai = NULL;
144     struct in6_addr in6addr;
145     int idx = if_nametoindex(\"iface1\");
146     int s = socket(AF_INET6, SOCK_STREAM, 0);
147     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
148     if (ret != 0) {
149         const char *es = gai_strerror(ret);
150     }
151
152     freeaddrinfo(ai);
153     {
154         int val = 1;
155 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
156 #define IPV6_V6ONLY 26
157 #endif
158         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
159                          (const void *)&val, sizeof(val));
160     }
161
162     return 0;
163 }" HAVE_IPV6)
164
165 check_c_source_compiles("
166 #include <sys/socket.h>
167
168 int main(void) {
169     struct sockaddr_storage s;
170
171     return 0;
172 }" HAVE_SOCKADDR_STORAGE)
173
174 check_c_source_compiles("
175 void test_destructor_attribute(void) __attribute__ ((destructor));
176
177 void test_destructor_attribute(void)
178 {
179     return;
180 }
181
182 int main(void) {
183     return 0;
184 }" HAVE_DESTRUCTOR_ATTRIBUTE)
185
186 check_c_source_compiles("
187 __thread int tls;
188
189 int main(void) {
190     return 0;
191 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
192
193 check_c_source_compiles("
194 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
195
196 int main(void) {
197     return 0;
198 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
199
200 # If this produces a warning treat it as error!
201 set(CMAKE_REQUIRED_FLAGS "-Werror")
202 check_c_source_compiles("
203 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
204
205 void test_address_sanitizer_attribute(void)
206 {
207     return;
208 }
209
210 int main(void) {
211     return 0;
212 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
213 set(CMAKE_REQUIRED_FLAGS)
214
215 check_library_exists(dl dlopen "" HAVE_LIBDL)
216 if (HAVE_LIBDL)
217     find_library(DLFCN_LIBRARY dl)
218     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
219 endif (HAVE_LIBDL)
220
221 if (OSX)
222     set(HAVE_APPLE 1)
223 endif (OSX)
224
225 # ENDIAN
226 if (NOT WIN32)
227     test_big_endian(WORDS_BIGENDIAN)
228 endif (NOT WIN32)
229
230 check_type_size(pid_t SIZEOF_PID_T)
231
232 set(SWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "swrap required system libraries")