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 # Detect if Check is being used in another build as a subproject
25 # probably with command FetchContent*().
26 set(THIS_IS_MAIN_PROJECT TRUE)
27 if(DEFINED PROJECT_NAME)
28 set(THIS_IS_MAIN_PROJECT FALSE)
32 # export(PACKAGE) does not populate package registry by default. (NEW)
33 cmake_policy(SET CMP0090 NEW)
36 # target_sources() leaves relative source file paths unmodified. (OLD)
37 cmake_policy(SET CMP0076 OLD)
40 DESCRIPTION "Unit Testing Framework for C"
43 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
45 ###############################################################################
48 # Set CMAKE_BUILD_TYPE to Debug if source directory is a Git repository
49 # or user does not override on the command line
52 ###############################################################################
53 # Configure a project for testing with CTest/CDash
56 macro(extract_version file setting_name)
57 file(STRINGS ${file} VERSION_NUMBER REGEX "^${setting_name}")
58 string(REPLACE "=" ";" VERSION_NUMBER_LIST ${VERSION_NUMBER})
59 list(GET VERSION_NUMBER_LIST 1 ${setting_name})
60 endmacro(extract_version)
62 extract_version(configure.ac CHECK_MAJOR_VERSION)
63 extract_version(configure.ac CHECK_MINOR_VERSION)
64 extract_version(configure.ac CHECK_MICRO_VERSION)
66 set(PROJECT_VERSION_MAJOR ${CHECK_MAJOR_VERSION})
67 set(PROJECT_VERSION_MINOR ${CHECK_MINOR_VERSION})
68 set(PROJECT_VERSION_PATCH ${CHECK_MICRO_VERSION})
69 set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
71 ###############################################################################
72 # Provides install directory variables as defined by the GNU Coding Standards.
73 include(GNUInstallDirs)
75 ###############################################################################
76 # Follow ISO C99 standard
77 set(CMAKE_C_STANDARD 99)
78 set(CMAKE_C_STANDARD_REQUIRED ON)
79 set(CMAKE_C_EXTENSIONS ON) # Use GNU extensions and POSIX standard
81 ###############################################################################
83 option(CHECK_ENABLE_TESTS
84 "Deprecated: Enable the compilation and running of Check's unit tests" ON)
85 if(NOT CHECK_ENABLE_TESTS)
86 message(DEPRECATION "The option CHECK_ENABLE_TESTS is deprecated. Use option BUILD_TESTING.")
87 # TODO Remove this option by Check 0.15.0!
88 endif(NOT CHECK_ENABLE_TESTS)
90 option(CHECK_ENABLE_GCOV
91 "Turn on test coverage" OFF)
92 if (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
93 message(FATAL_ERROR "Code Coverage (gcov) only works if GNU compiler is used!")
94 endif (CHECK_ENABLE_GCOV AND NOT ${CMAKE_C_COMPILER_ID} MATCHES "GNU")
96 option(ENABLE_MEMORY_LEAKING_TESTS
97 "Enable certain memory leaking tests only if valgrind is not used in testing" ON)
99 ###############################################################################
100 # Check system and architecture
107 set(_WIN32_WINNT ${WINVER})
111 add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
112 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
113 add_definitions(-D_CRT_NONSTDC_NO_WARNINGS)
116 ###############################################################################
117 include(CheckCSourceCompiles)
118 include(CheckCSourceRuns)
119 include(CheckFunctionExists)
120 include(CheckIncludeFile)
121 include(CheckIncludeFiles)
122 include(CheckLibraryExists)
123 include(CheckStructMember)
124 include(CheckSymbolExists)
125 include(CheckTypeExists)
126 include(CheckTypeSize)
128 ###############################################################################
131 macro(ck_check_include_file header var)
132 check_include_files("${INCLUDES};${header}" ${var})
134 set(INCLUDES ${INCLUDES} ${header})
136 endmacro(ck_check_include_file)
138 # Some FreeBSD headers assume sys/types.h was already included.
139 ck_check_include_file("sys/types.h" HAVE_SYS_TYPES_H)
141 # Alphabetize the rest unless there's a compelling reason
142 ck_check_include_file("errno.h" HAVE_ERRNO_H)
143 ck_check_include_file("inttypes.h" HAVE_INTTYPES_H)
144 ck_check_include_file("limits.h" HAVE_LIMITS_H)
145 ck_check_include_file("regex.h" HAVE_REGEX_H)
146 ck_check_include_file("signal.h" HAVE_SIGNAL_H)
147 ck_check_include_file("stdarg.h" HAVE_STDARG_H)
148 ck_check_include_file("stdint.h" HAVE_STDINT_H)
149 ck_check_include_file("stdlib.h" HAVE_STDLIB_H)
150 ck_check_include_file("string.h" HAVE_STRING_H)
151 ck_check_include_file("strings.h" HAVE_STRINGS_H)
152 ck_check_include_file("sys/time.h" HAVE_SYS_TIME_H)
153 ck_check_include_file("time.h" HAVE_TIME_H)
154 ck_check_include_file("unistd.h" HAVE_UNISTD_H)
156 ###############################################################################
158 check_function_exists(fork HAVE_FORK)
159 check_function_exists(getline HAVE_GETLINE)
160 check_function_exists(getpid HAVE_GETPID)
161 check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
162 check_function_exists(localtime_r HAVE_DECL_LOCALTIME_R)
163 check_function_exists(malloc HAVE_MALLOC)
164 check_function_exists(mkstemp HAVE_MKSTEMP)
165 check_function_exists(realloc HAVE_REALLOC)
166 check_function_exists(setenv HAVE_DECL_SETENV)
167 check_function_exists(sigaction HAVE_SIGACTION)
168 check_function_exists(strdup HAVE_DECL_STRDUP)
169 check_function_exists(strsignal HAVE_DECL_STRSIGNAL)
170 check_function_exists(_getpid HAVE__GETPID)
171 check_function_exists(_strdup HAVE__STRDUP)
172 check_function_exists(alarm HAVE_DECL_ALARM)
174 check_function_exists(regcomp HAVE_REGCOMP)
175 check_function_exists(regexec HAVE_REGEXEC)
178 # printf related checks
179 check_function_exists(snprintf HAVE_SNPRINTF_FUNCTION)
180 check_function_exists(vsnprintf HAVE_VSNPRINTF_FUNCTION)
181 check_symbol_exists(snprintf stdio.h HAVE_SNPRINTF_SYMBOL)
182 check_symbol_exists(vsnprintf stdio.h HAVE_VSNPRINTF_SYMBOL)
184 if(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
185 add_definitions(-Dsnprintf=rpl_snprintf)
186 set(snprintf rpl_snprintf)
187 add_definitions(-Dvsnprintf=rpl_vsnprintf)
188 set(vsnprintf rpl_vsnprintf)
189 else(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
191 add_definitions(-DHAVE_SNPRINTF=1)
192 set(HAVE_VSNPRINTF 1)
193 add_definitions(-DHAVE_VSNPRINTF=1)
194 endif(NOT HAVE_SNPRINTF_FUNCTION AND NOT HAVE_SNPRINTF_SYMBOL)
197 add_definitions(-DHAVE_FORK=1)
200 add_definitions(-DHAVE_FORK=0)
205 add_definitions(-DHAVE_MKSTEMP=1)
208 add_definitions(-DHAVE_MKSTEMP=0)
213 add_definitions(-DHAVE_DECL_ALARM=1)
214 set(HAVE_DECL_ALARM 1)
215 else(HAVE_DECL_ALARM)
216 add_definitions(-DHAVE_DECL_ALARM=0)
217 set(HAVE_DECL_ALARM 0)
218 endif(HAVE_DECL_ALARM)
220 if(HAVE_REGEX_H AND HAVE_REGCOMP AND HAVE_REGEXEC)
221 add_definitions(-DHAVE_REGEX=1)
223 add_definitions(-DENABLE_REGEX=1)
228 ###############################################################################
230 set(headers "limits.h")
233 list(APPEND headers "stdint.h")
237 list(APPEND headers "inttypes.h")
238 endif(HAVE_INTTYPES_H)
240 check_symbol_exists(INT64_MAX "${headers}" HAVE_INT64_MAX)
241 check_symbol_exists(INT64_MIN "${headers}" HAVE_INT64_MIN)
242 check_symbol_exists(UINT32_MAX "${headers}" HAVE_UINT32_MAX)
243 check_symbol_exists(UINT64_MAX "${headers}" HAVE_UINT64_MAX)
244 check_symbol_exists(SIZE_MAX "${headers}" HAVE_SIZE_MAX)
245 check_symbol_exists(SSIZE_MAX "limits.h" HAVE_SSIZE_MAX)
247 ###############################################################################
248 # Check struct members
250 # Check for tv_sec in struct timeval
251 if(NOT HAVE_SYS_TIME_H)
253 check_struct_member("struct timeval" tv_sec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_SEC)
254 check_struct_member("struct timeval" tv_usec "Winsock2.h" HAVE_STRUCT_TIMEVAL_TV_USEC)
255 check_struct_member("struct timespec" tv_sec "Winsock2.h" HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC)
256 check_struct_member("struct timespec" tv_sec "time.h" HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
257 check_struct_member("struct itimerspec" it_value "Winsock2.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
259 if(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
260 add_definitions(-DSTRUCT_TIMESPEC_DEFINITION_MISSING=1)
261 set(STRUCT_TIMESPEC_DEFINITION_MISSING 1)
262 endif(NOT HAVE_WINSOCK2_H_STRUCT_TIMESPEC_TV_SEC AND NOT HAVE_TIME_H_STRUCT_TIMESPEC_TV_SEC)
264 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
265 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
266 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
267 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
269 endif(NOT HAVE_SYS_TIME_H)
271 # OSX has sys/time.h, but it still lacks itimerspec
273 check_struct_member("struct itimerspec" it_value "sys/time.h" HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
274 if(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
275 add_definitions(-DSTRUCT_ITIMERSPEC_DEFINITION_MISSING=1)
276 set(STRUCT_ITIMERSPEC_DEFINITION_MISSING 1)
277 endif(NOT HAVE_STRUCT_ITIMERSPEC_IT_VALUE)
278 endif(HAVE_SYS_TIME_H)
280 ###############################################################################
281 # Check for integer types
282 check_type_size("short" SIZE_OF_SHORT)
283 check_type_size("int" SIZE_OF_INT)
284 check_type_size("long" SIZE_OF_LONG)
285 check_type_size("long long" SIZE_OF_LONG_LONG)
287 check_type_size("unsigned short" SIZE_OF_UNSIGNED_SHORT)
288 check_type_size("unsigned" SIZE_OF_UNSIGNED)
289 check_type_size("unsigned long" SIZE_OF_UNSIGNED_LONG)
290 check_type_size("unsigned long long" SIZE_OF_UNSIGNED_LONG_LONG)
292 check_type_size("__int64" __INT64)
293 check_type_size("unsigned __int64" UNSIGNED___INT64)
295 check_type_size(int16_t INT16_T)
296 check_type_size(int32_t INT32_T)
297 check_type_size(int64_t INT64_T)
298 check_type_size(intmax_t INTMAX_T)
299 check_type_size(uint8_t UINT8_T)
300 check_type_size(uint16_t UINT16_T)
301 check_type_size(uint32_t UINT32_T)
302 check_type_size(uint64_t UINT64_T)
303 check_type_size(uintmax_t UINTMAX_T)
306 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
307 check_type_size(clock_t CLOCK_T)
310 endif(NOT HAVE_CLOCK_T)
311 unset(CMAKE_EXTRA_INCLUDE_FILES)
313 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
314 check_type_size(clockid_t CLOCKID_T)
315 if(NOT HAVE_CLOCKID_T)
317 endif(NOT HAVE_CLOCKID_T)
318 unset(CMAKE_EXTRA_INCLUDE_FILES)
320 check_type_size(size_t SIZE_T)
322 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
323 set(size_t "uint64_t")
324 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
325 set(size_t "uint32_t")
326 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
327 endif(NOT HAVE_SIZE_T)
329 check_type_size(ssize_t SSIZE_T)
331 if("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
332 set(ssize_t "int64_t")
333 else("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
335 endif("${CMAKE_SIZEOF_VOID_P}" EQUAL 8)
336 endif(NOT HAVE_SSIZE_T)
338 check_type_size(pid_t PID_T)
343 MESSAGE(FATAL_ERROR "pid_t doesn't exist on this platform?")
345 endif(NOT HAVE_PID_T)
347 set(CMAKE_EXTRA_INCLUDE_FILES time.h)
348 check_type_size(timer_t TIMER_T)
351 endif(NOT HAVE_TIMER_T)
352 unset(CMAKE_EXTRA_INCLUDE_FILES)
354 ###############################################################################
357 check_library_exists(m floor "" HAVE_LIBM)
362 check_library_exists(rt clock_gettime "" HAVE_LIBRT)
365 ADD_DEFINITIONS(-DHAVE_LIBRT=1)
368 check_library_exists(subunit subunit_test_start "" HAVE_SUBUNIT)
370 set(SUBUNIT "subunit")
371 set(ENABLE_SUBUNIT 1)
372 add_definitions(-DENABLE_SUBUNIT=1)
374 set(ENABLE_SUBUNIT 0)
375 add_definitions(-DENABLE_SUBUNIT=0)
378 ###############################################################################
379 # Generate "config.h" from "cmake/config.h.in"
380 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.h.in
381 ${CMAKE_CURRENT_BINARY_DIR}/config.h)
382 # Param @ONLY not used on purpose!
383 include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
384 add_definitions(-DHAVE_CONFIG_H)
385 set(CONFIG_HEADER ${CMAKE_CURRENT_BINARY_DIR}/config.h)
387 ###############################################################################
388 # Generate "check_stdint.h" from "cmake/check_stdint.h.in"
390 # The corresponding GNU Autotools build of this project
391 # has m4 macro `m4/ax_create_stdint_h.m4` to create
392 # the file `check_stdint.h` from scratch.
393 # Include file `stdint.h` was introduced in C99 ANSI standard but
394 # many compilers were lacking behind or still are and
395 # have not implemented C99 or their `stdint.h` is not compatible.
396 # Therefore the m4 macro was needed to create the required datatypes.
398 # When converting to CMake we also want to abandon the m4 macros.
400 configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/check_stdint.h.in
401 ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h @ONLY)
403 FILES ${CMAKE_CURRENT_BINARY_DIR}/check_stdint.h
404 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
407 ###############################################################################
408 # Generate "check.pc", the package config (pkgconfig) file for libtool
409 set(prefix_save "${PREFIX}")
410 set(prefix "${CMAKE_INSTALL_PREFIX}")
411 set(exec_prefix "\${prefix}")
412 set(libdir "\${exec_prefix}/lib")
413 set(includedir "\${prefix}/include")
414 set(VERSION "${PROJECT_VERSION}")
417 set(LIBSUBUNIT_PC "libsubunit")
419 set(LIBSUBUNIT_PC "")
422 if (CHECK_ENABLE_GCOV)
423 set(GCOV_LIBS "-lgcov")
424 else (CHECK_ENABLE_GCOV)
426 endif (CHECK_ENABLE_GCOV)
432 set(LIBS "${LIBS} -lm")
436 set(LIBS "${LIBS} -lrt")
439 set(PTHREAD_CFLAGS "-pthread")
441 configure_file(check.pc.in check.pc @ONLY)
443 unset(PTHREAD_CFLAGS)
452 set(PREFIX "${prefix_save}")
455 FILES ${CMAKE_CURRENT_BINARY_DIR}/check.pc
456 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
459 ###############################################################################
461 add_subdirectory(lib)
462 add_subdirectory(src)
463 add_subdirectory(checkmk)
465 ###############################################################################
467 if (BUILD_TESTING AND THIS_IS_MAIN_PROJECT)
468 add_subdirectory(tests)
469 add_test(NAME check_check COMMAND check_check)
470 add_test(NAME check_check_export COMMAND check_check_export)
472 # Only offer to run shell scripts if we may have a working interpreter
473 if(UNIX OR MINGW OR MSYS)
474 add_test(NAME test_output.sh
475 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
476 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_output.sh)
477 add_test(NAME test_log_output.sh
478 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
479 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_log_output.sh)
480 add_test(NAME test_xml_output.sh
481 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
482 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_xml_output.sh)
483 add_test(NAME test_tap_output.sh
484 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
485 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_tap_output.sh)
486 add_test(NAME test_check_nofork.sh
487 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
488 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork.sh)
489 add_test(NAME test_check_nofork_teardown.sh
490 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
491 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_check_nofork_teardown.sh)
492 add_test(NAME test_set_max_msg_size.sh
493 WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests
494 COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_set_max_msg_size.sh)
495 endif(UNIX OR MINGW OR MSYS)
496 endif (BUILD_TESTING AND THIS_IS_MAIN_PROJECT)
498 ###############################################################################
499 # Export project, prepare a config and config-version files
500 string(TOLOWER ${PROJECT_NAME} EXPORT_NAME)
501 include(CMakePackageConfigHelpers)
502 configure_package_config_file(
503 ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${EXPORT_NAME}-config.cmake.in
504 ${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config.cmake
505 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/${EXPORT_NAME}/cmake
507 write_basic_package_version_file(
508 ${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config-version.cmake
509 VERSION ${PROJECT_VERSION}
510 COMPATIBILITY AnyNewerVersion
513 export(EXPORT check-targets
514 FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-targets.cmake"
515 NAMESPACE "${PROJECT_NAME}::"
518 install(EXPORT check-targets
519 NAMESPACE "${PROJECT_NAME}::"
520 FILE "${EXPORT_NAME}-targets.cmake"
521 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME}
525 "${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config.cmake"
526 "${CMAKE_CURRENT_BINARY_DIR}/cmake/${EXPORT_NAME}-config-version.cmake"
527 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${EXPORT_NAME}
530 # Store the current build directory in the CMake user package registry.
531 # This helps dependent projects use a package from the current
532 # project’s build tree, i.e. without installing it.
534 # In CMake 3.14 and below the export(PACKAGE) command populated
535 # the user package registry by default and users needed to set
536 # the CMAKE_EXPORT_NO_PACKAGE_REGISTRY to disable it,
537 # e.g. in automated build and packaging environments. Since
538 # the user package registry is stored outside the build tree,
539 # this side effect should not be enabled by default.
540 # Therefore CMake 3.15 and above prefer that export(PACKAGE) does nothing
541 # unless an explicit CMAKE_EXPORT_PACKAGE_REGISTRY variable is set.
542 export(PACKAGE "${PROJECT_NAME}")