annotate cmake/GenerateCppcheck.cmake @ 1296:13b56e9c7e7b

(issue149) licenses/README.txt additions and corrections.
author Bernhard Reiter <bernhard@intevation.de>
date Mon, 29 Sep 2014 16:12:47 +0200
parents 524cd4908656
children
rev   line source
1
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
1 # - Generate a cppcheck documentation for a project.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
2 # The function GENERATE_CPPCHECK is provided to create a "cppcheck" target that
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
3 # performs static code analysis using the cppcheck utility program.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
4 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
5 # GENERATE_CPPCHECK(SOURCES <sources to check...>
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
6 # [SUPPRESSION_FILE <file>]
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
7 # [ENABLE_IDS <id...>]
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 # [TARGET_NAME <name>]
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 # [INCLUDES <dir...>])
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11 # Generates a target "cppcheck" that executes cppcheck on the specified sources.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
12 # Sources may either be file names or directories containing files where all
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
13 # C++ files will be parsed automatically. Use directories whenever possible
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14 # because there is a limitation in arguments to pass to the cppcheck binary.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15 # SUPPRESSION_FILE may be give additionally to specify suppressions for#
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 # cppcheck. The sources mentioned in the suppression file must be in the same
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 # format like given for SOURCES. This means if you specified them relative to
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18 # CMAKE_CURRENT_SOURCE_DIR, then the same relative paths must be used in the
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19 # suppression file.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
20 # ENABLE_IDS allows to specify which additional cppcheck check ids to execute,
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
21 # e.g. all or style. They are combined with AND.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
22 # With TARGET_NAME a different name for the generated check target can be
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
23 # specified. This is useful if several calles to this function are made in one
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
24 # CMake project, as otherwise the target names collide.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
25 # Additional include directories for the cppcheck program can be given with
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
26 # INCLUDES.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
27 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
28 # cppcheck will be executed with CMAKE_CURRENT_SOURCE_DIR as working directory.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 # This function can always be called, even if no cppcheck was found. Then no
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
31 # target is created.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
32 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33 # Copyright (C) 2011 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 # This program is free software; you can redistribute it
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 # and/or modify it under the terms of the GNU General
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37 # Public License as published by the Free Software Foundation;
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 # either version 2, or (at your option)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39 # any later version.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41 # This program is distributed in the hope that it will be useful,
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 # but WITHOUT ANY WARRANTY; without even the implied warranty of
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 # GNU General Public License for more details.
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45 #
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 GET_FILENAME_COMPONENT(GENERATE_CPPCHECK_MODULE_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
48
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 # FIND_PACKAGE(Cppcheck)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50 find_program(CPPCHECK_EXECUTABLE
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51 cppcheck
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52 )
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54 if(CPPCHECK_EXECUTABLE)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 set(CPPCHECK_FOUND True)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56 endif()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58 include(ParseArguments)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 FUNCTION(GENERATE_CPPCHECK)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 IF(CPPCHECK_FOUND)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 PARSE_ARGUMENTS(ARG "SOURCES;SUPPRESSION_FILE;ENABLE_IDS;TARGET_NAME;INCLUDES" "" ${ARGN})
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 SET(TARGET_NAME "cppcheck")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 SET(TARGET_NAME_SUFFIX "")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 # parse target name
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
69 LIST(LENGTH ARG_TARGET_NAME TARGET_NAME_LENGTH)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
70 IF(${TARGET_NAME_LENGTH} EQUAL 1)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
71 SET(TARGET_NAME ${ARG_TARGET_NAME})
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
72 SET(TARGET_NAME_SUFFIX "-${ARG_TARGET_NAME}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
73 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
74
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
75 SET(CPPCHECK_CHECKFILE "${CMAKE_BINARY_DIR}/cppcheck-files${TARGET_NAME_SUFFIX}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
76 SET(CPPCHECK_REPORT_FILE "${CMAKE_BINARY_DIR}/cppcheck-report${TARGET_NAME_SUFFIX}.xml")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
77 SET(CPPCHECK_WRAPPER_SCRIPT "${CMAKE_BINARY_DIR}/cppcheck${TARGET_NAME_SUFFIX}.cmake")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
78
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
79 # write a list file containing all sources to check for the call to
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
80 # cppcheck
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
81 SET(SOURCE_ARGS "")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
82 FOREACH(SOURCE ${ARG_SOURCES})
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
83 SET(SOURCE_ARGS "${SOURCE_ARGS} \"${SOURCE}\"")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
84 ENDFOREACH()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
85
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
86 # prepare a cmake wrapper to write the stderr output of cppcheck to
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
87 # the result file
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
88
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
89 # suppression argument
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
90 LIST(LENGTH ARG_SUPPRESSION_FILE SUPPRESSION_FILE_LENGTH)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
91 IF(${SUPPRESSION_FILE_LENGTH} EQUAL 1)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
92 GET_FILENAME_COMPONENT(ABS "${ARG_SUPPRESSION_FILE}" ABSOLUTE)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
93 MESSAGE(STATUS "Using suppression file ${ABS}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
94 SET(SUPPRESSION_ARGUMENT --suppressions)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
95 SET(SUPPRESSION_FILE "\"${ABS}\"")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
96 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
97
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
98 # includes
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
99 SET(INCLUDE_ARGUMENTS "")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
100 FOREACH(INCLUDE ${ARG_INCLUDES})
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
101 SET(INCLUDE_ARGUMENTS "${INCLUDE_ARGUMENTS} \"-I${INCLUDE}\"")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
102 ENDFOREACH()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
103
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
104 # enabled ids
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
105 SET(ID_LIST "")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
106 FOREACH(ID ${ARG_ENABLE_IDS})
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
107 SET(ID_LIST "${ID_LIST},${ID}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
108 ENDFOREACH()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
109 IF(ID_LIST)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
110 STRING(LENGTH ${ID_LIST} LIST_LENGTH)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
111 MATH(EXPR FINAL_LIST_LENGTH "${LIST_LENGTH} - 1")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
112 STRING(SUBSTRING ${ID_LIST} 1 ${FINAL_LIST_LENGTH} FINAL_ID_LIST)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
113 SET(IDS_ARGUMENT "\"--enable=${FINAL_ID_LIST}\"")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
114 ELSE()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
115 SET(IDS_ARGUMENT "")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
116 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
117
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
118 FILE(WRITE ${CPPCHECK_WRAPPER_SCRIPT}
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
119 "
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
120 EXECUTE_PROCESS(COMMAND \"${CPPCHECK_EXECUTABLE}\" ${INCLUDE_ARGUMENTS} ${SUPPRESSION_ARGUMENT} ${SUPPRESSION_FILE} ${IDS_ARGUMENT} --inline-suppr --xml ${SOURCE_ARGS}
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
121 RESULT_VARIABLE CPPCHECK_EXIT_CODE
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
122 ERROR_VARIABLE ERROR_OUT
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
123 WORKING_DIRECTORY \"${CMAKE_CURRENT_SOURCE_DIR}\")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
124 IF(NOT CPPCHECK_EXIT_CODE EQUAL 0)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
125 MESSAGE(FATAL_ERROR \"Error executing cppcheck for target ${TARGET}, return code: \${CPPCHECK_EXIT_CODE}\")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
126 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
127 IF(ERROR_OUT)
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
128 MESSAGE(\"Detected errors:\\n\${ERROR_OUT}\")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
129 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
130 FILE(WRITE \"${CPPCHECK_REPORT_FILE}\" \"\${ERROR_OUT}\")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
131 "
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
132 )
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
133
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
134 ADD_CUSTOM_TARGET(${TARGET_NAME} ${CMAKE_COMMAND} -P "${CPPCHECK_WRAPPER_SCRIPT}"
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
135 COMMENT "Generating cppcheck result ${TARGET_NAME}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
136
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
137 MESSAGE(STATUS "Generating cppcheck target with name ${TARGET_NAME}")
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
138
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
139 ENDIF()
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
140
524cd4908656 Build infrastructure
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
141 ENDFUNCTION()

http://wald.intevation.org/projects/trustbridge/