|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Code coverage script for BehaviorTree.CPP |
| 5 | +# Based on .github/workflows/cmake_ubuntu.yml coverage job |
| 6 | + |
| 7 | +BUILD_DIR="build/coverage" |
| 8 | +COVERAGE_FILE="coverage.info" |
| 9 | +COVERAGE_HTML_DIR="coverage_report" |
| 10 | + |
| 11 | +echo "=== BehaviorTree.CPP Code Coverage ===" |
| 12 | + |
| 13 | +# Check for lcov |
| 14 | +if ! command -v lcov &> /dev/null; then |
| 15 | + echo "Error: lcov is not installed." |
| 16 | + echo "Install with: sudo apt-get install lcov" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +# Clean previous coverage data |
| 21 | +echo "Cleaning previous coverage data..." |
| 22 | +rm -rf "$BUILD_DIR" "$COVERAGE_FILE" "$COVERAGE_HTML_DIR" |
| 23 | + |
| 24 | +# Configure CMake with coverage flags (plain CMake approach) |
| 25 | +echo "Configuring CMake with coverage flags..." |
| 26 | +cmake -S . -B "$BUILD_DIR" \ |
| 27 | + -DCMAKE_BUILD_TYPE=Debug \ |
| 28 | + -DCMAKE_C_FLAGS="--coverage -fprofile-update=atomic" \ |
| 29 | + -DCMAKE_CXX_FLAGS="--coverage -fprofile-update=atomic" |
| 30 | + |
| 31 | +# Build |
| 32 | +echo "Building..." |
| 33 | +cmake --build "$BUILD_DIR" --parallel |
| 34 | + |
| 35 | +# Run tests |
| 36 | +echo "Running tests..." |
| 37 | +ctest --test-dir "$BUILD_DIR" --output-on-failure |
| 38 | + |
| 39 | +# Collect coverage |
| 40 | +echo "Collecting coverage data..." |
| 41 | +lcov --capture --directory "$BUILD_DIR" \ |
| 42 | + --output-file "$COVERAGE_FILE" |
| 43 | + |
| 44 | +# Extract only project source files |
| 45 | +echo "Filtering coverage data..." |
| 46 | +lcov --extract "$COVERAGE_FILE" \ |
| 47 | + "$(pwd)/include/*" \ |
| 48 | + "$(pwd)/src/*" \ |
| 49 | + --output-file "$COVERAGE_FILE" || true |
| 50 | + |
| 51 | +# Remove contrib files |
| 52 | +lcov --remove "$COVERAGE_FILE" \ |
| 53 | + '*/contrib/*' \ |
| 54 | + --output-file "$COVERAGE_FILE" || true |
| 55 | + |
| 56 | +# Show summary |
| 57 | +echo "" |
| 58 | +echo "=== Coverage Summary ===" |
| 59 | +lcov --list "$COVERAGE_FILE" |
| 60 | + |
| 61 | +# Generate HTML report |
| 62 | +if command -v genhtml &> /dev/null; then |
| 63 | + echo "" |
| 64 | + echo "Generating HTML report..." |
| 65 | + genhtml "$COVERAGE_FILE" --output-directory "$COVERAGE_HTML_DIR" |
| 66 | + echo "HTML report generated at: $COVERAGE_HTML_DIR/index.html" |
| 67 | +fi |
| 68 | + |
| 69 | +echo "" |
| 70 | +echo "Coverage data saved to: $COVERAGE_FILE" |
0 commit comments