tests/test_res_init.c: avoid using public ipv6 addresses from google in tests
[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     set(HAVE_LIBRESOLV TRUE)
61
62     # If we have a libresolv, we need to check functions linking the library
63     list(APPEND _REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
64 else()
65     message(STATUS "libresolv not found on ${CMAKE_SYSTEM_NAME}: Only dns faking will be available")
66 endif()
67
68 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
69 check_function_exists(res_init HAVE_RES_INIT)
70 check_function_exists(__res_init HAVE___RES_INIT)
71 unset(CMAKE_REQUIRED_LIBRARIES)
72
73 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
74 check_function_exists(res_ninit HAVE_RES_NINIT)
75 check_function_exists(__res_ninit HAVE___RES_NINIT)
76 unset(CMAKE_REQUIRED_LIBRARIES)
77
78 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
79 check_function_exists(res_close HAVE_RES_CLOSE)
80 check_function_exists(__res_close HAVE___RES_CLOSE)
81 unset(CMAKE_REQUIRED_LIBRARIES)
82
83 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
84 check_function_exists(res_nclose HAVE_RES_NCLOSE)
85 check_function_exists(__res_nclose HAVE___RES_NCLOSE)
86 unset(CMAKE_REQUIRED_LIBRARIES)
87
88 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
89 check_function_exists(res_query HAVE_RES_QUERY)
90 check_function_exists(__res_query HAVE___RES_QUERY)
91 unset(CMAKE_REQUIRED_LIBRARIES)
92
93 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
94 check_function_exists(res_nquery HAVE_RES_NQUERY)
95 check_function_exists(__res_nquery HAVE___RES_NQUERY)
96 unset(CMAKE_REQUIRED_LIBRARIES)
97
98 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
99 check_function_exists(res_search HAVE_RES_SEARCH)
100 check_function_exists(__res_search HAVE___RES_SEARCH)
101 unset(CMAKE_REQUIRED_LIBRARIES)
102
103 set(CMAKE_REQUIRED_LIBRARIES ${RESOLV_LIRBRARY})
104 check_function_exists(res_nsearch HAVE_RES_NSEARCH)
105 check_function_exists(__res_nsearch HAVE___RES_NSEARCH)
106 unset(CMAKE_REQUIRED_LIBRARIES)
107
108 check_symbol_exists(ns_name_compress "sys/types.h;arpa/nameser.h" HAVE_NS_NAME_COMPRESS)
109
110 if (UNIX)
111     if (NOT LINUX)
112         # libsocket (Solaris)
113         find_library(SOCKET_LIBRARY socket)
114         if (SOCKET_LIBRARY)
115             check_library_exists(${SOCKET_LIBRARY} getaddrinfo "" HAVE_LIBSOCKET)
116             if (HAVE_LIBSOCKET)
117                 list(APPEND _REQUIRED_LIBRARIES ${SOCKET_LIBRARY})
118             endif()
119         endif()
120
121         # libnsl/inet_pton (Solaris)
122         find_library(NSL_LIBRARY nsl)
123         if (NSL_LIBRARY)
124             check_library_exists(${NSL_LIBRARY} inet_pton "" HAVE_LIBNSL)
125             if (HAVE_LIBNSL)
126                 list(APPEND _REQUIRED_LIBRARIES ${NSL_LIBRARY})
127             endif()
128         endif()
129     endif (NOT LINUX)
130
131     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
132 endif (UNIX)
133
134 find_library(DLFCN_LIBRARY dl)
135 if (DLFCN_LIBRARY)
136     list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
137 else()
138     check_function_exists(dlopen HAVE_DLOPEN)
139     if (NOT HAVE_DLOPEN)
140         message(FATAL_ERROR "FATAL: No dlopen() function detected")
141     endif()
142 endif()
143
144 # IPV6
145 check_c_source_compiles("
146     #include <stdlib.h>
147     #include <sys/socket.h>
148     #include <netdb.h>
149     #include <netinet/in.h>
150     #include <net/if.h>
151
152 int main(void) {
153     struct sockaddr_storage sa_store;
154     struct addrinfo *ai = NULL;
155     struct in6_addr in6addr;
156     int idx = if_nametoindex(\"iface1\");
157     int s = socket(AF_INET6, SOCK_STREAM, 0);
158     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
159     if (ret != 0) {
160         const char *es = gai_strerror(ret);
161     }
162
163     freeaddrinfo(ai);
164     {
165         int val = 1;
166 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
167 #define IPV6_V6ONLY 26
168 #endif
169         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
170                          (const void *)&val, sizeof(val));
171     }
172
173     return 0;
174 }" HAVE_IPV6)
175
176 check_struct_has_member("struct __res_state" _u._ext.nsaddrs resolv.h HAVE_RESOLV_IPV6_NSADDRS)
177
178 check_c_source_compiles("
179 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
180
181 int main(void) {
182     return 0;
183 }" HAVE_ATTRIBUTE_PRINTF_FORMAT)
184
185 check_c_source_compiles("
186 void test_destructor_attribute(void) __attribute__ ((destructor));
187
188 void test_destructor_attribute(void)
189 {
190     return;
191 }
192
193 int main(void) {
194     return 0;
195 }" HAVE_DESTRUCTOR_ATTRIBUTE)
196
197 # ENDIAN
198 test_big_endian(WORDS_BIGENDIAN)
199
200 set(RWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "resolv_wrapper required system libraries")