1#! /usr/bin/env bash
2# Copyright 2023 Nordic Semiconductor ASA
3# SPDX-License-Identifier: Apache-2.0
4
5set -eu
6
7function display_help(){
8  echo "\
9_generate_coverage_report.sh [-help]
10  Generate an html coverage report for BabbleSim tests
11
12  You can call it from the Zephyr top level as:
13    tests/bsim/_generate_coverage_report.sh
14
15  Coverage files will be searched for in the folder pointed by
16  the variable WORK_DIR which by default is
17  ${ZEPHYR_BASE}/bsim_out
18
19  By default the output will be placed in \${WORK_DIR}/lcov_html/.
20  You can override this by setting the variable OUTPUT_DIR
21
22  By default it takes all coverage information generated by all run tests
23  but you can limit the search by setting WORK_DIR to some subfolder
24
25  You can also merge in the twister coverage report by setting
26  the variable TWISTER_COVERAGE_FILE, for example as:
27  TWISTER_COVERAGE_FILE=\${ZEPHYR_BASE}/twister-out/coverage.info
28  after having run twister, for example as
29  twister -p nrf52_bsim -T tests/bluetooth/ --coverage
30
31  Note: Generating a coverage report for many tests is a lengthy process
32"
33}
34
35# Parse command line
36if [ $# -ge 1 ]; then
37  if grep -Eiq "(\?|-\?|-h|help|-help|--help)" <<< $1 ; then
38    display_help
39    exit 0
40  fi
41fi
42
43WORK_DIR="${WORK_DIR:-${ZEPHYR_BASE}/bsim_out}"
44OUTPUT_DIR="${OUTPUT_DIR:-${WORK_DIR}}"
45TWISTER_COVERAGE_FILE="${TWISTER_COVERAGE_FILE:-""}"
46
47lcov --capture --directory ${WORK_DIR} --output-file ${OUTPUT_DIR}/coverage.pre.info \
48  -q --rc lcov_branch_coverage=1
49lcov --remove ${OUTPUT_DIR}/coverage.pre.info *generated* \
50  --output-file ${OUTPUT_DIR}/coverage.info -q --rc lcov_branch_coverage=1
51genhtml ${OUTPUT_DIR}/coverage.info ${TWISTER_COVERAGE_FILE} --output-directory \
52   ${OUTPUT_DIR}/lcov_html -q --ignore-errors source --branch-coverage --highlight --legend
53
54echo -e "\033[0;32mGenerated: ${OUTPUT_DIR}/lcov_html/index.html\033[0m"
55