summaryrefslogtreecommitdiff
path: root/tools/notsd-fixup
blob: 79b30b7c555aa8d49a4015595f3b6e40b44d6a1b (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
#!/usr/bin/env bash
# Copyright (C) 2015-2016  Luke Shumaker

# We wrap the programs called by xargs with `sh` because xargs only exits early
# if the status is 255, but we want to exit early for all non-zero statuses.
# We use xargs instead of `find -exec` because `-exec` won't do much of
# anything useful with the exit status.

fixup_makefiles() (
	find "$@" -type f -name Makefile | while read -r filename; do
		>&2 printf ' => fixup %q\n' "$filename"
		{
			<"$filename" sed -r \
			 -e "s|(/\.\.)*/config.mk|/$(realpath -ms --relative-to="$(dirname -- "$filename")" config.mk)|" \
			 -e '/^nested\.subdirs/d' \
			 -e '/^include \$\(topsrcdir\)\/build-aux\/Makefile\.tail\.mk$/d'
			echo
			find "$(dirname "$filename")" -mindepth 2 -maxdepth 2 -name Makefile -print0 |
			    xargs -r0 dirname -z -- |
			    xargs -r0 basename -a -z |
			    xargs -r0 printf 'nested.subdirs += %s\n' | sort
			echo
			echo 'include $(topsrcdir)/build-aux/Makefile.tail.mk'
		} | cat -s | build-aux/write-ifchanged "$filename"
	done
)

fixup_includes() (
	rm -rf -- "$0"--includes.cache
	find "$@" \( -name '*.h' -o -name '*.c' -o -name '*.gperf' -o -name '*.gperf.m4' \) -type f -print0 |
		xargs -r0 sh -c "$0--includes \"\$@\" || exit 255" --
	rm -rf -- "$0"--includes.cache
)

main() {
	set -e
	set -o pipefail
	export LC_COLLATE=C
	fixup_makefiles "$@"
	fixup_includes "$@"
}

main "$@"