File: CppcheckTargets.cmake

package info (click to toggle)
insighttoolkit5 5.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,404 kB
  • sloc: cpp: 783,697; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (298 lines) | stat: -rw-r--r-- 9,347 bytes parent folder | download | duplicates (2)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# - 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]) -
# Create a target to check a target's sources with cppcheck and the indicated options
# add_cppcheck_sources(<target-name> [UNUSED_FUNCTIONS] [STYLE] [POSSIBLE_ERROR] [FAIL_ON_WARNINGS]) -
# Create a target to check standalone sources with cppcheck and the indicated options
#
# Requires these CMake modules:
# Findcppcheck
#
# Requires CMake 3.3 or newer (uses the 'function' command and IN_LIST option)
#
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Addition:
# 2011 Arnaud Gelas <arnaud_gelas@hms.harvard.edu>
# Harvard Medical School
#
# 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
# https://www.boost.org/LICENSE_1_0.txt)

if(__add_cppcheck)
  return()
endif()
set(__add_cppcheck YES)

if(NOT CPPCHECK_FOUND)
  find_package(cppcheck QUIET)
endif()

if(CPPCHECK_FOUND)
  if(NOT TARGET all_cppcheck)
    add_custom_target(all_cppcheck)
    set_target_properties(all_cppcheck PROPERTIES EXCLUDE_FROM_ALL TRUE)
  endif()
endif()

# ------------------------------------------------------------------------------
macro(get_cppcheck_arg)

  if(FORCE IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_FORCE_ARG})
    list(REMOVE_ITEM _input FORCE)
  endif()

  if(VERBOSE IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_VERBOSE_ARG})
    list(REMOVE_ITEM _input VERBOSE)
  endif()

  if(QUIET IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_QUIET_ARG})
    list(REMOVE_ITEM _input QUIET)
  endif()

  if(ALL IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_ALL_ARG})
    list(REMOVE_ITEM _input ALL)
  endif()

  if(UNUSED_FUNCTIONS IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_UNUSEDFUNC_ARG})
    list(REMOVE_ITEM _input UNUSED_FUNCTIONS)
  endif()

  if(STYLE IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_STYLE_ARG})
    list(REMOVE_ITEM _input STYLE)
  endif()

  if(INFORMATION IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_INFORMATION_ARG})
    list(REMOVE_ITEM _input INFORMATION)
  endif()

  if(MISSING_INCLUDE IN_LIST _input)
    list(APPEND _cppcheck_args ${CPPCHECK_MISSING_INCLUDE_ARG})
    list(REMOVE_ITEM _input MISSING_INCLUDE)
  endif()

  if(FAIL_ON_WARNINGS IN_LIST _input)
    list(
      APPEND
      _cppcheck_args
      CPPCHECK_FAIL_REGULAR_EXPRESSION
      ${CPPCHECK_WARN_REGULAR_EXPRESSION})
    list(REMOVE_ITEM _input FAIL_ON_WARNINGS)
  endif()

endmacro()

# ------------------------------------------------------------------------------
# add_cppcheck_dir
function(
  add_cppcheck_dir
  _name
  _dir
  _include_dirs)
  if(CPPCHECK_FOUND)
    set(_cppcheck_args)
    set(_input ${ARGN})

    get_cppcheck_arg(${_input})

    # --------------------------------------------------------------
    foreach(_includeDirs ${_include_dirs})
      set(_cppcheck_include ${_cppcheck_include} -I${_includeDirs})
    endforeach()

    set(_cppcheck_compile_args ${_cppcheck_include})

    itk_add_test(
      NAME
      ${_name}CPPCheckTest
      COMMAND
      "${CPPCHECK_EXECUTABLE}"
      ${CPPCHECK_TEMPLATE_ARG}
      ${_cppcheck_args}
      ${_cppcheck_compile_args}
      ${_dir})

    set_tests_properties(${_name}CPPCheckTest PROPERTIES FAIL_REGULAR_EXPRESSION "${CPPCHECK_FAIL_REGULAR_EXPRESSION}")

    add_custom_command(
      TARGET all_cppcheck
      PRE_BUILD
      COMMAND ${CPPCHECK_EXECUTABLE} ${CPPCHECK_QUIET_ARG} ${CPPCHECK_TEMPLATE_ARG} ${_cppcheck_args}
              ${_cppcheck_compile_args} ${_dir}
      WORKING_DIRECTORY "${_dir}"
      COMMENT "${_name}_cppcheck: Running cppcheck on ${_dir}..."
      VERBATIM)
  endif()
endfunction()

# ------------------------------------------------------------------------------
# add_cppcheck_sources
function(add_cppcheck_sources _targetname)
  if(CPPCHECK_FOUND)
    # Normally --force should not be required, but since all compiler definitions
    # can't be detected, it is better to enforce testing all possibilities
    set(_cppcheck_args)
    set(_input ${ARGN})

    get_cppcheck_arg("${_input}")

    set(_files)
    foreach(_source ${_input})
      get_source_file_property(_cppcheck_loc "${_source}" LOCATION)
      if(_cppcheck_loc)
        # This file has a source file property, carry on.
        get_source_file_property(_cppcheck_lang "${_source}" LANGUAGE)
        if("${_cppcheck_lang}" MATCHES "CXX")
          list(APPEND _files "${_cppcheck_loc}")
        endif()
      else()
        # This file doesn't have source file properties - figure it out.
        get_filename_component(_cppcheck_loc "${_source}" ABSOLUTE)
        if(EXISTS "${_cppcheck_loc}")
          list(APPEND _files "${_cppcheck_loc}")
        else()
          message(
            FATAL_ERROR
              "Adding CPPCHECK for file target ${_targetname}: "
              "File ${_source} does not exist or needs a corrected path location "
              "since we think its absolute path is ${_cppcheck_loc}")
        endif()
      endif()
    endforeach()

    # let's take of include dirs here
    get_property(
      mytargINCLUDES
      DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      PROPERTY INCLUDE_DIRECTORIES)

    set(_cppcheck_include)
    foreach(_includeDirs ${mytargINCLUDES})
      set(_cppcheck_include ${_cppcheck_include} -I${_includeDirs})
    endforeach()

    # --------------------------------------------------------------
    # let's take of compile definitions here
    # NOTE: it does not work, you need to get all definitions by
    # another way
    get_property(
      mytargDEFINITIONS
      DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      PROPERTY COMPILE_DEFINITIONS)

    set(_cppcheck_def)
    foreach(_compiledef ${mytargDEFINITIONS})
      set(_cppcheck_def ${_cppcheck_def} -D${_compiledef})
    endforeach()

    # --------------------------------------------------------------
    set(_cppcheck_compile_args ${_cppcheck_include} ${_cppcheck_def})

    itk_add_test(
      NAME
      ${_targetname}CPPCheckTest
      COMMAND
      "${CPPCHECK_EXECUTABLE}"
      ${CPPCHECK_TEMPLATE_ARG}
      ${_cppcheck_args}
      ${_cppcheck_compile_args}
      ${_files})

    set_tests_properties(${_targetname}CPPCheckTest PROPERTIES FAIL_REGULAR_EXPRESSION
                                                               "${CPPCHECK_FAIL_REGULAR_EXPRESSION}")

    add_custom_command(
      TARGET all_cppcheck
      PRE_BUILD
      COMMAND ${CPPCHECK_EXECUTABLE} ${CPPCHECK_QUIET_ARG} ${CPPCHECK_TEMPLATE_ARG} ${_cppcheck_args}
              ${_cppcheck_compile_args} ${_files}
      WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      COMMENT "${_targetname}_cppcheck: Running cppcheck on target ${_targetname}..."
      VERBATIM)
  endif()
endfunction()

# ------------------------------------------------------------------------------
# add_cppcheck
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)
    set(_cppcheck_args)
    set(_input ${ARGN})

    get_cppcheck_arg(${_input})

    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")
        list(APPEND _files "${_cppcheck_loc}")
      endif()
    endforeach()

    # let's take of include dirs here
    get_property(
      mytargINCLUDES
      DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      PROPERTY INCLUDE_DIRECTORIES)

    set(_cppcheck_include)
    foreach(_includeDirs ${mytargINCLUDES})
      set(_cppcheck_include "${_cppcheck_include} -I ${_includeDirs}")
    endforeach()

    # --------------------------------------------------------------
    # let's take of compile definitions here
    get_property(
      mytargDEFINITIONS
      DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
      PROPERTY COMPILE_DEFINITIONS)

    set(_cppcheck_def)
    foreach(_compiledef ${mytargDEFINITIONS})
      set(_cppcheck_def ${_cppcheck_def} -D${_compiledef})
    endforeach()

    # --------------------------------------------------------------
    set(_cppcheck_compile_args --check-config ${_cppcheck_include} ${_cppcheck_def})

    itk_add_test(
      NAME
      ${_name}CPPCheckTest
      COMMAND
      "${CPPCHECK_EXECUTABLE}"
      ${CPPCHECK_TEMPLATE_ARG}
      ${_cppcheck_args}
      ${_cppcheck_compile_args}
      ${_files})

    set_tests_properties(${_name}CPPCheckTest PROPERTIES FAIL_REGULAR_EXPRESSION "${CPPCHECK_FAIL_REGULAR_EXPRESSION}")

    add_custom_command(
      TARGET all_cppcheck
      PRE_BUILD
      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)
  endif()

endfunction()