blob: 467e5e51222a5d8865e89f1a63ba38ba1f8b1747 [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/bin/bash
2#-------------------------------------------------------------------------------
3# Copyright (c) 2018-2019, Arm Limited and Contributors. All rights reserved.
4#
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
16##configuration (i.e. no build config file is specifyed on the command-line).
17##
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
35## might be invalid, and also a differnet file may be needed based on
36## used compiler switches (i.e. to match witdh specification and or default
37## 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.
40## This might need to be revied/changed in the future.
41##
42
43#Fail if any command exit with error.
44set -e
45
46#The location from where the script executes
47mypath=$(dirname $0)
48
Minos Galanakisea421232019-06-20 17:11:28 +010049#The cmake_exported project file in json format
50cmake_commmands=compile_commands.json
51
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010052. "$mypath/util_cmake.sh"
53
54
55#Library file for cppcheck
56library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg"
57suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt"
58
Minos Galanakisea421232019-06-20 17:11:28 +010059#Enable all additional checks by default
60additional_checklist="all"
61
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010062#Run cmake to get the compile_commands.json file
63echo
64echo '******* Generating compile_commandas.json ***************'
65echo
66generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTARGET_PLATFORM=AN521 -DCOMPILER=GNUARM"
Minos Galanakisea421232019-06-20 17:11:28 +010067
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010068#Enter the build directory
69bdir=$(make_build_dir_name "./" "cppcheck")
70pushd "$bdir" >/dev/null
Minos Galanakisea421232019-06-20 17:11:28 +010071
72#The following snippet allows cppcheck to be run differentially againist a
73#commit hash passed as first argument $1. It does not
74#affect the legacy functionality of the script, checking the whole codebase,
75#when called without an argument
76if [[ ! -z "$1" ]]
77 then
78 echo "Enabled git-diff mode againist hash: $1"
79
80 # Do not execute unused functioncheck when running in diff-mode
81 additional_checklist="style,performance,portability,information,missingInclude"
82 # Grep will set exit status to 1 if a commit does not contain c/cpp.. files
83 set +e
84 filtered_cmd_f=compile_commands_filtered.json
85 # Get a list of files modified by the commits between the reference and HEAD
86 flist=$(git diff-tree --no-commit-id --name-only -r $1 | grep -E '\S*\.(c|cpp|cc|cxx|inc|h)$')
87 flist=$(echo $flist | xargs)
88 echo -e "[" > $filtered_cmd_f
89 IFS=$' ' read -ra git_flist <<< "${flist}"
90
91 for fl in "${git_flist[@]}"; do
92 echo "Looking for reference of file: $fl"
93
94 # dry run the command to see if there any ouput
95 JSON_CMD=$(grep -B 3 "\"file\": \".*$fl\"" $cmake_commmands)
96
97 if [ -n "${JSON_CMD}" ]; then
98 command_matched=1
99 grep -B 3 "\"file\": \".*$fl\"" $cmake_commmands >> $filtered_cmd_f
100 echo -e "}," >> $filtered_cmd_f
101 fi
102 done
103 set -e
104
105 # Only continue if files in the patch are included in the build commands
106 if [ -n "${command_matched}" ]; then
107 sed -i '$ d' $filtered_cmd_f
108 echo -e "}\n]" >> $filtered_cmd_f
109
110 cat $filtered_cmd_f > $cmake_commmands
111 else
112 # Always generate an empty file for other stages of ci expecting one
113 echo "CppCheck: Ignoring files not contained in the build config"
114 echo "Files Ignored: $flist"
115 cat <<-EOF > chk-config.xml
116 <?xml version="1.0" encoding="UTF-8"?>
117 <results version="2">
118 <cppcheck version="$(cppcheck --version)"/>
119 <errors>
120 </errors>
121 </results>
122 EOF
123 cp chk-config.xml chk-src.xml
124 exit 0
125 fi
126fi
127
128
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100129#Build the external projects to get all headers installed to plases from where
130#tf-m code uses them
131echo
132echo '******* Install external projects to their final place ***************'
133echo
Minos Galanakisea421232019-06-20 17:11:28 +0100134make -j mbedtls_mcuboot_lib_install
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100135
136#Now run cppcheck.
137echo
138echo '******* checking cppcheck configuration ***************'
139echo
Minos Galanakisea421232019-06-20 17:11:28 +0100140
141cppcheck --xml --check-config --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-config.xml
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100142
143echo
144echo '******* analyzing files with cppcheck ***************'
145echo
Minos Galanakisea421232019-06-20 17:11:28 +0100146cppcheck --xml --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-src.xml
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100147popd
148
149echo
150echo '******* Please check chk-config.xml and chk-src.xml for the results. ***************'
151echo