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