blob: 3dc88f3e49f56891fe2f10fb561565658b785a95 [file] [log] [blame]
Basil Eljuse4b14afb2020-09-30 13:07:23 +01001#!/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
16set +x
17set -e
18
19ERROR_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###############################################################################
30err() {
31 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" | tee -a ${ERROR_FILE} 1>&2
32}
33
34touch ${ERROR_FILE}
35if ! [ -x "$(command -v lcov)" ]; then
36 err 'Error: lcov is not installed. Install it with:\nsudo apt install lcov\n'
37 exit 1
38fi
39
40###############################################################################
41# Prints script usage.
42# Arguments:
43# None
44# Outputs:
45# Writes usage to stdout
46###############################################################################
47usage()
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
66JSON_PATH=output_file.json
67OUTDIR=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###############################################################################
81parse_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
116parse_arguments $@
117
118if [ -z "$LOCAL_WORKSPACE" ] || [ -z "$CONFIG_JSON" ]; then
119 usage
120fi
121
122if [ ! -d "$LOCAL_WORKSPACE" ]; then
123 err "$LOCAL_WORKSPACE doesn't exist\n"
124 exit 1
125fi
126
127if [ ! -f "$CONFIG_JSON" ]; then
128 err "$CONFIG_JSON doesn't exist\n"
129 exit 1
130fi
131
132clear
133echo "Generating intermediate layer file '$JSON_PATH'..."
134python3 intermediate_layer.py --config-json "$CONFIG_JSON" --local-workspace $LOCAL_WORKSPACE
135echo "Converting intermediate layer file to info file..."
136python3 generate_info_file.py --workspace $LOCAL_WORKSPACE --json $JSON_PATH
137echo "Generating LCOV report at '$OUTDIR'..."
138genhtml --branch-coverage coverage.info --output-directory $OUTDIR
139mv coverage.info $OUTDIR/coverage.info
140mv error_log.txt $OUTDIR/error_log.txt