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