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
31# This script must be invoked from the project's root.
32
33set -eu
34
35. framework/scripts/project_detection.sh
36
37# Collect stats and build a HTML report.
38lcov_library_report () {
39    rm -rf Coverage
40    mkdir Coverage Coverage/tmp
41    # Pass absolute paths as lcov output files. This works around a bug
42    # whereby lcov tries to create the output file in the root directory
43    # if it has emitted a warning. A fix was released in lcov 1.13 in 2016.
44    # Ubuntu 16.04 is affected, 18.04 and above are not.
45    # https://github.com/linux-test-project/lcov/commit/632c25a0d1f5e4d2f4fd5b28ce7c8b86d388c91f
46    COVTMP=$PWD/Coverage/tmp
47    lcov --capture --initial ${lcov_dirs} -o "$COVTMP/files.info"
48    lcov --rc lcov_branch_coverage=1 --capture ${lcov_dirs} -o "$COVTMP/tests.info"
49    lcov --rc lcov_branch_coverage=1 --add-tracefile "$COVTMP/files.info" --add-tracefile "$COVTMP/tests.info" -o "$COVTMP/all.info"
50    lcov --rc lcov_branch_coverage=1 --remove "$COVTMP/all.info" -o "$COVTMP/final.info" '*.h'
51    gendesc tests/Descriptions.txt -o "$COVTMP/descriptions"
52    genhtml --title "$title" --description-file "$COVTMP/descriptions" --keep-descriptions --legend --branch-coverage -o Coverage "$COVTMP/final.info"
53    rm -f "$COVTMP/"*.info "$COVTMP/descriptions"
54    echo "Coverage report in: Coverage/index.html"
55}
56
57# Reset the traces to 0.
58lcov_reset_traces () {
59    # Location with plain make
60    for dir in ${library_dirs}; do
61        rm -f ${dir}/*.gcda
62    done
63    # Location with CMake
64    for dir in ${library_dirs}; do
65        rm -f ${dir}/CMakeFiles/*.dir/*.gcda
66    done
67}
68
69if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
70    help
71    exit
72fi
73
74if in_mbedtls_repo; then
75    library_dirs='library tf-psa-crypto/core tf-psa-crypto/drivers/builtin'
76    title='Mbed TLS'
77else
78    library_dirs='core drivers/builtin'
79    title='TF-PSA-Crypto'
80fi
81
82lcov_dirs=""
83for dir in ${library_dirs}; do
84    lcov_dirs="${lcov_dirs} --directory ${dir}"
85done
86
87main=lcov_library_report
88while getopts r OPTLET; do
89    case $OPTLET in
90        r) main=lcov_reset_traces;;
91        *) help 2>&1; exit 120;;
92    esac
93done
94shift $((OPTIND - 1))
95
96"$main" "$@"
97