Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 214 lines (192 sloc) 6.025 kB
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
1 # - Run cppcheck on c++ source files as a custom target and a test
2 #
3 # include(CppcheckTargets)
06c0b29 @rpavlik Update the cppcheck scripts
authored
4 # add_cppcheck(<target-name> [UNUSED_FUNCTIONS] [STYLE] [POSSIBLE_ERROR] [FAIL_ON_WARNINGS]) -
92c3711 @rpavlik Update cmake
authored
5 # Create a target to check a target's sources with cppcheck and the indicated options
06c0b29 @rpavlik Update the cppcheck scripts
authored
6 # add_cppcheck_sources(<target-name> [UNUSED_FUNCTIONS] [STYLE] [POSSIBLE_ERROR] [FAIL_ON_WARNINGS]) -
92c3711 @rpavlik Update cmake
authored
7 # Create a target to check standalone sources with cppcheck and the indicated options
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
8 #
9 # Requires these CMake modules:
10 # Findcppcheck
11 #
12 # Requires CMake 2.6 or newer (uses the 'function' command)
13 #
14 # Original Author:
15 # 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
16 # http://academic.cleardefinition.com
17 # Iowa State University HCI Graduate Program/VRAC
afeb216 @rpavlik Put BSL 1.0 license notice on all cmake files
authored
18 #
19 # Copyright Iowa State University 2009-2010
20 # Distributed under the Boost Software License, Version 1.0.
21 # (See accompanying file LICENSE_1_0.txt or copy at
22 # http://www.boost.org/LICENSE_1_0.txt)
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
23
24 if(__add_cppcheck)
25 return()
26 endif()
27 set(__add_cppcheck YES)
28
29 if(NOT CPPCHECK_FOUND)
30 find_package(cppcheck QUIET)
31 endif()
32
33 if(CPPCHECK_FOUND)
34 if(NOT TARGET all_cppcheck)
35 add_custom_target(all_cppcheck)
36 set_target_properties(all_cppcheck PROPERTIES EXCLUDE_FROM_ALL TRUE)
37 endif()
38 endif()
39
92c3711 @rpavlik Update cmake
authored
40 function(add_cppcheck_sources _targetname)
41 if(CPPCHECK_FOUND)
42 set(_cppcheck_args)
43 set(_input ${ARGN})
44 list(FIND _input UNUSED_FUNCTIONS _unused_func)
45 if("${_unused_func}" GREATER "-1")
46 list(APPEND _cppcheck_args ${CPPCHECK_UNUSEDFUNC_ARG})
47 list(REMOVE_AT _input ${_unused_func})
48 endif()
49
50 list(FIND _input STYLE _style)
51 if("${_style}" GREATER "-1")
52 list(APPEND _cppcheck_args ${CPPCHECK_STYLE_ARG})
53 list(REMOVE_AT _input ${_style})
54 endif()
55
56 list(FIND _input POSSIBLE_ERROR _poss_err)
57 if("${_poss_err}" GREATER "-1")
58 list(APPEND _cppcheck_args ${CPPCHECK_POSSIBLEERROR_ARG})
59 list(REMOVE_AT _input ${_poss_err})
60 endif()
61
06c0b29 @rpavlik Update the cppcheck scripts
authored
62 list(FIND _input FAIL_ON_WARNINGS _fail_on_warn)
63 if("${_fail_on_warn}" GREATER "-1")
032daa1 @rpavlik Run the decrufter on it all
authored
64 list(APPEND
65 CPPCHECK_FAIL_REGULAR_EXPRESSION
66 ${CPPCHECK_WARN_REGULAR_EXPRESSION})
06c0b29 @rpavlik Update the cppcheck scripts
authored
67 list(REMOVE_AT _input ${_unused_func})
68 endif()
69
92c3711 @rpavlik Update cmake
authored
70 set(_files)
71 foreach(_source ${_input})
72 get_source_file_property(_cppcheck_loc "${_source}" LOCATION)
73 if(_cppcheck_loc)
74 # This file has a source file property, carry on.
75 get_source_file_property(_cppcheck_lang "${_source}" LANGUAGE)
76 if("${_cppcheck_lang}" MATCHES "CXX")
77 list(APPEND _files "${_cppcheck_loc}")
78 endif()
79 else()
80 # This file doesn't have source file properties - figure it out.
81 get_filename_component(_cppcheck_loc "${_source}" ABSOLUTE)
82 if(EXISTS "${_cppcheck_loc}")
83 list(APPEND _files "${_cppcheck_loc}")
84 else()
032daa1 @rpavlik Run the decrufter on it all
authored
85 message(FATAL_ERROR
86 "Adding CPPCHECK for file target ${_targetname}: "
92c3711 @rpavlik Update cmake
authored
87 "File ${_source} does not exist or needs a corrected path location "
88 "since we think its absolute path is ${_cppcheck_loc}")
89 endif()
90 endif()
91 endforeach()
92
93 if("1.${CMAKE_VERSION}" VERSION_LESS "1.2.8.0")
94 # Older than CMake 2.8.0
95 add_test(${_targetname}_cppcheck_test
96 "${CPPCHECK_EXECUTABLE}"
97 ${CPPCHECK_TEMPLATE_ARG}
98 ${_cppcheck_args}
99 ${_files})
100 else()
101 # CMake 2.8.0 and newer
102 add_test(NAME
103 ${_targetname}_cppcheck_test
104 COMMAND
105 "${CPPCHECK_EXECUTABLE}"
106 ${CPPCHECK_TEMPLATE_ARG}
107 ${_cppcheck_args}
108 ${_files})
109 endif()
110
111 set_tests_properties(${_targetname}_cppcheck_test
112 PROPERTIES
113 FAIL_REGULAR_EXPRESSION
06c0b29 @rpavlik Update the cppcheck scripts
authored
114 "${CPPCHECK_FAIL_REGULAR_EXPRESSION}")
92c3711 @rpavlik Update cmake
authored
115
116 add_custom_command(TARGET
117 all_cppcheck
118 PRE_BUILD
119 COMMAND
120 ${CPPCHECK_EXECUTABLE}
121 ${CPPCHECK_QUIET_ARG}
122 ${CPPCHECK_TEMPLATE_ARG}
123 ${_cppcheck_args}
124 ${_files}
125 WORKING_DIRECTORY
126 "${CMAKE_CURRENT_SOURCE_DIR}"
127 COMMENT
128 "${_targetname}_cppcheck: Running cppcheck on target ${_targetname}..."
129 VERBATIM)
130 endif()
131 endfunction()
132
133 function(add_cppcheck _name)
d8743b9 @rpavlik Fix cppcheck module
authored
134 if(NOT TARGET ${_name})
032daa1 @rpavlik Run the decrufter on it all
authored
135 message(FATAL_ERROR
136 "add_cppcheck given a target name that does not exist: '${_name}' !")
d8743b9 @rpavlik Fix cppcheck module
authored
137 endif()
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
138 if(CPPCHECK_FOUND)
139 set(_cppcheck_args)
140
141 list(FIND ARGN UNUSED_FUNCTIONS _unused_func)
142 if("${_unused_func}" GREATER "-1")
143 list(APPEND _cppcheck_args ${CPPCHECK_UNUSEDFUNC_ARG})
144 endif()
145
146 list(FIND ARGN STYLE _style)
147 if("${_style}" GREATER "-1")
148 list(APPEND _cppcheck_args ${CPPCHECK_STYLE_ARG})
149 endif()
150
151 list(FIND ARGN POSSIBLE_ERROR _poss_err)
152 if("${_poss_err}" GREATER "-1")
153 list(APPEND _cppcheck_args ${CPPCHECK_POSSIBLEERROR_ARG})
154 endif()
155
06c0b29 @rpavlik Update the cppcheck scripts
authored
156 list(FIND _input FAIL_ON_WARNINGS _fail_on_warn)
157 if("${_fail_on_warn}" GREATER "-1")
032daa1 @rpavlik Run the decrufter on it all
authored
158 list(APPEND
159 CPPCHECK_FAIL_REGULAR_EXPRESSION
160 ${CPPCHECK_WARN_REGULAR_EXPRESSION})
06c0b29 @rpavlik Update the cppcheck scripts
authored
161 list(REMOVE_AT _input ${_unused_func})
162 endif()
163
d8743b9 @rpavlik Fix cppcheck module
authored
164 get_target_property(_cppcheck_sources "${_name}" SOURCES)
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
165 set(_files)
166 foreach(_source ${_cppcheck_sources})
167 get_source_file_property(_cppcheck_lang "${_source}" LANGUAGE)
168 get_source_file_property(_cppcheck_loc "${_source}" LOCATION)
169 if("${_cppcheck_lang}" MATCHES "CXX")
170 list(APPEND _files "${_cppcheck_loc}")
171 endif()
172 endforeach()
173
174 if("1.${CMAKE_VERSION}" VERSION_LESS "1.2.8.0")
175 # Older than CMake 2.8.0
d8743b9 @rpavlik Fix cppcheck module
authored
176 add_test(${_name}_cppcheck_test
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
177 "${CPPCHECK_EXECUTABLE}"
178 ${CPPCHECK_TEMPLATE_ARG}
179 ${_cppcheck_args}
180 ${_files})
181 else()
182 # CMake 2.8.0 and newer
183 add_test(NAME
d8743b9 @rpavlik Fix cppcheck module
authored
184 ${_name}_cppcheck_test
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
185 COMMAND
186 "${CPPCHECK_EXECUTABLE}"
187 ${CPPCHECK_TEMPLATE_ARG}
188 ${_cppcheck_args}
189 ${_files})
190 endif()
191
d8743b9 @rpavlik Fix cppcheck module
authored
192 set_tests_properties(${_name}_cppcheck_test
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
193 PROPERTIES
194 FAIL_REGULAR_EXPRESSION
06c0b29 @rpavlik Update the cppcheck scripts
authored
195 "${CPPCHECK_FAIL_REGULAR_EXPRESSION}")
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
196
197 add_custom_command(TARGET
198 all_cppcheck
199 PRE_BUILD
200 COMMAND
201 ${CPPCHECK_EXECUTABLE}
202 ${CPPCHECK_QUIET_ARG}
203 ${CPPCHECK_TEMPLATE_ARG}
204 ${_cppcheck_args}
205 ${_files}
206 WORKING_DIRECTORY
207 "${CMAKE_CURRENT_SOURCE_DIR}"
208 COMMENT
d8743b9 @rpavlik Fix cppcheck module
authored
209 "${_name}_cppcheck: Running cppcheck on target ${_name}..."
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
210 VERBATIM)
211 endif()
92c3711 @rpavlik Update cmake
authored
212
bc08abb @rpavlik Add cmake build files, fix config - it builds and links!
authored
213 endfunction()
Something went wrong with that request. Please try again.