blob: faf14f34e826f1d05de5b48b2e79c05e2275d77d [file] [log] [blame]
Gilles Peskine3d4ea542022-11-30 17:35:44 +01001#!/bin/sh
2
Gilles Peskineeff88032022-11-30 17:51:44 +01003help () {
4 cat <<EOF
Gilles Peskine749a0d72022-11-30 18:08:14 +01005Usage: $0 [-r]
Gilles Peskineeff88032022-11-30 17:51:44 +01006Collect coverage statistics of library code into an HTML report.
7
8General instructions:
Gilles Peskine202b1a02022-12-01 17:41:36 +010091. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
10 programs with LDFLAGS="--coverage".
Gilles Peskineeff88032022-11-30 17:51:44 +010011 This can be an out-of-tree build.
Gilles Peskine202b1a02022-12-01 17:41:36 +010012 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
Gilles Peskineeff88032022-11-30 17:51:44 +0100172. 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.
Gilles Peskine749a0d72022-11-30 18:08:14 +0100215. 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.
Gilles Peskineeff88032022-11-30 17:51:44 +010025EOF
26}
27
28# Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000029# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskineeff88032022-11-30 17:51:44 +010030
31set -eu
32
Thomas Daubneybbb41f72023-12-05 11:42:51 +000033# Project detection
34PROJECT_NAME_FILE='./scripts/project_name.txt'
35
36in_mbedtls_repo () {
37 grep -Fxq "Mbed TLS" "$PROJECT_NAME_FILE"
38 }
Thomas Daubney11120f92023-10-19 15:27:59 +010039
Gilles Peskine749a0d72022-11-30 18:08:14 +010040# Collect stats and build a HTML report.
41lcov_library_report () {
Gilles Peskineeff88032022-11-30 17:51:44 +010042 rm -rf Coverage
Gilles Peskinee628f292022-11-30 17:56:58 +010043 mkdir Coverage Coverage/tmp
Gilles Peskine539d7d52024-03-13 17:19:17 +010044 # Pass absolute paths as lcov output files. This works around a bug
45 # whereby lcov tries to create the output file in the root directory
46 # if it has emitted a warning. A fix was released in lcov 1.13 in 2016.
47 # Ubuntu 16.04 is affected, 18.04 and above are not.
48 # https://github.com/linux-test-project/lcov/commit/632c25a0d1f5e4d2f4fd5b28ce7c8b86d388c91f
49 COVTMP=$PWD/Coverage/tmp
50 lcov --capture --initial --directory $library_dir -o "$COVTMP/files.info"
51 lcov --rc lcov_branch_coverage=1 --capture --directory $library_dir -o "$COVTMP/tests.info"
52 lcov --rc lcov_branch_coverage=1 --add-tracefile "$COVTMP/files.info" --add-tracefile "$COVTMP/tests.info" -o "$COVTMP/all.info"
53 lcov --rc lcov_branch_coverage=1 --remove "$COVTMP/all.info" -o "$COVTMP/final.info" '*.h'
54 gendesc tests/Descriptions.txt -o "$COVTMP/descriptions"
55 genhtml --title "$title" --description-file "$COVTMP/descriptions" --keep-descriptions --legend --branch-coverage -o Coverage "$COVTMP/final.info"
56 rm -f "$COVTMP/"*.info "$COVTMP/descriptions"
Gilles Peskineeff88032022-11-30 17:51:44 +010057 echo "Coverage report in: Coverage/index.html"
58}
59
Gilles Peskine749a0d72022-11-30 18:08:14 +010060# Reset the traces to 0.
61lcov_reset_traces () {
62 # Location with plain make
Thomas Daubney11120f92023-10-19 15:27:59 +010063 rm -f $library_dir/*.gcda
Gilles Peskine749a0d72022-11-30 18:08:14 +010064 # Location with CMake
Thomas Daubney11120f92023-10-19 15:27:59 +010065 rm -f $library_dir/CMakeFiles/*.dir/*.gcda
Gilles Peskine749a0d72022-11-30 18:08:14 +010066}
67
Gilles Peskineeff88032022-11-30 17:51:44 +010068if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
69 help
70 exit
71fi
72
Thomas Daubneybbb41f72023-12-05 11:42:51 +000073if in_mbedtls_repo; then
Thomas Daubney11120f92023-10-19 15:27:59 +010074 library_dir='library'
75 title='Mbed TLS'
76else
Thomas Daubneyc0ae5692023-10-23 17:25:52 +010077 library_dir='core'
Thomas Daubney11120f92023-10-19 15:27:59 +010078 title='TF-PSA-Crypto'
79fi
80
Gilles Peskine749a0d72022-11-30 18:08:14 +010081main=lcov_library_report
82while getopts r OPTLET; do
83 case $OPTLET in
84 r) main=lcov_reset_traces;;
85 *) help 2>&1; exit 120;;
86 esac
87done
88shift $((OPTIND - 1))
89
90"$main" "$@"