blob: 6bba02fd24251895484da0cbd690e043cc18d55b [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
29# SPDX-License-Identifier: Apache-2.0
30#
31# Licensed under the Apache License, Version 2.0 (the "License"); you may
32# not use this file except in compliance with the License.
33# You may obtain a copy of the License at
34#
35# http://www.apache.org/licenses/LICENSE-2.0
36#
37# Unless required by applicable law or agreed to in writing, software
38# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
39# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
40# See the License for the specific language governing permissions and
41# limitations under the License.
42
43set -eu
44
Gilles Peskine749a0d72022-11-30 18:08:14 +010045# Collect stats and build a HTML report.
46lcov_library_report () {
Gilles Peskineeff88032022-11-30 17:51:44 +010047 rm -rf Coverage
Gilles Peskinee628f292022-11-30 17:56:58 +010048 mkdir Coverage Coverage/tmp
49 lcov --capture --initial --directory library -o Coverage/tmp/files.info
50 lcov --rc lcov_branch_coverage=1 --capture --directory library -o Coverage/tmp/tests.info
51 lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info
52 lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h'
53 gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions
Gilles Peskinee820c0a2023-08-03 17:45:20 +020054 genhtml --title "Mbed TLS" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info
Gilles Peskinee628f292022-11-30 17:56:58 +010055 rm -f Coverage/tmp/*.info Coverage/tmp/descriptions
Gilles Peskineeff88032022-11-30 17:51:44 +010056 echo "Coverage report in: Coverage/index.html"
57}
58
Gilles Peskine749a0d72022-11-30 18:08:14 +010059# Reset the traces to 0.
60lcov_reset_traces () {
61 # Location with plain make
62 rm -f library/*.gcda
63 # Location with CMake
64 rm -f library/CMakeFiles/*.dir/*.gcda
65}
66
Gilles Peskineeff88032022-11-30 17:51:44 +010067if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
68 help
69 exit
70fi
71
Gilles Peskine749a0d72022-11-30 18:08:14 +010072main=lcov_library_report
73while getopts r OPTLET; do
74 case $OPTLET in
75 r) main=lcov_reset_traces;;
76 *) help 2>&1; exit 120;;
77 esac
78done
79shift $((OPTIND - 1))
80
81"$main" "$@"