cmake: Fix cmocka >= 1.1.6 find_package() in CONFIG mode
[priv_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(PRIV_WRAPPER_PACKAGE ${PROJECT_NAME})
11 set(PRIV_WRAPPER_VERSION ${PROJECT_VERSION})
12
13 set(BINARYDIR ${CMAKE_BINARY_DIR})
14 set(SOURCEDIR ${CMAKE_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         unset(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/prctl.h HAVE_SYS_PRCTL_H)
47 check_include_file(sys/time.h HAVE_SYS_TIME_H)
48 check_include_file(sys/resource.h HAVE_SYS_RESOURCE_H)
49
50 # FUNCTIONS
51 check_function_exists(chroot HAVE_CHROOT)
52 check_function_exists(prctl HAVE_PRCTL)
53 check_function_exists(pledge HAVE_PLEDGE)
54 check_function_exists(setrlimit HAVE_SETRLIMIT)
55
56 check_prototype_definition(setrlimit
57     "int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlp)"
58     "-1"
59     "sys/resource.h"
60     HAVE_SETRLIMIT_RLIMIT_RESOURCE_T)
61
62 if (UNIX)
63     find_library(DLFCN_LIBRARY dl)
64     if (DLFCN_LIBRARY)
65         list(APPEND _REQUIRED_LIBRARIES ${DLFCN_LIBRARY})
66     else()
67         check_function_exists(dlopen HAVE_DLOPEN)
68         if (NOT HAVE_DLOPEN)
69             message(FATAL_ERROR "FATAL: No dlopen() function detected")
70         endif()
71     endif()
72 endif (UNIX)
73
74 ###########################################################
75 # For detecting attributes we need to treat warnings as
76 # errors
77 set(CMAKE_REQUIRED_FLAGS "-Werror")
78
79 check_c_source_compiles("
80 void test_constructor_attribute(void) __attribute__ ((constructor));
81
82 void test_constructor_attribute(void)
83 {
84     return;
85 }
86
87 int main(void) {
88     return 0;
89 }" HAVE_CONSTRUCTOR_ATTRIBUTE)
90
91 check_c_source_compiles("
92 void test_destructor_attribute(void) __attribute__ ((destructor));
93
94 void test_destructor_attribute(void)
95 {
96     return;
97 }
98
99 int main(void) {
100     return 0;
101 }" HAVE_DESTRUCTOR_ATTRIBUTE)
102
103 check_c_source_compiles("
104 #define FALL_THROUGH __attribute__((fallthrough))
105
106 int main(void) {
107     int i = 2;
108
109     switch (i) {
110     case 0:
111         FALL_THROUGH;
112     case 1:
113         break;
114     default:
115         break;
116     }
117
118     return 0;
119 }" HAVE_FALLTHROUGH_ATTRIBUTE)
120
121 check_c_source_compiles("
122 void log_fn(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
123
124 int main(void) {
125     return 0;
126 }" HAVE_FUNCTION_ATTRIBUTE_FORMAT)
127
128 check_c_source_compiles("
129 void test_address_sanitizer_attribute(void) __attribute__((no_sanitize_address));
130
131 void test_address_sanitizer_attribute(void)
132 {
133     return;
134 }
135
136 int main(void) {
137     return 0;
138 }" HAVE_ADDRESS_SANITIZER_ATTRIBUTE)
139
140 # Stop treating wanrings as errors
141 unset(CMAKE_REQUIRED_FLAGS)
142 ###########################################################
143 # ENDIAN
144 if (NOT WIN32)
145     test_big_endian(WORDS_BIGENDIAN)
146 endif (NOT WIN32)
147
148 set(PWRAP_REQUIRED_LIBRARIES ${_REQUIRED_LIBRARIES} CACHE INTERNAL "priv_wrapper required system libraries")