1515# src/tools/pginclude/headerscheck
1616# Copyright (c) 2009-2024, PostgreSQL Global Development Group
1717
18+ # option to check for C++ compatibility
19+ if [ " $1 " = " --cplusplus" ]; then
20+ cplusplus=true
21+ shift
22+ else
23+ cplusplus=false
24+ fi
25+
1826if [ -z " $1 " ]; then
1927 srcdir=" ."
2028else
2937
3038me=` basename $0 `
3139
40+ # These switches are g++ specific, you may override if necessary.
41+ CXXFLAGS=${CXXFLAGS:- -fsyntax-only -Wall}
42+
3243# Pull some info from configure's results.
3344MGLOB=" $builddir /src/Makefile.global"
3445CPPFLAGS=` sed -n ' s/^CPPFLAGS[ ]*=[ ]*//p' " $MGLOB " `
3546CFLAGS=` sed -n ' s/^CFLAGS[ ]*=[ ]*//p' " $MGLOB " `
3647CC=` sed -n ' s/^CC[ ]*=[ ]*//p' " $MGLOB " `
48+ CXX=` sed -n ' s/^CXX[ ]*=[ ]*//p' " $MGLOB " `
3749PG_SYSROOT=` sed -n ' s/^PG_SYSROOT[ ]*=[ ]*//p' " $MGLOB " `
3850perl_includespec=` sed -n ' s/^perl_includespec[ ]*=[ ]*//p' " $MGLOB " `
3951python_includespec=` sed -n ' s/^python_includespec[ ]*=[ ]*//p' " $MGLOB " `
@@ -43,6 +55,22 @@ CPPFLAGS=`echo "$CPPFLAGS" | sed "s|\\\$(PG_SYSROOT)|$PG_SYSROOT|g"`
4355
4456# (EXTRAFLAGS is not set here, but user can pass it in if need be.)
4557
58+ if $cplusplus ; then
59+ ext=cpp
60+ COMPILER=${CXX:- g++}
61+ # Extract any -I and -D switches from CPPFLAGS.
62+ for flag in $CPPFLAGS ; do
63+ case $flag in
64+ -I* |-D* ) CXXPPFLAGS=" $CXXPPFLAGS $flag " ;;
65+ esac
66+ done
67+ COMPILER_FLAGS=" $CXXPPFLAGS $CXXFLAGS "
68+ else
69+ ext=c
70+ COMPILER=${CC:- gcc}
71+ COMPILER_FLAGS=" $CPPFLAGS $CFLAGS "
72+ fi
73+
4674# Create temp directory.
4775tmp=` mktemp -d /tmp/$me .XXXXXX`
4876
6997 # Additional Windows-specific headers.
7098 test " $f " = src/include/port/win32_port.h && continue
7199 test " $f " = src/include/port/win32/netdb.h && continue
100+ $cplusplus && test " $f " = src/include/port/win32/sys/resource.h && continue
72101 test " $f " = src/include/port/win32/sys/socket.h && continue
73102 test " $f " = src/include/port/win32_msvc/dirent.h && continue
74103 test " $f " = src/include/port/win32_msvc/utime.h && continue
134163 test " $f " = src/test/isolation/specparse.h && continue
135164
136165 # This produces a "no previous prototype" warning.
137- test " $f " = src/include/storage/checksum_impl.h && continue
166+ ! $cplusplus && test " $f " = src/include/storage/checksum_impl.h && continue
138167
139168 # ppport.h is not under our control, so we can't make it standalone.
140169 test " $f " = src/pl/plperl/ppport.h && continue
144173 # printf_hack.h produces "unused function" warnings.
145174 test " $f " = src/interfaces/ecpg/test/printf_hack.h && continue
146175
176+ if $cplusplus ; then
177+ # pg_trace.h and utils/probes.h can include sys/sdt.h from SystemTap,
178+ # which itself contains C++ code and so won't compile with a C++
179+ # compiler under extern "C" linkage.
180+ test " $f " = src/include/pg_trace.h && continue
181+ test " $f " = src/include/utils/probes.h && continue
182+
183+ # pg_dump is not C++-clean because it uses "public" and "namespace"
184+ # as field names, which is unfortunate but we won't change it now.
185+ test " $f " = src/bin/pg_dump/compress_gzip.h && continue
186+ test " $f " = src/bin/pg_dump/compress_io.h && continue
187+ test " $f " = src/bin/pg_dump/compress_lz4.h && continue
188+ test " $f " = src/bin/pg_dump/compress_none.h && continue
189+ test " $f " = src/bin/pg_dump/compress_zstd.h && continue
190+ test " $f " = src/bin/pg_dump/parallel.h && continue
191+ test " $f " = src/bin/pg_dump/pg_backup_archiver.h && continue
192+ test " $f " = src/bin/pg_dump/pg_dump.h && continue
193+ fi
194+
147195 # OK, create .c file to include this .h file.
148196 {
197+ $cplusplus && echo ' extern "C" {'
149198 # Ideally we'd pre-include only the appropriate one of
150199 # postgres.h, postgres_fe.h, or c.h. We don't always have enough
151200 # info to guess which, but in some subdirectories there's a
174223 echo ' #include "postgres.h"' ;;
175224 esac
176225 echo " #include \" $f \" "
177- } > $tmp /test.c
226+ $cplusplus && echo ' };'
227+ } > $tmp /test.$ext
178228
179229 # Some subdirectories need extra -I switches.
180230 case " $f " in
193243 esac
194244
195245 # Run the test.
196- if ! ${CC :- gcc} $CPPFLAGS $CFLAGS -I $builddir -I $srcdir \
246+ if ! $COMPILER $COMPILER_FLAGS -I $builddir -I $srcdir \
197247 -I $builddir /src/include -I $srcdir /src/include \
198248 -I $builddir /src/interfaces/libpq -I $srcdir /src/interfaces/libpq \
199- $EXTRAINCLUDES $EXTRAFLAGS -c $tmp /test.c -o $tmp /test.o
249+ $EXTRAINCLUDES $EXTRAFLAGS -c $tmp /test.$ext -o $tmp /test.o
200250 then
201251 exit_status=1
202252 fi
0 commit comments