forked from Eyescale/CMake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonCPPCheck.cmake
118 lines (101 loc) · 3.91 KB
/
CommonCPPCheck.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
# - Run cppcheck on c++ source files as a custom target and a test
#
# include(CppcheckTargets)
# common_cppcheck(<target-name> [FILES] [UNUSED_FUNCTIONS] [STYLE]
# [POSSIBLE_ERROR] [FAIL_ON_WARNINGS]
# [EXCLUDE_PATTERN]) -
# Create a target to check a target's sources with cppcheck and the
# indicated options
#
# Requires these CMake modules:
# Findcppcheck
#
# Accepts the following input variables:
# * CPPCHECK_EXTRA_ARGS for additional command line parameters to cppcheck
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# 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(TARGET ${PROJECT_NAME}-cppcheck)
return()
endif()
include(CMakeParseArguments)
include(GetSourceFilesFromTarget)
if(NOT CPPCHECK_FOUND)
find_package(cppcheck 1.66 QUIET)
endif()
if(NOT CPPCHECK_FOUND)
add_custom_target(${PROJECT_NAME}-cppcheck
COMMENT "cppcheck executable not found")
set_target_properties(${PROJECT_NAME}-cppcheck PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cppcheck)
endif()
if(NOT TARGET cppcheck)
add_custom_target(cppcheck)
set_target_properties(cppcheck PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON)
endif()
function(common_cppcheck _name)
if(NOT CPPCHECK_FOUND)
return()
endif()
if(CPPCHECK_IGNORED_PATHS)
string(REPLACE " " " -i" _ignored_paths ${CPPCHECK_IGNORED_PATHS})
set(CPPCHECK_IGNORED_PATHS -i${_ignored_paths})
endif()
set(_cppcheck_args ${CPPCHECK_IGNORED_PATHS} --error-exitcode=2
--inline-suppr --suppress=unusedFunction --suppress=unmatchedSuppression
--suppress=missingInclude --suppress=preprocessorErrorDirective
${CPPCHECK_EXTRA_ARGS})
set(oneValueArgs UNUSED_FUNCTIONS STYLE POSSIBLE_ERROR FAIL_ON_WARNINGS EXCLUDE_PATTERN)
set(multiValueArgs FILES)
cmake_parse_arguments(common_cppcheck "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT common_cppcheck_EXCLUDE_PATTERN)
set(common_cppcheck_EXCLUDE_PATTERN "^$") # Empty string regex
endif()
if(common_cppcheck_UNUSED_FUNCTIONS)
list(APPEND _cppcheck_args ${CPPCHECK_UNUSEDFUNC_ARG})
endif()
if(common_cppcheck_STYLE)
list(APPEND _cppcheck_args ${CPPCHECK_STYLE_ARG})
endif()
if(common_cppcheck_POSSIBLE_ERROR)
list(APPEND _cppcheck_args ${CPPCHECK_POSSIBLEERROR_ARG})
endif()
if(common_cppcheck_FAIL_ON_WARNINGS)
list(APPEND CPPCHECK_FAIL_REGULAR_EXPRESSION
${CPPCHECK_WARN_REGULAR_EXPRESSION})
endif()
set(_files ${common_cppcheck_FILES})
if(NOT _files)
get_source_files(${_name} ${common_cppcheck_EXCLUDE_PATTERN})
if(NOT ${_name}_FILES) # nothing to check
return()
endif()
set(_files ${${_name}_FILES})
endif()
add_test(NAME ${_name}-cppcheck
COMMAND ${CPPCHECK_EXECUTABLE} ${CPPCHECK_QUIET_ARG}
${CPPCHECK_TEMPLATE_ARG} ${_cppcheck_args} ${_files}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set_tests_properties(${_name}-cppcheck PROPERTIES LABELS ${PROJECT_NAME}-unit)
add_custom_target(${_name}-runcppcheck
COMMAND ${CPPCHECK_EXECUTABLE} ${CPPCHECK_QUIET_ARG}
${CPPCHECK_TEMPLATE_ARG} ${_cppcheck_args} ${_files}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
set_target_properties(${_name}-runcppcheck PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cppcheck)
if(NOT TARGET ${PROJECT_NAME}-cppcheck)
add_custom_target(${PROJECT_NAME}-cppcheck)
set_target_properties(${PROJECT_NAME}-cppcheck PROPERTIES
EXCLUDE_FROM_DEFAULT_BUILD ON FOLDER ${PROJECT_NAME}/tests/cppcheck)
endif()
add_dependencies(${PROJECT_NAME}-cppcheck ${_name}-runcppcheck)
add_dependencies(cppcheck ${PROJECT_NAME}-cppcheck)
endfunction()