Leonardo Sandoval | d05adab | 2020-08-10 14:01:54 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 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); |
Leonardo Sandoval | d05adab | 2020-08-10 14:01:54 -0500 | [diff] [blame] | 27 | # - the AArch32 cross-toolchain. |
| 28 | |
| 29 | # Bail out as soon as an error is encountered |
| 30 | set -e |
| 31 | |
| 32 | |
| 33 | function do_check_tools() |
| 34 | { |
| 35 | local mode="$1" |
| 36 | |
| 37 | echo |
| 38 | echo "Checking all required tools are available..." |
| 39 | echo |
| 40 | |
| 41 | # Print version of the Coverity tools. |
| 42 | # This also serves as a check that the tools are available. |
| 43 | cov-configure --ident |
| 44 | cov-build --ident |
| 45 | if [[ "$mode" == "offline" ]]; then |
| 46 | cov-analyze --ident |
| 47 | fi |
| 48 | |
Leonardo Sandoval | c503d7e | 2020-08-10 15:47:11 -0500 | [diff] [blame] | 49 | # Check that the GNUARM cross-toolchain is available. |
Leonardo Sandoval | d05adab | 2020-08-10 14:01:54 -0500 | [diff] [blame] | 50 | arm-none-eabi-gcc --version |
| 51 | |
| 52 | echo |
| 53 | echo "Checks complete." |
| 54 | echo |
| 55 | } |
| 56 | |
| 57 | |
| 58 | function do_configure() |
| 59 | { |
| 60 | # Create Coverity's configuration directory and its intermediate directory. |
| 61 | rm -rf cov-config cov-int |
| 62 | mkdir cov-config cov-int |
| 63 | |
| 64 | # Generate Coverity's configuration files. |
| 65 | # |
| 66 | # This needs to be done for each compiler. |
| 67 | # Each invocation of the cov-configure command adds a compiler configuration in |
| 68 | # its own subdirectory, and the top XML configuration file contains an include |
| 69 | # directive for that compiler-specific configuration. |
Leonardo Sandoval | c503d7e | 2020-08-10 15:47:11 -0500 | [diff] [blame] | 70 | # 1) AArch32 compiler |
Leonardo Sandoval | d05adab | 2020-08-10 14:01:54 -0500 | [diff] [blame] | 71 | cov-configure \ |
| 72 | --comptype gcc \ |
| 73 | --template \ |
| 74 | --compiler arm-none-eabi-gcc \ |
| 75 | --config cov-config/config.xml |
| 76 | } |
| 77 | |
| 78 | |
| 79 | function do_build() |
| 80 | { |
| 81 | local build_cmd=("$*") |
| 82 | |
| 83 | echo |
| 84 | echo "* The software will be built using the following command line:" |
| 85 | echo "$build_cmd" |
| 86 | echo |
| 87 | |
| 88 | # Build the instrumented binaries. |
| 89 | cov-build \ |
| 90 | --config cov-config/config.xml \ |
| 91 | --dir cov-int \ |
| 92 | $build_cmd |
| 93 | |
| 94 | echo |
| 95 | echo "Build complete." |
| 96 | echo |
| 97 | } |
| 98 | |
| 99 | |
| 100 | function do_analyze() |
| 101 | { |
| 102 | local out="$1" |
| 103 | local src_tree="$2" |
| 104 | local profile="$3" |
| 105 | out="${profile}_${out}" |
| 106 | |
| 107 | echo |
| 108 | echo "Starting the local analysis..." |
| 109 | echo " (Profile: $profile)" |
| 110 | echo |
| 111 | echo "The results will be saved into '$out'." |
| 112 | echo |
| 113 | |
| 114 | results_dir=$(pwd) |
| 115 | cd "$src_tree" |
| 116 | |
| 117 | # Analyze the instrumented binaries. |
| 118 | # Get the analysis settings from the right profile file. |
| 119 | cov-analyze \ |
| 120 | $(cat $(dirname "$0")/coverity_profile_${profile}) \ |
| 121 | ${analysis_settings[@]} \ |
| 122 | --dir "$results_dir/cov-int" \ |
| 123 | --verbose 0 \ |
| 124 | --redirect stdout,"$results_dir/$out" |
| 125 | |
| 126 | # Generate HTML pages |
| 127 | cov-format-errors \ |
| 128 | --html-output "$results_dir/results/html/${profile}" \ |
| 129 | --filesort \ |
| 130 | --dir "$results_dir/cov-int" |
| 131 | |
| 132 | # Generate text report |
| 133 | mkdir -p "$results_dir/results/text" |
| 134 | cov-format-errors \ |
| 135 | --emacs-style \ |
| 136 | --filesort \ |
| 137 | --dir "$results_dir/cov-int" \ |
| 138 | > "$results_dir/results/text/${profile}" |
| 139 | cd - |
| 140 | echo "Analysis complete." |
| 141 | } |
| 142 | |
| 143 | |
| 144 | function create_results_tarball() |
| 145 | { |
| 146 | local tarball_name="$1" |
| 147 | |
| 148 | echo |
| 149 | echo "Creating the tarball containing the results of the analysis..." |
| 150 | echo |
| 151 | tar -czvf "$tarball_name" cov-int/ |
| 152 | echo |
| 153 | echo "Complete." |
| 154 | echo |
| 155 | } |
| 156 | |
| 157 | |
| 158 | ############################################################################### |
| 159 | PHASE="$1" |
| 160 | echo "Coverity: phase '$PHASE'" |
| 161 | shift |
| 162 | |
| 163 | case $PHASE in |
| 164 | check_tools) |
| 165 | ANALYSIS_MODE="$1" |
| 166 | do_check_tools "$ANALYSIS_MODE" |
| 167 | ;; |
| 168 | |
| 169 | configure) |
| 170 | do_configure |
| 171 | ;; |
| 172 | |
| 173 | build) |
| 174 | do_build "$1" |
| 175 | ;; |
| 176 | |
| 177 | analyze) |
| 178 | OUTPUT_FILE="$1" |
| 179 | SOURCE_TREE="$2" |
| 180 | ANALYSIS_PROFILE="$3" |
| 181 | do_analyze "$OUTPUT_FILE" "$SOURCE_TREE" "$ANALYSIS_PROFILE" |
| 182 | ;; |
| 183 | |
| 184 | package) |
| 185 | OUTPUT_FILE="$1" |
| 186 | create_results_tarball "$OUTPUT_FILE" |
| 187 | ;; |
| 188 | |
| 189 | *) |
| 190 | echo "Invalid phase '$PHASE'" |
| 191 | esac |