From d78d5d7ed0a3d5d80dfe14832481953dfa450772 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 23 Aug 2026 15:52:37 -0400 Subject: [PATCH] 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 --- CMakeLists.txt | 3 ++ cmake/Modules/Cppcheck.cmake | 99 ++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 cmake/Modules/Cppcheck.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 17ec6ed94..3b08f0cec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/Modules/Cppcheck.cmake b/cmake/Modules/Cppcheck.cmake new file mode 100644 index 000000000..6c2215f00 --- /dev/null +++ b/cmake/Modules/Cppcheck.cmake @@ -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()