Soby Mathew | 7faa95e | 2023-11-22 17:04:10 +0000 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: BSD-3-Clause |
| 3 | # SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | # |
| 5 | |
| 6 | find_program(RMM_CPPCHECK_EXE "cppcheck" DOC "Path to Cppcheck") |
| 7 | |
| 8 | if(NOT RMM_CPPCHECK_EXE) |
| 9 | message(FATAL_ERROR "Could not find cppcheck executable.") |
| 10 | endif() |
| 11 | |
| 12 | # |
| 13 | # Set up checkers. |
| 14 | # |
| 15 | set(cppcheck-flags) |
| 16 | |
| 17 | list(APPEND cppcheck-flags "--enable=all") |
| 18 | list(APPEND cppcheck-flags "--xml") |
| 19 | list(APPEND cppcheck-flags "--xml-version=2") |
| 20 | list(APPEND cppcheck-flags "--template=gcc") |
Shruti Gupta | e825dfc | 2024-04-15 11:09:25 +0100 | [diff] [blame^] | 21 | list(APPEND cppcheck-flags "--check-level=exhaustive") |
Soby Mathew | 7faa95e | 2023-11-22 17:04:10 +0000 | [diff] [blame] | 22 | |
| 23 | if(CPPCHECK_MISRA) |
| 24 | list(APPEND cppcheck-flags "--addon=${SOURCE_DIR}/tools/cppcheck/misra.json") |
| 25 | set(CPPCHECK_OUTPUT "${BUILD_DIR}/tools/cppcheck/cppcheck_misra.xml") |
| 26 | set(CPPCHECK_BUILD_DIR "${BUILD_DIR}/tools/cppcheck/dump_misra") |
| 27 | else() |
| 28 | set(CPPCHECK_OUTPUT "${BUILD_DIR}/tools/cppcheck/cppcheck.xml") |
| 29 | set(CPPCHECK_BUILD_DIR "${BUILD_DIR}/tools/cppcheck/dump") |
| 30 | endif() |
| 31 | |
| 32 | list(APPEND cppcheck-flags "--output-file=${CPPCHECK_OUTPUT}") |
| 33 | list(APPEND cppcheck-flags "--cppcheck-build-dir=${CPPCHECK_BUILD_DIR}") |
Shruti Gupta | e825dfc | 2024-04-15 11:09:25 +0100 | [diff] [blame^] | 34 | list(APPEND cppcheck-flags "--checkers-report=${BUILD_DIR}/tools/cppcheck/checkers.log") |
Soby Mathew | 7faa95e | 2023-11-22 17:04:10 +0000 | [diff] [blame] | 35 | |
| 36 | # |
| 37 | # Exclude files or directories we don't want to receive warnings about. |
| 38 | # |
| 39 | list(APPEND cppcheck-flags "-i${SOURCE_DIR}/ext/") |
| 40 | list(APPEND cppcheck-flags "-i${SOURCE_DIR}/lib/libc") |
| 41 | |
| 42 | # |
| 43 | # If you want to suppress specific files without using an inline suppression, |
| 44 | # do it in `suppressions.txt`. |
| 45 | # |
| 46 | list(APPEND cppcheck-flags |
| 47 | "--inline-suppr" # Allow inline suppressions |
| 48 | "--suppressions-list=${SOURCE_DIR}/tools/cppcheck/suppressions.txt") |
| 49 | |
| 50 | # |
| 51 | # Configure the platform file. This communicates certain implementation details to |
| 52 | # Cppcheck and avoid false positives. |
| 53 | # |
| 54 | set(toolchain-xml |
| 55 | "${SOURCE_DIR}/tools/cppcheck-aarch64-platform.xml") |
| 56 | |
| 57 | list(APPEND cppcheck-flags "--platform=${toolchain-xml}") |
| 58 | set(COMPILE_COMMANDS_FILE "${BUILD_DIR}/compile_commands.json") |
| 59 | if(NOT EXISTS "${COMPILE_COMMANDS_FILE}") |
| 60 | message(FATAL_ERROR "Please configure with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON.") |
| 61 | endif() |
| 62 | |
| 63 | # |
| 64 | # Create the output directory |
| 65 | # |
| 66 | file(MAKE_DIRECTORY "${CPPCHECK_BUILD_DIR}") |
| 67 | |
| 68 | set(EXE_CPPCHECK_TWICE) |
| 69 | |
| 70 | # |
| 71 | # Workaround for "internal errors" reported for cppcheck-misra. |
| 72 | # Run CPPCheck 2nd time if the output file is not created. |
| 73 | # |
| 74 | if(CPPCHECK_MISRA AND (NOT EXISTS "${CPPCHECK_OUTPUT}")) |
| 75 | set(EXE_CPPCHECK_TWICE 1) |
| 76 | endif() |
| 77 | |
| 78 | execute_process( |
| 79 | WORKING_DIRECTORY ${SOURCE_DIR} |
| 80 | COMMAND ${RMM_CPPCHECK_EXE} |
| 81 | --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags} |
| 82 | ) |
| 83 | |
| 84 | if(EXE_CPPCHECK_TWICE) |
| 85 | execute_process( |
| 86 | WORKING_DIRECTORY ${SOURCE_DIR} |
| 87 | COMMAND ${RMM_CPPCHECK_EXE} |
| 88 | --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags} |
| 89 | ) |
| 90 | endif() |