cb2ea9282f84eee2334a74b0e01f3e7c4b122b2f
[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 check_function_exists(open64 HAVE_OPEN64)
67 check_function_exists(fopen64 HAVE_FOPEN64)
68
69 check_function_exists(pledge HAVE_PLEDGE)
70
71
72 if (UNIX)
73     if (NOT LINUX)
74         # libsocket (Solaris)
75         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
76         if (HAVE_LIBSOCKET)
77           set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} socket)
78         endif (HAVE_LIBSOCKET)
79
80         # libnsl/inet_pton (Solaris)
81         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
82         if (HAVE_LIBNSL)
83             set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} nsl)
84         endif (HAVE_LIBNSL)
85     endif (NOT LINUX)
86
87     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
88 endif (UNIX)
89
90 set(SWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "socket_wrapper required system libraries")
91
92 # STRUCTS
93 check_struct_has_member("struct in_pktinfo" ipi_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN_PKTINFO)
94 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
95 check_struct_has_member("struct in6_pktinfo" ipi6_addr "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_IN6_PKTINFO)
96 set(CMAKE_REQUIRED_FLAGS)
97
98 # STRUCT MEMBERS
99 check_struct_has_member("struct sockaddr" sa_len "sys/types.h;sys/socket.h;netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
100 check_struct_has_member("struct msghdr" msg_control "sys/types.h;sys/socket.h" HAVE_STRUCT_MSGHDR_MSG_CONTROL)
101
102 # PROTOTYPES
103 check_prototype_definition(gettimeofday
104     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
105     "-1"
106     "sys/time.h"
107     HAVE_GETTIMEOFDAY_TZ)
108
109 check_prototype_definition(gettimeofday
110     "int gettimeofday(struct timeval *tv, void *tzp)"
111     "-1"
112     "sys/time.h"
113     HAVE_GETTIMEOFDAY_TZ_VOID)
114
115 check_prototype_definition(accept
116     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
117     "-1"
118     "sys/types.h;sys/socket.h"
119     HAVE_ACCEPT_PSOCKLEN_T)
120
121 check_prototype_definition(ioctl
122     "int ioctl(int s, int r, ...)"
123     "-1"
124     "unistd.h;sys/ioctl.h"
125     HAVE_IOCTL_INT)
126
127 if (HAVE_EVENTFD)
128     check_prototype_definition(eventfd
129         "int eventfd(unsigned int count, int flags)"
130         "-1"
131         "sys/eventfd.h"
132         HAVE_EVENTFD_UNSIGNED_INT)
133 endif (HAVE_EVENTFD)
134
135 # IPV6
136 check_c_source_compiles("
137     #include <stdlib.h>
138     #include <sys/socket.h>
139     #include <netdb.h>
140     #include <netinet/in.h>
141     #include <net/if.h>
142
143 int main(void) {
144     struct sockaddr_storage sa_store;
145     struct addrinfo *ai = NULL;
146     struct in6_addr in6addr;
147     int idx = if_nametoindex(\"iface1\");
148     int s = socket(AF_INET6, SOCK_STREAM, 0);
149     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
150     if (ret != 0) {
151         const char *es = gai_strerror(ret);
152     }
153
154     freeaddrinfo(ai);
155     {
156         int val = 1;
157 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
158 #define IPV6_V6ONLY 26
159 #endif
160         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
161                          (const void *)&val, sizeof(val));
162     }
163
164     return 0;
165 }" HAVE_IPV6)
166
167 check_c_source_compiles("
168 #include <sys/socket.h>
169
170 int main(void) {
171     struct sockaddr_storage s;
172
173     return 0;
174 }" HAVE_SOCKADDR_STORAGE)
175
176 check_c_source_compiles("
177 void test_constructor_attribute(void) __attribute__ ((constructor));
178
179 void test_constructor_attribute(void)
180 {
181     return;
182 }
183
184 int main(void) {
185     return 0;
186 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
187
188 check_c_source_compiles("
189 void test_destructor_attribute(void) __attribute__ ((destructor));
190
191 void test_destructor_attribute(void)
192 {
193     return;
194 }
195
196 int main(void) {
197     return 0;
198 }" HAVE_DESTRUCTOR_ATTRIBUTE)
199
200 check_c_source_compiles("
201 #define FALL_THROUGH __attribute__((fallthrough))
202
203 enum direction_e {
204     UP = 0,
205     DOWN,
206 };
207
208 int main(void) {
209     enum direction_e key = UP;
210     int i = 10;
211     int j = 0;
212
213     switch (key) {
214     case UP:
215         i = 5;
216         FALL_THROUGH;
217     case DOWN:
218         j = i * 2;
219         break;
220     default:
221         break;
222     }
223
224     return 0;
225 }" HAVE_FALLTHROUGH_ATTRIBUTE)
226
227 check_c_source_compiles("
228 __thread int tls;
229
230 int main(void) {
231     return 0;
232 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
233
234 check_c_source_compiles("
235 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
236
237 int main(void) {
238     return 0;
239 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
240
241 # If this produces a warning treat it as error!
242 set(CMAKE_REQUIRED_FLAGS "-Werror")
243 check_c_source_compiles("
244 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
245
246 void test_address_sanitizer_attribute(void)
247 {
248     return;
249 }
250
251 int main(void) {
252     return 0;
253 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
254 set(CMAKE_REQUIRED_FLAGS)
255
256 check_library_exists(dl dlopen "" HAVE_LIBDL)
257 if (HAVE_LIBDL)
258     find_library(DLFCN_LIBRARY dl)
259     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
260 endif (HAVE_LIBDL)
261
262 if (OSX)
263     set(HAVE_APPLE 1)
264 endif (OSX)
265
266 # ENDIAN
267 if (NOT WIN32)
268     test_big_endian(WORDS_BIGENDIAN)
269 endif (NOT WIN32)
270
271 check_type_size(pid_t SIZEOF_PID_T)
272
273 set(SWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "swrap required system libraries")