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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 29 | # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 30 | |
| 31 | set -eu |
| 32 | |
Thomas Daubney | bbb41f7 | 2023-12-05 11:42:51 +0000 | [diff] [blame] | 33 | # Project detection |
| 34 | PROJECT_NAME_FILE='./scripts/project_name.txt' |
Thomas Daubney | bda3e46 | 2024-07-30 15:52:58 +0100 | [diff] [blame] | 35 | if read -r PROJECT_NAME < "$PROJECT_NAME_FILE"; then :; else |
| 36 | echo "$PROJECT_NAME_FILE does not exist... Exiting..." >&2 |
| 37 | exit 1 |
| 38 | fi |
Thomas Daubney | bbb41f7 | 2023-12-05 11:42:51 +0000 | [diff] [blame] | 39 | |
| 40 | in_mbedtls_repo () { |
Thomas Daubney | bda3e46 | 2024-07-30 15:52:58 +0100 | [diff] [blame] | 41 | test "$PROJECT_NAME" = "Mbed TLS" |
Thomas Daubney | 6a864dc | 2024-07-26 17:31:46 +0100 | [diff] [blame] | 42 | } |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 43 | |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 44 | # Collect stats and build a HTML report. |
| 45 | lcov_library_report () { |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 46 | rm -rf Coverage |
Gilles Peskine | e628f29 | 2022-11-30 17:56:58 +0100 | [diff] [blame] | 47 | mkdir Coverage Coverage/tmp |
Gilles Peskine | 539d7d5 | 2024-03-13 17:19:17 +0100 | [diff] [blame] | 48 | # Pass absolute paths as lcov output files. This works around a bug |
| 49 | # whereby lcov tries to create the output file in the root directory |
| 50 | # if it has emitted a warning. A fix was released in lcov 1.13 in 2016. |
| 51 | # Ubuntu 16.04 is affected, 18.04 and above are not. |
| 52 | # https://github.com/linux-test-project/lcov/commit/632c25a0d1f5e4d2f4fd5b28ce7c8b86d388c91f |
| 53 | COVTMP=$PWD/Coverage/tmp |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 54 | lcov --capture --initial ${lcov_dirs} -o "$COVTMP/files.info" |
| 55 | lcov --rc lcov_branch_coverage=1 --capture ${lcov_dirs} -o "$COVTMP/tests.info" |
Gilles Peskine | 539d7d5 | 2024-03-13 17:19:17 +0100 | [diff] [blame] | 56 | lcov --rc lcov_branch_coverage=1 --add-tracefile "$COVTMP/files.info" --add-tracefile "$COVTMP/tests.info" -o "$COVTMP/all.info" |
| 57 | lcov --rc lcov_branch_coverage=1 --remove "$COVTMP/all.info" -o "$COVTMP/final.info" '*.h' |
| 58 | gendesc tests/Descriptions.txt -o "$COVTMP/descriptions" |
| 59 | genhtml --title "$title" --description-file "$COVTMP/descriptions" --keep-descriptions --legend --branch-coverage -o Coverage "$COVTMP/final.info" |
| 60 | rm -f "$COVTMP/"*.info "$COVTMP/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 |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 67 | for dir in ${library_dirs}; do |
| 68 | rm -f ${dir}/*.gcda |
| 69 | done |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 70 | # Location with CMake |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 71 | for dir in ${library_dirs}; do |
| 72 | rm -f ${dir}/CMakeFiles/*.dir/*.gcda |
| 73 | done |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Gilles Peskine | eff8803 | 2022-11-30 17:51:44 +0100 | [diff] [blame] | 76 | if [ $# -gt 0 ] && [ "$1" = "--help" ]; then |
| 77 | help |
| 78 | exit |
| 79 | fi |
| 80 | |
Thomas Daubney | bbb41f7 | 2023-12-05 11:42:51 +0000 | [diff] [blame] | 81 | if in_mbedtls_repo; then |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 82 | library_dirs='library tf-psa-crypto/core tf-psa-crypto/drivers/builtin' |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 83 | title='Mbed TLS' |
| 84 | else |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 85 | library_dirs='core drivers/builtin' |
Thomas Daubney | 11120f9 | 2023-10-19 15:27:59 +0100 | [diff] [blame] | 86 | title='TF-PSA-Crypto' |
| 87 | fi |
| 88 | |
Ronald Cron | 4802aaa | 2024-10-18 09:32:19 +0200 | [diff] [blame^] | 89 | lcov_dirs="" |
| 90 | for dir in ${library_dirs}; do |
| 91 | lcov_dirs="${lcov_dirs} --directory ${dir}" |
| 92 | done |
| 93 | |
Gilles Peskine | 749a0d7 | 2022-11-30 18:08:14 +0100 | [diff] [blame] | 94 | main=lcov_library_report |
| 95 | while getopts r OPTLET; do |
| 96 | case $OPTLET in |
| 97 | r) main=lcov_reset_traces;; |
| 98 | *) help 2>&1; exit 120;; |
| 99 | esac |
| 100 | done |
| 101 | shift $((OPTIND - 1)) |
| 102 | |
| 103 | "$main" "$@" |