blob: ad267b07b78d4da91fd5a56df32271608fa8fd3b [file] [log] [blame]
onqtamfa113992016-03-18 18:41:25 +02001if(common_included)
hardlyb1e7e142014-08-06 00:43:51 +03002 return()
3endif()
onqtamfa113992016-03-18 18:41:25 +02004set(common_included true)
5
6include(CMakeParseArguments)
hardlyb1e7e142014-08-06 00:43:51 +03007
onqtam4cdf5d82016-03-15 16:25:19 +02008# cache this for use inside of the function
9set(CURRENT_LIST_DIR_CACHED ${CMAKE_CURRENT_LIST_DIR})
10
onqtam222ecad2016-03-15 11:54:37 +020011enable_testing()
12
13set(TEST_MODE "NORMAL" CACHE STRING "Test mode - normal/run through valgrind/collect output/compare with output")
14set_property(CACHE TEST_MODE PROPERTY STRINGS "NORMAL;VALGRIND;COLLECT;COMPARE")
15
16# add a customized overloaded version of add_test() to suite my needs
17function(add_test)
onqtam8ccce3b2016-04-20 20:46:05 +030018 cmake_parse_arguments(ARG "NO_VALGRIND;NO_OUTPUT" "NAME" "COMMAND" ${ARGN})
onqtam222ecad2016-03-15 11:54:37 +020019 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()
onqtamaa605c12016-04-21 16:34:04 +030034 # 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-in-filenames=1")
36
onqtam222ecad2016-03-15 11:54:37 +020037 string(STRIP ${the_command} the_command)
38
onqtam222ecad2016-03-15 11:54:37 +020039 if(${TEST_MODE} STREQUAL "COLLECT" OR ${TEST_MODE} STREQUAL "COMPARE")
40 if(NOT ARG_NO_OUTPUT)
41 file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test_output/)
42 set(the_test_mode ${TEST_MODE})
onqtam4cdf5d82016-03-15 16:25:19 +020043 list(APPEND ADDITIONAL_FLAGS -DTEST_OUTPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/test_output/${ARG_NAME}.txt)
44 list(APPEND ADDITIONAL_FLAGS -DTEST_TEMP_FILE=${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/temp_test_output.txt)
onqtam222ecad2016-03-15 11:54:37 +020045 endif()
46 endif()
47
48 list(APPEND ADDITIONAL_FLAGS -DTEST_MODE=${the_test_mode})
49
onqtam4cdf5d82016-03-15 16:25:19 +020050 _add_test(NAME ${ARG_NAME} COMMAND ${CMAKE_COMMAND} -DCOMMAND=${the_command} ${ADDITIONAL_FLAGS} -P ${CURRENT_LIST_DIR_CACHED}/exec_test.cmake)
onqtam222ecad2016-03-15 11:54:37 +020051endfunction()
52
hardlyb1e7e142014-08-06 00:43:51 +030053macro(add_compiler_flags)
54 foreach(flag ${ARGV})
55 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
56 endforeach()
57endmacro()
58
59if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
onqtamf3a680f2016-04-30 03:15:07 +030060 add_compiler_flags(-Werror)
hardlyb1e7e142014-08-06 00:43:51 +030061 add_compiler_flags(-std=c++98)
62 add_compiler_flags(-pedantic)
63 add_compiler_flags(-pedantic-errors)
64 add_compiler_flags(-fvisibility=hidden)
65 add_compiler_flags(-fstrict-aliasing)
66endif()
67
68if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
hardlyb1e7e142014-08-06 00:43:51 +030069 add_compiler_flags(-ansi)
70 add_compiler_flags(-Wall)
71 add_compiler_flags(-Wextra)
onqtam50af6672016-05-01 18:55:52 +030072 add_compiler_flags(-fstack-protector-all)
73 add_compiler_flags(-funsafe-loop-optimizations)
74 add_compiler_flags(-fdiagnostics-show-option)
hardlyb1e7e142014-08-06 00:43:51 +030075 add_compiler_flags(-Wconversion)
76 add_compiler_flags(-Wno-missing-field-initializers)
77 add_compiler_flags(-Wold-style-cast)
78 add_compiler_flags(-Wfloat-equal)
79 add_compiler_flags(-Wlogical-op)
80 add_compiler_flags(-Wundef)
81 add_compiler_flags(-Wredundant-decls)
82 add_compiler_flags(-Wshadow)
83 add_compiler_flags(-Wstrict-overflow=5)
84 add_compiler_flags(-Wwrite-strings)
85 add_compiler_flags(-Wpointer-arith)
86 add_compiler_flags(-Wcast-qual)
87 add_compiler_flags(-Wformat=2)
88 add_compiler_flags(-Wswitch-default)
89 add_compiler_flags(-Wmissing-include-dirs)
90 add_compiler_flags(-Wcast-align)
91 add_compiler_flags(-Wformat-nonliteral)
92 add_compiler_flags(-Wparentheses)
93 add_compiler_flags(-Winit-self)
94 add_compiler_flags(-Wuninitialized)
95 add_compiler_flags(-Wswitch-enum)
96 add_compiler_flags(-Wno-endif-labels)
97 add_compiler_flags(-Wunused-function)
98 add_compiler_flags(-Wnon-virtual-dtor)
99 add_compiler_flags(-Wno-pmf-conversions)
100 add_compiler_flags(-Wctor-dtor-privacy)
101 add_compiler_flags(-Wsign-promo)
onqtam1cda0962016-04-19 17:40:43 +0300102 add_compiler_flags(-Wsign-conversion)
hardlyb1e7e142014-08-06 00:43:51 +0300103 add_compiler_flags(-Wdisabled-optimization)
hardlyb1e7e142014-08-06 00:43:51 +0300104 add_compiler_flags(-Weffc++)
105 add_compiler_flags(-Winline)
106 add_compiler_flags(-Winvalid-pch)
107 add_compiler_flags(-Wstack-protector)
108 add_compiler_flags(-Wunsafe-loop-optimizations)
109 add_compiler_flags(-Wmissing-declarations)
110 add_compiler_flags(-Woverloaded-virtual)
onqtam1cda0962016-04-19 17:40:43 +0300111 add_compiler_flags(-Wstrict-null-sentinel)
onqtam336f95e2016-04-20 00:31:51 +0300112 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
113 add_compiler_flags(-Wnoexcept)
114 endif()
onqtamfe5482f2016-04-30 03:24:20 +0300115 #add_compiler_flags(-Waggregate-return) # GCC 4.8 does not silence this even with "#pragma GCC diagnostic ignored"
hardlyb1e7e142014-08-06 00:43:51 +0300116
117 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
118 add_compiler_flags(-Wdouble-promotion)
119 add_compiler_flags(-Wtrampolines)
120 add_compiler_flags(-Wzero-as-null-pointer-constant)
121 add_compiler_flags(-Wuseless-cast)
122 add_compiler_flags(-Wvector-operation-performance)
123 add_compiler_flags(-Wsized-deallocation)
124 endif()
125
126 if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.3)
127 add_compiler_flags(-Wshift-overflow=2)
128 add_compiler_flags(-Wnull-dereference)
129 add_compiler_flags(-Wduplicated-cond)
onqtam6b7eb052016-03-18 12:43:55 +0200130 add_compiler_flags(-Wmisleading-indentation)
131 add_compiler_flags(-Wshift-negative-value)
hardlyb1e7e142014-08-06 00:43:51 +0300132 endif()
133endif()
134
135if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
136 add_compiler_flags(-Weverything)
137endif()
138
139if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
onqtamf3a680f2016-04-30 03:15:07 +0300140 add_compiler_flags(/WX)
hardlyb1e7e142014-08-06 00:43:51 +0300141 add_compiler_flags(/W4) # /Wall is too aggressive - even the standard C headers give thousands of errors...
142endif()