generated from cpp-best-practices/cmake_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticAnalyzers.cmake
112 lines (100 loc) · 3.78 KB
/
StaticAnalyzers.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
macro(cpp_template_enable_cppcheck WARNINGS_AS_ERRORS CPPCHECK_OPTIONS)
find_program(CPPCHECK cppcheck)
if(CPPCHECK)
if(CMAKE_GENERATOR MATCHES ".*Visual Studio.*")
set(CPPCHECK_TEMPLATE "vs")
else()
set(CPPCHECK_TEMPLATE "gcc")
endif()
if("${CPPCHECK_OPTIONS}" STREQUAL "")
# Enable all warnings that are actionable by the user of this toolset
# style should enable the other 3, but we'll be explicit just in case
set(SUPPRESS_DIR "*:${CMAKE_CURRENT_BINARY_DIR}/_deps/*.h")
message(STATUS "CPPCHECK_OPTIONS suppress: ${SUPPRESS_DIR}")
set(CMAKE_CXX_CPPCHECK
${CPPCHECK}
--template=${CPPCHECK_TEMPLATE}
--enable=style,performance,warning,portability
--inline-suppr
# We cannot act on a bug/missing feature of cppcheck
--suppress=cppcheckError
--suppress=internalAstError
# if a file does not have an internalAstError, we get an unmatchedSuppression error
--suppress=unmatchedSuppression
# noisy and incorrect sometimes
--suppress=passedByValue
# ignores code that cppcheck thinks is invalid C++
--suppress=syntaxError
--suppress=preprocessorErrorDirective
--inconclusive
--suppress=${SUPPRESS_DIR})
else()
# if the user provides a CPPCHECK_OPTIONS with a template specified, it will override this template
set(CMAKE_CXX_CPPCHECK ${CPPCHECK} --template=${CPPCHECK_TEMPLATE} ${CPPCHECK_OPTIONS})
endif()
if(NOT
"${CMAKE_CXX_STANDARD}"
STREQUAL
"")
set(CMAKE_CXX_CPPCHECK ${CMAKE_CXX_CPPCHECK} --std=c++${CMAKE_CXX_STANDARD})
endif()
if(${WARNINGS_AS_ERRORS})
list(APPEND CMAKE_CXX_CPPCHECK --error-exitcode=2)
endif()
else()
message(${WARNING_MESSAGE} "cppcheck requested but executable not found")
endif()
endmacro()
macro(cpp_template_enable_clang_tidy target WARNINGS_AS_ERRORS)
find_program(CLANGTIDY clang-tidy)
if(CLANGTIDY)
if(NOT
CMAKE_CXX_COMPILER_ID
MATCHES
".*Clang")
get_target_property(TARGET_PCH ${target} INTERFACE_PRECOMPILE_HEADERS)
if("${TARGET_PCH}" STREQUAL "TARGET_PCH-NOTFOUND")
get_target_property(TARGET_PCH ${target} PRECOMPILE_HEADERS)
endif()
if(NOT ("${TARGET_PCH}" STREQUAL "TARGET_PCH-NOTFOUND"))
message(
SEND_ERROR
"clang-tidy cannot be enabled with non-clang compiler and PCH, clang-tidy fails to handle gcc's PCH file")
endif()
endif()
# construct the clang-tidy command line
set(CLANG_TIDY_OPTIONS
${CLANGTIDY}
-extra-arg=-Wno-unknown-warning-option
-extra-arg=-Wno-ignored-optimization-argument
-extra-arg=-Wno-unused-command-line-argument
-p)
# set standard
if(NOT
"${CMAKE_CXX_STANDARD}"
STREQUAL
"")
if("${CLANG_TIDY_OPTIONS_DRIVER_MODE}" STREQUAL "cl")
set(CLANG_TIDY_OPTIONS ${CLANG_TIDY_OPTIONS} -extra-arg=/std:c++${CMAKE_CXX_STANDARD})
else()
set(CLANG_TIDY_OPTIONS ${CLANG_TIDY_OPTIONS} -extra-arg=-std=c++${CMAKE_CXX_STANDARD})
endif()
endif()
# set warnings as errors
if(${WARNINGS_AS_ERRORS})
list(APPEND CLANG_TIDY_OPTIONS -warnings-as-errors=*)
endif()
message("Also setting clang-tidy globally")
set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_OPTIONS})
else()
message(${WARNING_MESSAGE} "clang-tidy requested but executable not found")
endif()
endmacro()
macro(cpp_template_enable_include_what_you_use)
find_program(INCLUDE_WHAT_YOU_USE include-what-you-use)
if(INCLUDE_WHAT_YOU_USE)
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${INCLUDE_WHAT_YOU_USE})
else()
message(${WARNING_MESSAGE} "include-what-you-use requested but executable not found")
endif()
endmacro()