Gilles Peskine | 3d4ea54 | 2022-11-30 17:35:44 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 3 | help () { |
| 4 | cat <<EOF |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 5 | Usage: $0 [-r] |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 6 | Collect coverage statistics of library code into an HTML report. |
| 7 | |
| 8 | General instructions: |
Gilles Peskine | 202b1a0 | 2022-12-01 17:41:36 +0100 | [diff] [blame] | 9 | 1. Build the library with CFLAGS="--coverage -O0 -g3" and link the test |
| 10 | programs with LDFLAGS="--coverage". |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 11 | This can be an out-of-tree build. |
Gilles Peskine | 202b1a0 | 2022-12-01 17:41:36 +0100 | [diff] [blame] | 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 |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 17 | 2. Run whatever tests you want. |
| 18 | 3. Run this script from the parent of the directory containing the library |
| 19 | object files and coverage statistics files. |
| 20 | 4. Browse the coverage report in Coverage/index.html. |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 21 | 5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report. |
| 22 | |
| 23 | Options |
| 24 | -r Reset traces. Run this before re-testing to get fresh measurements. |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 25 | EOF |
| 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 | |
| 43 | set -eu |
| 44 | |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 45 | # Repository detection |
Thomas Daubney | c0ae569 | 2023-10-23 17:25:52 +0100 | [diff] [blame^] | 46 | in_mbedtls_build_dir () { |
| 47 | test -d library |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 50 | # Collect stats and build a HTML report. |
| 51 | lcov_library_report () { |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 52 | rm -rf Coverage |
Gilles Peskine | e628f29 | 2022-11-30 17:56:58 +0100 | [diff] [blame] | 53 | mkdir Coverage Coverage/tmp |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 54 | lcov --capture --initial --directory $library_dir -o Coverage/tmp/files.info |
| 55 | lcov --rc lcov_branch_coverage=1 --capture --directory $library_dir -o Coverage/tmp/tests.info |
Gilles Peskine | e628f29 | 2022-11-30 17:56:58 +0100 | [diff] [blame] | 56 | lcov --rc lcov_branch_coverage=1 --add-tracefile Coverage/tmp/files.info --add-tracefile Coverage/tmp/tests.info -o Coverage/tmp/all.info |
| 57 | lcov --rc lcov_branch_coverage=1 --remove Coverage/tmp/all.info -o Coverage/tmp/final.info '*.h' |
| 58 | gendesc tests/Descriptions.txt -o Coverage/tmp/descriptions |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 59 | genhtml --title "$title" --description-file Coverage/tmp/descriptions --keep-descriptions --legend --branch-coverage -o Coverage Coverage/tmp/final.info |
Gilles Peskine | e628f29 | 2022-11-30 17:56:58 +0100 | [diff] [blame] | 60 | rm -f Coverage/tmp/*.info Coverage/tmp/descriptions |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 61 | echo "Coverage report in: Coverage/index.html" |
| 62 | } |
| 63 | |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 64 | # Reset the traces to 0. |
| 65 | lcov_reset_traces () { |
| 66 | # Location with plain make |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 67 | rm -f $library_dir/*.gcda |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 68 | # Location with CMake |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 69 | rm -f $library_dir/CMakeFiles/*.dir/*.gcda |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 72 | if [ $# -gt 0 ] && [ "$1" = "--help" ]; then |
| 73 | help |
| 74 | exit |
| 75 | fi |
| 76 | |
Thomas Daubney | c0ae569 | 2023-10-23 17:25:52 +0100 | [diff] [blame^] | 77 | if in_mbedtls_build_dir; then |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 78 | library_dir='library' |
| 79 | title='Mbed TLS' |
| 80 | else |
Thomas Daubney | c0ae569 | 2023-10-23 17:25:52 +0100 | [diff] [blame^] | 81 | library_dir='core' |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 82 | title='TF-PSA-Crypto' |
| 83 | fi |
| 84 | |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 85 | main=lcov_library_report |
| 86 | while getopts r OPTLET; do |
| 87 | case $OPTLET in |
| 88 | r) main=lcov_reset_traces;; |
| 89 | *) help 2>&1; exit 120;; |
| 90 | esac |
| 91 | done |
| 92 | shift $((OPTIND - 1)) |
| 93 | |
| 94 | "$main" "$@" |