blob: 9ed35214ffd66da36b489b5b5709845d5f73f9a8 [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/bin/bash
2#-------------------------------------------------------------------------------
Xinyu Zhangd938b912021-04-29 14:06:12 +08003# Copyright (c) 2018-2021, 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
Dean Birch62c4f082020-01-17 16:13:26 +000046RAW_OUTPUT=0
47
48while getopts "hr" opt ; do
49 case "$opt" in
50 h)
51 echo "Usage: $(basename -- "$0") [-h] [-r] [git_hash]"
52 echo " -r, Raw output. (Default is to create xml reports)."
53 echo " -h, Script help"
54 exit 0
55 ;;
56 r)
57 RAW_OUTPUT=1
58 ;;
59 esac
60done
61
62shift $((OPTIND-1))
63
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010064#The location from where the script executes
65mypath=$(dirname $0)
66
Minos Galanakisea421232019-06-20 17:11:28 +010067#The cmake_exported project file in json format
Dean Birch62c4f082020-01-17 16:13:26 +000068cmake_commands=compile_commands.json
Minos Galanakisea421232019-06-20 17:11:28 +010069
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010070. "$mypath/util_cmake.sh"
71
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010072#Library file for cppcheck
73library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg"
74suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt"
Xinyu Zhang11df53b2020-12-11 14:26:29 +080075toolchain_file="$(fix_win_path $(get_full_path ./))/toolchain_GNUARM.cmake"
Xinyu Zhangd938b912021-04-29 14:06:12 +080076test_repo="$(fix_win_path $(get_full_path ./))/../tf-m-tests"
77mbedtls_repo="$(fix_win_path $(get_full_path ./))/../mbedtls"
Xinyu Zhangd938b912021-04-29 14:06:12 +080078mcuboot_repo="$(fix_win_path $(get_full_path ./))/../mcuboot"
79
80#Cmake compile params
Summer Qin3c2b5722021-05-26 10:43:45 +080081cmake_params="-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTFM_PLATFORM=arm/mps2/an521 -DTFM_TOOLCHAIN_FILE=$toolchain_file"
Xinyu Zhangbfa38ac2021-07-23 14:54:47 +080082cmake_params="$cmake_params -DTFM_TEST_REPO_PATH=$test_repo -DMBEDCRYPTO_PATH=$mbedtls_repo -DMCUBOOT_PATH=$mcuboot_repo"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010083
Minos Galanakisea421232019-06-20 17:11:28 +010084#Enable all additional checks by default
85additional_checklist="all"
86
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010087#Run cmake to get the compile_commands.json file
88echo
Dean Birch62c4f082020-01-17 16:13:26 +000089echo '******* Generating compile_commands.json ***************'
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010090echo
Xinyu Zhangd938b912021-04-29 14:06:12 +080091generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "$cmake_params"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010092#Enter the build directory
93bdir=$(make_build_dir_name "./" "cppcheck")
94pushd "$bdir" >/dev/null
Minos Galanakisea421232019-06-20 17:11:28 +010095
Dean Birch62c4f082020-01-17 16:13:26 +000096#The following snippet allows cppcheck to be run differentially against a
Minos Galanakisea421232019-06-20 17:11:28 +010097#commit hash passed as first argument $1. It does not
98#affect the legacy functionality of the script, checking the whole codebase,
99#when called without an argument
100if [[ ! -z "$1" ]]
101 then
Dean Birch62c4f082020-01-17 16:13:26 +0000102 echo "Enabled git-diff mode against hash: $1"
Minos Galanakisea421232019-06-20 17:11:28 +0100103
Xinyu Zhang7de294d2021-03-08 10:09:32 +0800104 # Do not execute unused function check and information check when running in diff-mode
105 additional_checklist="style,performance,portability,missingInclude"
Minos Galanakisea421232019-06-20 17:11:28 +0100106 # Grep will set exit status to 1 if a commit does not contain c/cpp.. files
107 set +e
108 filtered_cmd_f=compile_commands_filtered.json
109 # Get a list of files modified by the commits between the reference and HEAD
110 flist=$(git diff-tree --no-commit-id --name-only -r $1 | grep -E '\S*\.(c|cpp|cc|cxx|inc|h)$')
111 flist=$(echo $flist | xargs)
112 echo -e "[" > $filtered_cmd_f
113 IFS=$' ' read -ra git_flist <<< "${flist}"
114
115 for fl in "${git_flist[@]}"; do
116 echo "Looking for reference of file: $fl"
117
Dean Birch62c4f082020-01-17 16:13:26 +0000118 # dry run the command to see if there any output
119 JSON_CMD=$(grep -B 3 "\"file\": \".*$fl\"" $cmake_commands)
Minos Galanakisea421232019-06-20 17:11:28 +0100120
121 if [ -n "${JSON_CMD}" ]; then
122 command_matched=1
Dean Birch62c4f082020-01-17 16:13:26 +0000123 grep -B 3 "\"file\": \".*$fl\"" $cmake_commands >> $filtered_cmd_f
Minos Galanakisea421232019-06-20 17:11:28 +0100124 echo -e "}," >> $filtered_cmd_f
125 fi
126 done
127 set -e
128
129 # Only continue if files in the patch are included in the build commands
130 if [ -n "${command_matched}" ]; then
131 sed -i '$ d' $filtered_cmd_f
132 echo -e "}\n]" >> $filtered_cmd_f
133
Dean Birch62c4f082020-01-17 16:13:26 +0000134 cat $filtered_cmd_f > $cmake_commands
Minos Galanakisea421232019-06-20 17:11:28 +0100135 else
Minos Galanakisea421232019-06-20 17:11:28 +0100136 echo "CppCheck: Ignoring files not contained in the build config"
Dean Birch62c4f082020-01-17 16:13:26 +0000137 if [ "$RAW_OUTPUT" == "0" ] ; then
138 # Always generate an empty file for other stages of ci expecting one
Dean Birch15c8ffe2020-05-29 13:31:19 +0100139 echo "Files Ignored: $flist"
140 cat <<-EOF > chk-config.xml
141 <?xml version="1.0" encoding="UTF-8"?>
142 <results version="2">
143 <cppcheck version="$(cppcheck --version)"/>
144 <errors>
145 </errors>
146 </results>
147 EOF
148 cp chk-config.xml chk-src.xml
Dean Birch62c4f082020-01-17 16:13:26 +0000149 fi
Minos Galanakisea421232019-06-20 17:11:28 +0100150 exit 0
151 fi
152fi
153
Dean Birch62c4f082020-01-17 16:13:26 +0000154function cppcheck_failed {
Xinyu Zhangb314e102021-03-16 16:36:29 +0800155 echo "cppcheck failed."
156 echo "Check log for errors."
Xinyu Zhang7de294d2021-03-08 10:09:32 +0800157 exit 1
Dean Birch62c4f082020-01-17 16:13:26 +0000158}
Minos Galanakisea421232019-06-20 17:11:28 +0100159
Dean Birch62c4f082020-01-17 16:13:26 +0000160EXTRA_ARGS="--error-exitcode=1"
161if [ "$RAW_OUTPUT" != "1" ] ; then
162 # If not in raw output mode, use xml output.
163 EXTRA_ARGS="--xml"
164else
165 trap cppcheck_failed ERR
166fi
167CPPCHECK_ARGS="$EXTRA_ARGS --enable="$additional_checklist" --library="$library_file" --project=$cmake_commands --suppressions-list="$suppress_file" --inline-suppr"
168
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100169#Now run cppcheck.
170echo
171echo '******* checking cppcheck configuration ***************'
172echo
Minos Galanakisea421232019-06-20 17:11:28 +0100173
Dean Birch62c4f082020-01-17 16:13:26 +0000174if [ "$RAW_OUTPUT" == "1" ] ; then
175 cppcheck $CPPCHECK_ARGS --check-config > /dev/null
176else
177 cppcheck $CPPCHECK_ARGS --check-config 2>chk-config.xml
178fi
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100179
180echo
181echo '******* analyzing files with cppcheck ***************'
182echo
Dean Birch62c4f082020-01-17 16:13:26 +0000183if [ "$RAW_OUTPUT" == "1" ] ; then
184 cppcheck $CPPCHECK_ARGS > /dev/null
185 echo '******* cppcheck complete ***************'
186else
187 cppcheck $CPPCHECK_ARGS 2>chk-src.xml
188 echo
189 echo '******* Please check chk-config.xml and chk-src.xml for the results. ***************'
190 echo
191fi
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100192popd