pgrminclude 2.09 KB
Newer Older
1
:
Bruce Momjian's avatar
Bruce Momjian committed
2
# remove extra #include's
3

4
# $PostgreSQL: pgsql/src/tools/pginclude/pgrminclude,v 1.13 2006/07/12 16:28:27 momjian Exp $
5

6
trap "rm -f /tmp/$$.c /tmp/$$.o /tmp/$$ /tmp/$$a /tmp/$$b" 0 1 2 3 15
Bruce Momjian's avatar
Bruce Momjian committed
7
find . \( -name CVS -a -prune \) -o -type f -name '*.[ch]' -print | 
Bruce Momjian's avatar
Bruce Momjian committed
8
grep -v '\./postgres.h' |
9
grep -v '\./pg_config.h' |
Bruce Momjian's avatar
Bruce Momjian committed
10
grep -v '\./c.h' |
Bruce Momjian's avatar
Bruce Momjian committed
11
while read FILE
12
do
Bruce Momjian's avatar
Bruce Momjian committed
13
	if [ `expr $FILE : '.*\.h$'` -ne 0 ]
Bruce Momjian's avatar
Bruce Momjian committed
14 15 16
	then	IS_INCLUDE="Y"
	else	IS_INCLUDE="N"
	fi
Bruce Momjian's avatar
Bruce Momjian committed
17 18
	
	# loop through all includes
19
	cat "$FILE" | grep "^#include" |
Bruce Momjian's avatar
Bruce Momjian committed
20
	sed 's/^#include[ 	]*[<"]\([^>"]*\).*$/\1/g' |
21 22
	while read INCLUDE
	do
23 24 25 26
		if [ "$1" = "-v" ]
		then	echo "checking $FILE $INCLUDE"
		fi

27
		[ -s /usr/include/$INCLUDE ] && continue
28
		[ "$INCLUDE" = postgres.h ] && continue
Bruce Momjian's avatar
Bruce Momjian committed
29
		[ "$INCLUDE" = config.h ] && continue
Bruce Momjian's avatar
Bruce Momjian committed
30
		[ "$INCLUDE" = c.h ] && continue
Bruce Momjian's avatar
Bruce Momjian committed
31

Bruce Momjian's avatar
Bruce Momjian committed
32
		# preserve configure-specific includes
Bruce Momjian's avatar
Bruce Momjian committed
33
		# these includes are surrounded by #ifdef's
Bruce Momjian's avatar
Bruce Momjian committed
34
		grep -B1 '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
Bruce Momjian's avatar
Bruce Momjian committed
35
		     egrep -q '^#if|^#else' && continue
Bruce Momjian's avatar
Bruce Momjian committed
36
		grep -A1 '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
Bruce Momjian's avatar
Bruce Momjian committed
37
		     egrep -q '^#else|^#endif' && continue
Bruce Momjian's avatar
Bruce Momjian committed
38

39 40 41 42 43 44 45
		# 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

Bruce Momjian's avatar
Bruce Momjian committed
46
		# set up initial file contents
47 48 49
		grep -v '^#include[ 	][ 	]*[<"]'"$INCLUDE"'[>"]' \
			/tmp/$$a >/tmp/$$b

Bruce Momjian's avatar
Bruce Momjian committed
50 51 52 53
		if [ "$IS_INCLUDE" = "Y" ]
		then	echo "#include \"postgres.h\"" >/tmp/$$.c
		else	>/tmp/$$.c
		fi
Bruce Momjian's avatar
Bruce Momjian committed
54

55
		echo "#include \"/tmp/$$b\"" >>/tmp/$$.c
56
		echo "void include_test(void);" >>/tmp/$$.c
Bruce Momjian's avatar
Bruce Momjian committed
57 58 59 60
		echo "void include_test() {" >>/tmp/$$.c
		if [ "$IS_INCLUDE" = "Y" ]
		then	pgdefine "$FILE" >>/tmp/$$.c
		fi
61
		echo "}" >>/tmp/$$.c
Bruce Momjian's avatar
Bruce Momjian committed
62

63
		cc $CFLAGS -fsyntax-only -Werror -Wall -Wmissing-prototypes \
64
			-Wmissing-declarations -I/pg/include -I/pg/backend \
65 66
			-I/pg/interfaces/libpq -I`dirname $FILE` -c /tmp/$$.c \
			-o /tmp/$$.o >/tmp/$$ 2>&1
67 68
		if [ "$?" -eq 0 ]
		then	echo "$FILE $INCLUDE"
Bruce Momjian's avatar
Bruce Momjian committed
69 70 71 72 73
			if [ "$1" = "-v" ]
			then	cat /tmp/$$
				cat /tmp/$$b
				cat /tmp/$$.c
			fi
74
			mv /tmp/$$b "$FILE"
75
		fi
76 77
	done
done