cmake: Remove unsed dir definitions
[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
13 set(BINARYDIR ${uid_wrapper_BINARY_DIR})
14 set(SOURCEDIR ${uid_wrapper_SOURCE_DIR})
15
16 function(COMPILER_DUMPVERSION _OUTPUT_VERSION)
17     # Remove whitespaces from the argument.
18     # This is needed for CC="ccache gcc" cmake ..
19     string(REPLACE " " "" _C_COMPILER_ARG "${CMAKE_C_COMPILER_ARG1}")
20
21     execute_process(
22         COMMAND
23             ${CMAKE_C_COMPILER} ${_C_COMPILER_ARG} -dumpversion
24         OUTPUT_VARIABLE _COMPILER_VERSION
25     )
26
27     string(REGEX REPLACE "([0-9])\\.([0-9])(\\.[0-9])?" "\\1\\2"
28            _COMPILER_VERSION "${_COMPILER_VERSION}")
29
30     set(${_OUTPUT_VERSION} ${_COMPILER_VERSION} PARENT_SCOPE)
31 endfunction()
32
33 if(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
34     compiler_dumpversion(GNUCC_VERSION)
35     if (NOT GNUCC_VERSION EQUAL 34)
36         set(CMAKE_REQUIRED_FLAGS "-fvisibility=hidden")
37         check_c_source_compiles(
38 "void __attribute__((visibility(\"default\"))) test() {}
39 int main(void){ return 0; }
40 " WITH_VISIBILITY_HIDDEN)
41         set(CMAKE_REQUIRED_FLAGS "")
42     endif (NOT GNUCC_VERSION EQUAL 34)
43 endif(CMAKE_COMPILER_IS_GNUCC AND NOT MINGW AND NOT OS2)
44
45 # HEADERS
46 check_include_file(sys/types.h HAVE_SYS_TYPES_H)
47 check_include_file(sys/syscall.h HAVE_SYS_SYSCALL_H)
48 check_include_file(syscall.h HAVE_SYSCALL_H)
49 check_include_file(grp.h HAVE_GRP_H)
50 check_include_file(unistd.h HAVE_UNISTD_H)
51
52 # FUNCTIONS
53 check_function_exists(strncpy HAVE_STRNCPY)
54 check_function_exists(vsnprintf HAVE_VSNPRINTF)
55 check_function_exists(snprintf HAVE_SNPRINTF)
56
57 check_function_exists(seteuid HAVE_SETEUID)
58 check_function_exists(setreuid HAVE_SETREUID)
59 check_function_exists(setresuid HAVE_SETRESUID)
60 check_function_exists(getresuid HAVE_GETRESUID)
61 check_function_exists(getresgid HAVE_GETRESGID)
62
63 check_function_exists(setegid HAVE_SETEGID)
64 check_function_exists(setregid HAVE_SETREGID)
65 check_function_exists(setresgid HAVE_SETRESGID)
66
67 check_function_exists(getgroups HAVE_GETGROUPS)
68 check_function_exists(setgroups HAVE_SETGROUPS)
69
70 if (HAVE_SETGROUPS)
71     check_prototype_definition(setgroups
72         "int setgroups(int size, const gid_t *list)"
73         "-1"
74         "unistd.h"
75         HAVE_SETGROUPS_INT)
76 endif (HAVE_SETGROUPS)
77
78 check_function_exists(syscall HAVE_SYSCALL)
79
80 if (HAVE_SYSCALL)
81     set(CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
82
83     check_prototype_definition(syscall
84         "int syscall(int sysno, ...)"
85         "-1"
86         "unistd.h;sys/syscall.h"
87         HAVE_SYSCALL_INT)
88     set(CMAKE_REQUIRED_DEFINITIONS)
89 endif (HAVE_SYSCALL)
90
91 # OPTIONS
92
93 if (LINUX)
94     if (HAVE_SYS_SYSCALL_H)
95        list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYS_SYSCALL_H")
96     endif (HAVE_SYS_SYSCALL_H)
97     if (HAVE_SYSCALL_H)
98         list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYSCALL_H")
99     endif (HAVE_SYSCALL_H)
100
101 check_c_source_compiles("
102 #include <sys/types.h>
103 #ifdef HAVE_SYS_SYSCALL_H
104 #include <sys/syscall.h>
105 #endif
106 #ifdef HAVE_SYSCALL_H
107 #include <syscall.h>
108 #endif
109 #include <unistd.h>
110
111 int main(void) {
112     syscall(SYS_setresuid32, -1, -1, -1);
113     syscall(SYS_setresgid32, -1, -1, -1);
114     syscall(SYS_setreuid32, -1, -1);
115     syscall(SYS_setregid32, -1, -1);
116     syscall(SYS_setuid32, -1);
117     syscall(SYS_setgid32, -1);
118     syscall(SYS_setgroups32, 0, NULL);
119
120     return 0;
121 }" HAVE_LINUX_32BIT_SYSCALLS)
122
123     set(CMAKE_REQUIRED_DEFINITIONS)
124 endif (LINUX)
125
126 check_c_source_compiles("
127 #include <stdbool.h>
128 int main(void) {
129     bool x;
130     bool *p_x = &x;
131     __atomic_load(p_x, &x, __ATOMIC_RELAXED);
132     return 0;
133 }" HAVE_GCC_ATOMIC_BUILTINS)
134
135 check_c_source_compiles("
136 __thread int tls;
137
138 int main(void) {
139     return 0;
140 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
141
142 check_c_source_compiles("
143 void test_constructor_attribute(void) __attribute__ ((constructor));
144
145 void test_constructor_attribute(void)
146 {
147      return;
148 }
149
150 int main(void) {
151      return 0;
152 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
153
154 check_c_source_compiles("
155 void test_destructor_attribute(void) __attribute__ ((destructor));
156
157 void test_destructor_attribute(void)
158 {
159     return;
160 }
161
162 int main(void) {
163     return 0;
164 }" HAVE_DESTRUCTOR_ATTRIBUTE)
165
166 # If this produces a warning treat it as error!
167 set(CMAKE_REQUIRED_FLAGS "-Werror")
168 check_c_source_compiles("
169 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
170
171 void test_address_sanitizer_attribute(void)
172 {
173     return;
174 }
175
176 int main(void) {
177     return 0;
178 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
179 set(CMAKE_REQUIRED_FLAGS)
180
181 check_c_source_compiles("
182 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
183
184 int main(void) {
185     return 0;
186 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
187
188 check_c_source_compiles("
189 #define FALL_THROUGH __attribute__((fallthrough))
190
191 enum direction_e {
192     UP = 0,
193     DOWN,
194 };
195
196 int main(void) {
197     enum direction_e key = UP;
198     int i = 10;
199     int j = 0;
200
201     switch (key) {
202     case UP:
203         i = 5;
204         FALL_THROUGH;
205     case DOWN:
206         j = i * 2;
207         break;
208     default:
209         break;
210     }
211
212     return 0;
213 }" HAVE_FALLTHROUGH_ATTRIBUTE)
214
215 # SYSTEM LIBRARIES
216
217 find_library(DLFCN_LIBRARY dl)
218 if (DLFCN_LIBRARY)
219     list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
220 else()
221     check_function_exists(dlopen HAVE_DLOPEN)
222     if (NOT HAVE_DLOPEN)
223         message(FATAL_ERROR "FATAL: No dlopen() function detected")
224     endif()
225 endif()
226
227 if (OSX)
228     set(HAVE_APPLE 1)
229 endif (OSX)
230
231 # ENDIAN
232 if (NOT WIN32)
233     test_big_endian(WORDS_BIGENDIAN)
234 endif (NOT WIN32)
235
236 set(UIDWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "uidwrap required system libraries")