blob: fc90ab89b983ae83cb52b83c1d1ddc17b88843e4 [file] [log] [blame]
Soby Mathew7faa95e2023-11-22 17:04:10 +00001#
2# SPDX-License-Identifier: BSD-3-Clause
3# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4#
5
6find_program(RMM_CPPCHECK_EXE "cppcheck" DOC "Path to Cppcheck")
7
8if(NOT RMM_CPPCHECK_EXE)
9 message(FATAL_ERROR "Could not find cppcheck executable.")
10endif()
11
12#
13# Set up checkers.
14#
15set(cppcheck-flags)
16
17list(APPEND cppcheck-flags "--enable=all")
18list(APPEND cppcheck-flags "--xml")
19list(APPEND cppcheck-flags "--xml-version=2")
20list(APPEND cppcheck-flags "--template=gcc")
Shruti Guptae825dfc2024-04-15 11:09:25 +010021list(APPEND cppcheck-flags "--check-level=exhaustive")
Soby Mathew7faa95e2023-11-22 17:04:10 +000022
23if(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")
27else()
28 set(CPPCHECK_OUTPUT "${BUILD_DIR}/tools/cppcheck/cppcheck.xml")
29 set(CPPCHECK_BUILD_DIR "${BUILD_DIR}/tools/cppcheck/dump")
30endif()
31
32list(APPEND cppcheck-flags "--output-file=${CPPCHECK_OUTPUT}")
33list(APPEND cppcheck-flags "--cppcheck-build-dir=${CPPCHECK_BUILD_DIR}")
Shruti Guptae825dfc2024-04-15 11:09:25 +010034list(APPEND cppcheck-flags "--checkers-report=${BUILD_DIR}/tools/cppcheck/checkers.log")
Soby Mathew7faa95e2023-11-22 17:04:10 +000035
36#
37# Exclude files or directories we don't want to receive warnings about.
38#
39list(APPEND cppcheck-flags "-i${SOURCE_DIR}/ext/")
40list(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#
46list(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#
54set(toolchain-xml
55 "${SOURCE_DIR}/tools/cppcheck-aarch64-platform.xml")
56
57list(APPEND cppcheck-flags "--platform=${toolchain-xml}")
58set(COMPILE_COMMANDS_FILE "${BUILD_DIR}/compile_commands.json")
59if(NOT EXISTS "${COMPILE_COMMANDS_FILE}")
60 message(FATAL_ERROR "Please configure with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON.")
61endif()
62
63#
64# Create the output directory
65#
66file(MAKE_DIRECTORY "${CPPCHECK_BUILD_DIR}")
67
68set(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#
74if(CPPCHECK_MISRA AND (NOT EXISTS "${CPPCHECK_OUTPUT}"))
75 set(EXE_CPPCHECK_TWICE 1)
76endif()
77
78execute_process(
79 WORKING_DIRECTORY ${SOURCE_DIR}
80 COMMAND ${RMM_CPPCHECK_EXE}
81 --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags}
82 )
83
84if(EXE_CPPCHECK_TWICE)
85 execute_process(
86 WORKING_DIRECTORY ${SOURCE_DIR}
87 COMMAND ${RMM_CPPCHECK_EXE}
88 --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags}
89 )
90endif()