Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | #------------------------------------------------------------------------------- |
Minos Galanakis | 9c77582 | 2020-10-16 14:31:33 +0100 | [diff] [blame] | 3 | # Copyright (c) 2018-2020, Arm Limited and Contributors. All rights reserved. |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 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 |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 16 | ##configuration (i.e. no build config file is specified on the command-line). |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 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 |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 35 | ## 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 Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 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. |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 40 | ## This might need to be revised/changed in the future. |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 41 | ## |
| 42 | |
| 43 | #Fail if any command exit with error. |
| 44 | set -e |
| 45 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 46 | RAW_OUTPUT=0 |
| 47 | |
| 48 | while 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 |
| 60 | done |
| 61 | |
| 62 | shift $((OPTIND-1)) |
| 63 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 64 | #The location from where the script executes |
| 65 | mypath=$(dirname $0) |
| 66 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 67 | #The cmake_exported project file in json format |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 68 | cmake_commands=compile_commands.json |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 69 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 70 | . "$mypath/util_cmake.sh" |
| 71 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 72 | #Library file for cppcheck |
| 73 | library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg" |
| 74 | suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt" |
Xinyu Zhang | 11df53b | 2020-12-11 14:26:29 +0800 | [diff] [blame] | 75 | toolchain_file="$(fix_win_path $(get_full_path ./))/toolchain_GNUARM.cmake" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 76 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 77 | #Enable all additional checks by default |
| 78 | additional_checklist="all" |
| 79 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 80 | #Run cmake to get the compile_commands.json file |
| 81 | echo |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 82 | echo '******* Generating compile_commands.json ***************' |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 83 | echo |
Xinyu Zhang | 11df53b | 2020-12-11 14:26:29 +0800 | [diff] [blame] | 84 | generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTFM_PLATFORM=mps2/an521 -DTFM_TOOLCHAIN_FILE=$toolchain_file" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 85 | #Enter the build directory |
| 86 | bdir=$(make_build_dir_name "./" "cppcheck") |
| 87 | pushd "$bdir" >/dev/null |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 88 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 89 | #The following snippet allows cppcheck to be run differentially against a |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 90 | #commit hash passed as first argument $1. It does not |
| 91 | #affect the legacy functionality of the script, checking the whole codebase, |
| 92 | #when called without an argument |
| 93 | if [[ ! -z "$1" ]] |
| 94 | then |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 95 | echo "Enabled git-diff mode against hash: $1" |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 96 | |
Xinyu Zhang | 7de294d | 2021-03-08 10:09:32 +0800 | [diff] [blame] | 97 | # Do not execute unused function check and information check when running in diff-mode |
| 98 | additional_checklist="style,performance,portability,missingInclude" |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 99 | # Grep will set exit status to 1 if a commit does not contain c/cpp.. files |
| 100 | set +e |
| 101 | filtered_cmd_f=compile_commands_filtered.json |
| 102 | # Get a list of files modified by the commits between the reference and HEAD |
| 103 | flist=$(git diff-tree --no-commit-id --name-only -r $1 | grep -E '\S*\.(c|cpp|cc|cxx|inc|h)$') |
| 104 | flist=$(echo $flist | xargs) |
| 105 | echo -e "[" > $filtered_cmd_f |
| 106 | IFS=$' ' read -ra git_flist <<< "${flist}" |
| 107 | |
| 108 | for fl in "${git_flist[@]}"; do |
| 109 | echo "Looking for reference of file: $fl" |
| 110 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 111 | # dry run the command to see if there any output |
| 112 | JSON_CMD=$(grep -B 3 "\"file\": \".*$fl\"" $cmake_commands) |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 113 | |
| 114 | if [ -n "${JSON_CMD}" ]; then |
| 115 | command_matched=1 |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 116 | grep -B 3 "\"file\": \".*$fl\"" $cmake_commands >> $filtered_cmd_f |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 117 | echo -e "}," >> $filtered_cmd_f |
| 118 | fi |
| 119 | done |
| 120 | set -e |
| 121 | |
| 122 | # Only continue if files in the patch are included in the build commands |
| 123 | if [ -n "${command_matched}" ]; then |
| 124 | sed -i '$ d' $filtered_cmd_f |
| 125 | echo -e "}\n]" >> $filtered_cmd_f |
| 126 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 127 | cat $filtered_cmd_f > $cmake_commands |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 128 | else |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 129 | echo "CppCheck: Ignoring files not contained in the build config" |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 130 | if [ "$RAW_OUTPUT" == "0" ] ; then |
| 131 | # Always generate an empty file for other stages of ci expecting one |
Dean Birch | 15c8ffe | 2020-05-29 13:31:19 +0100 | [diff] [blame] | 132 | echo "Files Ignored: $flist" |
| 133 | cat <<-EOF > chk-config.xml |
| 134 | <?xml version="1.0" encoding="UTF-8"?> |
| 135 | <results version="2"> |
| 136 | <cppcheck version="$(cppcheck --version)"/> |
| 137 | <errors> |
| 138 | </errors> |
| 139 | </results> |
| 140 | EOF |
| 141 | cp chk-config.xml chk-src.xml |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 142 | fi |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 143 | exit 0 |
| 144 | fi |
| 145 | fi |
| 146 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 147 | function cppcheck_failed { |
Xinyu Zhang | e376a82 | 2021-02-25 13:22:31 +0800 | [diff] [blame] | 148 | # echo "cppcheck failed." |
| 149 | # echo "Check log for errors." |
| 150 | echo "CppCheck needs to be updated. Current Version would fail with no error." |
| 151 | echo "Skip voting for the time being." |
Xinyu Zhang | 7de294d | 2021-03-08 10:09:32 +0800 | [diff] [blame] | 152 | exit 1 |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 153 | } |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 154 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 155 | EXTRA_ARGS="--error-exitcode=1" |
| 156 | if [ "$RAW_OUTPUT" != "1" ] ; then |
| 157 | # If not in raw output mode, use xml output. |
| 158 | EXTRA_ARGS="--xml" |
| 159 | else |
| 160 | trap cppcheck_failed ERR |
| 161 | fi |
| 162 | CPPCHECK_ARGS="$EXTRA_ARGS --enable="$additional_checklist" --library="$library_file" --project=$cmake_commands --suppressions-list="$suppress_file" --inline-suppr" |
| 163 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 164 | #Now run cppcheck. |
| 165 | echo |
| 166 | echo '******* checking cppcheck configuration ***************' |
| 167 | echo |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame] | 168 | |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 169 | if [ "$RAW_OUTPUT" == "1" ] ; then |
| 170 | cppcheck $CPPCHECK_ARGS --check-config > /dev/null |
| 171 | else |
| 172 | cppcheck $CPPCHECK_ARGS --check-config 2>chk-config.xml |
| 173 | fi |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 174 | |
| 175 | echo |
| 176 | echo '******* analyzing files with cppcheck ***************' |
| 177 | echo |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 178 | if [ "$RAW_OUTPUT" == "1" ] ; then |
| 179 | cppcheck $CPPCHECK_ARGS > /dev/null |
| 180 | echo '******* cppcheck complete ***************' |
| 181 | else |
| 182 | cppcheck $CPPCHECK_ARGS 2>chk-src.xml |
| 183 | echo |
| 184 | echo '******* Please check chk-config.xml and chk-src.xml for the results. ***************' |
| 185 | echo |
| 186 | fi |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 187 | popd |