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") |
| 15 | |
onqtam | 29e6260 | 2016-05-30 19:45:10 +0300 | [diff] [blame] | 16 | # override add_test() to suite my needs |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 17 | function(add_test) |
| 18 | cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT" "NAME" "COMMAND" ${ARGN}) |
| 19 | if(NOT "${ARG_UNPARSED_ARGUMENTS}" STREQUAL "" OR "${ARG_NAME}" STREQUAL "" OR "${ARG_COMMAND}" STREQUAL "") |
| 20 | message(FATAL_ERROR "add_test() called with wrong options!") |
| 21 | endif() |
| 22 | |
| 23 | set(the_test_mode NORMAL) |
| 24 | |
| 25 | # construct the command that will be called by the exec_test.cmake script |
| 26 | set(the_command "") |
| 27 | if(${TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND) |
| 28 | set(the_test_mode VALGRIND) |
| 29 | set(the_command "valgrind -v --leak-check=full --track-origins=yes --error-exitcode=1") |
| 30 | endif() |
| 31 | foreach(cur ${ARG_COMMAND}) |
| 32 | set(the_command "${the_command} ${cur}") |
| 33 | endforeach() |
| 34 | # append the argument for removing paths from filenames in the output so tests give the same output everywhere |
| 35 | set(the_command "${the_command} --dt-no-path-filenames=1") |
| 36 | set(the_command "${the_command} --dt-no-exitcode=1") |
| 37 | |
| 38 | string(STRIP ${the_command} the_command) |
| 39 | |
| 40 | if(${TEST_MODE} STREQUAL "COLLECT" OR ${TEST_MODE} STREQUAL "COMPARE") |
| 41 | if(NOT ARG_NO_OUTPUT) |
| 42 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/) |
| 43 | set(the_test_mode ${TEST_MODE}) |
| 44 | list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt) |
| 45 | list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output.txt) |
| 46 | endif() |
| 47 | endif() |
| 48 | |
| 49 | list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode}) |
| 50 | |
| 51 | _add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake) |
| 52 | endfunction() |
| 53 | |
| 54 | macro(add_compiler_flags) |
| 55 | foreach(flag ${ARGV}) |
| 56 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}") |
| 57 | endforeach() |
| 58 | endmacro() |
| 59 | |
| 60 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") |
| 61 | add_compiler_flags(-Werror) |
| 62 | add_compiler_flags(-std=c++98) |
| 63 | add_compiler_flags(-pedantic) |
| 64 | add_compiler_flags(-pedantic-errors) |
| 65 | add_compiler_flags(-fvisibility=hidden) |
| 66 | add_compiler_flags(-fstrict-aliasing) |
| 67 | endif() |
| 68 | |
| 69 | if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") |
| 70 | add_compiler_flags(-ansi) |
| 71 | add_compiler_flags(-Wall) |
| 72 | add_compiler_flags(-Wextra) |
| 73 | add_compiler_flags(-fstack-protector-all) |
| 74 | add_compiler_flags(-funsafe-loop-optimizations) |
| 75 | add_compiler_flags(-fdiagnostics-show-option) |
| 76 | add_compiler_flags(-Wconversion) |
| 77 | add_compiler_flags(-Wno-missing-field-initializers) |
| 78 | add_compiler_flags(-Wold-style-cast) |
| 79 | add_compiler_flags(-Wfloat-equal) |
| 80 | add_compiler_flags(-Wlogical-op) |
| 81 | add_compiler_flags(-Wundef) |
| 82 | add_compiler_flags(-Wredundant-decls) |
| 83 | add_compiler_flags(-Wshadow) |
| 84 | add_compiler_flags(-Wstrict-overflow=5) |
| 85 | add_compiler_flags(-Wwrite-strings) |
| 86 | add_compiler_flags(-Wpointer-arith) |
| 87 | add_compiler_flags(-Wcast-qual) |
| 88 | add_compiler_flags(-Wformat=2) |
| 89 | add_compiler_flags(-Wswitch-default) |
| 90 | add_compiler_flags(-Wmissing-include-dirs) |
| 91 | add_compiler_flags(-Wcast-align) |
| 92 | add_compiler_flags(-Wformat-nonliteral) |
| 93 | add_compiler_flags(-Wparentheses) |
| 94 | add_compiler_flags(-Winit-self) |
| 95 | add_compiler_flags(-Wuninitialized) |
| 96 | add_compiler_flags(-Wswitch-enum) |
| 97 | add_compiler_flags(-Wno-endif-labels) |
| 98 | add_compiler_flags(-Wunused-function) |
| 99 | add_compiler_flags(-Wnon-virtual-dtor) |
| 100 | add_compiler_flags(-Wno-pmf-conversions) |
| 101 | add_compiler_flags(-Wctor-dtor-privacy) |
| 102 | add_compiler_flags(-Wsign-promo) |
| 103 | add_compiler_flags(-Wsign-conversion) |
| 104 | add_compiler_flags(-Wdisabled-optimization) |
| 105 | add_compiler_flags(-Weffc++) |
| 106 | add_compiler_flags(-Winline) |
| 107 | add_compiler_flags(-Winvalid-pch) |
| 108 | add_compiler_flags(-Wstack-protector) |
| 109 | add_compiler_flags(-Wunsafe-loop-optimizations) |
| 110 | add_compiler_flags(-Wmissing-declarations) |
| 111 | add_compiler_flags(-Woverloaded-virtual) |
| 112 | add_compiler_flags(-Wstrict-null-sentinel) |
| 113 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6) |
| 114 | add_compiler_flags(-Wnoexcept) |
| 115 | endif() |
| 116 | #add_compiler_flags(-Waggregate-return) # GCC 4.8 does not silence this even with "#pragma GCC diagnostic ignored" |
| 117 | |
| 118 | if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) |
| 119 | add_compiler_flags(-Wdouble-promotion) |
| 120 | add_compiler_flags(-Wtrampolines) |
| 121 | add_compiler_flags(-Wzero-as-null-pointer-constant) |
| 122 | add_compiler_flags(-Wuseless-cast) |
| 123 | add_compiler_flags(-Wvector-operation-performance) |
| 124 | add_compiler_flags(-Wsized-deallocation) |
| 125 | endif() |
| 126 | |
| 127 | if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.3) |
| 128 | add_compiler_flags(-Wshift-overflow=2) |
| 129 | add_compiler_flags(-Wnull-dereference) |
| 130 | add_compiler_flags(-Wduplicated-cond) |
| 131 | add_compiler_flags(-Wmisleading-indentation) |
| 132 | add_compiler_flags(-Wshift-negative-value) |
| 133 | endif() |
| 134 | endif() |
| 135 | |
| 136 | if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") |
| 137 | add_compiler_flags(-Weverything) |
| 138 | add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis |
| 139 | endif() |
| 140 | |
| 141 | if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") |
| 142 | add_compiler_flags(/WX) |
| 143 | add_compiler_flags(/W4) # /Wall is too aggressive - even the standard C headers give thousands of errors... |
| 144 | endif() |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 145 | |
| 146 | # add a custom target that assembles the single header when any of the parts are touched |
| 147 | |
| 148 | set(cat_cmd "cat") |
| 149 | set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../doctest/") |
| 150 | set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../doctest/parts/") |
| 151 | if(WIN32) |
| 152 | set(cat_cmd "type") |
| 153 | STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder}) |
| 154 | STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder}) |
| 155 | endif() |
| 156 | |
| 157 | add_custom_command( |
| 158 | OUTPUT ${doctest_include_folder}doctest.h |
| 159 | DEPENDS |
| 160 | ${doctest_parts_folder}doctest_fwd.h |
| 161 | ${doctest_parts_folder}doctest_impl.h |
onqtam | f720d43 | 2016-05-31 17:51:10 +0300 | [diff] [blame] | 162 | COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake |
onqtam | 7ffa84e | 2016-05-30 18:42:20 +0300 | [diff] [blame] | 163 | COMMENT "assembling the single header") |
| 164 | |
| 165 | add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h) |
onqtam | 29e6260 | 2016-05-30 19:45:10 +0300 | [diff] [blame] | 166 | |
| 167 | # override add_executable() to add a dependency on the header assembly target |
| 168 | function(add_executable name) |
| 169 | _add_executable(${name} ${ARGN}) |
| 170 | add_dependencies(${name} assemble_single_header) |
| 171 | endfunction() |
| 172 | |
| 173 | # override add_library() to add a dependency on the header assembly target |
| 174 | function(add_library name) |
| 175 | _add_library(${name} ${ARGN}) |
| 176 | add_dependencies(${name} assemble_single_header) |
| 177 | endfunction() |