Browse Source

cmake: Add Cppcheck CMake integration

Add a CMake cppcheck module that provides a new "make cppcheck" target
for running cppcheck against the project and generating a html report.

Use compile_commands.json for analysis, ensure generated Qt sources exist
before analysis, support project suppressions and parallel execution, and
generate an HTML report when cppcheck-htmlreport is available.

Cppcheck remains optional and is only required when BUILD_CPPCHECK is
enabled.

Signed-off-by: Robin Getz <rgetz503@gmail.com>
chirpmodem-v8.0.0
Robin Getz 3 months ago
parent
commit
d78d5d7ed0
No known key found for this signature in database
GPG Key ID: 51C7231FFED0E060
  1. 3
      CMakeLists.txt
  2. 99
      cmake/Modules/Cppcheck.cmake

3
CMakeLists.txt

@ -804,6 +804,9 @@ endif() @@ -804,6 +804,9 @@ endif()
# Set compiler options based on target architecture and selected extensions
include(CompilerOptions)
# Enable the Cppcheck CMake integration when available.
include(Cppcheck)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)

99
cmake/Modules/Cppcheck.cmake

@ -0,0 +1,99 @@ @@ -0,0 +1,99 @@
#
# Cppcheck static analysis support
#
option(BUILD_CPPCHECK "Build cppcheck target" OFF)
if(NOT BUILD_CPPCHECK)
return()
endif()
find_program(CPPCHECK_EXECUTABLE cppcheck)
if(NOT CPPCHECK_EXECUTABLE)
message(FATAL_ERROR
"BUILD_CPPCHECK is ON but cppcheck was not found")
endif()
message(STATUS "cppcheck found: ${CPPCHECK_EXECUTABLE}")
# Number of parallel jobs
include(ProcessorCount)
ProcessorCount(CPPCHECK_JOBS)
if(CPPCHECK_JOBS EQUAL 0)
set(CPPCHECK_JOBS 1)
endif()
set(CPPCHECK_OUTPUT_DIR
${CMAKE_BINARY_DIR}/cppcheck)
file(MAKE_DIRECTORY ${CPPCHECK_OUTPUT_DIR})
set(CPPCHECK_XML_OUTPUT
${CPPCHECK_OUTPUT_DIR}/cppcheck.xml)
# Optional suppressions
set(CPPCHECK_SUPPRESSIONS
${CMAKE_SOURCE_DIR}/cppcheck.suppress)
#style and performance back in later
set(CPPCHECK_OPTIONS
--project=${CMAKE_BINARY_DIR}/compile_commands.json
--check-level=exhaustive
--enable=warning,portability
--library=qt
--inconclusive
--inline-suppr
--xml
--xml-version=2
-j${CPPCHECK_JOBS}
-i ${CMAKE_BINARY_DIR}
--suppress=missingIncludeSystem
--relative-paths=${CMAKE_SOURCE_DIR}
--cppcheck-build-dir=${CMAKE_BINARY_DIR}/cppcheck-build
--checkers-report=${CMAKE_BINARY_DIR}/cppcheck-report-${CMAKE_PROJECT_NAME}.txt
--output-file=${CPPCHECK_XML_OUTPUT}
)
if(EXISTS ${CPPCHECK_SUPPRESSIONS})
list(APPEND CPPCHECK_OPTIONS
--suppressions-list=${CPPCHECK_SUPPRESSIONS})
endif()
add_custom_target(cppcheck
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/cppcheck-build
# Ensure all generated Qt AUTOGEN sources referenced by
# compile_commands.json exist before invoking cppcheck.
COMMAND ${CMAKE_COMMAND} --build .
COMMAND ${CPPCHECK_EXECUTABLE}
${CPPCHECK_OPTIONS}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running cppcheck static analysis"
VERBATIM
)
# These targets are excluded from the default build but still contribute
# generated sources referenced by compile_commands.json.
add_dependencies(cppcheck modemmeshcore_smoke modmeshcore_cli)
#
# Optional HTML report
#
find_program(CPPCHECK_HTMLREPORT_EXECUTABLE cppcheck-htmlreport)
if(CPPCHECK_HTMLREPORT_EXECUTABLE)
add_custom_command(TARGET cppcheck POST_BUILD
COMMAND ${CPPCHECK_HTMLREPORT_EXECUTABLE}
--file=${CPPCHECK_XML_OUTPUT}
--report-dir=${CPPCHECK_OUTPUT_DIR}/html
--source-dir=${CMAKE_SOURCE_DIR}
COMMENT "Generating cppcheck HTML report"
VERBATIM
)
endif()
Loading…
Cancel
Save