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