]> granicus.if.org Git - check/blob - CMakeLists.txt
Add HAVE_UNISTD_H to CMake and config.h.in
[check] / CMakeLists.txt
1 #
2 # Check: a unit test framework for C
3 #
4 # Copyright (C) 2011 Mateusz Loskot
5 # Copyright (C) 2001, 2002 Arien Malec
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
21 #
22 cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
23 project(check
24     DESCRIPTION "Unit Testing Framework for C"
25     LANGUAGES C)
26
27 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
28
29 macro(extract_version file setting_name)
30   file(STRINGS ${file} VERSION_NUMBER REGEX "^${setting_name}")
31   string(REPLACE "=" ";" VERSION_NUMBER_LIST ${VERSION_NUMBER})
32   list(GET VERSION_NUMBER_LIST 1 ${setting_name})
33 endmacro(extract_version)
34
35 extract_version(configure.ac CHECK_MAJOR_VERSION)
36 extract_version(configure.ac CHECK_MINOR_VERSION)
37 extract_version(configure.ac CHECK_MICRO_VERSION)
38
39 set(PROJECT_VERSION_MAJOR ${CHECK_MAJOR_VERSION})
40 set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION})
41 set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION})
42 set(PROJECT_VERSION_TWEAK 0)
43 set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_TWEAK}")
44
45 set(check_VERSION
46   "${CHECK_MAJOR_VERSION}.${CHECK_MINOR_VERSION}.${CHECK_MICRO_VERSION}")
47
48 set(MEMORY_LEAKING_TESTS_ENABLED 1)
49
50 ###############################################################################
51 # Set build features
52 set(CMAKE_BUILD_TYPE Debug)
53
54 ###############################################################################
55 # Adhere strictly to old ANSI C89 / ISO C90 standard
56 set(CMAKE_C_STANDARD 90)
57 set(CMAKE_C_STANDARD_REQUIRED ON)
58 set(CMAKE_C_EXTENSIONS ON)          # Use GNU extensions and POSIX standard
59
60 ###############################################################################
61 # Option
62 option(CHECK_ENABLE_TESTS
63   "Enable the compilation and running of Check's unit tests" ON)
64
65 ###############################################################################
66 # Check system and architecture
67 if(WIN32)
68   if(MSVC60)
69     set(WINVER 0x0400)
70   else()
71     set(WINVER 0x0500)
72   endif()
73   set(_WIN32_WINNT ${WINVER})
74 endif(WIN32)
75
76 if(MSVC)
77   add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
78   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
79   add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
80 endif(MSVC)
81
82 ###############################################################################
83 include(CheckCSourceCompiles)
84 include(CheckCSourceRuns)
85 include(CheckFunctionExists)
86 include(CheckIncludeFile)
87 include(CheckIncludeFiles)
88 include(CheckLibraryExists)
89 include(CheckStructMember)
90 include(CheckSymbolExists)
91 include(CheckTypeExists)
92 include(CheckTypeSize)
93
94 ###############################################################################
95 # Check headers
96 set(INCLUDES "")
97 macro(ck_check_include_file header var)
98   check_include_files("${INCLUDES};${header}" ${var})
99   if(${var})
100     set(INCLUDES ${INCLUDES} ${header})
101   endif(${var})
102 endmacro(ck_check_include_file)
103
104 # Some FreeBSD headers assume sys/types.h was already included.
105 ck_check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
106
107 # Alphabetize the rest unless there's a compelling reason
108 ck_check_include_file("errno.h" HAVE_ERRNO_H)
109 ck_check_include_file("inttypes.h" HAVE_INTTYPES_H)
110 ck_check_include_file("limits.h" HAVE_LIMITS_H)
111 ck_check_include_file("regex.h" HAVE_REGEX_H)
112 ck_check_include_file("signal.h" HAVE_SIGNAL_H)
113 ck_check_include_file("stdarg.h" HAVE_STDARG_H)
114 ck_check_include_file("stdint.h" HAVE_STDINT_H)
115 ck_check_include_file("stdlib.h" HAVE_STDLIB_H)
116 ck_check_include_file("string.h" HAVE_STRING_H)
117 ck_check_include_file("strings.h" HAVE_STRINGS_H)
118 ck_check_include_file("sys/time.h" HAVE_SYS_TIME_H)
119 ck_check_include_file("time.h" HAVE_TIME_H)
120 ck_check_include_file("unistd.h" HAVE_UNISTD_H)
121
122 ###############################################################################
123 # Check functions
124 check_function_exists(fork HAVE_FORK)
125 check_function_exists(getline HAVE_GETLINE)
126 check_function_exists(getpid HAVE_GETPID)
127 check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
128 check_function_exists(localtime_r HAVE_DECL_LOCALTIME_R)
129 check_function_exists(malloc HAVE_MALLOC)
130 check_function_exists(mkstemp HAVE_MKSTEMP)
131 check_function_exists(realloc HAVE_REALLOC)
132 check_function_exists(setenv HAVE_DECL_SETENV)
133 check_function_exists(sigaction HAVE_SIGACTION)
134 check_function_exists(strdup HAVE_DECL_STRDUP)
135 check_function_exists(strsignal HAVE_DECL_STRSIGNAL)
136 check_function_exists(_getpid HAVE__GETPID)
137 check_function_exists(_strdup HAVE__STRDUP)
138 check_function_exists(alarm HAVE_DECL_ALARM)
139 if (HAVE_REGEX_H)
140   check_function_exists(regcomp HAVE_REGCOMP)
141   check_function_exists(regexec HAVE_REGEXEC)
142 endif()
143
144 # printf related checks
145 check_function_exists(snprintf HAVE_SNPRINTF_FUNCTION)
146 check_function_exists(vsnprintf HAVE_VSNPRINTF_FUNCTION)
147 check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF_SYMBOL)
148 check_symbol_exists(vsnprintf stdio.h HAVE_VSNPRINTF_SYMBOL)
149
150 if(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
151     add_definitions(-Dsnprintf=rpl_snprintf)
152     set(snprintf rpl_snprintf)
153     add_definitions(-Dvsnprintf=rpl_vsnprintf)
154     set(vsnprintf rpl_vsnprintf)
155 else(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
156     set(HAVE_SNPRINTF 1)
157     add_definitions(-DHAVE_SNPRINTF=1)
158     set(HAVE_VSNPRINTF 1)
159     add_definitions(-DHAVE_VSNPRINTF=1)
160 endif(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
161
162 if(HAVE_FORK)
163     add_definitions(-DHAVE_FORK=1)
164     set(HAVE_FORK 1)
165 else(HAVE_FORK)
166     add_definitions(-DHAVE_FORK=0)
167     set(HAVE_FORK 0)
168 endif(HAVE_FORK)
169
170 if(HAVE_MKSTEMP)
171     add_definitions(-DHAVE_MKSTEMP=1)
172     set(HAVE_MKSTEMP 1)
173 else(HAVE_MKSTEMP)
174     add_definitions(-DHAVE_MKSTEMP=0)
175     set(HAVE_MKSTEMP 0)
176 endif(HAVE_MKSTEMP)
177
178 if(HAVE_DECL_ALARM)
179     add_definitions(-DHAVE_DECL_ALARM=1)
180     set(HAVE_DECL_ALARM 1)
181 else(HAVE_DECL_ALARM)
182     add_definitions(-DHAVE_DECL_ALARM=0)
183     set(HAVE_DECL_ALARM 0)
184 endif(HAVE_DECL_ALARM)
185
186 if(HAVE_REGEX_H AND HAVE_REGCOMP AND HAVE_REGEXEC)
187     add_definitions(-DHAVE_REGEX=1)
188     set(HAVE_REGEX 1)
189     add_definitions(-DENABLE_REGEX=1)
190     set(ENABLE_REGEX 1)
191 endif()
192
193
194 ###############################################################################
195 # Check defines
196 set(headers "limits.h")
197
198 if(HAVE_STDINT_H)
199   list(APPEND headers "stdint.h")
200 endif(HAVE_STDINT_H)
201
202 if(HAVE_INTTYPES_H)
203   list(APPEND headers "inttypes.h")
204 endif(HAVE_INTTYPES_H)
205
206 check_symbol_exists(INT64_MAX "${headers}" HAVE_INT64_MAX)
207 check_symbol_exists(INT64_MIN "${headers}" HAVE_INT64_MIN)
208 check_symbol_exists(UINT32_MAX "${headers}" HAVE_UINT32_MAX)
209 check_symbol_exists(UINT64_MAX "${headers}" HAVE_UINT64_MAX)
210 check_symbol_exists(SIZE_MAX "${headers}" HAVE_SIZE_MAX)
211 check_symbol_exists(SSIZE_MAX "limits.h"   HAVE_SSIZE_MAX)
212
213 ###############################################################################
214 # Check struct members
215
216 # Check for  tv_sec in struct timeval
217 if(NOT HAVE_SYS_TIME_H)
218     if(MSVC)
219         check_struct_member("struct timeval" tv_sec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_SEC)
220         check_struct_member("struct timeval" tv_usec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_USEC)
221         check_struct_member("struct timespec" tv_sec "Winsock2.h" HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC)
222         check_struct_member("struct timespec" tv_sec "time.h" HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
223         check_struct_member("struct itimerspec" it_value "Winsock2.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
224
225         if(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
226             add_definitions(-DSTRUCT_TIMESPEC_DEFINITION_MISSING=1)
227             set(STRUCT_TIMESPEC_DEFINITION_MISSING 1)
228         endif(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
229
230         if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
231             add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
232             set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
233         endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
234     endif(MSVC)
235 endif(NOT HAVE_SYS_TIME_H)
236
237 # OSX has sys/time.h, but it still lacks itimerspec
238 if(HAVE_SYS_TIME_H)
239     check_struct_member("struct itimerspec" it_value "sys/time.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
240     if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
241         add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
242         set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
243     endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
244 endif(HAVE_SYS_TIME_H)
245
246 ###############################################################################
247 # Check for integer types
248 check_type_size("short" SIZE_OF_SHORT)
249 check_type_size("int" SIZE_OF_INT)
250 check_type_size("long" SIZE_OF_LONG)
251 check_type_size("long long" SIZE_OF_LONG_LONG)
252
253 check_type_size("unsigned short" SIZE_OF_UNSIGNED_SHORT)
254 check_type_size("unsigned" SIZE_OF_UNSIGNED)
255 check_type_size("unsigned long" SIZE_OF_UNSIGNED_LONG)
256 check_type_size("unsigned long long" SIZE_OF_UNSIGNED_LONG_LONG)
257
258 check_type_size("__int64" __INT64)
259 check_type_size("unsigned __int64" UNSIGNED___INT64)
260
261 check_type_size(int16_t INT16_T)
262 check_type_size(int32_t INT32_T)
263 check_type_size(int64_t INT64_T)
264 check_type_size(intmax_t INTMAX_T)
265 check_type_size(uint8_t UINT8_T)
266 check_type_size(uint16_t UINT16_T)
267 check_type_size(uint32_t UINT32_T)
268 check_type_size(uint64_t UINT64_T)
269 check_type_size(uintmax_t UINTMAX_T)
270
271 #
272 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
273 check_type_size(clock_t CLOCK_T)
274 if(NOT HAVE_CLOCK_T)
275   set(clock_t int)
276 endif(NOT HAVE_CLOCK_T)
277 unset(CMAKE_EXTRA_INCLUDE_FILES)
278 #
279 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
280 check_type_size(clockid_t CLOCKID_T)
281 if(NOT HAVE_CLOCKID_T)
282   set(clockid_t int)
283 endif(NOT HAVE_CLOCKID_T)
284 unset(CMAKE_EXTRA_INCLUDE_FILES)
285 #
286 check_type_size(size_t SIZE_T)
287 if(NOT HAVE_SIZE_T)
288   if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
289     set(size_t "uint64_t")
290   else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
291     set(size_t   "uint32_t")
292   endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
293 endif(NOT HAVE_SIZE_T)
294 #
295 check_type_size(ssize_t SSIZE_T)
296 if(NOT HAVE_SSIZE_T)
297   if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
298     set(ssize_t "int64_t")
299   else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
300     set(ssize_t "long")
301   endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
302 endif(NOT HAVE_SSIZE_T)
303 #
304 check_type_size(pid_t PID_T)
305 if(NOT HAVE_PID_T)
306   if(WIN32)
307     set(pid_t "int")
308   else(WIN32)
309     MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?")
310   endif(WIN32)
311 endif(NOT HAVE_PID_T)
312 #
313 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
314 check_type_size(timer_t TIMER_T)
315 if(NOT HAVE_TIMER_T)
316   set(timer_t int)
317 endif(NOT HAVE_TIMER_T)
318 unset(CMAKE_EXTRA_INCLUDE_FILES)
319
320 ###############################################################################
321 # Check libraries
322
323 check_library_exists(m floor "" HAVE_LIBM)
324 if (HAVE_LIBM)
325     set (LIBM "m")
326 endif (HAVE_LIBM)
327
328 check_library_exists(rt clock_gettime "" HAVE_LIBRT)
329 if (HAVE_LIBRT)
330     set(LIBRT "rt")
331     ADD_DEFINITIONS(-DHAVE_LIBRT=1)
332 endif (HAVE_LIBRT)
333
334 check_library_exists(subunit subunit_test_start "" HAVE_SUBUNIT)
335 if (HAVE_SUBUNIT)
336     set(SUBUNIT "subunit")
337     set(ENABLE_SUBUNIT 1)
338     add_definitions(-DENABLE_SUBUNIT=1)
339 else(HAVE_SUBUNIT)
340     set(ENABLE_SUBUNIT 0)
341     add_definitions(-DENABLE_SUBUNIT=0)
342 endif (HAVE_SUBUNIT)
343
344 ###############################################################################
345 # Generate "config.h" from "cmake/config.h.in"
346 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in
347   ${CMAKE_CURRENT_BINARY_DIR}/config.h)
348 include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
349 add_definitions(-DHAVE_CONFIG_H)
350 set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
351 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in
352   ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h)
353 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h DESTINATION include)
354
355 ###############################################################################
356 # Subdirectories
357 add_subdirectory(lib)
358 add_subdirectory(src)
359 add_subdirectory(checkmk)
360
361 ###############################################################################
362 # Unit tests
363 if (CHECK_ENABLE_TESTS)
364   add_subdirectory(tests)
365   enable_testing()
366   add_test(NAME check_check COMMAND check_check)
367   add_test(NAME check_check_export COMMAND check_check_export)
368
369   # Only offer to run shell scripts if we may have a working interpreter
370   if(UNIX OR MINGW OR MSYS)
371     add_test(NAME test_output.sh
372       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
373       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh)
374     add_test(NAME test_log_output.sh
375       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
376       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh)
377     add_test(NAME test_xml_output.sh
378       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
379       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh)
380     add_test(NAME test_tap_output.sh
381       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
382       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh)
383     add_test(NAME test_check_nofork.sh
384       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
385       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh)
386     add_test(NAME test_check_nofork_teardown.sh
387       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
388       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh)
389     add_test(NAME test_set_max_msg_size.sh
390       WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
391       COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh)
392   endif(UNIX OR MINGW OR MSYS)
393 endif()
394
395 ###############################################################################
396 # Export project, prepare a config and config-version files
397 set(LIB_INSTALL_DIR lib CACHE FILEPATH "lib INSTALL DIR")
398 set(EXPORT_NAME ${PROJECT_NAME})
399 include(CMakePackageConfigHelpers)
400 configure_package_config_file(
401     ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EXPORT_NAME}-config.cmake.in
402     ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake
403     INSTALL_DESTINATION ${LIB_INSTALL_DIR}/${EXPORT_NAME}/cmake
404 )
405 write_basic_package_version_file(
406     ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake
407     VERSION ${check_VERSION}
408     COMPATIBILITY AnyNewerVersion
409 )
410
411 export(EXPORT check-targets
412     FILE "${CMAKE_CURRENT_BINARY_DIR}/check-targets.cmake"
413     NAMESPACE Check::
414 )
415
416 install(EXPORT check-targets
417     NAMESPACE Check::
418     FILE check-targets.cmake
419     DESTINATION lib/cmake/${EXPORT_NAME}
420 )
421 install(
422     FILES
423         "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake"
424         "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake"
425     DESTINATION lib/cmake/${EXPORT_NAME}
426 )
427