nwrap: Add a destructor to free resources.
[obnox/cwrap/nss_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/types.h HAVE_SYS_TYPES_H)
51 check_include_file(pwd.h HAVE_PWD_H)
52 check_include_file(grp.h HAVE_GRP_H)
53 check_include_file(nss.h HAVE_NSS_H)
54 check_include_file(nss_common.h HAVE_NSS_COMMON_H)
55
56 # FUNCTIONS
57 check_function_exists(strncpy HAVE_STRNCPY)
58 check_function_exists(vsnprintf HAVE_VSNPRINTF)
59 check_function_exists(snprintf HAVE_SNPRINTF)
60
61 check_function_exists(getpwnam_r HAVE_GETPWNAM_R)
62 check_function_exists(getpwuid_r HAVE_GETPWUID_R)
63 check_function_exists(getpwent_r HAVE_GETPWENT_R)
64
65 check_function_exists(getgrnam_r HAVE_GETGRNAM_R)
66 check_function_exists(getgrgid_r HAVE_GETGRGID_R)
67 check_function_exists(getgrent_r HAVE_GETGRENT_R)
68
69 check_function_exists(getgrouplist HAVE_GETGROUPLIST)
70
71 check_function_exists(gethostbyaddr_r HAVE_GETHOSTBYADDR_R)
72 check_function_exists(gethostbyname_r HAVE_GETHOSTBYNAME_R)
73
74 if (WIN32)
75     check_function_exists(_vsnprintf_s HAVE__VSNPRINTF_S)
76     check_function_exists(_vsnprintf HAVE__VSNPRINTF)
77     check_function_exists(_snprintf HAVE__SNPRINTF)
78     check_function_exists(_snprintf_s HAVE__SNPRINTF_S)
79 endif (WIN32)
80
81 if (UNIX)
82     if (NOT LINUX)
83         # libsocket (Solaris)
84         check_library_exists(socket getaddrinfo "" HAVE_LIBSOCKET)
85         if (HAVE_LIBSOCKET)
86           set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} socket)
87         endif (HAVE_LIBSOCKET)
88
89         # libnsl/inet_pton (Solaris)
90         check_library_exists(nsl inet_pton "" HAVE_LIBNSL)
91         if (HAVE_LIBNSL)
92             set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} nsl)
93         endif (HAVE_LIBNSL)
94     endif (NOT LINUX)
95
96     check_function_exists(getaddrinfo HAVE_GETADDRINFO)
97 endif (UNIX)
98
99 if (SOLARIS)
100     check_function_exists(__posix_getpwnam_r HAVE___POSIX_GETPWNAM_R)
101     check_function_exists(__posix_getpwuid_r HAVE___POSIX_GETPWUID_R)
102
103     check_function_exists(__posix_getgrgid_r HAVE___POSIX_GETGRGID_R)
104     check_function_exists(__posix_getgrnam_r HAVE___POSIX_GETGRNAM_R)
105 endif (SOLARIS)
106
107 check_function_exists(asprintf HAVE_ASPRINTF)
108 if (UNIX AND HAVE_ASPRINTF)
109     add_definitions(-D_GNU_SOURCE)
110 endif (UNIX AND HAVE_ASPRINTF)
111
112 set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_PTHREAD_SEMANTICS)
113 check_prototype_definition(getpwent_r
114     "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
115     "NULL"
116     "unistd.h;pwd.h"
117     HAVE_SOLARIS_GETPWENT_R)
118
119 check_prototype_definition(getpwnam_r
120     "int getpwnam_r(const char *name, struct passwd *pwd, char *buf, int buflen, struct passwd **ppwd)"
121     "-1"
122     "unistd.h;pwd.h"
123     HAVE_SOLARIS_GETPWNAM_R)
124
125 check_prototype_definition(getpwuid_r
126     "int getpwuid_r(uid_t uid, struct passwd *pwd, char *buf, int buflen, struct passwd **ppwd)"
127     "-1"
128     "unistd.h;pwd.h"
129     HAVE_SOLARIS_GETPWUID_R)
130
131 check_prototype_definition(getgrent_r
132     "struct group *getgrent_r(struct group *src, char *buf, int buflen)"
133     "NULL"
134     "unistd.h;grp.h"
135     HAVE_SOLARIS_GETGRENT_R)
136
137 check_prototype_definition(getgrnam_r
138     "int getgrnam_r(const char *name, struct group *grp, char *buf, int buflen, struct group **pgrp)"
139     "-1"
140     "unistd.h;grp.h"
141     HAVE_SOLARIS_GETGRNAM_R)
142
143 check_prototype_definition(getgrgid_r
144     "int getgrgid_r(gid_t gid, struct group *grp, char *buf, int buflen, struct group **pgrp)"
145     "-1"
146     "unistd.h;grp.h"
147     HAVE_SOLARIS_GETGRGID_R)
148
149 check_prototype_definition(sethostent
150     "int sethostent(int stayopen)"
151     "-1"
152     "unistd.h;netdb.h"
153     HAVE_SOLARIS_SETHOSTENT)
154
155 check_prototype_definition(endhostent
156     "int endhostent(void)"
157     "-1"
158     "unistd.h;netdb.h"
159     HAVE_SOLARIS_ENDHOSTENT)
160
161 check_prototype_definition(gethostname
162     "int gethostname(char *name, int len)"
163     "-1"
164     "unistd.h;netdb.h"
165     HAVE_SOLARIS_GETHOSTNAME)
166 set(CMAKE_REQUIRED_DEFINITIONS)
167
168 check_prototype_definition(setgrent
169     "int setgrent(void)"
170     "-1"
171     "unistd.h;grp.h"
172     HAVE_BSD_SETGRENT)
173
174 check_prototype_definition(getnameinfo
175     "int getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host, socklen_t __hostlen, char *serv, socklen_t servlen, int flags)"
176     "-1"
177     "unistd.h;netdb.h"
178     HAVE_LINUX_GETNAMEINFO)
179
180 check_prototype_definition(getnameinfo
181     "int getnameinfo (const struct sockaddr *sa, socklen_t salen, char *host, socklen_t __hostlen, char *serv, socklen_t servlen, unsigned int flags)"
182     "-1"
183     "unistd.h;netdb.h"
184     HAVE_LINUX_GETNAMEINFO_UNSIGNED)
185
186 # STRUCT MEMBERS
187 check_struct_has_member("struct sockaddr" sa_len "sys/socket.h netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
188
189 # IPV6
190 check_c_source_compiles("
191     #include <stdlib.h>
192     #include <sys/socket.h>
193     #include <netdb.h>
194     #include <netinet/in.h>
195     #include <net/if.h>
196
197 int main(void) {
198     struct sockaddr_storage sa_store;
199     struct addrinfo *ai = NULL;
200     struct in6_addr in6addr;
201     int idx = if_nametoindex(\"iface1\");
202     int s = socket(AF_INET6, SOCK_STREAM, 0);
203     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
204     if (ret != 0) {
205         const char *es = gai_strerror(ret);
206     }
207
208     freeaddrinfo(ai);
209     {
210         int val = 1;
211 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
212 #define IPV6_V6ONLY 26
213 #endif
214         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
215                          (const void *)&val, sizeof(val));
216     }
217
218     return 0;
219 }" HAVE_IPV6)
220
221 check_c_source_compiles("
222 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
223
224 int main(void) {
225     return 0;
226 }" HAVE_ATTRIBUTE_PRINTF_FORMAT)
227
228 check_c_source_compiles("
229 void test_destructor_attribute(void) __attribute__ ((destructor));
230
231 void test_destructor_attribute(void)
232 {
233     return;
234 }
235
236 int main(void) {
237     return 0;
238 }" HAVE_DESTRUCTOR_ATTRIBUTE)
239
240 check_library_exists(dl dlopen "" HAVE_LIBDL)
241 if (HAVE_LIBDL)
242     find_library(DLFCN_LIBRARY dl)
243     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
244 endif (HAVE_LIBDL)
245
246 # ENDIAN
247 if (NOT WIN32)
248     test_big_endian(WORDS_BIGENDIAN)
249 endif (NOT WIN32)
250
251 set(NWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "nss_wrapper required system libraries")