forked from doctest/doctest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.cmake
211 lines (179 loc) · 8.75 KB
/
common.cmake
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
include(CMakeParseArguments)
# cache this for use inside of the function
set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR})
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
enable_testing()
find_package(Threads)
set(DOCTEST_TEST_MODE "COMPARE" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output")
set_property(CACHE DOCTEST_TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE")
function(doctest_add_test_impl)
cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT;XML_OUTPUT;JUNIT_OUTPUT" "NAME" "COMMAND" ${ARGN})
if(NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "" OR "${ARG_NAME}" STREQUAL "" OR "${ARG_COMMAND}" STREQUAL "")
message(FATAL_ERROR "doctest_add_test() called with wrong options!")
endif()
set(the_test_mode NORMAL)
# construct the command that will be called by the exec_test.cmake script
set(the_command "")
if(${DOCTEST_TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND)
set(the_test_mode VALGRIND)
set(the_command "valgrind -v --leak-check=full --track-origins=yes --error-exitcode=1")
endif()
foreach(cur ${ARG_COMMAND})
set(the_command "${the_command} ${cur}")
endforeach()
if(ARG_XML_OUTPUT)
set(the_command "${the_command} --reporters=xml")
set(ARG_NAME ${ARG_NAME}_xml)
endif()
if(ARG_JUNIT_OUTPUT)
set(the_command "${the_command} --reporters=junit")
set(ARG_NAME ${ARG_NAME}_junit)
endif()
# append the argument for removing paths from filenames in the output so tests give the same output everywhere
set(the_command "${the_command} --dt-no-path-filenames=1")
# append the argument for substituting source line numbers with 0 in the output so tests give the same output when lines change a bit
set(the_command "${the_command} --dt-no-line-numbers=1")
# append the argument for ignoring the exit code of the test programs because some are intended to have failing tests
set(the_command "${the_command} --dt-no-exitcode=1")
# append the argument for using the same line format in the output - so gcc/non-gcc builds have the same output
set(the_command "${the_command} --dt-gnu-file-line=0")
# append the argument for skipping any time-related output so that the reference output from reporters is stable on CI
set(the_command "${the_command} --dt-no-time-in-output=1")
string(STRIP ${the_command} the_command)
if(${DOCTEST_TEST_MODE} STREQUAL "COLLECT" OR ${DOCTEST_TEST_MODE} STREQUAL "COMPARE")
if(NOT ARG_NO_OUTPUT)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/)
set(the_test_mode ${DOCTEST_TEST_MODE})
list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt)
list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output_${ARG_NAME}.txt)
endif()
endif()
list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode})
add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake)
endfunction()
# a custom version of add_test() to suite my needs
function(doctest_add_test)
doctest_add_test_impl(${ARGN})
doctest_add_test_impl(${ARGN} XML_OUTPUT)
doctest_add_test_impl(${ARGN} JUNIT_OUTPUT)
endfunction()
macro(add_compiler_flags)
foreach(flag ${ARGV})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
endforeach()
endmacro()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compiler_flags(-Werror)
add_compiler_flags(-fstrict-aliasing)
# The following options are not valid when clang-cl is used.
if(NOT MSVC)
add_compiler_flags(-pedantic)
add_compiler_flags(-pedantic-errors)
add_compiler_flags(-fvisibility=hidden)
endif()
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
#add_compiler_flags(-Wno-unknown-pragmas)
add_compiler_flags(-Wall)
add_compiler_flags(-Wextra)
add_compiler_flags(-fdiagnostics-show-option)
add_compiler_flags(-Wconversion)
add_compiler_flags(-Wold-style-cast)
add_compiler_flags(-Wfloat-equal)
add_compiler_flags(-Wlogical-op)
add_compiler_flags(-Wundef)
add_compiler_flags(-Wredundant-decls)
add_compiler_flags(-Wshadow)
add_compiler_flags(-Wstrict-overflow=5)
add_compiler_flags(-Wwrite-strings)
add_compiler_flags(-Wpointer-arith)
add_compiler_flags(-Wcast-qual)
add_compiler_flags(-Wformat=2)
add_compiler_flags(-Wswitch-default)
add_compiler_flags(-Wmissing-include-dirs)
add_compiler_flags(-Wcast-align)
add_compiler_flags(-Wswitch-enum)
add_compiler_flags(-Wnon-virtual-dtor)
add_compiler_flags(-Wctor-dtor-privacy)
add_compiler_flags(-Wsign-conversion)
add_compiler_flags(-Wdisabled-optimization)
add_compiler_flags(-Weffc++)
add_compiler_flags(-Winvalid-pch)
add_compiler_flags(-Wmissing-declarations)
add_compiler_flags(-Woverloaded-virtual)
add_compiler_flags(-Wunused-but-set-variable)
add_compiler_flags(-Wunused-result)
# add_compiler_flags(-Wsuggest-override)
# add_compiler_flags(-Wmultiple-inheritance)
# add_compiler_flags(-Wcatch-value)
# add_compiler_flags(-Wsuggest-attribute=cold)
# add_compiler_flags(-Wsuggest-attribute=const)
# add_compiler_flags(-Wsuggest-attribute=format)
# add_compiler_flags(-Wsuggest-attribute=malloc)
# add_compiler_flags(-Wsuggest-attribute=noreturn)
# add_compiler_flags(-Wsuggest-attribute=pure)
# add_compiler_flags(-Wsuggest-final-methods)
# add_compiler_flags(-Wsuggest-final-types)
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
add_compiler_flags(-Wnoexcept)
endif()
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
add_compiler_flags(-Wno-missing-field-initializers)
endif()
# no way to silence it in the expression decomposition macros: _Pragma() in macros doesn't work for the c++ front-end of g++
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543
# Also the warning is completely worthless nowadays - https://stackoverflow.com/questions/14016993
#add_compiler_flags(-Waggregate-return)
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
add_compiler_flags(-Wdouble-promotion)
add_compiler_flags(-Wtrampolines)
add_compiler_flags(-Wzero-as-null-pointer-constant)
add_compiler_flags(-Wuseless-cast)
add_compiler_flags(-Wvector-operation-performance)
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
add_compiler_flags(-Wshift-overflow=2)
add_compiler_flags(-Wnull-dereference)
add_compiler_flags(-Wduplicated-cond)
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
add_compiler_flags(-Walloc-zero)
add_compiler_flags(-Walloca)
add_compiler_flags(-Wduplicated-branches)
endif()
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)
add_compiler_flags(-Wcast-align=strict)
endif()
endif()
# necessary for some older compilers which don't default to C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compiler_flags(-Weverything)
add_compiler_flags(-Wno-c++98-compat)
add_compiler_flags(-Wno-c++98-compat-pedantic)
add_compiler_flags(-Wno-c++98-compat-bind-to-temporary-copy)
add_compiler_flags(-Wno-c++98-compat-local-type-template-args)
add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis
endif()
if(MSVC)
add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC
add_compiler_flags(/permissive-) # force standard conformance - this is the better flag than /Za
add_compiler_flags(/WX)
add_compiler_flags(/Wall) # turns on warnings from levels 1 through 4 which are off by default - https://msdn.microsoft.com/en-us/library/23k5d385.aspx
add_compiler_flags(
/wd4514 # unreferenced inline function has been removed
/wd4571 # SEH related
/wd4710 # function not inlined
/wd4711 # function 'x' selected for automatic inline expansion
/wd4616 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/t7ab6xtd.aspx
/wd4619 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/tacee08d.aspx
#/wd4820 # padding in structs
#/wd4625 # copy constructor was implicitly defined as deleted
#/wd4626 # assignment operator was implicitly defined as deleted
#/wd5027 # move assignment operator was implicitly defined as deleted
#/wd5026 # move constructor was implicitly defined as deleted
#/wd4623 # default constructor was implicitly defined as deleted
)
endif()