onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | if(common_included) |
| 2 | return() |
| 3 | endif() |
| 4 | set(common_included true) |
| 5 | |
| 6 | include(CMakeParseArguments) |
| 7 | |
| 8 | # cache this for use inside of the function |
| 9 | set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR}) |
| 10 | |
| 11 | enable_testing() |
| 12 | |
| 13 | set(TEST_MODE "COMPARE" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output") |
| 14 | set_property(CACHE TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE") |
onqtam | 5cab8e9 | 2016-10-10 17:58:38 +0300 | [diff] [blame] | 15 | option(WITH_CPP11 "With C++11 enabled" OFF) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 16 | |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 17 | # a custom version of add_test() to suite my needs |
| 18 | function(doctest_add_test) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 19 | cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT" "NAME" "COMMAND" ${ARGN}) |
| 20 | 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] | 21 | message(FATAL_ERROR "doctest_add_test() called with wrong options!") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 22 | endif() |
| 23 | |
| 24 | set(the_test_mode NORMAL) |
| 25 | |
| 26 | # construct the command that will be called by the exec_test.cmake script |
| 27 | set(the_command "") |
| 28 | if(${TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND) |
| 29 | set(the_test_mode VALGRIND) |
| 30 | set(the_command "valgrind -v --leak-check=full --track-origins=yes --error-exitcode=1") |
| 31 | endif() |
| 32 | foreach(cur ${ARG_COMMAND}) |
| 33 | set(the_command "${the_command} ${cur}") |
| 34 | endforeach() |
| 35 | # append the argument for removing paths from filenames in the output so tests give the same output everywhere |
| 36 | set(the_command "${the_command} --dt-no-path-filenames=1") |
onqtam | 1fc3dc7 | 2017-03-14 14:30:09 +0200 | [diff] [blame] | 37 | # append the argument for substituting source line numbers with 0 in the output so tests give the same output when lines change a bit |
| 38 | set(the_command "${the_command} --dt-no-line-numbers=1") |
onqtam | 9f934f8 | 2016-08-02 12:42:19 +0300 | [diff] [blame] | 39 | # 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] | 40 | set(the_command "${the_command} --dt-no-exitcode=1") |
onqtam | 0b51b7b | 2016-11-08 14:27:30 +0200 | [diff] [blame] | 41 | # append the argument for not printing the framework version so reference output doesn't have to be recommitted when the version is bumped |
| 42 | set(the_command "${the_command} --dt-no-version=1") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 43 | |
| 44 | string(STRIP ${the_command} the_command) |
| 45 | |
| 46 | if(${TEST_MODE} STREQUAL "COLLECT" OR ${TEST_MODE} STREQUAL "COMPARE") |
| 47 | if(NOT ARG_NO_OUTPUT) |
| 48 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/) |
| 49 | set(the_test_mode ${TEST_MODE}) |
| 50 | list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt) |
| 51 | list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output.txt) |
| 52 | endif() |
| 53 | endif() |
| 54 | |
| 55 | list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode}) |
| 56 | |
onqtam | db5eee9 | 2016-09-15 17:12:50 +0300 | [diff] [blame] | 57 | 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] | 58 | endfunction() |
| 59 | |
onqtam | 61954c3 | 2017-04-16 17:22:44 +0300 | [diff] [blame^] | 60 | function(doctest_add_executable name) |
| 61 | add_executable(${name} ${ARGN}) |
| 62 | add_dependencies(${name} assemble_single_header) |
| 63 | endfunction() |
| 64 | |
| 65 | function(doctest_add_library name) |
| 66 | add_library(${name} ${ARGN}) |
| 67 | add_dependencies(${name} assemble_single_header) |
| 68 | endfunction() |
| 69 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 70 | macro(add_compiler_flags) |
| 71 | foreach(flag ${ARGV}) |
| 72 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") |
| 73 | endforeach() |
| 74 | endmacro() |
| 75 | |
| 76 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") |
| 77 | add_compiler_flags(-Werror) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 78 | add_compiler_flags(-pedantic) |
| 79 | add_compiler_flags(-pedantic-errors) |
| 80 | add_compiler_flags(-fvisibility=hidden) |
| 81 | add_compiler_flags(-fstrict-aliasing) |
onqtam | 5cab8e9 | 2016-10-10 17:58:38 +0300 | [diff] [blame] | 82 | |
| 83 | if(WITH_CPP11) |
| 84 | add_compiler_flags(-std=c++0x) |
| 85 | else() |
| 86 | add_compiler_flags(-std=c++98) |
| 87 | endif() |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 88 | endif() |
| 89 | |
| 90 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 91 | add_compiler_flags(-Wall) |
| 92 | add_compiler_flags(-Wextra) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 93 | add_compiler_flags(-fdiagnostics-show-option) |
| 94 | add_compiler_flags(-Wconversion) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 95 | add_compiler_flags(-Wold-style-cast) |
| 96 | add_compiler_flags(-Wfloat-equal) |
| 97 | add_compiler_flags(-Wlogical-op) |
| 98 | add_compiler_flags(-Wundef) |
| 99 | add_compiler_flags(-Wredundant-decls) |
| 100 | add_compiler_flags(-Wshadow) |
| 101 | add_compiler_flags(-Wstrict-overflow=5) |
| 102 | add_compiler_flags(-Wwrite-strings) |
| 103 | add_compiler_flags(-Wpointer-arith) |
| 104 | add_compiler_flags(-Wcast-qual) |
| 105 | add_compiler_flags(-Wformat=2) |
| 106 | add_compiler_flags(-Wswitch-default) |
| 107 | add_compiler_flags(-Wmissing-include-dirs) |
| 108 | add_compiler_flags(-Wcast-align) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 109 | add_compiler_flags(-Wswitch-enum) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 110 | add_compiler_flags(-Wnon-virtual-dtor) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 111 | add_compiler_flags(-Wctor-dtor-privacy) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 112 | add_compiler_flags(-Wsign-conversion) |
| 113 | add_compiler_flags(-Wdisabled-optimization) |
| 114 | add_compiler_flags(-Weffc++) |
| 115 | add_compiler_flags(-Winline) |
| 116 | add_compiler_flags(-Winvalid-pch) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 117 | add_compiler_flags(-Wmissing-declarations) |
| 118 | add_compiler_flags(-Woverloaded-virtual) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 119 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) |
| 120 | add_compiler_flags(-Wnoexcept) |
| 121 | endif() |
| 122 | #add_compiler_flags(-Waggregate-return) # GCC 4.8 does not silence this even with "#pragma GCC diagnostic ignored" |
| 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 | b382bd3 | 2016-08-01 20:53:56 +0300 | [diff] [blame] | 132 | if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 6.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 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 | |
| 138 | if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 7.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) |
| 139 | add_compiler_flags(-Walloc-zero) |
| 140 | add_compiler_flags(-Walloca) |
| 141 | endif() |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 142 | endif() |
| 143 | |
| 144 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 145 | add_compiler_flags(-Weverything) |
| 146 | add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis |
| 147 | endif() |
| 148 | |
| 149 | if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") |
onqtam | 8edeaaf | 2016-08-16 07:12:58 +0300 | [diff] [blame] | 150 | add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 151 | add_compiler_flags(/WX) |
| 152 | add_compiler_flags(/W4) # /Wall is too aggressive - even the standard C headers give thousands of errors... |
| 153 | endif() |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 154 | |
| 155 | # add a custom target that assembles the single header when any of the parts are touched |
| 156 | |
| 157 | set(cat_cmd "cat") |
onqtam | 08cf7dc | 2017-04-16 11:37:22 +0300 | [diff] [blame] | 158 | set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/") |
| 159 | set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/parts/") |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 160 | if(WIN32) |
| 161 | set(cat_cmd "type") |
| 162 | STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder}) |
| 163 | STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder}) |
| 164 | endif() |
| 165 | |
| 166 | add_custom_command( |
| 167 | OUTPUT ${doctest_include_folder}doctest.h |
| 168 | DEPENDS |
| 169 | ${doctest_parts_folder}doctest_fwd.h |
| 170 | ${doctest_parts_folder}doctest_impl.h |
onqtam | f720d43 | 2016-05-31 17:51:10 +0300 | [diff] [blame] | 171 | COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 172 | COMMENT "assembling the single header") |
| 173 | |
| 174 | add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h) |