summaryrefslogtreecommitdiff
path: root/scripts/gen_coverage_report.sh
blob: bae2141db8aed41c558e03626fedb59536e6cebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash

# Exit on error
set -e

# Project root
PROJECT_ROOT=$(cd "$(dirname "$0")/.." && pwd)
BUILD_DIR="${PROJECT_ROOT}/build_coverage"
REPORT_DIR="${PROJECT_ROOT}/build_coverage/coverage_report"

# Common lcov options to handle AppleClang quirks
LCOV_OPTS="--ignore-errors mismatch,inconsistent,gcov,format,unsupported,category"

echo "=== Code Coverage Report Generator ==="
echo "Project Root: ${PROJECT_ROOT}"

# Check for lcov
if ! command -v lcov &> /dev/null; then
    echo "Error: lcov is not installed."
    echo "Please install it using: brew install lcov"
    exit 1
fi

# Cleanup previous build if requested (optional, but safer for coverage)
# rm -rf "${BUILD_DIR}"

mkdir -p "${BUILD_DIR}"
cd "${BUILD_DIR}"

echo "--- Configuring CMake with Coverage ---"
cmake "${PROJECT_ROOT}" -DDEMO_ENABLE_COVERAGE=ON -DDEMO_BUILD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug

echo "--- Building Tests ---"
# Build only the test targets to save time, or all if needed.
# Building 'all' ensures everything is compiled with coverage flags.
cmake --build . -j $(sysctl -n hw.ncpu)

echo "--- Running Tests ---"
# Run tests and ignore failures so we still get a report?
# Ideally tests should pass. Let's allow failures for now so we see what's covered.
ctest --output-on-failure || true

echo "--- Capturing Coverage Data ---"
# Capture initial coverage data
lcov --capture --directory . --output-file coverage.info $LCOV_OPTS

echo "--- Filtering Coverage Data ---"
# Remove system headers and third-party libraries
lcov --remove coverage.info '/usr/*' "${PROJECT_ROOT}/third_party/*" "${PROJECT_ROOT}/build_coverage/*" '*/test_*' --output-file coverage_filtered.info $LCOV_OPTS

echo "--- Generating HTML Report ---"
genhtml coverage_filtered.info --output-directory "${REPORT_DIR}" $LCOV_OPTS

echo "=== Coverage Report Generated ==="
echo "Open the report with:"
echo "open ${REPORT_DIR}/index.html"