2 # Check: a unit test framework for C
4 # Copyright (C) 2011 Mateusz Loskot
5 # Copyright (C) 2001, 2002 Arien Malec
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.
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.
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.
22 cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
24 DESCRIPTION "Unit Testing Framework for C"
27 ###############################################################################
28 # Configure a project for testing with CTest/CDash
31 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
33 macro(extract_version file setting_name)
34 file(STRINGS ${file} VERSION_NUMBER REGEX "^${setting_name}")
35 string(REPLACE "=" ";" VERSION_NUMBER_LIST ${VERSION_NUMBER})
36 list(GET VERSION_NUMBER_LIST 1 ${setting_name})
37 endmacro(extract_version)
39 extract_version(configure.ac CHECK_MAJOR_VERSION)
40 extract_version(configure.ac CHECK_MINOR_VERSION)
41 extract_version(configure.ac CHECK_MICRO_VERSION)
43 set(PROJECT_VERSION_MAJOR ${CHECK_MAJOR_VERSION})
44 set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION})
45 set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION})
46 set(PROJECT_VERSION_TWEAK 0)
47 set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}.${PROJECT_VERSION_TWEAK}")
50 "${CHECK_MAJOR_VERSION}.${CHECK_MINOR_VERSION}.${CHECK_MICRO_VERSION}")
52 set(MEMORY_LEAKING_TESTS_ENABLED 1)
54 ###############################################################################
56 set(CMAKE_BUILD_TYPE Debug)
58 ###############################################################################
59 # Adhere strictly to old ANSI C89 / ISO C90 standard
60 set(CMAKE_C_STANDARD 90)
61 set(CMAKE_C_STANDARD_REQUIRED ON)
62 set(CMAKE_C_EXTENSIONS ON) # Use GNU extensions and POSIX standard
64 ###############################################################################
66 option(CHECK_ENABLE_TESTS
67 "Deprecated: Enable the compilation and running of Check's unit tests" ON)
68 if(NOT CHECK_ENABLE_TESTS)
69 message(DEPRECATION "The option CHECK_ENABLE_TESTS is deprecated. Use option BUILD_TESTING.")
70 # TODO Remove this option by Check 0.15.0!
71 endif(NOT CHECK_ENABLE_TESTS)
72 option(CHECK_ENABLE_GCOV
73 "Turn on test coverage" OFF)
74 if (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
75 message(FATAL_ERROR "Code Coverage (gcov) only works if GNU compiler is used!")
76 endif (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
78 ###############################################################################
79 # Check system and architecture
86 set(_WIN32_WINNT ${WINVER})
90 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
91 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
92 add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
95 ###############################################################################
96 include(CheckCSourceCompiles)
97 include(CheckCSourceRuns)
98 include(CheckFunctionExists)
99 include(CheckIncludeFile)
100 include(CheckIncludeFiles)
101 include(CheckLibraryExists)
102 include(CheckStructMember)
103 include(CheckSymbolExists)
104 include(CheckTypeExists)
105 include(CheckTypeSize)
107 ###############################################################################
110 macro(ck_check_include_file header var)
111 check_include_files("${INCLUDES};${header}" ${var})
113 set(INCLUDES ${INCLUDES} ${header})
115 endmacro(ck_check_include_file)
117 # Some FreeBSD headers assume sys/types.h was already included.
118 ck_check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
120 # Alphabetize the rest unless there's a compelling reason
121 ck_check_include_file("errno.h" HAVE_ERRNO_H)
122 ck_check_include_file("inttypes.h" HAVE_INTTYPES_H)
123 ck_check_include_file("limits.h" HAVE_LIMITS_H)
124 ck_check_include_file("regex.h" HAVE_REGEX_H)
125 ck_check_include_file("signal.h" HAVE_SIGNAL_H)
126 ck_check_include_file("stdarg.h" HAVE_STDARG_H)
127 ck_check_include_file("stdint.h" HAVE_STDINT_H)
128 ck_check_include_file("stdlib.h" HAVE_STDLIB_H)
129 ck_check_include_file("string.h" HAVE_STRING_H)
130 ck_check_include_file("strings.h" HAVE_STRINGS_H)
131 ck_check_include_file("sys/time.h" HAVE_SYS_TIME_H)
132 ck_check_include_file("time.h" HAVE_TIME_H)
133 ck_check_include_file("unistd.h" HAVE_UNISTD_H)
135 ###############################################################################
137 check_function_exists(fork HAVE_FORK)
138 check_function_exists(getline HAVE_GETLINE)
139 check_function_exists(getpid HAVE_GETPID)
140 check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
141 check_function_exists(localtime_r HAVE_DECL_LOCALTIME_R)
142 check_function_exists(malloc HAVE_MALLOC)
143 check_function_exists(mkstemp HAVE_MKSTEMP)
144 check_function_exists(realloc HAVE_REALLOC)
145 check_function_exists(setenv HAVE_DECL_SETENV)
146 check_function_exists(sigaction HAVE_SIGACTION)
147 check_function_exists(strdup HAVE_DECL_STRDUP)
148 check_function_exists(strsignal HAVE_DECL_STRSIGNAL)
149 check_function_exists(_getpid HAVE__GETPID)
150 check_function_exists(_strdup HAVE__STRDUP)
151 check_function_exists(alarm HAVE_DECL_ALARM)
153 check_function_exists(regcomp HAVE_REGCOMP)
154 check_function_exists(regexec HAVE_REGEXEC)
157 # printf related checks
158 check_function_exists(snprintf HAVE_SNPRINTF_FUNCTION)
159 check_function_exists(vsnprintf HAVE_VSNPRINTF_FUNCTION)
160 check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF_SYMBOL)
161 check_symbol_exists(vsnprintf stdio.h HAVE_VSNPRINTF_SYMBOL)
163 if(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
164 add_definitions(-Dsnprintf=rpl_snprintf)
165 set(snprintf rpl_snprintf)
166 add_definitions(-Dvsnprintf=rpl_vsnprintf)
167 set(vsnprintf rpl_vsnprintf)
168 else(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
170 add_definitions(-DHAVE_SNPRINTF=1)
171 set(HAVE_VSNPRINTF 1)
172 add_definitions(-DHAVE_VSNPRINTF=1)
173 endif(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
176 add_definitions(-DHAVE_FORK=1)
179 add_definitions(-DHAVE_FORK=0)
184 add_definitions(-DHAVE_MKSTEMP=1)
187 add_definitions(-DHAVE_MKSTEMP=0)
192 add_definitions(-DHAVE_DECL_ALARM=1)
193 set(HAVE_DECL_ALARM 1)
194 else(HAVE_DECL_ALARM)
195 add_definitions(-DHAVE_DECL_ALARM=0)
196 set(HAVE_DECL_ALARM 0)
197 endif(HAVE_DECL_ALARM)
199 if(HAVE_REGEX_H AND HAVE_REGCOMP AND HAVE_REGEXEC)
200 add_definitions(-DHAVE_REGEX=1)
202 add_definitions(-DENABLE_REGEX=1)
207 ###############################################################################
209 set(headers "limits.h")
212 list(APPEND headers "stdint.h")
216 list(APPEND headers "inttypes.h")
217 endif(HAVE_INTTYPES_H)
219 check_symbol_exists(INT64_MAX "${headers}" HAVE_INT64_MAX)
220 check_symbol_exists(INT64_MIN "${headers}" HAVE_INT64_MIN)
221 check_symbol_exists(UINT32_MAX "${headers}" HAVE_UINT32_MAX)
222 check_symbol_exists(UINT64_MAX "${headers}" HAVE_UINT64_MAX)
223 check_symbol_exists(SIZE_MAX "${headers}" HAVE_SIZE_MAX)
224 check_symbol_exists(SSIZE_MAX "limits.h" HAVE_SSIZE_MAX)
226 ###############################################################################
227 # Check struct members
229 # Check for tv_sec in struct timeval
230 if(NOT HAVE_SYS_TIME_H)
232 check_struct_member("struct timeval" tv_sec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_SEC)
233 check_struct_member("struct timeval" tv_usec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_USEC)
234 check_struct_member("struct timespec" tv_sec "Winsock2.h" HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC)
235 check_struct_member("struct timespec" tv_sec "time.h" HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
236 check_struct_member("struct itimerspec" it_value "Winsock2.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
238 if(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
239 add_definitions(-DSTRUCT_TIMESPEC_DEFINITION_MISSING=1)
240 set(STRUCT_TIMESPEC_DEFINITION_MISSING 1)
241 endif(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
243 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
244 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
245 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
246 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
248 endif(NOT HAVE_SYS_TIME_H)
250 # OSX has sys/time.h, but it still lacks itimerspec
252 check_struct_member("struct itimerspec" it_value "sys/time.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
253 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
254 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
255 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
256 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
257 endif(HAVE_SYS_TIME_H)
259 ###############################################################################
260 # Check for integer types
261 check_type_size("short" SIZE_OF_SHORT)
262 check_type_size("int" SIZE_OF_INT)
263 check_type_size("long" SIZE_OF_LONG)
264 check_type_size("long long" SIZE_OF_LONG_LONG)
266 check_type_size("unsigned short" SIZE_OF_UNSIGNED_SHORT)
267 check_type_size("unsigned" SIZE_OF_UNSIGNED)
268 check_type_size("unsigned long" SIZE_OF_UNSIGNED_LONG)
269 check_type_size("unsigned long long" SIZE_OF_UNSIGNED_LONG_LONG)
271 check_type_size("__int64" __INT64)
272 check_type_size("unsigned __int64" UNSIGNED___INT64)
274 check_type_size(int16_t INT16_T)
275 check_type_size(int32_t INT32_T)
276 check_type_size(int64_t INT64_T)
277 check_type_size(intmax_t INTMAX_T)
278 check_type_size(uint8_t UINT8_T)
279 check_type_size(uint16_t UINT16_T)
280 check_type_size(uint32_t UINT32_T)
281 check_type_size(uint64_t UINT64_T)
282 check_type_size(uintmax_t UINTMAX_T)
285 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
286 check_type_size(clock_t CLOCK_T)
289 endif(NOT HAVE_CLOCK_T)
290 unset(CMAKE_EXTRA_INCLUDE_FILES)
292 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
293 check_type_size(clockid_t CLOCKID_T)
294 if(NOT HAVE_CLOCKID_T)
296 endif(NOT HAVE_CLOCKID_T)
297 unset(CMAKE_EXTRA_INCLUDE_FILES)
299 check_type_size(size_t SIZE_T)
301 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
302 set(size_t "uint64_t")
303 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
304 set(size_t "uint32_t")
305 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
306 endif(NOT HAVE_SIZE_T)
308 check_type_size(ssize_t SSIZE_T)
310 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
311 set(ssize_t "int64_t")
312 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
314 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
315 endif(NOT HAVE_SSIZE_T)
317 check_type_size(pid_t PID_T)
322 MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?")
324 endif(NOT HAVE_PID_T)
326 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
327 check_type_size(timer_t TIMER_T)
330 endif(NOT HAVE_TIMER_T)
331 unset(CMAKE_EXTRA_INCLUDE_FILES)
333 ###############################################################################
336 check_library_exists(m floor "" HAVE_LIBM)
341 check_library_exists(rt clock_gettime "" HAVE_LIBRT)
344 ADD_DEFINITIONS(-DHAVE_LIBRT=1)
347 check_library_exists(subunit subunit_test_start "" HAVE_SUBUNIT)
349 set(SUBUNIT "subunit")
350 set(ENABLE_SUBUNIT 1)
351 add_definitions(-DENABLE_SUBUNIT=1)
353 set(ENABLE_SUBUNIT 0)
354 add_definitions(-DENABLE_SUBUNIT=0)
357 ###############################################################################
358 # Generate "config.h" from "cmake/config.h.in"
359 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in
360 ${CMAKE_CURRENT_BINARY_DIR}/config.h)
361 include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
362 add_definitions(-DHAVE_CONFIG_H)
363 set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
364 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in
365 ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h)
366 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h DESTINATION include)
368 ###############################################################################
369 # Generate "check.pc", the package config (pkgconfig) file for libtool
370 set(prefix_save "${PREFIX}")
371 set(prefix "${CMAKE_INSTALL_PREFIX}")
372 set(exec_prefix "\${prefix}")
373 set(libdir "\${exec_prefix}/lib")
374 set(includedir "\${prefix}/include")
377 set(LIBSUBUNIT_PC "libsubunit")
379 set(LIBSUBUNIT_PC "")
382 if (CHECK_ENABLE_GCOV)
383 set(GCOV_LIBS "-lgcov")
384 else (CHECK_ENABLE_GCOV)
386 endif (CHECK_ENABLE_GCOV)
392 set(LIBS "${LIBS} -lm")
396 set(LIBS "${LIBS} -lrt")
399 set(PTHREAD_CFLAGS "-pthread")
401 configure_file(check.pc.in check.pc @ONLY)
403 unset(PTHREAD_CFLAGS)
411 set(PREFIX "${prefix_save}")
414 FILES ${CMAKE_CURRENT_BINARY_DIR}/check.pc
415 DESTINATION lib/pkgconfig
418 ###############################################################################
420 add_subdirectory(lib)
421 add_subdirectory(src)
422 add_subdirectory(checkmk)
424 ###############################################################################
427 add_subdirectory(tests)
428 add_test(NAME check_check COMMAND check_check)
429 add_test(NAME check_check_export COMMAND check_check_export)
431 # Only offer to run shell scripts if we may have a working interpreter
432 if(UNIX OR MINGW OR MSYS)
433 add_test(NAME test_output.sh
434 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
435 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh)
436 add_test(NAME test_log_output.sh
437 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
438 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh)
439 add_test(NAME test_xml_output.sh
440 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
441 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh)
442 add_test(NAME test_tap_output.sh
443 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
444 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh)
445 add_test(NAME test_check_nofork.sh
446 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
447 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh)
448 add_test(NAME test_check_nofork_teardown.sh
449 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
450 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh)
451 add_test(NAME test_set_max_msg_size.sh
452 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
453 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh)
454 endif(UNIX OR MINGW OR MSYS)
455 endif (BUILD_TESTING)
457 ###############################################################################
458 # Export project, prepare a config and config-version files
459 set(LIB_INSTALL_DIR lib CACHE FILEPATH "lib INSTALL DIR")
460 set(EXPORT_NAME ${PROJECT_NAME})
461 include(CMakePackageConfigHelpers)
462 configure_package_config_file(
463 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EXPORT_NAME}-config.cmake.in
464 ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake
465 INSTALL_DESTINATION ${LIB_INSTALL_DIR}/${EXPORT_NAME}/cmake
467 write_basic_package_version_file(
468 ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake
469 VERSION ${check_VERSION}
470 COMPATIBILITY AnyNewerVersion
473 export(EXPORT check-targets
474 FILE "${CMAKE_CURRENT_BINARY_DIR}/check-targets.cmake"
478 install(EXPORT check-targets
480 FILE check-targets.cmake
481 DESTINATION lib/cmake/${EXPORT_NAME}
485 "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake"
486 "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake"
487 DESTINATION lib/cmake/${EXPORT_NAME}