2022-11-30 16:35:44 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2022-11-30 16:51:44 +00:00
|
|
|
help () {
|
|
|
|
cat <<EOF
|
2022-11-30 17:08:14 +00:00
|
|
|
Usage: $0 [-r]
|
2022-11-30 16:51:44 +00:00
|
|
|
Collect coverage statistics of library code into an HTML report.
|
|
|
|
|
|
|
|
General instructions:
|
2022-12-01 16:41:36 +00:00
|
|
|
1. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
|
|
|
|
programs with LDFLAGS="--coverage".
|
2022-11-30 16:51:44 +00:00
|
|
|
This can be an out-of-tree build.
|
2022-12-01 16:41:36 +00:00
|
|
|
For example (in-tree):
|
|
|
|
make CFLAGS="--coverage -O0 -g3" LDFLAGS="--coverage"
|
|
|
|
Or (out-of-tree):
|
|
|
|
mkdir build-coverage && cd build-coverage &&
|
|
|
|
cmake -D CMAKE_BUILD_TYPE=Coverage .. && make
|
2022-11-30 16:51:44 +00:00
|
|
|
2. Run whatever tests you want.
|
|
|
|
3. Run this script from the parent of the directory containing the library
|
|
|
|
object files and coverage statistics files.
|
|
|
|
4. Browse the coverage report in Coverage/index.html.
|
2022-11-30 17:08:14 +00:00
|
|
|
5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
|
|
|
|
|
|
|
|
Options
|
|
|
|
-r Reset traces. Run this before re-testing to get fresh measurements.
|
2022-11-30 16:51:44 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
# Copyright The Mbed TLS Contributors
|
2023-11-02 19:47:20 +00:00
|
|
|
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2022-11-30 16:51:44 +00:00
|
|
|
|
2024-11-12 16:16:05 +00:00
|
|
|
# This script must be invoked from the project's root.
|
|
|
|
|
2022-11-30 16:51:44 +00:00
|
|
|
set -eu
|
|
|
|
|
2024-11-12 16:16:05 +00:00
|
|
|
. framework/scripts/project_detection.sh
|
2023-10-19 14:27:59 +00:00
|
|
|
|
2022-11-30 17:08:14 +00:00
|
|
|
# Collect stats and build a HTML report.
|
|
|
|
lcov_library_report () {
|
2022-11-30 16:51:44 +00:00
|
|
|
rm -rf Coverage
|
2022-11-30 16:56:58 +00:00
|
|
|
mkdir Coverage Coverage/tmp
|
2024-03-13 16:19:17 +00:00
|
|
|
# Pass absolute paths as lcov output files. This works around a bug
|
|
|
|
# whereby lcov tries to create the output file in the root directory
|
|
|
|
# if it has emitted a warning. A fix was released in lcov 1.13 in 2016.
|
|
|
|
# Ubuntu 16.04 is affected, 18.04 and above are not.
|
|
|
|
# https://github.com/linux-test-project/lcov/commit/632c25a0d1f5e4d2f4fd5b28ce7c8b86d388c91f
|
|
|
|
COVTMP=$PWD/Coverage/tmp
|
2024-10-18 07:32:19 +00:00
|
|
|
lcov --capture --initial ${lcov_dirs} -o "$COVTMP/files.info"
|
|
|
|
lcov --rc lcov_branch_coverage=1 --capture ${lcov_dirs} -o "$COVTMP/tests.info"
|
2024-03-13 16:19:17 +00:00
|
|
|
lcov --rc lcov_branch_coverage=1 --add-tracefile "$COVTMP/files.info" --add-tracefile "$COVTMP/tests.info" -o "$COVTMP/all.info"
|
|
|
|
lcov --rc lcov_branch_coverage=1 --remove "$COVTMP/all.info" -o "$COVTMP/final.info" '*.h'
|
|
|
|
gendesc tests/Descriptions.txt -o "$COVTMP/descriptions"
|
|
|
|
genhtml --title "$title" --description-file "$COVTMP/descriptions" --keep-descriptions --legend --branch-coverage -o Coverage "$COVTMP/final.info"
|
|
|
|
rm -f "$COVTMP/"*.info "$COVTMP/descriptions"
|
2022-11-30 16:51:44 +00:00
|
|
|
echo "Coverage report in: Coverage/index.html"
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:08:14 +00:00
|
|
|
# Reset the traces to 0.
|
|
|
|
lcov_reset_traces () {
|
|
|
|
# Location with plain make
|
2024-10-18 07:32:19 +00:00
|
|
|
for dir in ${library_dirs}; do
|
|
|
|
rm -f ${dir}/*.gcda
|
|
|
|
done
|
2022-11-30 17:08:14 +00:00
|
|
|
# Location with CMake
|
2024-10-18 07:32:19 +00:00
|
|
|
for dir in ${library_dirs}; do
|
|
|
|
rm -f ${dir}/CMakeFiles/*.dir/*.gcda
|
|
|
|
done
|
2022-11-30 17:08:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 16:51:44 +00:00
|
|
|
if [ $# -gt 0 ] && [ "$1" = "--help" ]; then
|
|
|
|
help
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2023-12-05 11:42:51 +00:00
|
|
|
if in_mbedtls_repo; then
|
2024-10-18 07:32:19 +00:00
|
|
|
library_dirs='library tf-psa-crypto/core tf-psa-crypto/drivers/builtin'
|
2023-10-19 14:27:59 +00:00
|
|
|
title='Mbed TLS'
|
|
|
|
else
|
2024-10-18 07:32:19 +00:00
|
|
|
library_dirs='core drivers/builtin'
|
2023-10-19 14:27:59 +00:00
|
|
|
title='TF-PSA-Crypto'
|
|
|
|
fi
|
|
|
|
|
2024-10-18 07:32:19 +00:00
|
|
|
lcov_dirs=""
|
|
|
|
for dir in ${library_dirs}; do
|
|
|
|
lcov_dirs="${lcov_dirs} --directory ${dir}"
|
|
|
|
done
|
|
|
|
|
2022-11-30 17:08:14 +00:00
|
|
|
main=lcov_library_report
|
|
|
|
while getopts r OPTLET; do
|
|
|
|
case $OPTLET in
|
|
|
|
r) main=lcov_reset_traces;;
|
|
|
|
*) help 2>&1; exit 120;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
|
|
|
"$main" "$@"
|