Basil Eljuse | 4b14afb | 2020-09-30 13:07:23 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | ############################################################################## |
| 4 | # Copyright (c) 2020, ARM Limited and Contributors. All rights reserved. |
| 5 | # |
| 6 | # SPDX-License-Identifier: BSD-3-Clause |
| 7 | ############################################################################## |
| 8 | |
| 9 | #============================================================================== |
| 10 | # FILE: branch_coverage.sh |
| 11 | # |
| 12 | # DESCRIPTION: Generates intermediate layer json file and then |
| 13 | # code coverage HTML reports using LCOV report Open Source tool |
| 14 | #============================================================================== |
| 15 | |
| 16 | set +x |
| 17 | set -e |
| 18 | |
| 19 | ERROR_FILE=coverage_error.log |
| 20 | |
| 21 | ############################################################################### |
| 22 | # Prints error message to STDERR and log file. |
| 23 | # Globals: |
| 24 | # ERROR_FILE |
| 25 | # Arguments: |
| 26 | # None |
| 27 | # Outputs: |
| 28 | # Writes error to STDERR and log file with a timestamp |
| 29 | ############################################################################### |
| 30 | err() { |
| 31 | echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" | tee -a ${ERROR_FILE} 1>&2 |
| 32 | } |
| 33 | |
| 34 | touch ${ERROR_FILE} |
| 35 | if ! [ -x "$(command -v lcov)" ]; then |
| 36 | err 'Error: lcov is not installed. Install it with:\nsudo apt install lcov\n' |
| 37 | exit 1 |
| 38 | fi |
| 39 | |
| 40 | ############################################################################### |
| 41 | # Prints script usage. |
| 42 | # Arguments: |
| 43 | # None |
| 44 | # Outputs: |
| 45 | # Writes usage to stdout |
| 46 | ############################################################################### |
| 47 | usage() |
| 48 | { |
| 49 | # print the usage information |
| 50 | printf "Usage: $(basename $0) [options]\n" |
| 51 | printf "\t params:\n" |
| 52 | printf "\t --config Configuration json file. Required.\n" |
| 53 | printf "\t --workspace Local workspace folder where source codes reside. \ |
| 54 | Required.\n" |
| 55 | printf "\t --json-path Intermediate json file name. Optional defaults to \ |
| 56 | 'output_file.json'\n" |
| 57 | printf "\t --outdir Report folder. Optional defaults to 'out'\n" |
| 58 | printf "\t -h|--help Display usage\n" |
| 59 | printf "Example of usage:\n" |
| 60 | printf "./branch_coverage.sh --config config_file.json \ |
| 61 | --workspace /server_side/source/ --outdir html_report\n" |
| 62 | exit 1 |
| 63 | } |
| 64 | |
| 65 | # default values |
| 66 | JSON_PATH=output_file.json |
| 67 | OUTDIR=out |
| 68 | |
| 69 | ############################################################################### |
| 70 | # Parse arguments. |
| 71 | # Globals: |
| 72 | # CONFIG_JSON |
| 73 | # LOCAL_WORKSPACE |
| 74 | # JSON_PATH |
| 75 | # OUTDIR |
| 76 | # Arguments: |
| 77 | # Command line arguments |
| 78 | # Outputs: |
| 79 | # Writes usage to stdout |
| 80 | ############################################################################### |
| 81 | parse_arguments() |
| 82 | { |
| 83 | while [ $# -gt 1 ] |
| 84 | do |
| 85 | key="$1" |
| 86 | case $key in |
| 87 | --config) |
| 88 | CONFIG_JSON="$2" |
| 89 | shift |
| 90 | ;; |
| 91 | --workspace) |
| 92 | LOCAL_WORKSPACE="$2" |
| 93 | shift |
| 94 | ;; |
| 95 | --json-path) |
| 96 | JSON_PATH="$2" |
| 97 | shift |
| 98 | ;; |
| 99 | --outdir) |
| 100 | OUTDIR="$2" |
| 101 | shift |
| 102 | ;; |
| 103 | -h|--help) |
| 104 | usage |
| 105 | ;; |
| 106 | *) |
| 107 | printf "Unknown argument $key\n" |
| 108 | usage |
| 109 | ;; |
| 110 | esac |
| 111 | shift |
| 112 | done |
| 113 | } |
| 114 | |
| 115 | |
| 116 | parse_arguments $@ |
| 117 | |
| 118 | if [ -z "$LOCAL_WORKSPACE" ] || [ -z "$CONFIG_JSON" ]; then |
| 119 | usage |
| 120 | fi |
| 121 | |
| 122 | if [ ! -d "$LOCAL_WORKSPACE" ]; then |
| 123 | err "$LOCAL_WORKSPACE doesn't exist\n" |
| 124 | exit 1 |
| 125 | fi |
| 126 | |
| 127 | if [ ! -f "$CONFIG_JSON" ]; then |
| 128 | err "$CONFIG_JSON doesn't exist\n" |
| 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | clear |
| 133 | echo "Generating intermediate layer file '$JSON_PATH'..." |
| 134 | python3 intermediate_layer.py --config-json "$CONFIG_JSON" --local-workspace $LOCAL_WORKSPACE |
| 135 | echo "Converting intermediate layer file to info file..." |
| 136 | python3 generate_info_file.py --workspace $LOCAL_WORKSPACE --json $JSON_PATH |
| 137 | echo "Generating LCOV report at '$OUTDIR'..." |
| 138 | genhtml --branch-coverage coverage.info --output-directory $OUTDIR |
| 139 | mv coverage.info $OUTDIR/coverage.info |
| 140 | mv error_log.txt $OUTDIR/error_log.txt |