blob: 9b946a871db310fd834583ef97468f439ff9e2e4 [file] [log] [blame]
Basil Eljuse4b14afb2020-09-30 13:07:23 +01001#!/usr/bin/env bash
2
3##############################################################################
Saul Romero74ddc022025-08-14 14:40:22 +00004# Copyright (c) 2025, ARM Limited and Contributors. All rights reserved.
Basil Eljuse4b14afb2020-09-30 13:07:23 +01005#
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"
Saul Romero74ddc022025-08-14 14:40:22 +000057 printf "\t --genhtml-<parameter>=arg Arguments to be passed to \
58 genhtml[Optional]\n"
Basil Eljuse4b14afb2020-09-30 13:07:23 +010059 printf "\t --outdir Report folder. Optional defaults to 'out'\n"
60 printf "\t -h|--help Display usage\n"
61 printf "Example of usage:\n"
62 printf "./branch_coverage.sh --config config_file.json \
Saul Romero74ddc022025-08-14 14:40:22 +000063 --workspace /server_side/source/ --outdir html_report \
64 --genhtml-title='My code coverage'\n"
Basil Eljuse4b14afb2020-09-30 13:07:23 +010065 exit 1
66}
67
68# default values
69JSON_PATH=output_file.json
70OUTDIR=out
71
72###############################################################################
73# Parse arguments.
74# Globals:
75# CONFIG_JSON
76# LOCAL_WORKSPACE
77# JSON_PATH
78# OUTDIR
79# Arguments:
80# Command line arguments
81# Outputs:
82# Writes usage to stdout
83###############################################################################
84parse_arguments()
85{
Saul Romero74ddc022025-08-14 14:40:22 +000086 genhtml_args=()
87 genhtml_params=()
88 while [ $# -ge 1 ]
Basil Eljuse4b14afb2020-09-30 13:07:23 +010089 do
90 key="$1"
91 case $key in
92 --config)
93 CONFIG_JSON="$2"
94 shift
95 ;;
96 --workspace)
97 LOCAL_WORKSPACE="$2"
98 shift
99 ;;
100 --json-path)
101 JSON_PATH="$2"
102 shift
103 ;;
104 --outdir)
105 OUTDIR="$2"
106 shift
107 ;;
Saul Romero74ddc022025-08-14 14:40:22 +0000108 --genhtml-*)
109 input="${key#--genhtml-}"
110 key="${input%%=*}"
111 value="${input#*=}"
112 genhtml_params+=("--${key}")
113 genhtml_args+=("${value}")
114 ;;
Basil Eljuse4b14afb2020-09-30 13:07:23 +0100115 -h|--help)
116 usage
117 ;;
118 *)
119 printf "Unknown argument $key\n"
120 usage
121 ;;
122 esac
123 shift
124 done
125}
126
127
Saul Romero74ddc022025-08-14 14:40:22 +0000128parse_arguments "$@"
Basil Eljuse4b14afb2020-09-30 13:07:23 +0100129
130if [ -z "$LOCAL_WORKSPACE" ] || [ -z "$CONFIG_JSON" ]; then
131 usage
132fi
133
134if [ ! -d "$LOCAL_WORKSPACE" ]; then
135 err "$LOCAL_WORKSPACE doesn't exist\n"
136 exit 1
137fi
138
139if [ ! -f "$CONFIG_JSON" ]; then
140 err "$CONFIG_JSON doesn't exist\n"
141 exit 1
142fi
143
Leonardo Sandoval2291baa2021-07-14 13:07:16 -0500144# clear may fail within a container-enviroment due to lack of
145# TERM enviroment, so ignore this and other possible errors
146clear || true
Saul Romero74ddc022025-08-14 14:40:22 +0000147#declare -p genhtml_params
148#declare -p genhtml_args
Leonardo Sandoval2291baa2021-07-14 13:07:16 -0500149
Basil Eljuse4b14afb2020-09-30 13:07:23 +0100150echo "Generating intermediate layer file '$JSON_PATH'..."
151python3 intermediate_layer.py --config-json "$CONFIG_JSON" --local-workspace $LOCAL_WORKSPACE
152echo "Converting intermediate layer file to info file..."
153python3 generate_info_file.py --workspace $LOCAL_WORKSPACE --json $JSON_PATH
154echo "Generating LCOV report at '$OUTDIR'..."
Saul Romero74ddc022025-08-14 14:40:22 +0000155s=""
156for (( j=0; j<${#genhtml_params[@]}; j++ ));
157do
158 s="${s} ${genhtml_params[$j]} "\"${genhtml_args[$j]}\"
159done
160
161eval genhtml --branch-coverage coverage.info --output-directory $OUTDIR $s || (genhtml --version;genhtml --help)
Basil Eljuse4b14afb2020-09-30 13:07:23 +0100162mv coverage.info $OUTDIR/coverage.info
163mv error_log.txt $OUTDIR/error_log.txt