cmake: Fix cmocka >= 1.1.6 find_package() in CONFIG mode
[resolv_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(BINARYDIR ${resolv_wrapper_BINARY_DIR})
11 set(SOURCEDIR ${resolv_wrapper_SOURCE_DIR})
12
13 function(COMPILER_DUMPVERSION _OUTPUT_VERSION)
14     # Remove whitespaces from the argument.
15     # This is needed for CC="ccache gcc" cmake ..
16     string(REPLACE " " "" _C_COMPILER_ARG "${CMAKE_C_COMPILER_ARG1}")
17
18     execute_process(
19         COMMAND
20             ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
21         OUTPUT_VARIABLE _COMPILER_VERSION
22     )
23
24     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
25            _COMPILER_VERSION "${_COMPILER_VERSION}")
26
27     set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
28 endfunction()
29
30 if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
31     compiler_dumpversion(GNUCC_VERSION)
32     if (NOT GNUCC_VERSION EQUAL 34)
33         set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
34         check_c_source_compiles(
35 "void __attribute__((visibility(\"default\"))) test() {}
36 int main(void){ return 0; }
37 " WITH_VISIBILITY_HIDDEN)
38         unset(CMAKE_REQUIRED_FLAGS)
39     endif (NOT GNUCC_VERSION EQUAL 34)
40 endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
41
42 # HEADERS
43 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
44 check_include_file(resolv.h HAVE_RESOLV_H)
45 check_include_file(arpa/nameser.h HAVE_ARPA_NAMESER_H)
46
47 # SYMBOLS
48 set(CMAKE_REQUIRED_FLAGS -D_GNU_SOURCE)
49 check_symbol_exists(program_invocation_short_name
50                     "errno.h"
51                     HAVE_PROGRAM_INVOCATION_SHORT_NAME)
52 unset(CMAKE_REQUIRED_FLAGS)
53
54 # FUNCTIONS
55 check_function_exists(getprogname HAVE_GETPROGNAME)
56 check_function_exists(getexecname HAVE_GETEXECNAME)
57
58 find_library(RESOLV_LIRBRARY resolv)
59 if (RESOLV_LIRBRARY)
60     check_function_exists(res_nquery HAVE_LIBC_RES_INIT)
61     check_function_exists(__res_query HAVE_LIBC__RES_INIT)
62
63     if (NOT HAVE_LIBC_RES_INIT AND NOT HAVE_LIBC__RES_INIT)
64         set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
65         check_function_exists(res_query HAVE_RES_QUERY)
66         check_function_exists(__res_query HAVE___RES_QUERY)
67         unset(CMAKE_REQUIRED_LIBRARIES)
68
69         if (HAVE_RES_QUERY OR HAVE___RES_QUERY)
70             set(HAVE_LIBRESOLV 1)
71
72         endif()
73     endif()
74
75     # If we have a libresolv, we need to check functions linking the library
76     list(APPEND _REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
77 endif()
78
79 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
80 check_function_exists(res_init HAVE_RES_INIT)
81 check_function_exists(__res_init HAVE___RES_INIT)
82 unset(CMAKE_REQUIRED_LIBRARIES)
83
84 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
85 check_function_exists(res_ninit HAVE_RES_NINIT)
86 check_function_exists(__res_ninit HAVE___RES_NINIT)
87 unset(CMAKE_REQUIRED_LIBRARIES)
88
89 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
90 check_function_exists(res_close HAVE_RES_CLOSE)
91 check_function_exists(__res_close HAVE___RES_CLOSE)
92 unset(CMAKE_REQUIRED_LIBRARIES)
93
94 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
95 check_function_exists(res_nclose HAVE_RES_NCLOSE)
96 check_function_exists(__res_nclose HAVE___RES_NCLOSE)
97 unset(CMAKE_REQUIRED_LIBRARIES)
98
99 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
100 check_function_exists(res_query HAVE_RES_QUERY)
101 check_function_exists(__res_query HAVE___RES_QUERY)
102 unset(CMAKE_REQUIRED_LIBRARIES)
103
104 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
105 check_function_exists(res_nquery HAVE_RES_NQUERY)
106 check_function_exists(__res_nquery HAVE___RES_NQUERY)
107 unset(CMAKE_REQUIRED_LIBRARIES)
108
109 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
110 check_function_exists(res_search HAVE_RES_SEARCH)
111 check_function_exists(__res_search HAVE___RES_SEARCH)
112 unset(CMAKE_REQUIRED_LIBRARIES)
113
114 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
115 check_function_exists(res_nsearch HAVE_RES_NSEARCH)
116 check_function_exists(__res_nsearch HAVE___RES_NSEARCH)
117 unset(CMAKE_REQUIRED_LIBRARIES)
118
119 check_symbol_exists(ns_name_compress "sys/types.h;arpa/nameser.h" HAVE_NS_NAME_COMPRESS)
120
121 if (UNIX)
122     if (NOT LINUX)
123         # libsocket (Solaris)
124         find_library(SOCKET_LIBRARY socket)
125         if (SOCKET_LIBRARY)
126             check_library_exists(${SOCKET_LIBRARY} getaddrinfo "" HAVE_LIBSOCKET)
127             if (HAVE_LIBSOCKET)
128                 list(APPEND _REQUIRED_LIBRARIES ${SOCKET_LIBRARY})
129             endif()
130         endif()
131
132         # libnsl/inet_pton (Solaris)
133         find_library(NSL_LIBRARY nsl)
134         if (NSL_LIBRARY)
135             check_library_exists(${NSL_LIBRARY} inet_pton "" HAVE_LIBNSL)
136             if (HAVE_LIBNSL)
137                 list(APPEND _REQUIRED_LIBRARIES ${NSL_LIBRARY})
138             endif()
139         endif()
140     endif (NOT LINUX)
141
142     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
143 endif (UNIX)
144
145 find_library(DLFCN_LIBRARY dl)
146 if (DLFCN_LIBRARY)
147     list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
148 else()
149     check_function_exists(dlopen HAVE_DLOPEN)
150     if (NOT HAVE_DLOPEN)
151         message(FATAL_ERROR "FATAL: No dlopen() function detected")
152     endif()
153 endif()
154
155 # IPV6
156 check_c_source_compiles("
157     #include <stdlib.h>
158     #include <sys/socket.h>
159     #include <netdb.h>
160     #include <netinet/in.h>
161     #include <net/if.h>
162
163 int main(void) {
164     struct sockaddr_storage sa_store;
165     struct addrinfo *ai = NULL;
166     struct in6_addr in6addr;
167     int idx = if_nametoindex(\"iface1\");
168     int s = socket(AF_INET6, SOCK_STREAM, 0);
169     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
170     if (ret != 0) {
171         const char *es = gai_strerror(ret);
172     }
173
174     freeaddrinfo(ai);
175     {
176         int val = 1;
177 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
178 #define IPV6_V6ONLY 26
179 #endif
180         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
181                          (const void *)&val, sizeof(val));
182     }
183
184     return 0;
185 }" HAVE_IPV6)
186
187 check_struct_has_member("struct __res_state" _u._ext.nsaddrs
188                         "sys/socket.h;netinet/in.h;resolv.h"
189                         HAVE_RES_STATE_U_EXT_NSADDRS)
190 check_struct_has_member("union res_sockaddr_union" sin
191                         "sys/socket.h;netinet/in.h;resolv.h"
192                         HAVE_RES_SOCKADDR_UNION_SIN)
193 check_struct_has_member("union res_sockaddr_union" sin6
194                         "sys/socket.h;netinet/in.h;resolv.h"
195                         HAVE_RES_SOCKADDR_UNION_SIN6)
196
197 check_c_source_compiles("
198 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
199
200 int main(void) {
201     return 0;
202 }" HAVE_ATTRIBUTE_PRINTF_FORMAT)
203
204 check_c_source_compiles("
205 void test_destructor_attribute(void) __attribute__ ((destructor));
206
207 void test_destructor_attribute(void)
208 {
209     return;
210 }
211
212 int main(void) {
213     return 0;
214 }" HAVE_DESTRUCTOR_ATTRIBUTE)
215
216 # ENDIAN
217 test_big_endian(WORDS_BIGENDIAN)
218
219 if (NOT HAVE_LIBRESOLV)
220     message(STATUS "Limited features: Only dns faking will be available")
221 endif()
222
223 set(RWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "resolv_wrapper required system libraries")