src: Fix build on OpenIndiana.
[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
52 # FUNCTIONS
53 check_function_exists(strncpy HAVE_STRNCPY)
54 check_function_exists(vsnprintf HAVE_VSNPRINTF)
55 check_function_exists(snprintf HAVE_SNPRINTF)
56
57 if (WIN32)
58     check_function_exists(_vsnprintf_s HAVE__VSNPRINTF_S)
59     check_function_exists(_vsnprintf HAVE__VSNPRINTF)
60     check_function_exists(_snprintf HAVE__SNPRINTF)
61     check_function_exists(_snprintf_s HAVE__SNPRINTF_S)
62 endif (WIN32)
63
64 if (UNIX)
65     if (NOT LINUX)
66         # libsocket (Solaris)
67         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
68         if (HAVE_LIBSOCKET)
69           set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} socket)
70         endif (HAVE_LIBSOCKET)
71
72         # libnsl/inet_pton (Solaris)
73         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
74         if (HAVE_LIBNSL)
75             set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} nsl)
76         endif (HAVE_LIBNSL)
77     endif (NOT LINUX)
78
79     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
80 endif (UNIX)
81
82 set(LIBSSH_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "libssh required system libraries")
83
84 # STRUCT MEMBERS
85 check_struct_has_member("struct sockaddr" sa_len "sys/socket.h netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
86
87 # PROTOTYPES
88 check_prototype_definition(gettimeofday
89     "int gettimeofday(struct timeval *tv, struct timezone *tz)"
90     "-1"
91     "sys/time.h"
92     HAVE_GETTIMEOFDAY_TZ)
93
94 check_prototype_definition(gettimeofday
95     "int gettimeofday(struct timeval *tv, void *tzp)"
96     "-1"
97     "sys/time.h"
98     HAVE_GETTIMEOFDAY_TZ_VOID)
99
100 check_prototype_definition(accept
101     "int accept(int s, struct sockaddr *addr, Psocklen_t addrlen)"
102     "-1"
103     "sys/types.h;sys/socket.h"
104     HAVE_ACCEPT_PSOCKLEN_T)
105
106 check_prototype_definition(ioctl
107     "int accept(int s, int r, ...)"
108     "-1"
109     "unistd.h"
110     HAVE_IOCTL_INT)
111
112 # IPV6
113 check_c_source_compiles("
114     #include <stdlib.h>
115     #include <sys/socket.h>
116     #include <netdb.h>
117     #include <netinet/in.h>
118     #include <net/if.h>
119
120 int main(void) {
121     struct sockaddr_storage sa_store;
122     struct addrinfo *ai = NULL;
123     struct in6_addr in6addr;
124     int idx = if_nametoindex(\"iface1\");
125     int s = socket(AF_INET6, SOCK_STREAM, 0);
126     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
127     if (ret != 0) {
128         const char *es = gai_strerror(ret);
129     }
130
131     freeaddrinfo(ai);
132     {
133         int val = 1;
134 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
135 #define IPV6_V6ONLY 26
136 #endif
137         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
138                          (const void *)&val, sizeof(val));
139     }
140
141     return 0;
142 }" HAVE_IPV6)
143
144 check_c_source_compiles("
145 #include <sys/socket.h>
146
147 int main(void) {
148     struct sockaddr_storage s;
149
150     return 0;
151 }" HAVE_SOCKADDR_STORAGE)
152
153 check_c_source_compiles("
154 void test_destructor_attribute(void) __attribute__ ((destructor));
155
156 void test_destructor_attribute(void)
157 {
158     return;
159 }
160
161 int main(void) {
162     return 0;
163 }" HAVE_DESTRUCTOR_ATTRIBUTE)
164
165 check_c_source_compiles("
166 __thread int tls;
167
168 int main(void) {
169     return 0;
170 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
171
172 check_library_exists(dl dlopen "" HAVE_LIBDL)
173 if (HAVE_LIBDL)
174     find_library(DLFCN_LIBRARY dl)
175     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
176 endif (HAVE_LIBDL)
177
178 if (OSX)
179     set(HAVE_APPLE 1)
180 endif (OSX)
181
182 # ENDIAN
183 if (NOT WIN32)
184     test_big_endian(WORDS_BIGENDIAN)
185 endif (NOT WIN32)
186
187 set(SWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "swrap required system libraries")