onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | include(CMakeParseArguments) |
| 2 | |
| 3 | # cache this for use inside of the function |
| 4 | set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR}) |
| 5 | |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 6 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 7 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 8 | enable_testing() |
| 9 | |
onqtam | 7b7d161 | 2018-08-17 15:56:09 +0300 | [diff] [blame] | 10 | find_package(Threads) |
| 11 | |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 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") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 14 | |
onqtam | c6cc9ff | 2019-03-15 17:22:50 +0200 | [diff] [blame] | 15 | function(doctest_add_test_impl) |
onqtam | 1fb630b | 2020-06-05 00:14:56 +0300 | [diff] [blame^] | 16 | cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT;XML_OUTPUT;JUNIT_OUTPUT" "NAME" "COMMAND" ${ARGN}) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 17 | if(NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "" OR "${ARG_NAME}" STREQUAL "" OR "${ARG_COMMAND}" STREQUAL "") |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 18 | message(FATAL_ERROR "doctest_add_test() called with wrong options!") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 19 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 20 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 21 | set(the_test_mode NORMAL) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 22 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 23 | # construct the command that will be called by the exec_test.cmake script |
| 24 | set(the_command "") |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 25 | if(${DOCTEST_TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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() |
onqtam | c6cc9ff | 2019-03-15 17:22:50 +0200 | [diff] [blame] | 32 | if(ARG_XML_OUTPUT) |
onqtam | 2377a0c | 2019-03-20 21:04:24 +0200 | [diff] [blame] | 33 | set(the_command "${the_command} --reporters=xml") |
onqtam | c6cc9ff | 2019-03-15 17:22:50 +0200 | [diff] [blame] | 34 | set(ARG_NAME ${ARG_NAME}_xml) |
| 35 | endif() |
onqtam | 1fb630b | 2020-06-05 00:14:56 +0300 | [diff] [blame^] | 36 | if(ARG_JUNIT_OUTPUT) |
| 37 | set(the_command "${the_command} --reporters=junit") |
| 38 | set(ARG_NAME ${ARG_NAME}_junit) |
| 39 | endif() |
| 40 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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") |
onqtam | 1fc3dc7 | 2017-03-14 14:30:09 +0200 | [diff] [blame] | 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") |
onqtam | 9f934f8 | 2016-08-02 12:42:19 +0300 | [diff] [blame] | 45 | # append the argument for ignoring the exit code of the test programs because some are intended to have failing tests |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 46 | set(the_command "${the_command} --dt-no-exitcode=1") |
onqtam | 7907692 | 2018-05-09 21:22:31 +0300 | [diff] [blame] | 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") |
onqtam | 1fb630b | 2020-06-05 00:14:56 +0300 | [diff] [blame^] | 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") |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 51 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 52 | string(STRIP ${the_command} the_command) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 53 | |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 54 | if(${DOCTEST_TEST_MODE} STREQUAL "COLLECT" OR ${DOCTEST_TEST_MODE} STREQUAL "COMPARE") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 55 | if(NOT ARG_NO_OUTPUT) |
| 56 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/) |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 57 | set(the_test_mode ${DOCTEST_TEST_MODE}) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 58 | list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt) |
onqtam | 366c368 | 2019-03-19 13:58:02 +0200 | [diff] [blame] | 59 | list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output_${ARG_NAME}.txt) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 60 | endif() |
| 61 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 62 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 63 | list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode}) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 64 | |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 65 | add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 66 | endfunction() |
| 67 | |
onqtam | c6cc9ff | 2019-03-15 17:22:50 +0200 | [diff] [blame] | 68 | # a custom version of add_test() to suite my needs |
| 69 | function(doctest_add_test) |
| 70 | doctest_add_test_impl(${ARGN}) |
onqtam | 2377a0c | 2019-03-20 21:04:24 +0200 | [diff] [blame] | 71 | doctest_add_test_impl(${ARGN} XML_OUTPUT) |
onqtam | 1fb630b | 2020-06-05 00:14:56 +0300 | [diff] [blame^] | 72 | doctest_add_test_impl(${ARGN} JUNIT_OUTPUT) |
onqtam | c6cc9ff | 2019-03-15 17:22:50 +0200 | [diff] [blame] | 73 | endfunction() |
| 74 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 83 | add_compiler_flags(-fstrict-aliasing) |
Daan De Meyer | 3c6834b | 2019-09-10 15:40:53 +0200 | [diff] [blame] | 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() |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 91 | endif() |
| 92 | |
| 93 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 94 | #add_compiler_flags(-Wno-unknown-pragmas) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 95 | add_compiler_flags(-Wall) |
| 96 | add_compiler_flags(-Wextra) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 97 | add_compiler_flags(-fdiagnostics-show-option) |
| 98 | add_compiler_flags(-Wconversion) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 113 | add_compiler_flags(-Wswitch-enum) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 114 | add_compiler_flags(-Wnon-virtual-dtor) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 115 | add_compiler_flags(-Wctor-dtor-privacy) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 116 | add_compiler_flags(-Wsign-conversion) |
| 117 | add_compiler_flags(-Wdisabled-optimization) |
| 118 | add_compiler_flags(-Weffc++) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 119 | add_compiler_flags(-Winvalid-pch) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 120 | add_compiler_flags(-Wmissing-declarations) |
| 121 | add_compiler_flags(-Woverloaded-virtual) |
onqtam | a726e9e | 2019-03-31 13:34:09 +0300 | [diff] [blame] | 122 | add_compiler_flags(-Wunused-but-set-variable) |
| 123 | add_compiler_flags(-Wunused-result) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 124 | |
onqtam | c30dcc2 | 2019-03-31 14:01:53 +0300 | [diff] [blame] | 125 | # add_compiler_flags(-Wsuggest-override) |
onqtam | 3433fba | 2019-03-31 13:59:26 +0300 | [diff] [blame] | 126 | # add_compiler_flags(-Wmultiple-inheritance) |
| 127 | # add_compiler_flags(-Wcatch-value) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 128 | # add_compiler_flags(-Wsuggest-attribute=cold) |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 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) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 136 | |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 137 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) |
| 138 | add_compiler_flags(-Wnoexcept) |
| 139 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 140 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 141 | # no way to silence it in the expression decomposition macros: _Pragma() in macros doesn't work for the c++ front-end of g++ |
| 142 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578 |
| 143 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543 |
Antoine Cœur | f55bdc7 | 2020-02-03 03:31:02 +0800 | [diff] [blame] | 144 | # Also the warning is completely worthless nowadays - https://stackoverflow.com/questions/14016993 |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 145 | #add_compiler_flags(-Waggregate-return) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 146 | |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 147 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) |
| 148 | add_compiler_flags(-Wdouble-promotion) |
| 149 | add_compiler_flags(-Wtrampolines) |
| 150 | add_compiler_flags(-Wzero-as-null-pointer-constant) |
| 151 | add_compiler_flags(-Wuseless-cast) |
| 152 | add_compiler_flags(-Wvector-operation-performance) |
| 153 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 154 | |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 155 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0) |
| 156 | add_compiler_flags(-Wshift-overflow=2) |
| 157 | add_compiler_flags(-Wnull-dereference) |
| 158 | add_compiler_flags(-Wduplicated-cond) |
| 159 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 160 | |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 161 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0) |
| 162 | add_compiler_flags(-Walloc-zero) |
| 163 | add_compiler_flags(-Walloca) |
| 164 | add_compiler_flags(-Wduplicated-branches) |
| 165 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 166 | |
onqtam | b4b95d2 | 2019-03-31 13:55:03 +0300 | [diff] [blame] | 167 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) |
| 168 | add_compiler_flags(-Wcast-align=strict) |
| 169 | endif() |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 170 | endif() |
| 171 | |
Daan De Meyer | f972602 | 2019-10-10 22:16:49 +0200 | [diff] [blame] | 172 | # necessary for some older compilers which don't default to C++11 |
| 173 | set(CMAKE_CXX_STANDARD 11) |
| 174 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 175 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 176 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 177 | add_compiler_flags(-Weverything) |
onqtam | 4d87d56 | 2018-07-03 22:23:42 +0300 | [diff] [blame] | 178 | add_compiler_flags(-Wno-c++98-compat) |
| 179 | add_compiler_flags(-Wno-c++98-compat-pedantic) |
| 180 | add_compiler_flags(-Wno-c++98-compat-bind-to-temporary-copy) |
| 181 | add_compiler_flags(-Wno-c++98-compat-local-type-template-args) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 182 | add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis |
| 183 | endif() |
| 184 | |
Martin Moene | d8a7edc | 2017-08-01 17:08:08 +0200 | [diff] [blame] | 185 | if(MSVC) |
onqtam | 8edeaaf | 2016-08-16 07:12:58 +0300 | [diff] [blame] | 186 | add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC |
onqtam | 50cbb80 | 2017-12-09 17:17:39 +0200 | [diff] [blame] | 187 | add_compiler_flags(/permissive-) # force standard conformance - this is the better flag than /Za |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 188 | add_compiler_flags(/WX) |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 189 | 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 |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 190 | |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 191 | add_compiler_flags( |
| 192 | /wd4514 # unreferenced inline function has been removed |
| 193 | /wd4571 # SEH related |
| 194 | /wd4710 # function not inlined |
| 195 | /wd4711 # function 'x' selected for automatic inline expansion |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 196 | |
onqtam | 5ed699b | 2017-10-28 21:57:15 +0300 | [diff] [blame] | 197 | /wd4616 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/t7ab6xtd.aspx |
| 198 | /wd4619 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/tacee08d.aspx |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 199 | |
onqtam | 5ed699b | 2017-10-28 21:57:15 +0300 | [diff] [blame] | 200 | #/wd4820 # padding in structs |
| 201 | #/wd4625 # copy constructor was implicitly defined as deleted |
| 202 | #/wd4626 # assignment operator was implicitly defined as deleted |
| 203 | #/wd5027 # move assignment operator was implicitly defined as deleted |
| 204 | #/wd5026 # move constructor was implicitly defined as deleted |
| 205 | #/wd4623 # default constructor was implicitly defined as deleted |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 206 | ) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 207 | endif() |