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