-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
90 lines (79 loc) · 3.71 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# This file is part of the library test suite and is not to be used in production.
# For production use please add libserard/serard.c to your build system directly.
# This software is distributed under the terms of the MIT License.
# Copyright (c) 2022 OpenCyphal.
# Author: Pavel Kirienko <[email protected]>
cmake_minimum_required(VERSION 3.20)
project(serard_tests C CXX)
enable_testing()
set(CTEST_OUTPUT_ON_FAILURE ON)
set(library_dir "${CMAKE_SOURCE_DIR}/libserard")
set(tests_dir "${CMAKE_SOURCE_DIR}/tests")
# Use -DNO_STATIC_ANALYSIS=1 to suppress static analysis.
# If not suppressed, the tools used here shall be available, otherwise the build will fail.
if (NOT NO_STATIC_ANALYSIS)
# clang-tidy (separate config files per directory)
find_program(clang_tidy NAMES clang-tidy)
if (NOT clang_tidy)
message(FATAL_ERROR "Could not locate clang-tidy")
endif ()
message(STATUS "Using clang-tidy: ${clang_tidy}")
set(CMAKE_C_CLANG_TIDY ${clang_tidy})
set(CMAKE_CXX_CLANG_TIDY ${clang_tidy})
endif ()
# clang-format
find_program(clang_format NAMES clang-format)
if (NOT clang_format)
message(STATUS "Could not locate clang-format")
else ()
file(GLOB format_files ${library_dir}/*.[ch] ${tests_dir}/*.[ch]pp)
message(STATUS "Using clang-format: ${clang_format}; files: ${format_files}")
add_custom_target(format COMMAND ${clang_format} -i -fallback-style=none -style=file --verbose ${format_files})
endif ()
set(CMAKE_CXX_STANDARD 20)
set(CXX_EXTENSIONS OFF)
add_compile_options(
-Wall -Wextra -Werror -pedantic -fstrict-aliasing -Wdouble-promotion -Wswitch-enum -Wfloat-equal -Wundef
-Wconversion -Wtype-limits -Wsign-conversion -Wcast-align
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Woverloaded-virtual -Wnon-virtual-dtor -Wsign-promo")
include_directories(${tests_dir}/catch ${library_dir})
add_definitions(-DCATCH_CONFIG_FAST_COMPILE=1 -DCATCH_CONFIG_ENABLE_ALL_STRINGMAKERS=1)
set(common_sources ${tests_dir}/catch/main.cpp ${library_dir}/serard.c)
function(gen_test name files compile_definitions compile_flags link_flags c_standard)
add_executable(${name} ${common_sources} ${files})
target_compile_definitions(${name} PUBLIC ${compile_definitions})
target_link_libraries(${name} pthread)
set_target_properties(
${name}
PROPERTIES
COMPILE_FLAGS "${compile_flags}"
LINK_FLAGS "${link_flags}"
C_STANDARD "${c_standard}"
)
add_test("run_${name}" "${name}" --rng-seed time)
endfunction()
function(gen_test_matrix name files compile_definitions compile_flags)
gen_test("${name}_x64_c99" "${files}" "${compile_definitions}" "${compile_flags} -m64" "-m64" "99")
gen_test("${name}_x32_c99" "${files}" "${compile_definitions}" "${compile_flags} -m32" "-m32" "99")
gen_test("${name}_x64_c11" "${files}" "${compile_definitions}" "${compile_flags} -m64" "-m64" "11")
gen_test("${name}_x32_c11" "${files}" "${compile_definitions}" "${compile_flags} -m32" "-m32" "11")
# Coverage is only available for GCC builds.
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
gen_test("${name}_cov"
"${files}"
"${compile_definitions}"
"${compile_flags} -g -O0 --coverage"
"--coverage"
"11")
endif ()
endfunction()
# Disable missing declaration warning to allow exposure of private definitions.
gen_test_matrix(test_private
"${tests_dir}/test_private.cpp"
"-DSERARD_CONFIG_HEADER=\"${CMAKE_CURRENT_SOURCE_DIR}/serard_config_private.h\""
"-Wno-missing-declarations")
gen_test_matrix(test_public
"${tests_dir}/test_public.cpp"
""
"-Wmissing-declarations")