blob: cdba5f0fd508bbc8f79920c6e4ab435eb1b6f001 [file] [log] [blame]
onqtamb759fb42017-08-01 18:38:26 +03001if(doctest_common_included)
onqtama985f5a2017-04-16 22:48:07 +03002 return()
3endif()
onqtamb759fb42017-08-01 18:38:26 +03004set(doctest_common_included true)
onqtama985f5a2017-04-16 22:48:07 +03005
onqtam4a655632016-05-26 14:20:52 +03006include(CMakeParseArguments)
7
8# cache this for use inside of the function
9set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR})
10
onqtam119cfb62017-04-17 10:46:55 +030011set_property(GLOBAL PROPERTY USE_FOLDERS ON)
12
onqtam4a655632016-05-26 14:20:52 +030013enable_testing()
14
onqtam56d91702017-04-16 21:03:58 +030015set(DOCTEST_TEST_MODE "COMPARE" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output")
16set_property(CACHE DOCTEST_TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE")
onqtam4a655632016-05-26 14:20:52 +030017
onqtamdb5eee92016-09-15 17:12:50 +030018# a custom version of add_test() to suite my needs
19function(doctest_add_test)
onqtam4a655632016-05-26 14:20:52 +030020 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 "")
onqtamdb5eee92016-09-15 17:12:50 +030022 message(FATAL_ERROR "doctest_add_test() called with wrong options!")
onqtam4a655632016-05-26 14:20:52 +030023 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 "")
onqtam56d91702017-04-16 21:03:58 +030029 if(${DOCTEST_TEST_MODE} STREQUAL "VALGRIND" AND NOT ARG_NO_VALGRIND)
onqtam4a655632016-05-26 14:20:52 +030030 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")
onqtam1fc3dc72017-03-14 14:30:09 +020038 # 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")
onqtam9f934f82016-08-02 12:42:19 +030040 # append the argument for ignoring the exit code of the test programs because some are intended to have failing tests
onqtam4a655632016-05-26 14:20:52 +030041 set(the_command "${the_command} --dt-no-exitcode=1")
42
43 string(STRIP ${the_command} the_command)
44
onqtam56d91702017-04-16 21:03:58 +030045 if(${DOCTEST_TEST_MODE} STREQUAL "COLLECT" OR ${DOCTEST_TEST_MODE} STREQUAL "COMPARE")
onqtam4a655632016-05-26 14:20:52 +030046 if(NOT ARG_NO_OUTPUT)
47 file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/)
onqtam56d91702017-04-16 21:03:58 +030048 set(the_test_mode ${DOCTEST_TEST_MODE})
onqtam4a655632016-05-26 14:20:52 +030049 list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt)
50 list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output.txt)
51 endif()
52 endif()
53
54 list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode})
55
onqtamdb5eee92016-09-15 17:12:50 +030056 add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake)
onqtam4a655632016-05-26 14:20:52 +030057endfunction()
58
onqtam61954c32017-04-16 17:22:44 +030059function(doctest_add_executable name)
60 add_executable(${name} ${ARGN})
61 add_dependencies(${name} assemble_single_header)
62endfunction()
63
64function(doctest_add_library name)
65 add_library(${name} ${ARGN})
66 add_dependencies(${name} assemble_single_header)
67endfunction()
68
onqtam4a655632016-05-26 14:20:52 +030069macro(add_compiler_flags)
70 foreach(flag ${ARGV})
71 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
72 endforeach()
73endmacro()
74
75if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
76 add_compiler_flags(-Werror)
onqtam4a655632016-05-26 14:20:52 +030077 add_compiler_flags(-pedantic)
78 add_compiler_flags(-pedantic-errors)
79 add_compiler_flags(-fvisibility=hidden)
80 add_compiler_flags(-fstrict-aliasing)
81endif()
82
83if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
onqtam4a655632016-05-26 14:20:52 +030084 add_compiler_flags(-Wall)
85 add_compiler_flags(-Wextra)
onqtam4a655632016-05-26 14:20:52 +030086 add_compiler_flags(-fdiagnostics-show-option)
87 add_compiler_flags(-Wconversion)
onqtam4a655632016-05-26 14:20:52 +030088 add_compiler_flags(-Wold-style-cast)
89 add_compiler_flags(-Wfloat-equal)
90 add_compiler_flags(-Wlogical-op)
91 add_compiler_flags(-Wundef)
92 add_compiler_flags(-Wredundant-decls)
93 add_compiler_flags(-Wshadow)
94 add_compiler_flags(-Wstrict-overflow=5)
95 add_compiler_flags(-Wwrite-strings)
96 add_compiler_flags(-Wpointer-arith)
97 add_compiler_flags(-Wcast-qual)
98 add_compiler_flags(-Wformat=2)
99 add_compiler_flags(-Wswitch-default)
100 add_compiler_flags(-Wmissing-include-dirs)
101 add_compiler_flags(-Wcast-align)
onqtam4a655632016-05-26 14:20:52 +0300102 add_compiler_flags(-Wswitch-enum)
onqtam4a655632016-05-26 14:20:52 +0300103 add_compiler_flags(-Wnon-virtual-dtor)
onqtam4a655632016-05-26 14:20:52 +0300104 add_compiler_flags(-Wctor-dtor-privacy)
onqtam4a655632016-05-26 14:20:52 +0300105 add_compiler_flags(-Wsign-conversion)
106 add_compiler_flags(-Wdisabled-optimization)
107 add_compiler_flags(-Weffc++)
108 add_compiler_flags(-Winline)
109 add_compiler_flags(-Winvalid-pch)
onqtam4a655632016-05-26 14:20:52 +0300110 add_compiler_flags(-Wmissing-declarations)
111 add_compiler_flags(-Woverloaded-virtual)
onqtam4a655632016-05-26 14:20:52 +0300112 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
113 add_compiler_flags(-Wnoexcept)
114 endif()
onqtamabf39d22017-10-28 21:30:45 +0300115
116 # no way to silence it in the expression decomposition macros: _Pragma() in macros doesn't work for the c++ front-end of g++
117 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55578
118 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69543
119 # Also the warning is completely worthless nowadays - http://stackoverflow.com/questions/14016993
120 #add_compiler_flags(-Waggregate-return)
onqtam4a655632016-05-26 14:20:52 +0300121
122 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
123 add_compiler_flags(-Wdouble-promotion)
124 add_compiler_flags(-Wtrampolines)
125 add_compiler_flags(-Wzero-as-null-pointer-constant)
126 add_compiler_flags(-Wuseless-cast)
127 add_compiler_flags(-Wvector-operation-performance)
onqtam4a655632016-05-26 14:20:52 +0300128 endif()
129
onqtamffb75302017-10-19 18:05:59 +0300130 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 6.0)
onqtam4a655632016-05-26 14:20:52 +0300131 add_compiler_flags(-Wshift-overflow=2)
132 add_compiler_flags(-Wnull-dereference)
133 add_compiler_flags(-Wduplicated-cond)
onqtam4a655632016-05-26 14:20:52 +0300134 endif()
onqtamf63c5102017-02-25 20:00:52 +0200135
onqtamffb75302017-10-19 18:05:59 +0300136 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
onqtamf63c5102017-02-25 20:00:52 +0200137 add_compiler_flags(-Walloc-zero)
138 add_compiler_flags(-Walloca)
onqtamffb75302017-10-19 18:05:59 +0300139 add_compiler_flags(-Wduplicated-branches)
140 add_compiler_flags(-Wrestrict)
onqtamf63c5102017-02-25 20:00:52 +0200141 endif()
onqtam4a655632016-05-26 14:20:52 +0300142endif()
143
144if(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
147endif()
148
Martin Moened8a7edc2017-08-01 17:08:08 +0200149if(MSVC)
onqtam8edeaaf2016-08-16 07:12:58 +0300150 add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC
onqtam50cbb802017-12-09 17:17:39 +0200151 add_compiler_flags(/permissive-) # force standard conformance - this is the better flag than /Za
onqtam4a655632016-05-26 14:20:52 +0300152 add_compiler_flags(/WX)
onqtamabf39d22017-10-28 21:30:45 +0300153 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
154
155 add_compiler_flags(
156 /wd4514 # unreferenced inline function has been removed
157 /wd4571 # SEH related
158 /wd4710 # function not inlined
159 /wd4711 # function 'x' selected for automatic inline expansion
onqtam5ed699b2017-10-28 21:57:15 +0300160
161 /wd4616 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/t7ab6xtd.aspx
162 /wd4619 # invalid compiler warnings - https://msdn.microsoft.com/en-us/library/tacee08d.aspx
163
164 #/wd4820 # padding in structs
165 #/wd4625 # copy constructor was implicitly defined as deleted
166 #/wd4626 # assignment operator was implicitly defined as deleted
167 #/wd5027 # move assignment operator was implicitly defined as deleted
168 #/wd5026 # move constructor was implicitly defined as deleted
169 #/wd4623 # default constructor was implicitly defined as deleted
onqtamabf39d22017-10-28 21:30:45 +0300170 )
onqtam4a655632016-05-26 14:20:52 +0300171endif()
onqtam7ffa84e2016-05-30 18:42:20 +0300172
173# add a custom target that assembles the single header when any of the parts are touched
174
onqtam08cf7dc2017-04-16 11:37:22 +0300175set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/")
176set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/parts/")
onqtam7ffa84e2016-05-30 18:42:20 +0300177if(WIN32)
onqtam7ffa84e2016-05-30 18:42:20 +0300178 STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder})
179 STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder})
180endif()
181
182add_custom_command(
183 OUTPUT ${doctest_include_folder}doctest.h
184 DEPENDS
185 ${doctest_parts_folder}doctest_fwd.h
186 ${doctest_parts_folder}doctest_impl.h
onqtamf720d432016-05-31 17:51:10 +0300187 COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake
onqtam7ffa84e2016-05-30 18:42:20 +0300188 COMMENT "assembling the single header")
189
190add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h)