@@ -7,87 +7,120 @@ thenecho "pgdefine must be in your PATH" 1>&2
77fi
88
99trap "rm -f /tmp/$$.c /tmp/$$.o /tmp/$$ /tmp/$$a /tmp/$$b" 0 1 2 3 15
10- # do include files first
11- (find . \( -name .git -a -prune \) -o -type f -name '*.h' -print;
12- find . \( -name .git -a -prune \) -o -type f -name '*.c' -print ) |
13- grep -v '\./postgres.h' |
14- grep -v '\./postgres_fe.h' |
15- grep -v '\./pg_config.h' |
16- grep -v '\./c.h' |
17- while read FILE
18- do
19- if [ `expr $FILE : '.*\.h$'` -ne 0 ]
20- thenIS_INCLUDE="Y"
21- elseIS_INCLUDE="N"
10+
11+ if [ "$1" = "-v" ]
12+ thenVERBOSE="Y"
13+ elseVERBOSE=""
14+ fi
15+
16+ verbose_output() {
17+ if [ "$VERBOSE" ]
18+ thencat /tmp/$$
19+ cat /tmp/$$b
20+ nl /tmp/$$.c
2221fi
22+ }
2323
24- # loop through all includes
24+ process_includes_in_file() {
25+ # loop through all includes mentioned in the file
2526cat "$FILE" |
2627grep "^#include\>" |
2728grep -v '/\* *pgrminclude *ignore *\*/' |
2829sed 's/^#include[ ]*[<"]\([^>"]*\).*$/\1/g' |
2930grep -v 'parser/kwlist\.h' |
3031grep -v '\.c$' |
3132while read INCLUDE
32- do
33- if [ "$1" = "-v" ]
33+ doif [ "$VERBOSE" ]
3434thenecho "checking $FILE $INCLUDE"
3535fi
36+ compile_file
37+ done
38+ }
3639
37- [ -s /usr/include/$INCLUDE ] && continue
38- [ "$INCLUDE" = postgres.h ] && continue
39- [ "$INCLUDE" = postgres_fe.h ] && continue
40- [ "$INCLUDE" = pg_config.h ] && continue
41- [ "$INCLUDE" = c.h ] && continue
40+ compile_file() {
41+ [ "$INCLUDE" -a -s /usr/include/"$INCLUDE" ] && continue
42+ [ "$INCLUDE" = "postgres.h" ] && continue
43+ [ "$INCLUDE" = "postgres_fe.h" ] && continue
44+ [ "$INCLUDE" = "pg_config.h" ] && continue
45+ [ "$INCLUDE" = "c.h" ] && continue
4246
43- # preserve configure-specific includes
44- # these includes are surrounded by #ifdef's
45- grep -B1 '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
46- egrep -q '^#if|^#else|^#elif' && continue
47- grep -A1 '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
48- egrep -q '^#else|^#elif|^#endif' && continue
47+ # preserve configure-specific includes
48+ # these includes are surrounded by #ifdef's
49+ grep -B1 '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
50+ egrep -q '^#if|^#else|^#elif' && continue
51+ grep -A1 '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' "$FILE" |
52+ egrep -q '^#else|^#elif|^#endif' && continue
4953
50- # Remove all #if and #ifdef blocks because the blocks
51- # might contain code that is not compiled on this platform.
52- cat "$FILE" |
53- grep -v "^#if" |
54- grep -v "^#else" |
55- grep -v "^#elif" |
56- grep -v "^#endif" >/tmp/$$a
54+ # Remove all #if and #ifdef blocks because the blocks
55+ # might contain code that is not compiled on this platform.
56+ cat "$FILE" |
57+ grep -v "^#if" |
58+ grep -v "^#else" |
59+ grep -v "^#elif" |
60+ grep -v "^#endif" |
61+ # with #if blocks gone, now undef #defines to avoid redefine
62+ # warning and failure
63+ sed 's/#define[ ][ ]*\([A-Za-z0-9_]*\).*$/#undef \1\n&/' >/tmp/$$a
5764
58- # set up initial file contents
59- grep -v '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' \
60- /tmp/$$a >/tmp/$$b
65+ # set up initial file contents
66+ grep -v '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' \
67+ /tmp/$$a >/tmp/$$b
6168
62- if [ "$IS_INCLUDE" = "Y" ]
63- thenecho "#include \"postgres.h\"" >/tmp/$$.c
64- else>/tmp/$$.c
65- fi
69+ if [ "$IS_INCLUDE" = "Y" ]
70+ thenecho "#include \"postgres.h\"" >/tmp/$$.c
71+ else>/tmp/$$.c
72+ fi
6673
67- echo "#include \"/tmp/$$b\"" >>/tmp/$$.c
68- echo "void include_test(void);" >>/tmp/$$.c
69- echo "void include_test() {" >>/tmp/$$.c
70- if [ "$IS_INCLUDE" = "Y" ]
71- thenpgdefine "$FILE" >>/tmp/$$.c
72- fi
73- echo "}" >>/tmp/$$.c
74+ echo "#include \"/tmp/$$b\"" >>/tmp/$$.c
75+ # supress fcinfo errors
76+ echo "#undef PG_GETARG_DATUM" >>/tmp/$$.c
77+ echo "#define PG_GETARG_DATUM(n)" >>/tmp/$$.c
78+ echo "void include_test(void);" >>/tmp/$$.c
79+ echo "void include_test() {" >>/tmp/$$.c
80+ if [ "$IS_INCLUDE" = "Y" ]
81+ thenpgdefine "$FILE" >>/tmp/$$.c
82+ fi
83+ echo "}" >>/tmp/$$.c
7484
75- # Use -O1 to get warnings only generated by optimization,
76- # but -O2 is too slow.
77- cc -fsyntax-only -Werror -Wall -Wmissing-prototypes \
78- -Wmissing-declarations -I/pg/include -I/pg/backend \
79- -I/pg/interfaces/libpq -I`dirname $FILE` $CFLAGS -O1 -c /tmp/$$.c \
80- -o /tmp/$$.o >/tmp/$$ 2>&1
81- if [ "$?" -eq 0 ]
82- thenecho "$FILE $INCLUDE"
83- if [ "$1" = "-v" ]
84- thencat /tmp/$$
85- cat /tmp/$$b
86- cat /tmp/$$.c
87- fi
88- grep -v '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' \
89- "$FILE" >/tmp/$$b
90- mv /tmp/$$b "$FILE"
91- fi
92- done
85+ # Use -O1 to get warnings only generated by optimization,
86+ # but -O2 is too slow.
87+ cc -fsyntax-only -Werror -Wall -Wmissing-prototypes \
88+ -Wmissing-declarations -I/pg/include -I/pg/backend \
89+ -I/pg/interfaces/libpq -I`dirname $FILE` $CFLAGS -O1 -c /tmp/$$.c \
90+ -o /tmp/$$.o >/tmp/$$ 2>&1
91+ if [ "$?" -eq 0 ]
92+ then[ "$INCLUDE" -o "$VERBOSE" ] && echo "$FILE $INCLUDE"
93+ grep -v '^#include[ ][ ]*[<"]'"$INCLUDE"'[>"]' \
94+ "$FILE" >/tmp/$$b
95+ mv /tmp/$$b "$FILE"
96+ return 0
97+ elsereturn 1
98+ fi
99+ }
100+
101+ # Process include files first because they can affect the compilation
102+ # of *.c files.
103+ (find . \( -name .git -a -prune \) -o -type f -name '*.h' -print;
104+ find . \( -name .git -a -prune \) -o -type f -name '*.c' -print ) |
105+ grep -v '/postgres.h$' |
106+ grep -v '/postgres_fe.h$' |
107+ grep -v '/pg_config.h$' |
108+ grep -v '\./c.h$' |
109+ while read FILE
110+ do
111+ if [ `expr $FILE : '.*\.h$'` -ne 0 ]
112+ thenIS_INCLUDE="Y"
113+ elseIS_INCLUDE="N"
114+ fi
115+
116+ # Can we compile the file with all existing includes?
117+ INCLUDE=""
118+ compile_file
119+ # If the file can't be compiled on its own, there is no sense
120+ # trying to remove the include files.
121+ if [ "$?" -ne 0 ]
122+ thenecho "cannot compile $FILE with existing includes"
123+ verbose_output
124+ elseprocess_includes_in_file
125+ fi
93126done