Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 1 | #!/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. |
| 44 | set -e |
| 45 | |
| 46 | #The location from where the script executes |
| 47 | mypath=$(dirname $0) |
| 48 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 49 | #The cmake_exported project file in json format |
| 50 | cmake_commmands=compile_commands.json |
| 51 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 52 | . "$mypath/util_cmake.sh" |
| 53 | |
| 54 | |
| 55 | #Library file for cppcheck |
| 56 | library_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/arm-cortex-m.cfg" |
| 57 | suppress_file="$(fix_win_path $(get_full_path $mypath))/cppcheck/tfm-suppress-list.txt" |
| 58 | |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 59 | #Enable all additional checks by default |
| 60 | additional_checklist="all" |
| 61 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 62 | #Run cmake to get the compile_commands.json file |
| 63 | echo |
| 64 | echo '******* Generating compile_commandas.json ***************' |
| 65 | echo |
| 66 | generate_project $(fix_win_path $(get_full_path ./)) "./" "cppcheck" "-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTARGET_PLATFORM=AN521 -DCOMPILER=GNUARM" |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 67 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 68 | #Enter the build directory |
| 69 | bdir=$(make_build_dir_name "./" "cppcheck") |
| 70 | pushd "$bdir" >/dev/null |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 71 | |
| 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 |
| 76 | if [[ ! -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 |
| 126 | fi |
| 127 | |
| 128 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 129 | #Build the external projects to get all headers installed to plases from where |
| 130 | #tf-m code uses them |
| 131 | echo |
| 132 | echo '******* Install external projects to their final place ***************' |
| 133 | echo |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 134 | make -j mbedtls_mcuboot_lib_install |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 135 | |
| 136 | #Now run cppcheck. |
| 137 | echo |
| 138 | echo '******* checking cppcheck configuration ***************' |
| 139 | echo |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 140 | |
| 141 | cppcheck --xml --check-config --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-config.xml |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 142 | |
| 143 | echo |
| 144 | echo '******* analyzing files with cppcheck ***************' |
| 145 | echo |
Minos Galanakis | ea42123 | 2019-06-20 17:11:28 +0100 | [diff] [blame^] | 146 | cppcheck --xml --enable="$additional_checklist" --library="$library_file" --project=$cmake_commmands --suppressions-list="$suppress_file" --inline-suppr 2>chk-src.xml |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 147 | popd |
| 148 | |
| 149 | echo |
| 150 | echo '******* Please check chk-config.xml and chk-src.xml for the results. ***************' |
| 151 | echo |