1#!/bin/sh
2
3help () {
4    cat <<EOF
5Usage: $0 [-r]
6Collect coverage statistics of library code into an HTML report.
7
8General instructions:
91. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
10   programs with LDFLAGS="--coverage".
11   This can be an out-of-tree build.
12   For example (in-tree):
13        make CFLAGS="--coverage -O0 -g3" LDFLAGS="--coverage"
14   Or (out-of-tree):
15        mkdir build-coverage && cd build-coverage &&
16        cmake -D CMAKE_BUILD_TYPE=Coverage .. && make
172. Run whatever tests you want.
183. Run this script from the parent of the directory containing the library
19   object files and coverage statistics files.
204. Browse the coverage report in Coverage/index.html.
215. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
22
23Options
24  -r    Reset traces. Run this before re-testing to get fresh measurements.
25EOF
26}
27
28# Copyright The Mbed TLS Contributors
29# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
30
31set -eu
32
33# Collect stats and build a HTML report.
34lcov_library_report () {
35    rm -rf Coverage
36    mkdir Coverage Coverage/tmp
37    lcov --capture --initial --directory library -o Coverage/tmp/files.info
38    lcov --rc lcov_branch_coverage=1 --capture --directory library -o Coverage/tmp/tests.info
39    lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info
40    lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h'
41    gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions
42    genhtml --title "Mbed TLS" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info
43    rm -f Coverage/tmp/*.info Coverage/tmp/descriptions
44    echo "Coverage report in: Coverage/index.html"
45}
46
47# Reset the traces to 0.
48lcov_reset_traces () {
49    # Location with plain make
50    rm -f library/*.gcda
51    # Location with CMake
52    rm -f library/CMakeFiles/*.dir/*.gcda
53}
54
55if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
56    help
57    exit
58fi
59
60main=lcov_library_report
61while getopts r OPTLET; do
62    case $OPTLET in
63        r) main=lcov_reset_traces;;
64        *) help 2>&1; exit 120;;
65    esac
66done
67shift $((OPTIND - 1))
68
69"$main" "$@"
70