summaryrefslogtreecommitdiff
path: root/build-aux/gcov-prune
blob: dc190a95f2fc634b372585906694b737e109ac32 (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
#!/usr/bin/env bash
# build-aux/gcov-prune - Prune old GCC coverage files
#
# Copyright (C) 2025  Luke T. Shumaker <lukeshu@lukeshu.com>
# SPDX-License-Identifier: AGPL-3.0-or-later

set -e

[[ $# == 1 ]]

sourcedir="$(realpath -- .)"
builddir="$(realpath -- "$1")"

# `gcc` writes .gcno
# Running the program writes .gcda (updates existing files, concurrent-safe)
# GCC `gcov` post-processes .gcno+.gcda to .gcov
# `gcovr` is a Python script that calls `gcov` and merges and post-processes the .gcov files to other formats

# Prune orphaned .gcno files.
find "$builddir" -name '*.gcno' -printf '%P\0' | while read -d '' -r gcno_file; do
	rel_base="${gcno_file%/CMakeFiles/*}"
	src_file="$gcno_file"
	src_file="${src_file#*/CMakeFiles/*.dir/}"
	src_file="${src_file%.gcno}"
	src_file="${src_file//__/..}"
	src_file="$rel_base/$src_file"
	if [[ ! -e "$sourcedir/$src_file" || "$sourcedir/$src_file" -nt "$builddir/$gcno_file" ]]; then
		rm -fv -- "$builddir/$gcno_file"
	fi
done

# Prune all .gcda files.
find "$builddir" -name '*.gcda' -delete