xref: /third_party/mbedtls/scripts/lcov.sh (revision a8e1175b)
1a8e1175bSopenharmony_ci#!/bin/sh
2a8e1175bSopenharmony_ci
3a8e1175bSopenharmony_cihelp () {
4a8e1175bSopenharmony_ci    cat <<EOF
5a8e1175bSopenharmony_ciUsage: $0 [-r]
6a8e1175bSopenharmony_ciCollect coverage statistics of library code into an HTML report.
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ciGeneral instructions:
9a8e1175bSopenharmony_ci1. Build the library with CFLAGS="--coverage -O0 -g3" and link the test
10a8e1175bSopenharmony_ci   programs with LDFLAGS="--coverage".
11a8e1175bSopenharmony_ci   This can be an out-of-tree build.
12a8e1175bSopenharmony_ci   For example (in-tree):
13a8e1175bSopenharmony_ci        make CFLAGS="--coverage -O0 -g3" LDFLAGS="--coverage"
14a8e1175bSopenharmony_ci   Or (out-of-tree):
15a8e1175bSopenharmony_ci        mkdir build-coverage && cd build-coverage &&
16a8e1175bSopenharmony_ci        cmake -D CMAKE_BUILD_TYPE=Coverage .. && make
17a8e1175bSopenharmony_ci2. Run whatever tests you want.
18a8e1175bSopenharmony_ci3. Run this script from the parent of the directory containing the library
19a8e1175bSopenharmony_ci   object files and coverage statistics files.
20a8e1175bSopenharmony_ci4. Browse the coverage report in Coverage/index.html.
21a8e1175bSopenharmony_ci5. After rework, run "$0 -r", then re-test and run "$0" to get a fresh report.
22a8e1175bSopenharmony_ci
23a8e1175bSopenharmony_ciOptions
24a8e1175bSopenharmony_ci  -r    Reset traces. Run this before re-testing to get fresh measurements.
25a8e1175bSopenharmony_ciEOF
26a8e1175bSopenharmony_ci}
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_ci# Copyright The Mbed TLS Contributors
29a8e1175bSopenharmony_ci# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
30a8e1175bSopenharmony_ci
31a8e1175bSopenharmony_ciset -eu
32a8e1175bSopenharmony_ci
33a8e1175bSopenharmony_ci# Repository detection
34a8e1175bSopenharmony_ciin_mbedtls_build_dir () {
35a8e1175bSopenharmony_ci    test -d library
36a8e1175bSopenharmony_ci}
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_ci# Collect stats and build a HTML report.
39a8e1175bSopenharmony_cilcov_library_report () {
40a8e1175bSopenharmony_ci    rm -rf Coverage
41a8e1175bSopenharmony_ci    mkdir Coverage Coverage/tmp
42a8e1175bSopenharmony_ci    # Pass absolute paths as lcov output files. This works around a bug
43a8e1175bSopenharmony_ci    # whereby lcov tries to create the output file in the root directory
44a8e1175bSopenharmony_ci    # if it has emitted a warning. A fix was released in lcov 1.13 in 2016.
45a8e1175bSopenharmony_ci    # Ubuntu 16.04 is affected, 18.04 and above are not.
46a8e1175bSopenharmony_ci    # https://github.com/linux-test-project/lcov/commit/632c25a0d1f5e4d2f4fd5b28ce7c8b86d388c91f
47a8e1175bSopenharmony_ci    COVTMP=$PWD/Coverage/tmp
48a8e1175bSopenharmony_ci    lcov --capture --initial --directory $library_dir -o "$COVTMP/files.info"
49a8e1175bSopenharmony_ci    lcov --rc lcov_branch_coverage=1 --capture --directory $library_dir -o "$COVTMP/tests.info"
50a8e1175bSopenharmony_ci    lcov --rc lcov_branch_coverage=1 --add-tracefile "$COVTMP/files.info" --add-tracefile "$COVTMP/tests.info" -o "$COVTMP/all.info"
51a8e1175bSopenharmony_ci    lcov --rc lcov_branch_coverage=1 --remove "$COVTMP/all.info" -o "$COVTMP/final.info" '*.h'
52a8e1175bSopenharmony_ci    gendesc tests/Descriptions.txt -o "$COVTMP/descriptions"
53a8e1175bSopenharmony_ci    genhtml --title "$title" --description-file "$COVTMP/descriptions" --keep-descriptions --legend --branch-coverage -o Coverage "$COVTMP/final.info"
54a8e1175bSopenharmony_ci    rm -f "$COVTMP/"*.info "$COVTMP/descriptions"
55a8e1175bSopenharmony_ci    echo "Coverage report in: Coverage/index.html"
56a8e1175bSopenharmony_ci}
57a8e1175bSopenharmony_ci
58a8e1175bSopenharmony_ci# Reset the traces to 0.
59a8e1175bSopenharmony_cilcov_reset_traces () {
60a8e1175bSopenharmony_ci    # Location with plain make
61a8e1175bSopenharmony_ci    rm -f $library_dir/*.gcda
62a8e1175bSopenharmony_ci    # Location with CMake
63a8e1175bSopenharmony_ci    rm -f $library_dir/CMakeFiles/*.dir/*.gcda
64a8e1175bSopenharmony_ci}
65a8e1175bSopenharmony_ci
66a8e1175bSopenharmony_ciif [ $# -gt 0 ] && [ "$1" = "--help" ]; then
67a8e1175bSopenharmony_ci    help
68a8e1175bSopenharmony_ci    exit
69a8e1175bSopenharmony_cifi
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ciif in_mbedtls_build_dir; then
72a8e1175bSopenharmony_ci    library_dir='library'
73a8e1175bSopenharmony_ci    title='Mbed TLS'
74a8e1175bSopenharmony_cielse
75a8e1175bSopenharmony_ci    library_dir='core'
76a8e1175bSopenharmony_ci    title='TF-PSA-Crypto'
77a8e1175bSopenharmony_cifi
78a8e1175bSopenharmony_ci
79a8e1175bSopenharmony_cimain=lcov_library_report
80a8e1175bSopenharmony_ciwhile getopts r OPTLET; do
81a8e1175bSopenharmony_ci    case $OPTLET in
82a8e1175bSopenharmony_ci        r) main=lcov_reset_traces;;
83a8e1175bSopenharmony_ci        *) help 2>&1; exit 120;;
84a8e1175bSopenharmony_ci    esac
85a8e1175bSopenharmony_cidone
86a8e1175bSopenharmony_cishift $((OPTIND - 1))
87a8e1175bSopenharmony_ci
88a8e1175bSopenharmony_ci"$main" "$@"
89