blob: 81411a1e143abc8c2be85bb200fb4639df33fa1e [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")
21
22
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}")
34
35#
36# Exclude files or directories we don't want to receive warnings about.
37#
38list(APPEND cppcheck-flags "-i${SOURCE_DIR}/ext/")
39list(APPEND cppcheck-flags "-i${SOURCE_DIR}/lib/libc")
40
41#
42# If you want to suppress specific files without using an inline suppression,
43# do it in `suppressions.txt`.
44#
45list(APPEND cppcheck-flags
46 "--inline-suppr" # Allow inline suppressions
47 "--suppressions-list=${SOURCE_DIR}/tools/cppcheck/suppressions.txt")
48
49#
50# Configure the platform file. This communicates certain implementation details to
51# Cppcheck and avoid false positives.
52#
53set(toolchain-xml
54 "${SOURCE_DIR}/tools/cppcheck-aarch64-platform.xml")
55
56list(APPEND cppcheck-flags "--platform=${toolchain-xml}")
57set(COMPILE_COMMANDS_FILE "${BUILD_DIR}/compile_commands.json")
58if(NOT EXISTS "${COMPILE_COMMANDS_FILE}")
59 message(FATAL_ERROR "Please configure with -DCMAKE_EXPORT_COMPILE_COMMANDS=ON.")
60endif()
61
62#
63# Create the output directory
64#
65file(MAKE_DIRECTORY "${CPPCHECK_BUILD_DIR}")
66
67set(EXE_CPPCHECK_TWICE)
68
69#
70# Workaround for "internal errors" reported for cppcheck-misra.
71# Run CPPCheck 2nd time if the output file is not created.
72#
73if(CPPCHECK_MISRA AND (NOT EXISTS "${CPPCHECK_OUTPUT}"))
74 set(EXE_CPPCHECK_TWICE 1)
75endif()
76
77execute_process(
78 WORKING_DIRECTORY ${SOURCE_DIR}
79 COMMAND ${RMM_CPPCHECK_EXE}
80 --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags}
81 )
82
83if(EXE_CPPCHECK_TWICE)
84 execute_process(
85 WORKING_DIRECTORY ${SOURCE_DIR}
86 COMMAND ${RMM_CPPCHECK_EXE}
87 --project=${COMPILE_COMMANDS_FILE} ${cppcheck-flags}
88 )
89endif()