Leonardo Sandoval | 9dfdd1b | 2020-08-06 17:08:11 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 2 | # |
Zelalem | c9531f8 | 2020-08-04 15:37:08 -0500 | [diff] [blame] | 3 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 4 | # |
| 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 |
| 31 | set -e |
| 32 | |
| 33 | |
| 34 | function 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 Sandoval | 1c24ae5 | 2020-07-08 11:47:23 -0500 | [diff] [blame] | 51 | aarch64-none-elf-gcc --version |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 52 | |
| 53 | # Check that the AArch32 cross-toolchain is available. |
Leonardo Sandoval | 1c24ae5 | 2020-07-08 11:47:23 -0500 | [diff] [blame] | 54 | arm-none-eabi-gcc --version |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 55 | |
| 56 | echo |
| 57 | echo "Checks complete." |
| 58 | echo |
| 59 | } |
| 60 | |
| 61 | |
| 62 | function 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 Sandoval | 1c24ae5 | 2020-07-08 11:47:23 -0500 | [diff] [blame] | 78 | --compiler aarch64-none-elf-gcc \ |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 79 | --config cov-config/config.xml |
| 80 | # 2) AArch32 compiler |
| 81 | cov-configure \ |
| 82 | --comptype gcc \ |
| 83 | --template \ |
Zelalem | c9531f8 | 2020-08-04 15:37:08 -0500 | [diff] [blame] | 84 | --compiler arm-none-eabi-gcc \ |
Fathi Boudra | 422bf77 | 2019-12-02 11:10:16 +0200 | [diff] [blame] | 85 | --config cov-config/config.xml |
| 86 | } |
| 87 | |
| 88 | |
| 89 | function 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 | |
| 110 | function 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 | |
| 154 | function 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 | ############################################################################### |
| 169 | PHASE="$1" |
| 170 | echo "Coverity: phase '$PHASE'" |
| 171 | shift |
| 172 | |
| 173 | case $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'" |
| 201 | esac |