Skip to content

Commit 8e5e3ff

Browse files
committed
Don't put library-supplied -L/-I switches before user-supplied ones.
For many optional libraries, we extract the -L and -l switches needed to link the library from a helper program such as llvm-config. In some cases we put the resulting -L switches into LDFLAGS ahead of -L switches specified via --with-libraries. That risks breaking the user's intention for --with-libraries. It's not such a problem if the library's -L switch points to a directory containing only that library, but on some platforms a library helper may "helpfully" offer a switch such as -L/usr/lib that points to a directory holding all standard libraries. If the user specified --with-libraries in hopes of overriding the standard build of some library, the -L/usr/lib switch prevents that from happening since it will come before the user-specified directory. To fix, avoid inserting these switches directly into LDFLAGS during configure, instead adding them to LIBDIRS or SHLIB_LINK. They will still eventually get added to LDFLAGS, but only after the switches coming from --with-libraries. The same problem exists for -I switches: those coming from --with-includes should appear before any coming from helper programs such as llvm-config. We have not heard field complaints about this case, but it seems certain that a user attempting to override a standard library could have issues. The changes for this go well beyond configure itself, however, because many Makefiles have occasion to manipulate CPPFLAGS to insert locally-desirable -I switches, and some of them got it wrong. The correct ordering is any -I switches pointing at within-the- source-tree-or-build-tree directories, then those from the tree-wide CPPFLAGS, then those from helper programs. There were several places that risked pulling in a system-supplied copy of libpq headers, for example, instead of the in-tree files. (Commit cb36f8e fixed one instance of that a few months ago, but this exercise found more.) The Meson build scripts may or may not have any comparable problems, but I'll leave it to someone else to investigate that. Reported-by: Charles Samborski <demurgos@demurgos.net> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/70f2155f-27ca-4534-b33d-7750e20633d7@demurgos.net Backpatch-through: 13
1 parent d5f014d commit 8e5e3ff

File tree

12 files changed

+33
-33
lines changed

12 files changed

+33
-33
lines changed

config/llvm.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# -----------------
55
#
66
# Look for the LLVM installation, check that it's new enough, set the
7-
# corresponding LLVM_{CFLAGS,CXXFLAGS,BINPATH} and LDFLAGS
7+
# corresponding LLVM_{CFLAGS,CXXFLAGS,BINPATH,LIBS}
88
# variables. Also verify that CLANG is available, to transform C
99
# into bitcode.
1010
#
@@ -55,7 +55,7 @@ AC_DEFUN([PGAC_LLVM_SUPPORT],
5555
5656
for pgac_option in `$LLVM_CONFIG --ldflags`; do
5757
case $pgac_option in
58-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
58+
-L*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
5959
esac
6060
done
6161

config/programs.m4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ AC_DEFUN([PGAC_CHECK_LIBCURL],
290290
pgac_save_LDFLAGS=$LDFLAGS
291291
pgac_save_LIBS=$LIBS
292292
293-
CPPFLAGS="$LIBCURL_CPPFLAGS $CPPFLAGS"
294-
LDFLAGS="$LIBCURL_LDFLAGS $LDFLAGS"
293+
CPPFLAGS="$CPPFLAGS $LIBCURL_CPPFLAGS"
294+
LDFLAGS="$LDFLAGS $LIBCURL_LDFLAGS"
295295
296296
AC_CHECK_HEADER(curl/curl.h, [],
297297
[AC_MSG_ERROR([header file <curl/curl.h> is required for --with-libcurl])])

configure

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5194,7 +5194,7 @@ fi
51945194

51955195
for pgac_option in `$LLVM_CONFIG --ldflags`; do
51965196
case $pgac_option in
5197-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
5197+
-L*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
51985198
esac
51995199
done
52005200

@@ -9436,12 +9436,12 @@ fi
94369436
# Note the user could also set XML2_CFLAGS/XML2_LIBS directly
94379437
for pgac_option in $XML2_CFLAGS; do
94389438
case $pgac_option in
9439-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
9439+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
94409440
esac
94419441
done
94429442
for pgac_option in $XML2_LIBS; do
94439443
case $pgac_option in
9444-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
9444+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
94459445
esac
94469446
done
94479447
fi
@@ -9666,12 +9666,12 @@ fi
96669666
# note that -llz4 will be added by AC_CHECK_LIB below.
96679667
for pgac_option in $LZ4_CFLAGS; do
96689668
case $pgac_option in
9669-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
9669+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
96709670
esac
96719671
done
96729672
for pgac_option in $LZ4_LIBS; do
96739673
case $pgac_option in
9674-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
9674+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
96759675
esac
96769676
done
96779677
fi
@@ -9807,12 +9807,12 @@ fi
98079807
# note that -lzstd will be added by AC_CHECK_LIB below.
98089808
for pgac_option in $ZSTD_CFLAGS; do
98099809
case $pgac_option in
9810-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
9810+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
98119811
esac
98129812
done
98139813
for pgac_option in $ZSTD_LIBS; do
98149814
case $pgac_option in
9815-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
9815+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
98169816
esac
98179817
done
98189818
fi
@@ -12723,8 +12723,8 @@ if test "$with_libcurl" = yes ; then
1272312723
pgac_save_LDFLAGS=$LDFLAGS
1272412724
pgac_save_LIBS=$LIBS
1272512725

12726-
CPPFLAGS="$LIBCURL_CPPFLAGS $CPPFLAGS"
12727-
LDFLAGS="$LIBCURL_LDFLAGS $LDFLAGS"
12726+
CPPFLAGS="$CPPFLAGS $LIBCURL_CPPFLAGS"
12727+
LDFLAGS="$LDFLAGS $LIBCURL_LDFLAGS"
1272812728

1272912729
ac_fn_c_check_header_mongrel "$LINENO" "curl/curl.h" "ac_cv_header_curl_curl_h" "$ac_includes_default"
1273012730
if test "x$ac_cv_header_curl_curl_h" = xyes; then :
@@ -16658,7 +16658,7 @@ fi
1665816658

1665916659
if test "$with_icu" = yes; then
1666016660
ac_save_CPPFLAGS=$CPPFLAGS
16661-
CPPFLAGS="$ICU_CFLAGS $CPPFLAGS"
16661+
CPPFLAGS="$CPPFLAGS $ICU_CFLAGS"
1666216662

1666316663
# Verify we have ICU's header files
1666416664
ac_fn_c_check_header_mongrel "$LINENO" "unicode/ucol.h" "ac_cv_header_unicode_ucol_h" "$ac_includes_default"
@@ -18876,7 +18876,7 @@ Use --without-tcl to disable building PL/Tcl." "$LINENO" 5
1887618876
fi
1887718877
# now that we have TCL_INCLUDE_SPEC, we can check for <tcl.h>
1887818878
ac_save_CPPFLAGS=$CPPFLAGS
18879-
CPPFLAGS="$TCL_INCLUDE_SPEC $CPPFLAGS"
18879+
CPPFLAGS="$CPPFLAGS $TCL_INCLUDE_SPEC"
1888018880
ac_fn_c_check_header_mongrel "$LINENO" "tcl.h" "ac_cv_header_tcl_h" "$ac_includes_default"
1888118881
if test "x$ac_cv_header_tcl_h" = xyes; then :
1888218882

@@ -18945,7 +18945,7 @@ fi
1894518945
# check for <Python.h>
1894618946
if test "$with_python" = yes; then
1894718947
ac_save_CPPFLAGS=$CPPFLAGS
18948-
CPPFLAGS="$python_includespec $CPPFLAGS"
18948+
CPPFLAGS="$CPPFLAGS $python_includespec"
1894918949
ac_fn_c_check_header_mongrel "$LINENO" "Python.h" "ac_cv_header_Python_h" "$ac_includes_default"
1895018950
if test "x$ac_cv_header_Python_h" = xyes; then :
1895118951

configure.ac

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,12 +1103,12 @@ if test "$with_libxml" = yes ; then
11031103
# Note the user could also set XML2_CFLAGS/XML2_LIBS directly
11041104
for pgac_option in $XML2_CFLAGS; do
11051105
case $pgac_option in
1106-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
1106+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
11071107
esac
11081108
done
11091109
for pgac_option in $XML2_LIBS; do
11101110
case $pgac_option in
1111-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
1111+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
11121112
esac
11131113
done
11141114
fi
@@ -1152,12 +1152,12 @@ if test "$with_lz4" = yes; then
11521152
# note that -llz4 will be added by AC_CHECK_LIB below.
11531153
for pgac_option in $LZ4_CFLAGS; do
11541154
case $pgac_option in
1155-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
1155+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
11561156
esac
11571157
done
11581158
for pgac_option in $LZ4_LIBS; do
11591159
case $pgac_option in
1160-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
1160+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
11611161
esac
11621162
done
11631163
fi
@@ -1177,12 +1177,12 @@ if test "$with_zstd" = yes; then
11771177
# note that -lzstd will be added by AC_CHECK_LIB below.
11781178
for pgac_option in $ZSTD_CFLAGS; do
11791179
case $pgac_option in
1180-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
1180+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
11811181
esac
11821182
done
11831183
for pgac_option in $ZSTD_LIBS; do
11841184
case $pgac_option in
1185-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
1185+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
11861186
esac
11871187
done
11881188
fi
@@ -1944,7 +1944,7 @@ fi
19441944

19451945
if test "$with_icu" = yes; then
19461946
ac_save_CPPFLAGS=$CPPFLAGS
1947-
CPPFLAGS="$ICU_CFLAGS $CPPFLAGS"
1947+
CPPFLAGS="$CPPFLAGS $ICU_CFLAGS"
19481948

19491949
# Verify we have ICU's header files
19501950
AC_CHECK_HEADER(unicode/ucol.h, [],
@@ -2344,7 +2344,7 @@ Use --without-tcl to disable building PL/Tcl.])
23442344
fi
23452345
# now that we have TCL_INCLUDE_SPEC, we can check for <tcl.h>
23462346
ac_save_CPPFLAGS=$CPPFLAGS
2347-
CPPFLAGS="$TCL_INCLUDE_SPEC $CPPFLAGS"
2347+
CPPFLAGS="$CPPFLAGS $TCL_INCLUDE_SPEC"
23482348
AC_CHECK_HEADER(tcl.h, [], [AC_MSG_ERROR([header file <tcl.h> is required for Tcl])])
23492349
CPPFLAGS=$ac_save_CPPFLAGS
23502350
fi
@@ -2381,7 +2381,7 @@ fi
23812381
# check for <Python.h>
23822382
if test "$with_python" = yes; then
23832383
ac_save_CPPFLAGS=$CPPFLAGS
2384-
CPPFLAGS="$python_includespec $CPPFLAGS"
2384+
CPPFLAGS="$CPPFLAGS $python_includespec"
23852385
AC_CHECK_HEADER(Python.h, [], [AC_MSG_ERROR([header file <Python.h> is required for Python])])
23862386
CPPFLAGS=$ac_save_CPPFLAGS
23872387
fi

src/Makefile.global.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ CPP = @CPP@
254254
CPPFLAGS = @CPPFLAGS@
255255
PG_SYSROOT = @PG_SYSROOT@
256256

257-
override CPPFLAGS := $(ICU_CFLAGS) $(LIBNUMA_CFLAGS) $(LIBURING_CFLAGS) $(CPPFLAGS)
257+
override CPPFLAGS += $(ICU_CFLAGS) $(LIBNUMA_CFLAGS) $(LIBURING_CFLAGS)
258258

259259
ifdef PGXS
260260
override CPPFLAGS := -I$(includedir_server) -I$(includedir_internal) $(CPPFLAGS)

src/backend/jit/llvm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ endif
3131
# All files in this directory use LLVM.
3232
CFLAGS += $(LLVM_CFLAGS)
3333
CXXFLAGS += $(LLVM_CXXFLAGS)
34-
override CPPFLAGS := $(LLVM_CPPFLAGS) $(CPPFLAGS)
34+
override CPPFLAGS += $(LLVM_CPPFLAGS)
3535
SHLIB_LINK += $(LLVM_LIBS)
3636

3737
# Because this module includes C++ files, we need to use a C++

src/bin/initdb/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include $(top_builddir)/src/Makefile.global
2020
# from libpq, else we have risks of version skew if we run with a libpq
2121
# shared library from a different PG version. Define
2222
# USE_PRIVATE_ENCODING_FUNCS to ensure that that happens.
23-
override CPPFLAGS := -DUSE_PRIVATE_ENCODING_FUNCS -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(ICU_CFLAGS) $(CPPFLAGS)
23+
override CPPFLAGS := -DUSE_PRIVATE_ENCODING_FUNCS -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(CPPFLAGS) $(ICU_CFLAGS)
2424

2525
# We need libpq only because fe_utils does.
2626
LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) $(ICU_LIBS)

src/common/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ libpgcommon_shlib.a: $(OBJS_SHLIB)
163163
# The JSON API normally exits on out-of-memory; disable that behavior for shared
164164
# library builds. This requires libpq's pqexpbuffer.h.
165165
jsonapi_shlib.o: override CPPFLAGS += -DJSONAPI_USE_PQEXPBUFFER
166-
jsonapi_shlib.o: override CPPFLAGS += -I$(libpq_srcdir)
166+
jsonapi_shlib.o: override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
167167

168168
# Because this uses its own compilation rule, it doesn't use the
169169
# dependency tracking logic from Makefile.global. To make sure that

src/interfaces/libpq-oauth/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ NAME = pq-oauth-$(MAJORVERSION)
2424
override shlib := lib$(NAME)$(DLSUFFIX)
2525
override stlib := libpq-oauth.a
2626

27-
override CPPFLAGS := -I$(libpq_srcdir) -I$(top_builddir)/src/port $(LIBCURL_CPPFLAGS) $(CPPFLAGS)
27+
override CPPFLAGS := -I$(libpq_srcdir) -I$(top_builddir)/src/port $(CPPFLAGS) $(LIBCURL_CPPFLAGS)
2828

2929
OBJS = \
3030
$(WIN32RES)

src/interfaces/libpq/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ NAME= pq
2424
SO_MAJOR_VERSION= 5
2525
SO_MINOR_VERSION= $(MAJORVERSION)
2626

27-
override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port
27+
override CPPFLAGS := -I$(srcdir) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port $(CPPFLAGS)
2828
ifneq ($(PORTNAME), win32)
2929
override CFLAGS += $(PTHREAD_CFLAGS)
3030
endif

0 commit comments

Comments
 (0)