337314c7d68c64e59d059bed53237827598cd975
[obnox/cwrap/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 ${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(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(setreuid HAVE_SETRESUID)
64
65 check_function_exists(setegid HAVE_SETEGID)
66 check_function_exists(setregid HAVE_SETREGID)
67 check_function_exists(setregid HAVE_SETRESGID)
68
69 check_function_exists(getgroups HAVE_GETGROUPS)
70 check_function_exists(setgroups HAVE_SETGROUPS)
71
72 check_function_exists(syscall HAVE_SYSCALL)
73
74 if (HAVE_SYSCALL)
75     add_definitions(-D_GNU_SOURCE)
76 endif (HAVE_SYSCALL)
77
78 # OPTIONS
79
80 if (LINUX)
81     if (HAVE_SYS_SYSCALL_H)
82        list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYS_SYSCALL_H")
83     endif (HAVE_SYS_SYSCALL_H)
84     if (HAVE_SYSCALL_H)
85         list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DHAVE_SYSCALL_H")
86     endif (HAVE_SYSCALL_H)
87
88 check_c_source_compiles("
89 #include <sys/types.h>
90 #ifdef HAVE_SYS_SYSCALL_H
91 #include <sys/syscall.h>
92 #endif
93 #ifdef HAVE_SYSCALL_H
94 #include <syscall.h>
95 #endif
96 #include <unistd.h>
97
98 int main(void) {
99     syscall(SYS_setresuid32, -1, -1, -1);
100     syscall(SYS_setresgid32, -1, -1, -1);
101     syscall(SYS_setreuid32, -1, -1);
102     syscall(SYS_setregid32, -1, -1);
103     syscall(SYS_setuid32, -1);
104     syscall(SYS_setgid32, -1);
105     syscall(SYS_setgroups32, 0, NULL);
106
107     return 0;
108 }" HAVE_LINUX_32BIT_SYSCALLS)
109
110     set(CMAKE_REQUIRED_DEFINITIONS)
111 endif (LINUX)
112
113 check_c_source_compiles("
114 __thread int tls;
115
116 int main(void) {
117     return 0;
118 }" HAVE_GCC_THREAD_LOCAL_STORAGE)
119
120
121 # SYSTEM LIBRARIES
122
123 check_library_exists(dl dlopen "" HAVE_LIBDL)
124 if (HAVE_LIBDL)
125     find_library(DLFCN_LIBRARY dl)
126     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${DLFCN_LIBRARY})
127 endif (HAVE_LIBDL)
128
129 # ENDIAN
130 if (NOT WIN32)
131     test_big_endian(WORDS_BIGENDIAN)
132 endif (NOT WIN32)
133
134 set(UIDWRAP_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CACHE INTERNAL "uidwrap required system libraries")