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