aboutsummaryrefslogtreecommitdiff
path: root/src/tools/pginclude/pgrminclude
blob: a8ec10a486afff1e1ef3c7334602faa0508181ae (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
:
# remove extra #include's

# src/tools/pginclude/pgrminclude

trap "rm -f /tmp/$$.c /tmp/$$.o /tmp/$$ /tmp/$$a /tmp/$$b" 0 1 2 3 15
find . \( -name CVS -a -prune \) -o -type f -name '*.[ch]' -print |
grep -v '\./postgres.h' |
grep -v '\./pg_config.h' |
grep -v '\./c.h' |
while read FILE
do
	if [ `expr $FILE : '.*\.h$'` -ne 0 ]
	then	IS_INCLUDE="Y"
	else	IS_INCLUDE="N"
	fi

	# loop through all includes
	cat "$FILE" | grep "^#include" |
	sed 's/^#include[ 	]*[<"]\([^>"]*\).*$/\1/g' |
	while read INCLUDE
	do
		if [ "$1" = "-v" ]
		then	echo "checking $FILE $INCLUDE"
		fi

		[ -s /usr/include/$INCLUDE ] && continue
		[ "$INCLUDE" = postgres.h ] && continue
		[ "$INCLUDE" = postgres_fe.h ] && continue
		[ "$INCLUDE" = config.h ] && continue
		[ "$INCLUDE" = c.h ] && continue

		# preserve configure-specific includes
		# these includes are surrounded by #ifdef's
		grep -B1 '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
		     egrep -q '^#if|^#else' && continue
		grep -A1 '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
		     egrep -q '^#else|^#endif' && continue

		# remove defines from include files
		if [ "$IS_INCLUDE" = "Y" ]
		then	cat "$FILE" | grep -v "^#if" | grep -v "^#else" |
			grep -v "^#endif" | sed 's/->[a-zA-Z0-9_\.]*//g' >/tmp/$$a
		else	cat "$FILE" >/tmp/$$a
		fi

		# set up initial file contents
		grep -v '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' \
			/tmp/$$a >/tmp/$$b

		if [ "$IS_INCLUDE" = "Y" ]
		then	echo "#include \"postgres.h\"" >/tmp/$$.c
		else	>/tmp/$$.c
		fi

		echo "#include \"/tmp/$$b\"" >>/tmp/$$.c
		echo "void include_test(void);" >>/tmp/$$.c
		echo "void include_test() {" >>/tmp/$$.c
		if [ "$IS_INCLUDE" = "Y" ]
		then	pgdefine "$FILE" >>/tmp/$$.c
		fi
		echo "}" >>/tmp/$$.c

		cc -fsyntax-only -Werror -Wall -Wmissing-prototypes \
			-Wmissing-declarations -I/pg/include -I/pg/backend \
			-I/pg/interfaces/libpq -I`dirname $FILE` $CFLAGS -c /tmp/$$.c \
			-o /tmp/$$.o >/tmp/$$ 2>&1
		if [ "$?" -eq 0 ]
		then	echo "$FILE $INCLUDE"
			if [ "$1" = "-v" ]
			then	cat /tmp/$$
				cat /tmp/$$b
				cat /tmp/$$.c
			fi
			grep -v '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' \
				"$FILE" >/tmp/$$b
			mv /tmp/$$b "$FILE"
		fi
	done
done