#!/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"