#!/usr/bin/env bash
set -euo pipefail
CI_MODE=false
BUILD_DIR=""
for arg in "$@"; do
case "$arg" in
--ci)
CI_MODE=true
;;
-*)
echo "Unknown option: $arg" >&2
exit 1
;;
*)
if [[ -n "$BUILD_DIR" ]]; then
echo "Unexpected argument: $arg (build dir already set to '$BUILD_DIR')" >&2
exit 1
fi
BUILD_DIR="$arg"
;;
esac
done
BUILD_DIR="${BUILD_DIR:-build_coverage}"
REPORT_DIR="${BUILD_DIR}/coverage"
if [ "$CI_MODE" = false ]; then
if [ ! -f "${BUILD_DIR}/build.ninja" ]; then
meson setup "${BUILD_DIR}" -Db_coverage=true
fi
meson test -C "${BUILD_DIR}" --print-errorlogs
fi
mkdir -p "${REPORT_DIR}"
GCOVR_ARGS=(
"${BUILD_DIR}"
--root .
--filter 'openrtx/src/'
--filter 'openrtx/include/'
--exclude '.*subprojects.*'
--exclude '.*test.*'
--cobertura "${REPORT_DIR}/cobertura.xml"
--markdown "${REPORT_DIR}/coverage.md"
--print-summary
)
if [ "$CI_MODE" = false ]; then
GCOVR_ARGS+=(
--html-details "${REPORT_DIR}/index.html"
--decisions
--calls
)
fi
gcovr "${GCOVR_ARGS[@]}"
echo ""
echo "Cobertura XML: ${REPORT_DIR}/cobertura.xml"
if [ "$CI_MODE" = false ]; then
echo "HTML report: ${REPORT_DIR}/index.html"
fi