blob: b2b5f6865c85304acf730ddd4d4c22485a579cf3 [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/bin/bash
2#-------------------------------------------------------------------------------
Xinyu Zhangbbe2d762022-01-19 10:36:11 +08003# Copyright (c) 2018-2022, Arm Limited and Contributors. All rights reserved.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9##
10##@file
11##@brief Execute cppcheck
12##
13##This bash script can be used to execute cppcheck for the tf-m project.
14##It will use the CMake generated "compile_commands.json" file.
15##CMake is executed to generate the build commands for the "default" build
Dean Birch62c4f082020-01-17 16:13:26 +000016##configuration (i.e. no build config file is specified on the command-line).
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010017##
18##This file shall be executed from the root directory of the tf-m working copy.
19##
20##In order to have all include file in place, some CMake external projects will
21##be built, and thus C build tools for the default build configuration must be
22##available.
23##
24##The script will generate two XML output files:
25##file | description
26##--------|--------
27##chk-config.xml | The result of cppcheck configuration verification.
28##chk-src.xml. | The result of source file verification.
29##
30##@todo The current version of cppcheck seems to ignore command line parameters
31## when using the --project command line switch. As a result it is not
32## possible to define additional macros and include paths on the command
33## line. This results in some incorrect error and warning messages.
34##@todo The file cppcheck/arm-cortex-m.cfg needs to be revised. Some settings
Dean Birch62c4f082020-01-17 16:13:26 +000035## might be invalid, and also a different file may be needed based on
36## used compiler switches (i.e. to match width specification and or default
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010037## sign for some types).
38##@todo Currently cppcheck is only executed for the default build configuration
39## "ConfigDefault.cmake"for target AN521 of the "top level" project.
Dean Birch62c4f082020-01-17 16:13:26 +000040## This might need to be revised/changed in the future.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010041##
42
43#Fail if any command exit with error.
44set -e
45
Xinyu Zhang16a218e2022-10-11 17:21:39 +080046export PATH=$PATH:$GCC_10_3_PATH
Xinyu Zhangbbe2d762022-01-19 10:36:11 +080047
Dean Birch62c4f082020-01-17 16:13:26 +000048RAW_OUTPUT=0
49
50while getopts "hr" opt ; do
51 case "$opt" in
52 h)
53 echo "Usage: $(basename -- "$0") [-h] [-r] [git_hash]"
54 echo " -r, Raw output. (Default is to create xml reports)."
55 echo " -h, Script help"
56 exit 0
57 ;;
58 r)
59 RAW_OUTPUT=1
60 ;;
61 esac
62done
63
64shift $((OPTIND-1))
65
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010066#The location from where the script executes
67mypath=$(dirname $0)
68
Minos Galanakisea421232019-06-20 17:11:28 +010069#The cmake_exported project file in json format
Dean Birch62c4f082020-01-17 16:13:26 +000070cmake_commands=compile_commands.json
Minos Galanakisea421232019-06-20 17:11:28 +010071
Xinyu Zhang431b15d2023-10-26 15:34:43 +080072. "$mypath/utils/util_cmake.sh"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010073
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010074#Library file for cppcheck
75library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg"
76suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt"
Xinyu Zhang68247112021-07-23 15:01:53 +080077tfm_repo="$(fix_win_path $(get_full_path ./))"
78toolchain_file="$tfm_repo/toolchain_GNUARM.cmake"
79test_repo="$tfm_repo/../tf-m-tests"
80mbedtls_repo="$tfm_repo/../mbedtls"
81mcuboot_repo="$tfm_repo/../mcuboot"
82
83#Apply TF-M patches to dependency repos
84cnt=$(ls $tfm_repo/lib/ext/mcuboot/*.patch 2> /dev/null | wc -l)
85if [ "$cnt" != "0" ] ; then
86 cd $mcuboot_repo
87 git apply $tfm_repo/lib/ext/mcuboot/*.patch
88 cd -
89fi
90
91cnt=$(ls $tfm_repo/lib/ext/mbedcrypto/*.patch 2> /dev/null | wc -l)
92if [ "$cnt" != "0" ] ; then
93 cd $mbedtls_repo
94 git apply $tfm_repo/lib/ext/mbedcrypto/*.patch
95 cd -
96fi
Xinyu Zhangd938b912021-04-29 14:06:12 +080097
98#Cmake compile params
Summer Qin3c2b5722021-05-26 10:43:45 +080099cmake_params="-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTFM_PLATFORM=arm/mps2/an521 -DTFM_TOOLCHAIN_FILE=$toolchain_file"
Xinyu Zhangbfa38ac2021-07-23 14:54:47 +0800100cmake_params="$cmake_params -DTFM_TEST_REPO_PATH=$test_repo -DMBEDCRYPTO_PATH=$mbedtls_repo -DMCUBOOT_PATH=$mcuboot_repo"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100101
Minos Galanakisea421232019-06-20 17:11:28 +0100102#Enable all additional checks by default
103additional_checklist="all"
104
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100105#Run cmake to get the compile_commands.json file
106echo
Dean Birch62c4f082020-01-17 16:13:26 +0000107echo '******* Generating compile_commands.json ***************'
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100108echo
Xinyu Zhangd938b912021-04-29 14:06:12 +0800109generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "$cmake_params"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100110#Enter the build directory
111bdir=$(make_build_dir_name "./" "cppcheck")
112pushd "$bdir" >/dev/null
Minos Galanakisea421232019-06-20 17:11:28 +0100113
Dean Birch62c4f082020-01-17 16:13:26 +0000114#The following snippet allows cppcheck to be run differentially against a
Minos Galanakisea421232019-06-20 17:11:28 +0100115#commit hash passed as first argument $1. It does not
116#affect the legacy functionality of the script, checking the whole codebase,
117#when called without an argument
118if [[ ! -z "$1" ]]
119 then
Dean Birch62c4f082020-01-17 16:13:26 +0000120 echo "Enabled git-diff mode against hash: $1"
Minos Galanakisea421232019-06-20 17:11:28 +0100121
Xinyu Zhang7de294d2021-03-08 10:09:32 +0800122 # Do not execute unused function check and information check when running in diff-mode
123 additional_checklist="style,performance,portability,missingInclude"
Minos Galanakisea421232019-06-20 17:11:28 +0100124 # Grep will set exit status to 1 if a commit does not contain c/cpp.. files
125 set +e
126 filtered_cmd_f=compile_commands_filtered.json
127 # Get a list of files modified by the commits between the reference and HEAD
128 flist=$(git diff-tree --no-commit-id --name-only -r $1 | grep -E '\S*\.(c|cpp|cc|cxx|inc|h)$')
129 flist=$(echo $flist | xargs)
130 echo -e "[" > $filtered_cmd_f
131 IFS=$' ' read -ra git_flist <<< "${flist}"
132
133 for fl in "${git_flist[@]}"; do
134 echo "Looking for reference of file: $fl"
135
Dean Birch62c4f082020-01-17 16:13:26 +0000136 # dry run the command to see if there any output
137 JSON_CMD=$(grep -B 3 "\"file\": \".*$fl\"" $cmake_commands)
Minos Galanakisea421232019-06-20 17:11:28 +0100138
139 if [ -n "${JSON_CMD}" ]; then
140 command_matched=1
Dean Birch62c4f082020-01-17 16:13:26 +0000141 grep -B 3 "\"file\": \".*$fl\"" $cmake_commands >> $filtered_cmd_f
Minos Galanakisea421232019-06-20 17:11:28 +0100142 echo -e "}," >> $filtered_cmd_f
143 fi
144 done
145 set -e
146
147 # Only continue if files in the patch are included in the build commands
148 if [ -n "${command_matched}" ]; then
149 sed -i '$ d' $filtered_cmd_f
150 echo -e "}\n]" >> $filtered_cmd_f
151
Dean Birch62c4f082020-01-17 16:13:26 +0000152 cat $filtered_cmd_f > $cmake_commands
Minos Galanakisea421232019-06-20 17:11:28 +0100153 else
Minos Galanakisea421232019-06-20 17:11:28 +0100154 echo "CppCheck: Ignoring files not contained in the build config"
Dean Birch62c4f082020-01-17 16:13:26 +0000155 if [ "$RAW_OUTPUT" == "0" ] ; then
156 # Always generate an empty file for other stages of ci expecting one
Dean Birch15c8ffe2020-05-29 13:31:19 +0100157 echo "Files Ignored: $flist"
158 cat <<-EOF > chk-config.xml
159 <?xml version="1.0" encoding="UTF-8"?>
160 <results version="2">
161 <cppcheck version="$(cppcheck --version)"/>
162 <errors>
163 </errors>
164 </results>
165 EOF
166 cp chk-config.xml chk-src.xml
Dean Birch62c4f082020-01-17 16:13:26 +0000167 fi
Minos Galanakisea421232019-06-20 17:11:28 +0100168 exit 0
169 fi
170fi
171
Dean Birch62c4f082020-01-17 16:13:26 +0000172function cppcheck_failed {
Xinyu Zhangb314e102021-03-16 16:36:29 +0800173 echo "cppcheck failed."
174 echo "Check log for errors."
Xinyu Zhang7de294d2021-03-08 10:09:32 +0800175 exit 1
Dean Birch62c4f082020-01-17 16:13:26 +0000176}
Minos Galanakisea421232019-06-20 17:11:28 +0100177
Dean Birch62c4f082020-01-17 16:13:26 +0000178EXTRA_ARGS="--error-exitcode=1"
179if [ "$RAW_OUTPUT" != "1" ] ; then
180 # If not in raw output mode, use xml output.
181 EXTRA_ARGS="--xml"
182else
183 trap cppcheck_failed ERR
184fi
Amjad Ouled-Ameurf7ae75c2025-03-20 15:02:56 +0000185CPPCHECK_ARGS="$EXTRA_ARGS --enable="$additional_checklist" --library="$library_file" --project=$cmake_commands --suppressions-list="$suppress_file" --inline-suppr -j 8"
Dean Birch62c4f082020-01-17 16:13:26 +0000186
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100187#Now run cppcheck.
188echo
189echo '******* checking cppcheck configuration ***************'
190echo
Minos Galanakisea421232019-06-20 17:11:28 +0100191
Dean Birch62c4f082020-01-17 16:13:26 +0000192if [ "$RAW_OUTPUT" == "1" ] ; then
193 cppcheck $CPPCHECK_ARGS --check-config > /dev/null
194else
195 cppcheck $CPPCHECK_ARGS --check-config 2>chk-config.xml
196fi
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100197
198echo
199echo '******* analyzing files with cppcheck ***************'
200echo
Dean Birch62c4f082020-01-17 16:13:26 +0000201if [ "$RAW_OUTPUT" == "1" ] ; then
202 cppcheck $CPPCHECK_ARGS > /dev/null
203 echo '******* cppcheck complete ***************'
204else
205 cppcheck $CPPCHECK_ARGS 2>chk-src.xml
206 echo
207 echo '******* Please check chk-config.xml and chk-src.xml for the results. ***************'
208 echo
209fi
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100210popd