|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2025 GARDENA GmbH |
| 4 | + |
| 5 | +include(ProcessorCount) |
| 6 | +processorcount(CPPCHECK_JOBS_COUNT) |
| 7 | +if(N EQUAL 0) |
| 8 | + set(CPPCHECK_JOBS_COUNT 1) |
| 9 | +endif() |
| 10 | + |
| 11 | +find_program(CPPCHECK_EXE NAMES cppcheck REQUIRED) |
| 12 | +message(STATUS "Found SCA: cppcheck (${CPPCHECK_EXE})") |
| 13 | + |
| 14 | +# Cppcheck uses the compile_commands.json as input |
| 15 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 16 | + |
| 17 | +# Create an output directory for our tool |
| 18 | +set(output_dir ${CMAKE_BINARY_DIR}/sca/cppcheck) |
| 19 | +file(MAKE_DIRECTORY ${output_dir}) |
| 20 | + |
| 21 | +# Help Cppcheck parse the Zephyr headers, reduce runtime by ensuring all |
| 22 | +# relevant configs defined |
| 23 | +set_property( |
| 24 | + GLOBAL |
| 25 | + APPEND |
| 26 | + PROPERTY |
| 27 | + extra_post_build_commands |
| 28 | + COMMAND |
| 29 | + ${CMAKE_COMMAND} |
| 30 | + ARGS |
| 31 | + -E |
| 32 | + touch |
| 33 | + ${output_dir}/compiler-macros.c |
| 34 | +) |
| 35 | +set_property( |
| 36 | + GLOBAL |
| 37 | + APPEND |
| 38 | + PROPERTY extra_post_build_byproducts ${output_dir}/compiler-macros.c |
| 39 | +) |
| 40 | + |
| 41 | +# Note: This works only for GCC and compatibles. Please add support for other |
| 42 | +# compilers! |
| 43 | +set_property( |
| 44 | + GLOBAL |
| 45 | + APPEND |
| 46 | + PROPERTY |
| 47 | + extra_post_build_commands |
| 48 | + COMMAND |
| 49 | + ${CMAKE_C_COMPILER} |
| 50 | + ARGS |
| 51 | + -dM |
| 52 | + -E |
| 53 | + ${output_dir}/compiler-macros.c |
| 54 | + -o |
| 55 | + ${output_dir}/compiler-macros.h |
| 56 | +) |
| 57 | + |
| 58 | +set_property( |
| 59 | + GLOBAL |
| 60 | + APPEND |
| 61 | + PROPERTY extra_post_build_byproducts ${output_dir}/compiler-macros.h |
| 62 | +) |
| 63 | + |
| 64 | +add_custom_target( |
| 65 | + cppcheck |
| 66 | + ALL |
| 67 | + COMMAND |
| 68 | + ${CPPCHECK_EXE} --project=${CMAKE_BINARY_DIR}/compile_commands.json |
| 69 | + --xml # Write results in XML format |
| 70 | + --output-file=${output_dir}/cppcheck.xml # write output to file, not stderr |
| 71 | + --library=zephyr.cfg # Enable zephyr specific knowledge |
| 72 | + --quiet # Suppress progress reporting |
| 73 | + --include=${CMAKE_BINARY_DIR}/zephyr/include/generated/zephyr/autoconf.h |
| 74 | + --include=${output_dir}/compiler-macros.h # Use exact compiler macros |
| 75 | + -j ${CPPCHECK_JOBS_COUNT} # Parallelize analysis |
| 76 | + -D__cppcheck__ # As recommended by the Cppcheck manual |
| 77 | + --check-level=exhaustive # Prevent normalCheckLevelMaxBranches "findings" |
| 78 | + # --premium=safety-off # Premium only: Prevent non-zero exit code when issues found |
| 79 | + ${CODECHECKER_ANALYZE_OPTS} |
| 80 | + DEPENDS |
| 81 | + ${CMAKE_BINARY_DIR}/compile_commands.json |
| 82 | + ${output_dir}/compiler-macros.h |
| 83 | + BYPRODUCTS ${output_dir}/cppcheck.xml |
| 84 | + VERBATIM |
| 85 | +) |
| 86 | + |
| 87 | +# Cleanup helper files |
| 88 | +add_custom_command( |
| 89 | + TARGET cppcheck |
| 90 | + POST_BUILD |
| 91 | + COMMAND ${CMAKE_COMMAND} |
| 92 | + ARGS -E rm ${output_dir}/compiler-macros.c ${output_dir}/compiler-macros.h |
| 93 | +) |
| 94 | + |
| 95 | +find_program(CPPCHECK_HTMLREPORT_EXE NAMES cppcheck-htmlreport REQUIRED) |
| 96 | +message(STATUS "Found SCA: cppcheck-htmlreport (${CPPCHECK_HTMLREPORT_EXE})") |
| 97 | +add_custom_command( |
| 98 | + TARGET cppcheck |
| 99 | + POST_BUILD |
| 100 | + COMMAND |
| 101 | + ${CPPCHECK_HTMLREPORT_EXE} --title=Zephyr |
| 102 | + --file=${output_dir}/cppcheck.xml # Read in file created by Cppcheck |
| 103 | + --report-dir=${output_dir} # Set output directory |
| 104 | + ${CPPCHECK_HTMLREPORT_OPTS} |
| 105 | + BYPRODUCTS |
| 106 | + ${output_dir}/index.html |
| 107 | + ${output_dir}/stats.html |
| 108 | + ${output_dir}/style.css |
| 109 | + VERBATIM |
| 110 | +) |
0 commit comments