summaryrefslogtreecommitdiff
path: root/pwx/cleanup_headers.sh
blob: 55c5cefec8e931974d04d1f72e39b5f07a0f62d3 (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
#!/bin/bash

src_files=( $(find -type f -name '*.c') )
hdr_files=( $(find -type f -name '*.h') )

# Is this a meson build?
isMeson=0
if [[ -f meson.build ]]; then
	isMeson=1
fi

for hdr in $(find -type f -name '*.h') ; do
	h_dir="$(basename $(dirname $hdr))"
	h_file="$(basename $hdr .h)"

	# Is it listed in the Makefile.am or meson.build?
	if [[ 1 -eq $isMeson ]]; then
		if [[ 0 -lt $(grep -c "$h_dir/${h_file}.c" meson.build) ]] || \
		   [[ 0 -lt $(grep -c "$h_dir/${h_file}.h" meson.build) ]]; then
			# It is needed.
			continue 1
		fi
	else
		if [[ 0 -lt $(grep -c "$h_dir/${h_file}.c" Makefile.am) ]] || \
		   [[ 0 -lt $(grep -c "$h_dir/${h_file}.h" Makefile.am) ]]; then
			# It is needed.
			continue 1
		fi
	fi

	# Is it included in any source files?
	for src in "${src_files[@]}" ; do
		is_inc=$(grep -P '^#include' $src | grep -c "${h_file}.h")
		if [[ 0 -lt $is_inc ]]; then
			# it is indeed
			continue 2
		fi
	done

	# Is it included in any header files?
	for src in "${hdr_files[@]}" ; do

		# If we already removed $src, skip it
		[ -f "$src" ] || continue 1

		is_inc=$(grep '#include' $src | grep -c "${h_file}.h")
		if [[ 0 -lt $is_inc ]]; then
			# it is indeed
			continue 2
		fi
	done

	# As it was not included, remove it.
	git rm $hdr
done