blob: 718166ddb292d335ac2d2a4456a8150964ad20f9 [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:
91. Build the library with CFLAGS="--coverage -O0 -g3".
10 This can be an out-of-tree build.
112. Run whatever tests you want.
123. Run this script from the parent of the directory containing the library
13 object files and coverage statistics files.
144. Browse the coverage report in Coverage/index.html.
Gilles Peskine749a0d72022-11-30 18:08:14 +0100155. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
16
17Options
18 -r Reset traces. Run this before re-testing to get fresh measurements.
Gilles Peskineeff88032022-11-30 17:51:44 +010019EOF
20}
21
22# Copyright The Mbed TLS Contributors
23# SPDX-License-Identifier: Apache-2.0
24#
25# Licensed under the Apache License, Version 2.0 (the "License"); you may
26# not use this file except in compliance with the License.
27# You may obtain a copy of the License at
28#
29# http://www.apache.org/licenses/LICENSE-2.0
30#
31# Unless required by applicable law or agreed to in writing, software
32# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34# See the License for the specific language governing permissions and
35# limitations under the License.
36
37set -eu
38
Gilles Peskine749a0d72022-11-30 18:08:14 +010039# Collect stats and build a HTML report.
40lcov_library_report () {
Gilles Peskineeff88032022-11-30 17:51:44 +010041 rm -rf Coverage
Gilles Peskinee628f292022-11-30 17:56:58 +010042 mkdir Coverage Coverage/tmp
43 lcov --capture --initial --directory library -o Coverage/tmp/files.info
44 lcov --rc lcov_branch_coverage=1 --capture --directory library -o Coverage/tmp/tests.info
45 lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info
46 lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h'
47 gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions
48 genhtml --title "mbed TLS" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info
49 rm -f Coverage/tmp/*.info Coverage/tmp/descriptions
Gilles Peskineeff88032022-11-30 17:51:44 +010050 echo "Coverage report in: Coverage/index.html"
51}
52
Gilles Peskine749a0d72022-11-30 18:08:14 +010053# Reset the traces to 0.
54lcov_reset_traces () {
55 # Location with plain make
56 rm -f library/*.gcda
57 # Location with CMake
58 rm -f library/CMakeFiles/*.dir/*.gcda
59}
60
Gilles Peskineeff88032022-11-30 17:51:44 +010061if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
62 help
63 exit
64fi
65
Gilles Peskine749a0d72022-11-30 18:08:14 +010066main=lcov_library_report
67while getopts r OPTLET; do
68 case $OPTLET in
69 r) main=lcov_reset_traces;;
70 *) help 2>&1; exit 120;;
71 esac
72done
73shift $((OPTIND - 1))
74
75"$main" "$@"