cmake: Fix configure check for libdl
[uid_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 ${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(sys/syscall.h HAVE_SYS_SYSCALL_H)
52 check_include_file(syscall.h HAVE_SYSCALL_H)
53 check_include_file(grp.h HAVE_GRP_H)
54 check_include_file(unistd.h HAVE_UNISTD_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(seteuid HAVE_SETEUID)
62 check_function_exists(setreuid HAVE_SETREUID)
63 check_function_exists(setresuid HAVE_SETRESUID)
64 check_function_exists(getresuid HAVE_GETRESUID)
65 check_function_exists(getresgid HAVE_GETRESGID)
66
67 check_function_exists(setegid HAVE_SETEGID)
68 check_function_exists(setregid HAVE_SETREGID)
69 check_function_exists(setresgid HAVE_SETRESGID)
70
71 check_function_exists(getgroups HAVE_GETGROUPS)
72 check_function_exists(setgroups HAVE_SETGROUPS)
73
74 if (HAVE_SETGROUPS)
75     check_prototype_definition(setgroups
76         "int setgroups(int size, const gid_t *list)"
77         "-1"
78         "unistd.h"
79         HAVE_SETGROUPS_INT)
80 endif (HAVE_SETGROUPS)
81
82 check_function_exists(syscall HAVE_SYSCALL)
83
84 if (HAVE_SYSCALL)
85     add_definitions(-D_GNU_SOURCE)
86
87     check_prototype_definition(syscall
88         "int syscall(int sysno, ...)"
89         "-1"
90         "unistd.h;sys/syscall.h"
91         HAVE_SYSCALL_INT)
92 endif (HAVE_SYSCALL)
93
94 # OPTIONS
95
96 if (LINUX)
97     if (HAVE_SYS_SYSCALL_H)
98        list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYS_SYSCALL_H")
99     endif (HAVE_SYS_SYSCALL_H)
100     if (HAVE_SYSCALL_H)
101         list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYSCALL_H")
102     endif (HAVE_SYSCALL_H)
103
104 check_c_source_compiles("
105 #include <sys/types.h>
106 #ifdef HAVE_SYS_SYSCALL_H
107 #include <sys/syscall.h>
108 #endif
109 #ifdef HAVE_SYSCALL_H
110 #include <syscall.h>
111 #endif
112 #include <unistd.h>
113
114 int main(void) {
115     syscall(SYS_setresuid32, -1, -1, -1);
116     syscall(SYS_setresgid32, -1, -1, -1);
117     syscall(SYS_setreuid32, -1, -1);
118     syscall(SYS_setregid32, -1, -1);
119     syscall(SYS_setuid32, -1);
120     syscall(SYS_setgid32, -1);
121     syscall(SYS_setgroups32, 0, NULL);
122
123     return 0;
124 }" HAVE_LINUX_32BIT_SYSCALLS)
125
126     set(CMAKE_REQUIRED_DEFINITIONS)
127 endif (LINUX)
128
129 check_c_source_compiles("
130 #include <stdbool.h>
131 int main(void) {
132     bool x;
133     bool *p_x = &x;
134     __atomic_load(p_x, &x, __ATOMIC_RELAXED);
135     return 0;
136 }" HAVE_GCC_ATOMIC_BUILTINS)
137
138 check_c_source_compiles("
139 __thread int tls;
140
141 int main(void) {
142     return 0;
143 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
144
145 check_c_source_compiles("
146 void test_constructor_attribute(void) __attribute__ ((constructor));
147
148 void test_constructor_attribute(void)
149 {
150      return;
151 }
152
153 int main(void) {
154      return 0;
155 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
156
157 check_c_source_compiles("
158 void test_destructor_attribute(void) __attribute__ ((destructor));
159
160 void test_destructor_attribute(void)
161 {
162     return;
163 }
164
165 int main(void) {
166     return 0;
167 }" HAVE_DESTRUCTOR_ATTRIBUTE)
168
169 # If this produces a warning treat it as error!
170 set(CMAKE_REQUIRED_FLAGS "-Werror")
171 check_c_source_compiles("
172 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
173
174 void test_address_sanitizer_attribute(void)
175 {
176     return;
177 }
178
179 int main(void) {
180     return 0;
181 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
182 set(CMAKE_REQUIRED_FLAGS)
183
184 check_c_source_compiles("
185 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
186
187 int main(void) {
188     return 0;
189 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
190
191 check_c_source_compiles("
192 #define FALL_THROUGH __attribute__((fallthrough))
193
194 enum direction_e {
195     UP = 0,
196     DOWN,
197 };
198
199 int main(void) {
200     enum direction_e key = UP;
201     int i = 10;
202     int j = 0;
203
204     switch (key) {
205     case UP:
206         i = 5;
207         FALL_THROUGH;
208     case DOWN:
209         j = i * 2;
210         break;
211     default:
212         break;
213     }
214
215     return 0;
216 }" HAVE_FALLTHROUGH_ATTRIBUTE)
217
218 # SYSTEM LIBRARIES
219
220 find_library(DLFCN_LIBRARY dl)
221 if (DLFCN_LIBRARY)
222     list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
223 else()
224     check_function_exists(dlopen HAVE_DLOPEN)
225     if (NOT HAVE_DLOPEN)
226         message(FATAL_ERROR "FATAL: No dlopen() function detected")
227     endif()
228 endif()
229
230 if (OSX)
231     set(HAVE_APPLE 1)
232 endif (OSX)
233
234 # ENDIAN
235 if (NOT WIN32)
236     test_big_endian(WORDS_BIGENDIAN)
237 endif (NOT WIN32)
238
239 set(UIDWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "uidwrap required system libraries")