blob: cbf1060cd56ec7dafd805f6871c23c47a5e09b38 [file] [log] [blame]
Hugo L'Hostis8c7cb642021-04-22 12:12:05 +01001#!/bin/bash
2#-------------------------------------------------------------------------------
3# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
9set -e
10
11script_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
12
13. "$script_path/../utils.sh"
14
15TFM_PATH="$(fix_win_path $(get_full_path ./))"
16RAW_OUTPUT=0
17FAIL=0
18
19while getopts "hr" opt ; do
20 case "$opt" in
21 h)
22 echo "[SCF cppcheck] Usage: $(basename -- "$0") [-h] [-r] [git_hash]"
23 echo "[SCF cppcheck] -r, Raw output. (Default is to create xml reports)."
24 echo "[SCF cppcheck] -h, Script help"
25 exit 0
26 ;;
27 r)
28 RAW_OUTPUT=1
29 ;;
30 esac
31done
32
33
34if command -v cppcheck &> /dev/null
35then
36 echo "[SCF cppcheck] Using $(cppcheck --version)"
37else
38 echo "[SCF cppcheck] cppcheck not found - installing cppcheck"
39 source "$script_path/setup.sh"
40fi
41
42cd "$TFM_PATH"
43
44#Library file for cppcheck
45library_file="$(fix_win_path $(get_full_path $script_path))/config/arm-cortex-m.cfg"
46suppress_file="$(fix_win_path $(get_full_path $script_path))/config/tfm-suppress-list.txt"
47toolchain_file="$TFM_PATH/toolchain_GNUARM.cmake"
48
49compile_commands_path="$TFM_PATH/build-cppcheck/compile_commands.json"
50
51if [ -f "$compile_commands_path" ]; then
52 echo "[SCF cppcheck] $compile_commands_path already generated"
53else
54 echo "[SCF cppcheck] generating $compile_commands_path"
55 generate_project "$TFM_PATH" " ./" "cppcheck" "-DCMAKE_EXPORT_COMPILE_COMMANDS=1 -DTFM_PLATFORM=arm/mps2/an521 -DTFM_TOOLCHAIN_FILE=$toolchain_file"
56 sed -i 's/\\\\\\\"/\\\"/g' $compile_commands_path
57fi
58
59function cppcheck_failed {
60 echo "[SCF cppcheck: ERROR] cppcheck failed, errors detected. Please check" \
61 "logs for details."
62 exit 1
63}
64
65function check_ouput {
66 if grep -q "<error id" $TFM_PATH/checks_reports/cppchk-config.xml; then
67 cppcheck_failed
68 fi
69
70 if grep -q "<error id" $TFM_PATH/checks_reports/cppchk-src.xml; then
71 cppcheck_failed
72 fi
73
74 echo "[SCF cppcheck: PASS] cppcheck complete"
75 exit 0
76}
77
78EXTRA_ARGS="--error-exitcode=1"
79if [ "$RAW_OUTPUT" != "1" ] ; then
80 # If not in raw output mode, use xml output.
81 EXTRA_ARGS="--xml"
82fi
83
84trap cppcheck_failed ERR
85
86CPPCHECK_ARGS="$EXTRA_ARGS --enable=all --library="$library_file" --project=$compile_commands_path --suppressions-list="$suppress_file" --inline-suppr"
87
88if [ -d "$TFM_PATH/checks_reports/" ]; then
89 echo "[SCF cppcheck] Storing cppcheck reports to $TFM_PATH/checks_reports/"
90else
91 mkdir "$TFM_PATH/checks_reports"
92 echo "[SCF cppcheck] Storing cppcheck reports to $TFM_PATH/checks_reports/"
93fi
94
95#Now run cppcheck.
96echo
97echo '[SCF cppcheck] Checking cppcheck configuration'
98echo
99
100if [ "$RAW_OUTPUT" == "1" ] ; then
101 cppcheck $CPPCHECK_ARGS --check-config > /dev/null
102else
103 cppcheck $CPPCHECK_ARGS --check-config 2>checks_reports/cppchk-config.xml
104fi
105
106echo
107echo '[SCF cppcheck] analyzing files with cppcheck'
108echo
109if [ "$RAW_OUTPUT" == "1" ] ; then
110 cppcheck $CPPCHECK_ARGS > /dev/null
111 echo '[SCF cppcheck : PASS] cppcheck complete'
112 exit 0
113else
114 cppcheck $CPPCHECK_ARGS 2>checks_reports/cppchk-src.xml
115 check_ouput
116fi
117