forked from Eyescale/CMake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CppcheckTargets.cmake
126 lines (108 loc) · 3.93 KB
/
CppcheckTargets.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# - Run cppcheck on c++ source files as a custom target and a test
#
# include(CppcheckTargets)
# add_cppcheck(<target-name> [UNUSED_FUNCTIONS] [STYLE] [POSSIBLE_ERROR]
# [FAIL_ON_WARNINGS] [EXCLUDE_QT_MOC_FILES]) -
# Create a target to check a target's sources with cppcheck and the
# indicated options
#
# Requires these CMake modules:
# Findcppcheck
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
if(__add_cppcheck)
return()
endif()
set(__add_cppcheck YES)
if(NOT CPPCHECK_FOUND)
find_package(cppcheck 1.61)
endif()
if(NOT CPPCHECK_FOUND)
add_custom_target(cppcheck_${PROJECT_NAME}
COMMENT "cppcheck executable not found")
set_target_properties(cppcheck_${PROJECT_NAME} PROPERTIES EXCLUDE_FROM_ALL TRUE)
endif()
if(NOT TARGET cppcheck_${PROJECT_NAME})
add_custom_target(cppcheck_${PROJECT_NAME})
endif()
if(NOT TARGET cppcheck)
add_custom_target(cppcheck DEPENDS cppcheck_${PROJECT_NAME})
endif()
function(add_cppcheck _name)
if(NOT TARGET ${_name})
message(FATAL_ERROR
"add_cppcheck given a target name that does not exist: '${_name}' !")
endif()
if(CPPCHECK_FOUND)
if (CPPCHECK_IGNORED_PATHS)
string(REPLACE " " " -i" _ignored_paths ${CPPCHECK_IGNORED_PATHS})
set(CPPCHECK_IGNORED_PATHS -i${_ignored_paths})
endif(CPPCHECK_IGNORED_PATHS)
set(_cppcheck_args ${CPPCHECK_IGNORED_PATHS} -I ${PROJECT_SOURCE_DIR}
--error-exitcode=2 --inline-suppr
--suppress=unmatchedSuppression --suppress=preprocessorErrorDirective
${CPPCHECK_EXTRA_ARGS})
list(FIND ARGN UNUSED_FUNCTIONS _unused_func)
if("${_unused_func}" GREATER "-1")
list(APPEND _cppcheck_args ${CPPCHECK_UNUSEDFUNC_ARG})
endif()
list(FIND ARGN STYLE _style)
if("${_style}" GREATER "-1")
list(APPEND _cppcheck_args ${CPPCHECK_STYLE_ARG})
endif()
list(FIND ARGN POSSIBLE_ERROR _poss_err)
if("${_poss_err}" GREATER "-1")
list(APPEND _cppcheck_args ${CPPCHECK_POSSIBLEERROR_ARG})
endif()
list(FIND _input FAIL_ON_WARNINGS _fail_on_warn)
if("${_fail_on_warn}" GREATER "-1")
list(APPEND CPPCHECK_FAIL_REGULAR_EXPRESSION
${CPPCHECK_WARN_REGULAR_EXPRESSION})
endif()
list(FIND ARGN EXCLUDE_QT_MOC_FILES _exclude_moc_files)
if("${_exclude_moc_files}" GREATER "-1")
SET(_exclude_pattern ".*moc_.*\\.cxx$")
endif()
get_target_property(_cppcheck_sources "${_name}" SOURCES)
set(_files)
foreach(_source ${_cppcheck_sources})
get_source_file_property(_cppcheck_lang "${_source}" LANGUAGE)
get_source_file_property(_cppcheck_loc "${_source}" LOCATION)
if("${_cppcheck_lang}" MATCHES "CXX" AND NOT ${_cppcheck_loc} MATCHES ${_exclude_pattern})
list(APPEND _files "${_cppcheck_loc}")
endif()
endforeach()
if(NOT _files) # nothing to check
return()
endif()
add_test(NAME ${_name}_cppcheck_test
COMMAND "${CPPCHECK_EXECUTABLE}" ${CPPCHECK_TEMPLATE_ARG}
${_cppcheck_args} ${_files}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set_tests_properties(${_name}_cppcheck_test
PROPERTIES
FAIL_REGULAR_EXPRESSION
"${CPPCHECK_FAIL_REGULAR_EXPRESSION}")
add_custom_target(${_name}_cppcheck
COMMAND
${CPPCHECK_EXECUTABLE}
${CPPCHECK_QUIET_ARG}
${CPPCHECK_TEMPLATE_ARG}
${_cppcheck_args}
${_files}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "${_name}_cppcheck: Running cppcheck on target ${_name}..."
VERBATIM)
add_dependencies(cppcheck_${PROJECT_NAME} ${_name}_cppcheck)
endif()
endfunction()