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 # target_sources() leaves relative source file paths unmodified. (OLD)
25 cmake_policy(SET CMP0076 OLD)
28 DESCRIPTION "Unit Testing Framework for C"
31 ###############################################################################
32 # Configure a project for testing with CTest/CDash
35 set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
37 macro(extract_version file setting_name)
38 file(STRINGS ${file} VERSION_NUMBER REGEX "^${setting_name}")
39 string(REPLACE "=" ";" VERSION_NUMBER_LIST ${VERSION_NUMBER})
40 list(GET VERSION_NUMBER_LIST 1 ${setting_name})
41 endmacro(extract_version)
43 extract_version(configure.ac CHECK_MAJOR_VERSION)
44 extract_version(configure.ac CHECK_MINOR_VERSION)
45 extract_version(configure.ac CHECK_MICRO_VERSION)
47 set(PROJECT_VERSION_MAJOR ${CHECK_MAJOR_VERSION})
48 set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION})
49 set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION})
50 set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
52 set(MEMORY_LEAKING_TESTS_ENABLED 1)
54 ###############################################################################
56 set(CMAKE_BUILD_TYPE Debug)
58 ###############################################################################
59 # Provides install directory variables as defined by the GNU Coding Standards.
60 include(GNUInstallDirs)
62 ###############################################################################
63 # Adhere strictly to old ANSI C89 / ISO C90 standard
64 set(CMAKE_C_STANDARD 90)
65 set(CMAKE_C_STANDARD_REQUIRED ON)
66 set(CMAKE_C_EXTENSIONS ON) # Use GNU extensions and POSIX standard
68 ###############################################################################
70 option(CHECK_ENABLE_TESTS
71 "Deprecated: Enable the compilation and running of Check's unit tests" ON)
72 if(NOT CHECK_ENABLE_TESTS)
73 message(DEPRECATION "The option CHECK_ENABLE_TESTS is deprecated. Use option BUILD_TESTING.")
74 # TODO Remove this option by Check 0.15.0!
75 endif(NOT CHECK_ENABLE_TESTS)
76 option(CHECK_ENABLE_GCOV
77 "Turn on test coverage" OFF)
78 if (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
79 message(FATAL_ERROR "Code Coverage (gcov) only works if GNU compiler is used!")
80 endif (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
82 ###############################################################################
83 # Check system and architecture
90 set(_WIN32_WINNT ${WINVER})
94 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
95 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
96 add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
99 ###############################################################################
100 include(CheckCSourceCompiles)
101 include(CheckCSourceRuns)
102 include(CheckFunctionExists)
103 include(CheckIncludeFile)
104 include(CheckIncludeFiles)
105 include(CheckLibraryExists)
106 include(CheckStructMember)
107 include(CheckSymbolExists)
108 include(CheckTypeExists)
109 include(CheckTypeSize)
111 ###############################################################################
114 macro(ck_check_include_file header var)
115 check_include_files("${INCLUDES};${header}" ${var})
117 set(INCLUDES ${INCLUDES} ${header})
119 endmacro(ck_check_include_file)
121 # Some FreeBSD headers assume sys/types.h was already included.
122 ck_check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
124 # Alphabetize the rest unless there's a compelling reason
125 ck_check_include_file("errno.h" HAVE_ERRNO_H)
126 ck_check_include_file("inttypes.h" HAVE_INTTYPES_H)
127 ck_check_include_file("limits.h" HAVE_LIMITS_H)
128 ck_check_include_file("regex.h" HAVE_REGEX_H)
129 ck_check_include_file("signal.h" HAVE_SIGNAL_H)
130 ck_check_include_file("stdarg.h" HAVE_STDARG_H)
131 ck_check_include_file("stdint.h" HAVE_STDINT_H)
132 ck_check_include_file("stdlib.h" HAVE_STDLIB_H)
133 ck_check_include_file("string.h" HAVE_STRING_H)
134 ck_check_include_file("strings.h" HAVE_STRINGS_H)
135 ck_check_include_file("sys/time.h" HAVE_SYS_TIME_H)
136 ck_check_include_file("time.h" HAVE_TIME_H)
137 ck_check_include_file("unistd.h" HAVE_UNISTD_H)
139 ###############################################################################
141 check_function_exists(fork HAVE_FORK)
142 check_function_exists(getline HAVE_GETLINE)
143 check_function_exists(getpid HAVE_GETPID)
144 check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
145 check_function_exists(localtime_r HAVE_DECL_LOCALTIME_R)
146 check_function_exists(malloc HAVE_MALLOC)
147 check_function_exists(mkstemp HAVE_MKSTEMP)
148 check_function_exists(realloc HAVE_REALLOC)
149 check_function_exists(setenv HAVE_DECL_SETENV)
150 check_function_exists(sigaction HAVE_SIGACTION)
151 check_function_exists(strdup HAVE_DECL_STRDUP)
152 check_function_exists(strsignal HAVE_DECL_STRSIGNAL)
153 check_function_exists(_getpid HAVE__GETPID)
154 check_function_exists(_strdup HAVE__STRDUP)
155 check_function_exists(alarm HAVE_DECL_ALARM)
157 check_function_exists(regcomp HAVE_REGCOMP)
158 check_function_exists(regexec HAVE_REGEXEC)
161 # printf related checks
162 check_function_exists(snprintf HAVE_SNPRINTF_FUNCTION)
163 check_function_exists(vsnprintf HAVE_VSNPRINTF_FUNCTION)
164 check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF_SYMBOL)
165 check_symbol_exists(vsnprintf stdio.h HAVE_VSNPRINTF_SYMBOL)
167 if(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
168 add_definitions(-Dsnprintf=rpl_snprintf)
169 set(snprintf rpl_snprintf)
170 add_definitions(-Dvsnprintf=rpl_vsnprintf)
171 set(vsnprintf rpl_vsnprintf)
172 else(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
174 add_definitions(-DHAVE_SNPRINTF=1)
175 set(HAVE_VSNPRINTF 1)
176 add_definitions(-DHAVE_VSNPRINTF=1)
177 endif(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
180 add_definitions(-DHAVE_FORK=1)
183 add_definitions(-DHAVE_FORK=0)
188 add_definitions(-DHAVE_MKSTEMP=1)
191 add_definitions(-DHAVE_MKSTEMP=0)
196 add_definitions(-DHAVE_DECL_ALARM=1)
197 set(HAVE_DECL_ALARM 1)
198 else(HAVE_DECL_ALARM)
199 add_definitions(-DHAVE_DECL_ALARM=0)
200 set(HAVE_DECL_ALARM 0)
201 endif(HAVE_DECL_ALARM)
203 if(HAVE_REGEX_H AND HAVE_REGCOMP AND HAVE_REGEXEC)
204 add_definitions(-DHAVE_REGEX=1)
206 add_definitions(-DENABLE_REGEX=1)
211 ###############################################################################
213 set(headers "limits.h")
216 list(APPEND headers "stdint.h")
220 list(APPEND headers "inttypes.h")
221 endif(HAVE_INTTYPES_H)
223 check_symbol_exists(INT64_MAX "${headers}" HAVE_INT64_MAX)
224 check_symbol_exists(INT64_MIN "${headers}" HAVE_INT64_MIN)
225 check_symbol_exists(UINT32_MAX "${headers}" HAVE_UINT32_MAX)
226 check_symbol_exists(UINT64_MAX "${headers}" HAVE_UINT64_MAX)
227 check_symbol_exists(SIZE_MAX "${headers}" HAVE_SIZE_MAX)
228 check_symbol_exists(SSIZE_MAX "limits.h" HAVE_SSIZE_MAX)
230 ###############################################################################
231 # Check struct members
233 # Check for tv_sec in struct timeval
234 if(NOT HAVE_SYS_TIME_H)
236 check_struct_member("struct timeval" tv_sec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_SEC)
237 check_struct_member("struct timeval" tv_usec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_USEC)
238 check_struct_member("struct timespec" tv_sec "Winsock2.h" HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC)
239 check_struct_member("struct timespec" tv_sec "time.h" HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
240 check_struct_member("struct itimerspec" it_value "Winsock2.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
242 if(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
243 add_definitions(-DSTRUCT_TIMESPEC_DEFINITION_MISSING=1)
244 set(STRUCT_TIMESPEC_DEFINITION_MISSING 1)
245 endif(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
247 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
248 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
249 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
250 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
252 endif(NOT HAVE_SYS_TIME_H)
254 # OSX has sys/time.h, but it still lacks itimerspec
256 check_struct_member("struct itimerspec" it_value "sys/time.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
257 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
258 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
259 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
260 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
261 endif(HAVE_SYS_TIME_H)
263 ###############################################################################
264 # Check for integer types
265 check_type_size("short" SIZE_OF_SHORT)
266 check_type_size("int" SIZE_OF_INT)
267 check_type_size("long" SIZE_OF_LONG)
268 check_type_size("long long" SIZE_OF_LONG_LONG)
270 check_type_size("unsigned short" SIZE_OF_UNSIGNED_SHORT)
271 check_type_size("unsigned" SIZE_OF_UNSIGNED)
272 check_type_size("unsigned long" SIZE_OF_UNSIGNED_LONG)
273 check_type_size("unsigned long long" SIZE_OF_UNSIGNED_LONG_LONG)
275 check_type_size("__int64" __INT64)
276 check_type_size("unsigned __int64" UNSIGNED___INT64)
278 check_type_size(int16_t INT16_T)
279 check_type_size(int32_t INT32_T)
280 check_type_size(int64_t INT64_T)
281 check_type_size(intmax_t INTMAX_T)
282 check_type_size(uint8_t UINT8_T)
283 check_type_size(uint16_t UINT16_T)
284 check_type_size(uint32_t UINT32_T)
285 check_type_size(uint64_t UINT64_T)
286 check_type_size(uintmax_t UINTMAX_T)
289 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
290 check_type_size(clock_t CLOCK_T)
293 endif(NOT HAVE_CLOCK_T)
294 unset(CMAKE_EXTRA_INCLUDE_FILES)
296 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
297 check_type_size(clockid_t CLOCKID_T)
298 if(NOT HAVE_CLOCKID_T)
300 endif(NOT HAVE_CLOCKID_T)
301 unset(CMAKE_EXTRA_INCLUDE_FILES)
303 check_type_size(size_t SIZE_T)
305 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
306 set(size_t "uint64_t")
307 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
308 set(size_t "uint32_t")
309 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
310 endif(NOT HAVE_SIZE_T)
312 check_type_size(ssize_t SSIZE_T)
314 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
315 set(ssize_t "int64_t")
316 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
318 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
319 endif(NOT HAVE_SSIZE_T)
321 check_type_size(pid_t PID_T)
326 MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?")
328 endif(NOT HAVE_PID_T)
330 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
331 check_type_size(timer_t TIMER_T)
334 endif(NOT HAVE_TIMER_T)
335 unset(CMAKE_EXTRA_INCLUDE_FILES)
337 ###############################################################################
340 check_library_exists(m floor "" HAVE_LIBM)
345 check_library_exists(rt clock_gettime "" HAVE_LIBRT)
348 ADD_DEFINITIONS(-DHAVE_LIBRT=1)
351 check_library_exists(subunit subunit_test_start "" HAVE_SUBUNIT)
353 set(SUBUNIT "subunit")
354 set(ENABLE_SUBUNIT 1)
355 add_definitions(-DENABLE_SUBUNIT=1)
357 set(ENABLE_SUBUNIT 0)
358 add_definitions(-DENABLE_SUBUNIT=0)
361 ###############################################################################
362 # Generate "config.h" from "cmake/config.h.in"
363 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in
364 ${CMAKE_CURRENT_BINARY_DIR}/config.h)
365 # Param @ONLY not used on purpose!
366 include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
367 add_definitions(-DHAVE_CONFIG_H)
368 set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
370 ###############################################################################
371 # Generate "check_stdint.h" from "cmake/check_stdint.h.in"
373 # The corresponding GNU Autotools build of this project
374 # has m4 macro `m4/ax_create_stdint_h.m4` to create
375 # the file `check_stdint.h` from scratch.
376 # Include file `stdint.h` was introduced in C99 ANSI standard but
377 # many compilers were lacking behind or still are and
378 # have not implemented C99 or their `stdint.h` is not compatible.
379 # Therefore the m4 macro was needed to create the required datatypes.
381 # When converting to CMake we also want to abandon the m4 macros.
383 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in
384 ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h @ONLY)
386 FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h
387 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
390 ###############################################################################
391 # Generate "check.pc", the package config (pkgconfig) file for libtool
392 set(prefix_save "${PREFIX}")
393 set(prefix "${CMAKE_INSTALL_PREFIX}")
394 set(exec_prefix "\${prefix}")
395 set(libdir "\${exec_prefix}/lib")
396 set(includedir "\${prefix}/include")
397 set(VERSION "${PROJECT_VERSION}")
400 set(LIBSUBUNIT_PC "libsubunit")
402 set(LIBSUBUNIT_PC "")
405 if (CHECK_ENABLE_GCOV)
406 set(GCOV_LIBS "-lgcov")
407 else (CHECK_ENABLE_GCOV)
409 endif (CHECK_ENABLE_GCOV)
415 set(LIBS "${LIBS} -lm")
419 set(LIBS "${LIBS} -lrt")
422 set(PTHREAD_CFLAGS "-pthread")
424 configure_file(check.pc.in check.pc @ONLY)
426 unset(PTHREAD_CFLAGS)
435 set(PREFIX "${prefix_save}")
438 FILES ${CMAKE_CURRENT_BINARY_DIR}/check.pc
439 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
442 ###############################################################################
444 add_subdirectory(lib)
445 add_subdirectory(src)
446 add_subdirectory(checkmk)
448 ###############################################################################
451 add_subdirectory(tests)
452 add_test(NAME check_check COMMAND check_check)
453 add_test(NAME check_check_export COMMAND check_check_export)
455 # Only offer to run shell scripts if we may have a working interpreter
456 if(UNIX OR MINGW OR MSYS)
457 add_test(NAME test_output.sh
458 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
459 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh)
460 add_test(NAME test_log_output.sh
461 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
462 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh)
463 add_test(NAME test_xml_output.sh
464 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
465 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh)
466 add_test(NAME test_tap_output.sh
467 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
468 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh)
469 add_test(NAME test_check_nofork.sh
470 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
471 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh)
472 add_test(NAME test_check_nofork_teardown.sh
473 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
474 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh)
475 add_test(NAME test_set_max_msg_size.sh
476 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
477 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh)
478 endif(UNIX OR MINGW OR MSYS)
479 endif (BUILD_TESTING)
481 ###############################################################################
482 # Export project, prepare a config and config-version files
483 set(LIB_INSTALL_DIR lib CACHE FILEPATH "lib INSTALL DIR")
484 set(EXPORT_NAME ${PROJECT_NAME})
485 include(CMakePackageConfigHelpers)
486 configure_package_config_file(
487 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EXPORT_NAME}-config.cmake.in
488 ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake
489 INSTALL_DESTINATION ${LIB_INSTALL_DIR}/${EXPORT_NAME}/cmake
491 write_basic_package_version_file(
492 ${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake
493 VERSION ${PROJECT_VERSION}
494 COMPATIBILITY AnyNewerVersion
497 export(EXPORT check-targets
498 FILE "${CMAKE_CURRENT_BINARY_DIR}/check-targets.cmake"
502 install(EXPORT check-targets
504 FILE check-targets.cmake
505 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME}
509 "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config.cmake"
510 "${CMAKE_CURRENT_BINARY_DIR}/${EXPORT_NAME}-config-version.cmake"
511 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME}