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
|
# - Find cppcheck
# This module looks for cppcheck.
# This module defines the following variables:
#
# CPPCHECK_FOUND - Set to TRUE when cppcheck is found.
# CPPCHECK_EXECUTABLE - Path to the executable.
#
# and a convenience function for calling the utility:
#
# GENERATE_CPPCHECK(SOURCES <sources to check...>
# [SUPPRESSION_FILE <file>]
# [ENABLE_IDS <id...>]
# [TARGET_NAME <name>]
# [INCLUDES <dir...>])
#
# This generates a "cppcheck" target that executes cppcheck on the specified
# sources. Sources may be either file names or directories containing files
# where all C/++ files will be parsed automatically. Use directories whenever
# possible because there is a limitation in arguments to pass to the cppcheck
# binary.
#
# SUPPRESSION_FILE may be given additionally to specify suppressions for
# cppcheck. The sources mentioned in the suppression file must be in the same
# format as given for SOURCES. This means if you specify them relative to
# CMAKE_CURRENT_SOURCE_DIR, then the same relative paths must be used in the
# suppression file.
#
# ENABLE_IDS allows to specify which additional cppcheck check ids to execute,
# e.g. all or style.
#
# With TARGET_NAME a different name for the generated check target can be
# specified. This is useful if several calls to this function are made in one
# CMake project, as otherwise the target names would collide.
#
# Additional include directories for the cppcheck program can be given with
# INCLUDES.
#
# cppcheck will be executed with CMAKE_CURRENT_SOURCE_DIR as working directory.
#
# This function can be called even if cppcheck wasn't found. In that case no
# target is created.
#
#
#=============================================================================
# Copyright 2011 Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
# Copyright 2012 Přemysl Janouch <p.janouch at gmail dot com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
#=============================================================================
find_program (CPPCHECK_EXECUTABLE cppcheck)
mark_as_advanced (CPPCHECK_EXECUTABLE)
include (FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS (CppCheck DEFAULT_MSG CPPCHECK_EXECUTABLE)
include (ProcessArguments)
function (GENERATE_CPPCHECK)
if (NOT CPPCHECK_FOUND)
return ()
endif (NOT CPPCHECK_FOUND)
# Parse arguments given to this function
set (__names SOURCES SUPPRESION_FILE ENABLE_IDS TARGET_NAME INCLUDES)
set (__need YES NO NO NO NO)
set (__want 1 1 1 1 1)
set (__more YES NO YES NO YES)
set (__skip 0 0 0 0 0)
set (__argv ${ARGV})
PROCESS_ARGUMENTS (__argv __names __need __want __more __skip "opt_")
# Get target name
set (target_name "cppcheck")
set (target_name_suffix "")
if (opt_target_name)
set (target_name ${opt_target_name_param})
set (target_suffix "-${target_name}")
endif (opt_target_name)
set (cppcheck_base "${PROJECT_BINARY_DIR}/cppcheck${target_suffix}")
set (cppcheck_report_file "${cppcheck_base}-report.log")
set (cppcheck_wrapper_script "${cppcheck_base}.cmake")
# Prepare a command line for cppcheck
set (source_args ${opt_sources_param})
set (options "--inline-suppr")
# Suppression argument
if (opt_suppression_file)
get_filename_component (abs "${opt_suppression_file_param}" ABSOLUTE)
set (options "${options} --suppressions \"${abs}\"")
endif (opt_suppression_file)
# Includes
foreach (include ${opt_includes_param})
set (options "${options} -I \"${include}\"")
endforeach (include)
# Enabled ids
if (opt_enable_ids)
set (id_list "")
foreach (id ${opt_enable_ids_param})
set (id_list "${id_list},${id}")
endforeach (id)
string (SUBSTRING ${id_list} 1 -1 id_list)
set (options "${options} \"--enable=${id_list}\"")
endif (opt_enable_ids)
# Create a wrapper script which redirects stderr of cppcheck to a file
file (WRITE ${cppcheck_wrapper_script} "
execute_process (
COMMAND \"${CPPCHECK_EXECUTABLE}\" ${options} ${source_args}
RESULT_VARIABLE exit_code
ERROR_VARIABLE error_out
WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\")
if (exit_code)
message (FATAL_ERROR \"Error executing cppcheck\")
endif (exit_code)
if (error_out)
message (\"\\nDetected errors:\\n\${error_out}\")
endif (error_out)
file (WRITE \"${cppcheck_report_file}\" \"\${error_out}\")
")
add_custom_target (${target_name}
COMMAND ${CMAKE_COMMAND} -P "${cppcheck_wrapper_script}"
DEPENDS "${cppcheck_wrapper_script}"
COMMENT "Calling cppcheck static code analyzer" VERBATIM)
endfunction (GENERATE_CPPCHECK)
|