blob: e567c2baf30f2dd9c8d753a51452265d3418d236 [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001if(common_included)
2 return()
3endif()
4set(common_included true)
5
6include(CMakeParseArguments)
7
8# cache this for use inside of the function
9set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR})
10
11enable_testing()
12
13set(TEST_MODE "COMPARE" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output")
14set_property(CACHE TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE")
onqtam5cab8e92016-10-10 17:58:38 +030015option(WITH_CPP11 "With C++11 enabled" OFF)
onqtam4a655632016-05-26 14:20:52 +030016
onqtamdb5eee92016-09-15 17:12:50 +030017# a custom version of add_test() to suite my needs
18function(doctest_add_test)
onqtam4a655632016-05-26 14:20:52 +030019 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 "")
onqtamdb5eee92016-09-15 17:12:50 +030021 message(FATAL_ERROR "doctest_add_test() called with wrong options!")
onqtam4a655632016-05-26 14:20:52 +030022 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")
onqtam1fc3dc72017-03-14 14:30:09 +020037 # 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")
onqtam9f934f82016-08-02 12:42:19 +030039 # 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 +030040 set(the_command "${the_command} --dt-no-exitcode=1")
onqtam0b51b7b2016-11-08 14:27:30 +020041 # 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")
onqtam4a655632016-05-26 14:20:52 +030043
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
onqtamdb5eee92016-09-15 17:12:50 +030057 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 +030058endfunction()
59
60macro(add_compiler_flags)
61 foreach(flag ${ARGV})
62 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
63 endforeach()
64endmacro()
65
66if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
67 add_compiler_flags(-Werror)
onqtam4a655632016-05-26 14:20:52 +030068 add_compiler_flags(-pedantic)
69 add_compiler_flags(-pedantic-errors)
70 add_compiler_flags(-fvisibility=hidden)
71 add_compiler_flags(-fstrict-aliasing)
onqtam5cab8e92016-10-10 17:58:38 +030072
73 if(WITH_CPP11)
74 add_compiler_flags(-std=c++0x)
75 else()
76 add_compiler_flags(-std=c++98)
77 endif()
onqtam4a655632016-05-26 14:20:52 +030078endif()
79
80if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
onqtam4a655632016-05-26 14:20:52 +030081 add_compiler_flags(-Wall)
82 add_compiler_flags(-Wextra)
onqtam4a655632016-05-26 14:20:52 +030083 add_compiler_flags(-fdiagnostics-show-option)
84 add_compiler_flags(-Wconversion)
onqtam4a655632016-05-26 14:20:52 +030085 add_compiler_flags(-Wold-style-cast)
86 add_compiler_flags(-Wfloat-equal)
87 add_compiler_flags(-Wlogical-op)
88 add_compiler_flags(-Wundef)
89 add_compiler_flags(-Wredundant-decls)
90 add_compiler_flags(-Wshadow)
91 add_compiler_flags(-Wstrict-overflow=5)
92 add_compiler_flags(-Wwrite-strings)
93 add_compiler_flags(-Wpointer-arith)
94 add_compiler_flags(-Wcast-qual)
95 add_compiler_flags(-Wformat=2)
96 add_compiler_flags(-Wswitch-default)
97 add_compiler_flags(-Wmissing-include-dirs)
98 add_compiler_flags(-Wcast-align)
onqtam4a655632016-05-26 14:20:52 +030099 add_compiler_flags(-Wswitch-enum)
onqtam4a655632016-05-26 14:20:52 +0300100 add_compiler_flags(-Wnon-virtual-dtor)
onqtam4a655632016-05-26 14:20:52 +0300101 add_compiler_flags(-Wctor-dtor-privacy)
onqtam4a655632016-05-26 14:20:52 +0300102 add_compiler_flags(-Wsign-conversion)
103 add_compiler_flags(-Wdisabled-optimization)
104 add_compiler_flags(-Weffc++)
105 add_compiler_flags(-Winline)
106 add_compiler_flags(-Winvalid-pch)
onqtam4a655632016-05-26 14:20:52 +0300107 add_compiler_flags(-Wmissing-declarations)
108 add_compiler_flags(-Woverloaded-virtual)
onqtam4a655632016-05-26 14:20:52 +0300109 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
110 add_compiler_flags(-Wnoexcept)
111 endif()
112 #add_compiler_flags(-Waggregate-return) # GCC 4.8 does not silence this even with "#pragma GCC diagnostic ignored"
113
114 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
115 add_compiler_flags(-Wdouble-promotion)
116 add_compiler_flags(-Wtrampolines)
117 add_compiler_flags(-Wzero-as-null-pointer-constant)
118 add_compiler_flags(-Wuseless-cast)
119 add_compiler_flags(-Wvector-operation-performance)
onqtam4a655632016-05-26 14:20:52 +0300120 endif()
121
onqtamb382bd32016-08-01 20:53:56 +0300122 if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 6.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0)
onqtam4a655632016-05-26 14:20:52 +0300123 add_compiler_flags(-Wshift-overflow=2)
124 add_compiler_flags(-Wnull-dereference)
125 add_compiler_flags(-Wduplicated-cond)
onqtam4a655632016-05-26 14:20:52 +0300126 endif()
onqtamf63c5102017-02-25 20:00:52 +0200127
128 if(CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 7.0 OR CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
129 add_compiler_flags(-Walloc-zero)
130 add_compiler_flags(-Walloca)
131 endif()
onqtam4a655632016-05-26 14:20:52 +0300132endif()
133
134if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
135 add_compiler_flags(-Weverything)
136 add_compiler_flags(-Qunused-arguments -fcolor-diagnostics) # needed for ccache integration on travis
137endif()
138
139if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
onqtam8edeaaf2016-08-16 07:12:58 +0300140 add_compiler_flags(/std:c++latest) # for post c++14 updates in MSVC
onqtam4a655632016-05-26 14:20:52 +0300141 add_compiler_flags(/WX)
142 add_compiler_flags(/W4) # /Wall is too aggressive - even the standard C headers give thousands of errors...
143endif()
onqtam7ffa84e2016-05-30 18:42:20 +0300144
145# add a custom target that assembles the single header when any of the parts are touched
146
147set(cat_cmd "cat")
onqtam08cf7dc2017-04-16 11:37:22 +0300148set(doctest_include_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/")
149set(doctest_parts_folder "${CURRENT_LIST_DIR_CACHED}/../../doctest/parts/")
onqtam7ffa84e2016-05-30 18:42:20 +0300150if(WIN32)
151 set(cat_cmd "type")
152 STRING(REGEX REPLACE "/" "\\\\" doctest_include_folder ${doctest_include_folder})
153 STRING(REGEX REPLACE "/" "\\\\" doctest_parts_folder ${doctest_parts_folder})
154endif()
155
156add_custom_command(
157 OUTPUT ${doctest_include_folder}doctest.h
158 DEPENDS
159 ${doctest_parts_folder}doctest_fwd.h
160 ${doctest_parts_folder}doctest_impl.h
onqtamf720d432016-05-31 17:51:10 +0300161 COMMAND ${CMAKE_COMMAND} -P ${CURRENT_LIST_DIR_CACHED}/asemble_single_header.cmake
onqtam7ffa84e2016-05-30 18:42:20 +0300162 COMMENT "assembling the single header")
163
164add_custom_target(assemble_single_header ALL DEPENDS ${doctest_include_folder}doctest.h)
onqtam29e62602016-05-30 19:45:10 +0300165
166# override add_executable() to add a dependency on the header assembly target
167function(add_executable name)
168 _add_executable(${name} ${ARGN})
169 add_dependencies(${name} assemble_single_header)
170endfunction()
171
172# override add_library() to add a dependency on the header assembly target
173function(add_library name)
174 _add_library(${name} ${ARGN})
175 add_dependencies(${name} assemble_single_header)
176endfunction()