socket_wrapper: inject O_LARGEFILE to open[64|at]() if needed
[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(gnu/lib-names.h HAVE_GNU_LIB_NAMES_H)
52 check_include_file(rpc/rpc.h HAVE_RPC_RPC_H)
53
54 # SYMBOLS
55 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
56 check_symbol_exists(program_invocation_short_name
57                     "errno.h"
58                     HAVE_PROGRAM_INVOCATION_SHORT_NAME)
59 unset(CMAKE_REQUIRED_FLAGS)
60
61 # FUNCTIONS
62 check_function_exists(strncpy HAVE_STRNCPY)
63 check_function_exists(vsnprintf HAVE_VSNPRINTF)
64 check_function_exists(snprintf HAVE_SNPRINTF)
65 check_function_exists(signalfd HAVE_SIGNALFD)
66 check_function_exists(eventfd HAVE_EVENTFD)
67 check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
68 check_function_exists(bindresvport HAVE_BINDRESVPORT)
69 check_function_exists(accept4 HAVE_ACCEPT4)
70 check_function_exists(open64 HAVE_OPEN64)
71 check_function_exists(fopen64 HAVE_FOPEN64)
72 check_function_exists(getprogname HAVE_GETPROGNAME)
73 check_function_exists(getexecname HAVE_GETEXECNAME)
74 check_function_exists(pledge HAVE_PLEDGE)
75 check_function_exists(_socket HAVE__SOCKET)
76 check_function_exists(_close HAVE__CLOSE)
77 check_function_exists(__close_nocancel HAVE___CLOSE_NOCANCEL)
78
79 if (UNIX)
80     find_library(DLFCN_LIBRARY dl)
81     if (DLFCN_LIBRARY)
82         list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
83     else()
84         check_function_exists(dlopen HAVE_DLOPEN)
85         if (NOT HAVE_DLOPEN)
86             message(FATAL_ERROR "FATAL: No dlopen() function detected")
87         endif()
88     endif()
89
90     if (NOT LINUX)
91         # libsocket (Solaris)
92         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
93         if (HAVE_LIBSOCKET)
94             list(APPEND _REQUIRED_LIBRARIES socket)
95         endif (HAVE_LIBSOCKET)
96
97         # libnsl/inet_pton (Solaris)
98         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
99         if (HAVE_LIBNSL)
100             list(APPEND _REQUIRED_LIBRARIES nsl)
101         endif (HAVE_LIBNSL)
102     endif (NOT LINUX)
103
104     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
105 endif (UNIX)
106
107 # STRUCTS
108 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
109 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
110 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
111 unset(CMAKE_REQUIRED_FLAGS)
112
113 # STRUCT MEMBERS
114 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
115 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
116
117 # PROTOTYPES
118 check_prototype_definition(gettimeofday
119     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
120     "-1"
121     "sys/time.h"
122     HAVE_GETTIMEOFDAY_TZ)
123
124 check_prototype_definition(gettimeofday
125     "int gettimeofday(struct timeval *tv, void *tzp)"
126     "-1"
127     "sys/time.h"
128     HAVE_GETTIMEOFDAY_TZ_VOID)
129
130 check_prototype_definition(accept
131     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
132     "-1"
133     "sys/types.h;sys/socket.h"
134     HAVE_ACCEPT_PSOCKLEN_T)
135
136 check_prototype_definition(ioctl
137     "int ioctl(int s, int r, ...)"
138     "-1"
139     "unistd.h;sys/ioctl.h"
140     HAVE_IOCTL_INT)
141
142 if (HAVE_EVENTFD)
143     check_prototype_definition(eventfd
144         "int eventfd(unsigned int count, int flags)"
145         "-1"
146         "sys/eventfd.h"
147         HAVE_EVENTFD_UNSIGNED_INT)
148 endif (HAVE_EVENTFD)
149
150 # IPV6
151 check_c_source_compiles("
152     #include <stdlib.h>
153     #include <sys/socket.h>
154     #include <netdb.h>
155     #include <netinet/in.h>
156     #include <net/if.h>
157
158 int main(void) {
159     struct sockaddr_storage sa_store;
160     struct addrinfo *ai = NULL;
161     struct in6_addr in6addr;
162     int idx = if_nametoindex(\"iface1\");
163     int s = socket(AF_INET6, SOCK_STREAM, 0);
164     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
165     if (ret != 0) {
166         const char *es = gai_strerror(ret);
167     }
168
169     freeaddrinfo(ai);
170     {
171         int val = 1;
172 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
173 #define IPV6_V6ONLY 26
174 #endif
175         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
176                          (const void *)&val, sizeof(val));
177     }
178
179     return 0;
180 }" HAVE_IPV6)
181
182 check_c_source_compiles("
183 #include <sys/socket.h>
184
185 int main(void) {
186     struct sockaddr_storage s;
187
188     return 0;
189 }" HAVE_SOCKADDR_STORAGE)
190
191 ###########################################################
192 # For detecting attributes we need to treat warnings as
193 # errors
194 set(CMAKE_REQUIRED_FLAGS "-Werror")
195
196 check_c_source_compiles("
197 void test_constructor_attribute(void) __attribute__ ((constructor));
198
199 void test_constructor_attribute(void)
200 {
201     return;
202 }
203
204 int main(void) {
205     return 0;
206 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
207
208 check_c_source_compiles("
209 void test_destructor_attribute(void) __attribute__ ((destructor));
210
211 void test_destructor_attribute(void)
212 {
213     return;
214 }
215
216 int main(void) {
217     return 0;
218 }" HAVE_DESTRUCTOR_ATTRIBUTE)
219
220 check_c_source_compiles("
221 #pragma init (test_constructor)
222 void test_constructor(void);
223
224 void test_constructor(void)
225 {
226     return;
227 }
228
229 int main(void) {
230     return 0;
231 }" HAVE_PRAGMA_INIT)
232
233 check_c_source_compiles("
234 #pragma fini (test_destructor)
235 void test_destructor(void);
236
237 void test_destructor(void)
238 {
239     return;
240 }
241
242 int main(void) {
243     return 0;
244 }" HAVE_PRAGMA_FINI)
245
246 check_c_source_compiles("
247 #define FALL_THROUGH __attribute__((fallthrough))
248
249 int main(void) {
250     int i = 2;
251
252     switch (i) {
253     case 0:
254         FALL_THROUGH;
255     case 1:
256         break;
257     default:
258         break;
259     }
260
261     return 0;
262 }" HAVE_FALLTHROUGH_ATTRIBUTE)
263
264 check_c_source_compiles("
265 __thread int tls;
266
267 int main(void) {
268     return 0;
269 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
270
271 check_c_source_compiles("
272 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
273
274 int main(void) {
275     return 0;
276 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
277
278 check_c_source_compiles("
279 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
280
281 void test_address_sanitizer_attribute(void)
282 {
283     return;
284 }
285
286 int main(void) {
287     return 0;
288 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
289
290 # Stop treating wanrings as errors
291 unset(CMAKE_REQUIRED_FLAGS)
292 ###########################################################
293
294 if (OSX)
295     set(HAVE_APPLE 1)
296 endif (OSX)
297
298 # ENDIAN
299 if (NOT WIN32)
300     test_big_endian(WORDS_BIGENDIAN)
301 endif (NOT WIN32)
302
303 check_type_size(pid_t SIZEOF_PID_T)
304
305 set(SWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")