cmake: Check for nss.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(grp.h HAVE_GRP_H)
53 check_include_file(nss.h HAVE_NSS_H)
54
55 # FUNCTIONS
56 check_function_exists(strncpy HAVE_STRNCPY)
57 check_function_exists(vsnprintf HAVE_VSNPRINTF)
58 check_function_exists(snprintf HAVE_SNPRINTF)
59
60 check_function_exists(getpwnam_r HAVE_GETPWNAM_R)
61 check_function_exists(getpwuid_r HAVE_GETPWUID_R)
62 check_function_exists(getpwent_r HAVE_GETPWENT_R)
63
64 check_function_exists(getgrnam_r HAVE_GETGRNAM_R)
65 check_function_exists(getgrgid_r HAVE_GETGRGID_R)
66 check_function_exists(getgrent_r HAVE_GETGRENT_R)
67
68 check_function_exists(getgrouplist HAVE_GETGROUPLIST)
69
70 if (WIN32)
71     check_function_exists(_vsnprintf_s HAVE__VSNPRINTF_S)
72     check_function_exists(_vsnprintf HAVE__VSNPRINTF)
73     check_function_exists(_snprintf HAVE__SNPRINTF)
74     check_function_exists(_snprintf_s HAVE__SNPRINTF_S)
75 endif (WIN32)
76
77 check_function_exists(asprintf HAVE_ASPRINTF)
78 if (UNIX AND HAVE_ASPRINTF)
79     add_definitions(-D_GNU_SOURCE)
80 endif (UNIX AND HAVE_ASPRINTF)
81
82
83 check_prototype_definition(getpwent_r
84  "struct passwd *getpwent_r(struct passwd *src, char *buf, int buflen)"
85  "NULL"
86  "unistd.h;pwd.h"
87  SOLARIS_GETPWENT_R)
88
89 check_prototype_definition(getpwent_r
90  "struct group *getpwent_r(struct group *src, char *buf, int buflen)"
91  "NULL"
92  "unistd.h;pwd.h"
93  SOLARIS_GETGRENT_R)
94
95 # STRUCT MEMBERS
96 check_struct_has_member("struct sockaddr" sa_len "sys/socket.h netinet/in.h" HAVE_STRUCT_SOCKADDR_SA_LEN)
97
98 # IPV6
99 check_c_source_compiles("
100     #include <stdlib.h>
101     #include <sys/socket.h>
102     #include <netdb.h>
103     #include <netinet/in.h>
104     #include <net/if.h>
105
106 int main(void) {
107     struct sockaddr_storage sa_store;
108     struct addrinfo *ai = NULL;
109     struct in6_addr in6addr;
110     int idx = if_nametoindex(\"iface1\");
111     int s = socket(AF_INET6, SOCK_STREAM, 0);
112     int ret = getaddrinfo(NULL, NULL, NULL, &ai);
113     if (ret != 0) {
114         const char *es = gai_strerror(ret);
115     }
116
117     freeaddrinfo(ai);
118     {
119         int val = 1;
120 #ifdef HAVE_LINUX_IPV6_V6ONLY_26
121 #define IPV6_V6ONLY 26
122 #endif
123         ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY,
124                          (const void *)&val, sizeof(val));
125     }
126
127     return 0;
128 }" HAVE_IPV6)
129
130 check_library_exists(dl dlopen "" HAVE_LIBDL)
131 if (HAVE_LIBDL)
132     find_library(DLFCN_LIBRARY dl)
133     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
134 endif (HAVE_LIBDL)
135
136 # ENDIAN
137 if (NOT WIN32)
138     test_big_endian(WORDS_BIGENDIAN)
139 endif (NOT WIN32)
140
141 set(NSSWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "nsswrap required system libraries")