Gilles Peskine | 7f4705d | 2022-11-30 17:35:44 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Gilles Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 3 | help () { |
| 4 | cat <<EOF |
Gilles Peskine | 26aae47 | 2022-11-30 18:08:14 +0100 | [diff] [blame^] | 5 | Usage: $0 [-r] |
Gilles Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 6 | Collect coverage statistics of library code into an HTML report. |
| 7 | |
| 8 | General instructions: |
| 9 | 1. Build the library with CFLAGS="--coverage -O0 -g3". |
| 10 | This can be an out-of-tree build. |
| 11 | 2. Run whatever tests you want. |
| 12 | 3. Run this script from the parent of the directory containing the library |
| 13 | object files and coverage statistics files. |
| 14 | 4. Browse the coverage report in Coverage/index.html. |
Gilles Peskine | 26aae47 | 2022-11-30 18:08:14 +0100 | [diff] [blame^] | 15 | 5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report. |
| 16 | |
| 17 | Options |
| 18 | -r Reset traces. Run this before re-testing to get fresh measurements. |
Gilles Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 19 | EOF |
| 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 | |
| 37 | set -eu |
| 38 | |
Gilles Peskine | 26aae47 | 2022-11-30 18:08:14 +0100 | [diff] [blame^] | 39 | # Collect stats and build a HTML report. |
| 40 | lcov_library_report () { |
Gilles Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 41 | rm -rf Coverage |
Gilles Peskine | f11c33c | 2022-11-30 17:56:58 +0100 | [diff] [blame] | 42 | 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 Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 50 | echo "Coverage report in: Coverage/index.html" |
| 51 | } |
| 52 | |
Gilles Peskine | 26aae47 | 2022-11-30 18:08:14 +0100 | [diff] [blame^] | 53 | # Reset the traces to 0. |
| 54 | lcov_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 Peskine | 862e4a3 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 61 | if [ $# -gt 0 ] && [ "$1" = "--help" ]; then |
| 62 | help |
| 63 | exit |
| 64 | fi |
| 65 | |
Gilles Peskine | 26aae47 | 2022-11-30 18:08:14 +0100 | [diff] [blame^] | 66 | main=lcov_library_report |
| 67 | while getopts r OPTLET; do |
| 68 | case $OPTLET in |
| 69 | r) main=lcov_reset_traces;; |
| 70 | *) help 2>&1; exit 120;; |
| 71 | esac |
| 72 | done |
| 73 | shift $((OPTIND - 1)) |
| 74 | |
| 75 | "$main" "$@" |