onqtam | b759fb4 | 2017-08-01 18:38:26 +0300 | [diff] [blame] | 1 | if(doctest_common_included) |
onqtam | a985f5a | 2017-04-16 22:48:07 +0300 | [diff] [blame] | 2 | return() |
| 3 | endif() |
onqtam | b759fb4 | 2017-08-01 18:38:26 +0300 | [diff] [blame] | 4 | set(doctest_common_included true) |
onqtam | a985f5a | 2017-04-16 22:48:07 +0300 | [diff] [blame] | 5 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 6 | include(CMakeParseArguments) |
| 7 | |
| 8 | # cache this for use inside of the function |
| 9 | set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR}) |
| 10 | |
onqtam | 119cfb6 | 2017-04-17 10:46:55 +0300 | [diff] [blame] | 11 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) |
| 12 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 13 | enable_testing() |
| 14 | |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 15 | set(DOCTEST_TEST_MODE "COMPARE" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output") |
| 16 | set_property(CACHE DOCTEST_TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 17 | |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 18 | # a custom version of add_test() to suite my needs |
| 19 | function(doctest_add_test) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 20 | cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT" "NAME" "COMMAND" ${ARGN}) |
| 21 | 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] | 22 | message(FATAL_ERROR "doctest_add_test() called with wrong options!") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 23 | endif() |
| 24 | |
| 25 | set(the_test_mode NORMAL) |
| 26 | |
| 27 | # construct the command that will be called by the exec_test.cmake script |
| 28 | set(the_command "") |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 29 | if(${DOCTEST_TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 30 | set(the_test_mode VALGRIND) |
| 31 | set(the_command "valgrind -v --leak-check=full --track-origins=yes --error-exitcode=1") |
| 32 | endif() |
| 33 | foreach(cur ${ARG_COMMAND}) |
| 34 | set(the_command "${the_command} ${cur}") |
| 35 | endforeach() |
| 36 | # append the argument for removing paths from filenames in the output so tests give the same output everywhere |
| 37 | set(the_command "${the_command} --dt-no-path-filenames=1") |
onqtam | 1fc3dc7 | 2017-03-14 14:30:09 +0200 | [diff] [blame] | 38 | # append the argument for substituting source line numbers with 0 in the output so tests give the same output when lines change a bit |
| 39 | set(the_command "${the_command} --dt-no-line-numbers=1") |
onqtam | 9f934f8 | 2016-08-02 12:42:19 +0300 | [diff] [blame] | 40 | # 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] | 41 | set(the_command "${the_command} --dt-no-exitcode=1") |
onqtam | 7907692 | 2018-05-09 21:22:31 +0300 | [diff] [blame] | 42 | # append the argument for using the same line format in the output - so gcc/non-gcc builds have the same output |
| 43 | set(the_command "${the_command} --dt-gnu-file-line=0") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 44 | |
| 45 | string(STRIP ${the_command} the_command) |
| 46 | |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 47 | if(${DOCTEST_TEST_MODE} STREQUAL "COLLECT" OR ${DOCTEST_TEST_MODE} STREQUAL "COMPARE") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 48 | if(NOT ARG_NO_OUTPUT) |
| 49 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/) |
onqtam | 56d9170 | 2017-04-16 21:03:58 +0300 | [diff] [blame] | 50 | set(the_test_mode ${DOCTEST_TEST_MODE}) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 51 | list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt) |
| 52 | list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output.txt) |
| 53 | endif() |
| 54 | endif() |
| 55 | |
| 56 | list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode}) |
| 57 | |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 58 | 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] | 59 | endfunction() |
| 60 | |
onqtam | 61954c3 | 2017-04-16 17:22:44 +0300 | [diff] [blame] | 61 | function(doctest_add_executable name) |
| 62 | add_executable(${name} ${ARGN}) |
| 63 | add_dependencies(${name} assemble_single_header) |
| 64 | endfunction() |
| 65 | |
| 66 | function(doctest_add_library name) |
| 67 | add_library(${name} ${ARGN}) |
| 68 | add_dependencies(${name} assemble_single_header) |
| 69 | endfunction() |
| 70 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 71 | macro(add_compiler_flags) |
| 72 | foreach(flag ${ARGV}) |
| 73 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") |
| 74 | endforeach() |
| 75 | endmacro() |
| 76 | |
| 77 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") |
| 78 | add_compiler_flags(-Werror) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 79 | add_compiler_flags(-pedantic) |
| 80 | add_compiler_flags(-pedantic-errors) |
| 81 | add_compiler_flags(-fvisibility=hidden) |
| 82 | add_compiler_flags(-fstrict-aliasing) |
| 83 | endif() |
| 84 | |
| 85 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 86 | add_compiler_flags(-Wall) |
| 87 | add_compiler_flags(-Wextra) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 88 | add_compiler_flags(-fdiagnostics-show-option) |
| 89 | add_compiler_flags(-Wconversion) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 90 | add_compiler_flags(-Wold-style-cast) |
| 91 | add_compiler_flags(-Wfloat-equal) |
| 92 | add_compiler_flags(-Wlogical-op) |
| 93 | add_compiler_flags(-Wundef) |
| 94 | add_compiler_flags(-Wredundant-decls) |
| 95 | add_compiler_flags(-Wshadow) |
| 96 | add_compiler_flags(-Wstrict-overflow=5) |
| 97 | add_compiler_flags(-Wwrite-strings) |
| 98 | add_compiler_flags(-Wpointer-arith) |
| 99 | add_compiler_flags(-Wcast-qual) |
| 100 | add_compiler_flags(-Wformat=2) |
| 101 | add_compiler_flags(-Wswitch-default) |
| 102 | add_compiler_flags(-Wmissing-include-dirs) |
| 103 | add_compiler_flags(-Wcast-align) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 104 | add_compiler_flags(-Wswitch-enum) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 105 | add_compiler_flags(-Wnon-virtual-dtor) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 106 | add_compiler_flags(-Wctor-dtor-privacy) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 107 | add_compiler_flags(-Wsign-conversion) |
| 108 | add_compiler_flags(-Wdisabled-optimization) |
| 109 | add_compiler_flags(-Weffc++) |
| 110 | add_compiler_flags(-Winline) |
| 111 | add_compiler_flags(-Winvalid-pch) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 112 | add_compiler_flags(-Wmissing-declarations) |
| 113 | add_compiler_flags(-Woverloaded-virtual) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 114 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) |
| 115 | add_compiler_flags(-Wnoexcept) |
| 116 | endif() |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 117 | |
| 118 | # no way to silence it in the expression decomposition macros: _Pragma() in macros doesn't work for the c++ front-end of g++ |
| 119 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578 |
| 120 | # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543 |
| 121 | # Also the warning is completely worthless nowadays - http://stackoverflow.com/questions/14016993 |
| 122 | #add_compiler_flags(-Waggregate-return) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 123 | |
| 124 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) |
| 125 | add_compiler_flags(-Wdouble-promotion) |
| 126 | add_compiler_flags(-Wtrampolines) |
| 127 | add_compiler_flags(-Wzero-as-null-pointer-constant) |
| 128 | add_compiler_flags(-Wuseless-cast) |
| 129 | add_compiler_flags(-Wvector-operation-performance) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 130 | endif() |
| 131 | |
onqtam | ffb7530 | 2017-10-19 18:05:59 +0300 | [diff] [blame] | 132 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 133 | add_compiler_flags(-Wshift-overflow=2) |
| 134 | add_compiler_flags(-Wnull-dereference) |
| 135 | add_compiler_flags(-Wduplicated-cond) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 136 | endif() |
onqtam | f63c510 | 2017-02-25 20:00:52 +0200 | [diff] [blame] | 137 | |
onqtam | ffb7530 | 2017-10-19 18:05:59 +0300 | [diff] [blame] | 138 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0) |
onqtam | f63c510 | 2017-02-25 20:00:52 +0200 | [diff] [blame] | 139 | add_compiler_flags(-Walloc-zero) |
| 140 | add_compiler_flags(-Walloca) |
onqtam | ffb7530 | 2017-10-19 18:05:59 +0300 | [diff] [blame] | 141 | add_compiler_flags(-Wduplicated-branches) |
onqtam | 6b2c99f | 2018-05-08 19:33:38 +0300 | [diff] [blame] | 142 | endif() |
| 143 | |
| 144 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) |
| 145 | add_compiler_flags(-Wcast-align=strict) |
onqtam | f63c510 | 2017-02-25 20:00:52 +0200 | [diff] [blame] | 146 | endif() |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 147 | endif() |
| 148 | |
| 149 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 150 | add_compiler_flags(-Weverything) |
| 151 | add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis |
| 152 | endif() |
| 153 | |
Martin Moene | d8a7edc | 2017-08-01 17:08:08 +0200 | [diff] [blame] | 154 | if(MSVC) |
onqtam | 8edeaaf | 2016-08-16 07:12:58 +0300 | [diff] [blame] | 155 | add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC |
onqtam | 50cbb80 | 2017-12-09 17:17:39 +0200 | [diff] [blame] | 156 | 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] | 157 | add_compiler_flags(/WX) |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 158 | 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 |
| 159 | |
| 160 | add_compiler_flags( |
| 161 | /wd4514 # unreferenced inline function has been removed |
| 162 | /wd4571 # SEH related |
| 163 | /wd4710 # function not inlined |
| 164 | /wd4711 # function 'x' selected for automatic inline expansion |
onqtam | 5ed699b | 2017-10-28 21:57:15 +0300 | [diff] [blame] | 165 | |
| 166 | /wd4616 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/t7ab6xtd.aspx |
| 167 | /wd4619 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/tacee08d.aspx |
| 168 | |
| 169 | #/wd4820 # padding in structs |
| 170 | #/wd4625 # copy constructor was implicitly defined as deleted |
| 171 | #/wd4626 # assignment operator was implicitly defined as deleted |
| 172 | #/wd5027 # move assignment operator was implicitly defined as deleted |
| 173 | #/wd5026 # move constructor was implicitly defined as deleted |
| 174 | #/wd4623 # default constructor was implicitly defined as deleted |
onqtam | abf39d2 | 2017-10-28 21:30:45 +0300 | [diff] [blame] | 175 | ) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 176 | endif() |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 177 | |
| 178 | # add a custom target that assembles the single header when any of the parts are touched |
| 179 | |
onqtam | 08cf7dc | 2017-04-16 11:37:22 +0300 | [diff] [blame] | 180 | set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/") |
| 181 | set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/parts/") |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 182 | if(WIN32) |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 183 | STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder}) |
| 184 | STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder}) |
| 185 | endif() |
| 186 | |
| 187 | add_custom_command( |
| 188 | OUTPUT ${doctest_include_folder}doctest.h |
| 189 | DEPENDS |
| 190 | ${doctest_parts_folder}doctest_fwd.h |
| 191 | ${doctest_parts_folder}doctest_impl.h |
onqtam | f720d43 | 2016-05-31 17:51:10 +0300 | [diff] [blame] | 192 | COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 193 | COMMENT "assembling the single header") |
| 194 | |
| 195 | add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h) |