blob: b772b7e672891f7225113da29730f7a5b9d6c39f [file] [log] [blame]
Leonardo Sandoval9dfdd1b2020-08-06 17:08:11 -05001#!/usr/bin/env bash
Fathi Boudra422bf772019-12-02 11:10:16 +02002#
Zelalemc9531f82020-08-04 15:37:08 -05003# Copyright (c) 2019-2020, Arm Limited. All rights reserved.
Fathi Boudra422bf772019-12-02 11:10:16 +02004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8#
9# Run Coverity on a source tree.
10# Then:
11# - either produce a tarball ready to be submitted to Coverity Scan Online
12# [online mode]
13# - or locally analyze and create a text report and HTML pages of the analysis
14# [offline mode]
15#
16# The following arguments must be passed to this script:
17# 1. The command to use to build the software (this can be a script).
18# 2. The mode: "online" or "offline".
19# 3. The name of the output file to produce.
20# In the online mode, this should be a tarball name.
21# In the offline mode, this should be a text file name.
22# 4. In the offline mode, the path to the source tree to analyze.
23#
24# Assumptions:
25# The following tools are loaded in the PATH:
26# - the Coverity tools (cov-configure, cov-build, and so on);
27# - the AArch64 cross-toolchain;
28# - the AArch32 cross-toolchain.
29
30# Bail out as soon as an error is encountered
31set -e
32
33
34function do_check_tools()
35{
36 local mode="$1"
37
38 echo
39 echo "Checking all required tools are available..."
40 echo
41
42 # Print version of the Coverity tools.
43 # This also serves as a check that the tools are available.
44 cov-configure --ident
45 cov-build --ident
46 if [[ "$mode" == "offline" ]]; then
47 cov-analyze --ident
48 fi
49
50 # Check that the AArch64 cross-toolchain is available.
Leonardo Sandoval1c24ae52020-07-08 11:47:23 -050051 aarch64-none-elf-gcc --version
Fathi Boudra422bf772019-12-02 11:10:16 +020052
53 # Check that the AArch32 cross-toolchain is available.
Leonardo Sandoval1c24ae52020-07-08 11:47:23 -050054 arm-none-eabi-gcc --version
Fathi Boudra422bf772019-12-02 11:10:16 +020055
56 echo
57 echo "Checks complete."
58 echo
59}
60
61
62function do_configure()
63{
64 # Create Coverity's configuration directory and its intermediate directory.
65 rm -rf cov-config cov-int
66 mkdir cov-config cov-int
67
68 # Generate Coverity's configuration files.
69 #
70 # This needs to be done for each compiler.
71 # Each invocation of the cov-configure command adds a compiler configuration in
72 # its own subdirectory, and the top XML configuration file contains an include
73 # directive for that compiler-specific configuration.
74 # 1) AArch64 compiler
75 cov-configure \
76 --comptype gcc \
77 --template \
Leonardo Sandoval1c24ae52020-07-08 11:47:23 -050078 --compiler aarch64-none-elf-gcc \
Fathi Boudra422bf772019-12-02 11:10:16 +020079 --config cov-config/config.xml
80 # 2) AArch32 compiler
81 cov-configure \
82 --comptype gcc \
83 --template \
Zelalemc9531f82020-08-04 15:37:08 -050084 --compiler arm-none-eabi-gcc \
Fathi Boudra422bf772019-12-02 11:10:16 +020085 --config cov-config/config.xml
86}
87
88
89function do_build()
90{
91 local build_cmd=("$*")
92
93 echo
94 echo "* The software will be built using the following command line:"
95 echo "$build_cmd"
96 echo
97
98 # Build the instrumented binaries.
99 cov-build \
100 --config cov-config/config.xml \
101 --dir cov-int \
102 $build_cmd
103
104 echo
105 echo "Build complete."
106 echo
107}
108
109
110function do_analyze()
111{
112 local out="$1"
113 local src_tree="$2"
114 local profile="$3"
115 out="${profile}_${out}"
116
117 echo
118 echo "Starting the local analysis..."
119 echo " (Profile: $profile)"
120 echo
121 echo "The results will be saved into '$out'."
122 echo
123
124 results_dir=$(pwd)
125 cd "$src_tree"
126
127 # Analyze the instrumented binaries.
128 # Get the analysis settings from the right profile file.
129 cov-analyze \
130 $(cat $(dirname "$0")/coverity_profile_${profile}) \
131 ${analysis_settings[@]} \
132 --dir "$results_dir/cov-int" \
133 --verbose 0 \
134 --redirect stdout,"$results_dir/$out"
135
136 # Generate HTML pages
137 cov-format-errors \
138 --html-output "$results_dir/results/html/${profile}" \
139 --filesort \
140 --dir "$results_dir/cov-int"
141
142 # Generate text report
143 mkdir -p "$results_dir/results/text"
144 cov-format-errors \
145 --emacs-style \
146 --filesort \
147 --dir "$results_dir/cov-int" \
148 > "$results_dir/results/text/${profile}"
149 cd -
150 echo "Analysis complete."
151}
152
153
154function create_results_tarball()
155{
156 local tarball_name="$1"
157
158 echo
159 echo "Creating the tarball containing the results of the analysis..."
160 echo
161 tar -czvf "$tarball_name" cov-int/
162 echo
163 echo "Complete."
164 echo
165}
166
167
168###############################################################################
169PHASE="$1"
170echo "Coverity: phase '$PHASE'"
171shift
172
173case $PHASE in
174 check_tools)
175 ANALYSIS_MODE="$1"
176 do_check_tools "$ANALYSIS_MODE"
177 ;;
178
179 configure)
180 do_configure
181 ;;
182
183 build)
184 do_build "$1"
185 ;;
186
187 analyze)
188 OUTPUT_FILE="$1"
189 SOURCE_TREE="$2"
190 ANALYSIS_PROFILE="$3"
191 do_analyze "$OUTPUT_FILE" "$SOURCE_TREE" "$ANALYSIS_PROFILE"
192 ;;
193
194 package)
195 OUTPUT_FILE="$1"
196 create_results_tarball "$OUTPUT_FILE"
197 ;;
198
199 *)
200 echo "Invalid phase '$PHASE'"
201esac