@@ -7,87 +7,120 @@ then echo "pgdefine must be in your PATH" 1>&2
7
7
fi
8
8
9
9
trap "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
- then IS_INCLUDE="Y"
21
- else IS_INCLUDE="N"
10
+
11
+ if [ "$1" = "-v" ]
12
+ then VERBOSE="Y"
13
+ else VERBOSE=""
14
+ fi
15
+
16
+ verbose_output() {
17
+ if [ "$VERBOSE" ]
18
+ then cat /tmp/$$
19
+ cat /tmp/$$b
20
+ nl /tmp/$$.c
22
21
fi
22
+ }
23
23
24
- # loop through all includes
24
+ process_includes_in_file() {
25
+ # loop through all includes mentioned in the file
25
26
cat "$FILE" |
26
27
grep "^#include\>" |
27
28
grep -v '/\* *pgrminclude *ignore *\*/' |
28
29
sed 's/^#include[ ]*[<"]\([^>"]*\).*$/\1/g' |
29
30
grep -v 'parser/kwlist\.h' |
30
31
grep -v '\.c$' |
31
32
while read INCLUDE
32
- do
33
- if [ "$1" = "-v" ]
33
+ do if [ "$VERBOSE" ]
34
34
then echo "checking $FILE $INCLUDE"
35
35
fi
36
+ compile_file
37
+ done
38
+ }
36
39
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
42
46
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
49
53
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
57
64
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
61
68
62
- if [ "$IS_INCLUDE" = "Y" ]
63
- then echo "#include \"postgres.h\"" >/tmp/$$.c
64
- else >/tmp/$$.c
65
- fi
69
+ if [ "$IS_INCLUDE" = "Y" ]
70
+ then echo "#include \"postgres.h\"" >/tmp/$$.c
71
+ else >/tmp/$$.c
72
+ fi
66
73
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
- then pgdefine "$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
+ then pgdefine "$FILE" >>/tmp/$$.c
82
+ fi
83
+ echo "}" >>/tmp/$$.c
74
84
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
- then echo "$FILE $INCLUDE"
83
- if [ "$1" = "-v" ]
84
- then cat /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
+ else return 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
+ then IS_INCLUDE="Y"
113
+ else IS_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
+ then echo "cannot compile $FILE with existing includes"
123
+ verbose_output
124
+ else process_includes_in_file
125
+ fi
93
126
done
0 commit comments