Skip to content

Commit c5bd803

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 0ae8247 commit c5bd803

File tree

8 files changed

+18
-18
lines changed

8 files changed

+18
-18
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

configure

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5147,7 +5147,7 @@ fi
51475147

51485148
for pgac_option in `$LLVM_CONFIG --ldflags`; do
51495149
case $pgac_option in
5150-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
5150+
-L*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
51515151
esac
51525152
done
51535153

@@ -9214,12 +9214,12 @@ fi
92149214
# Note the user could also set XML2_CFLAGS/XML2_LIBS directly
92159215
for pgac_option in $XML2_CFLAGS; do
92169216
case $pgac_option in
9217-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
9217+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
92189218
esac
92199219
done
92209220
for pgac_option in $XML2_LIBS; do
92219221
case $pgac_option in
9222-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
9222+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
92239223
esac
92249224
done
92259225
fi
@@ -17034,7 +17034,7 @@ _ACEOF
1703417034

1703517035
if test "$with_icu" = yes; then
1703617036
ac_save_CPPFLAGS=$CPPFLAGS
17037-
CPPFLAGS="$ICU_CFLAGS $CPPFLAGS"
17037+
CPPFLAGS="$CPPFLAGS $ICU_CFLAGS"
1703817038

1703917039
# Verify we have ICU's header files
1704017040
ac_fn_c_check_header_mongrel "$LINENO" "unicode/ucol.h" "ac_cv_header_unicode_ucol_h" "$ac_includes_default"
@@ -19258,7 +19258,7 @@ Use --without-tcl to disable building PL/Tcl." "$LINENO" 5
1925819258
fi
1925919259
# now that we have TCL_INCLUDE_SPEC, we can check for <tcl.h>
1926019260
ac_save_CPPFLAGS=$CPPFLAGS
19261-
CPPFLAGS="$TCL_INCLUDE_SPEC $CPPFLAGS"
19261+
CPPFLAGS="$CPPFLAGS $TCL_INCLUDE_SPEC"
1926219262
ac_fn_c_check_header_mongrel "$LINENO" "tcl.h" "ac_cv_header_tcl_h" "$ac_includes_default"
1926319263
if test "x$ac_cv_header_tcl_h" = xyes; then :
1926419264

@@ -19327,7 +19327,7 @@ fi
1932719327
# check for <Python.h>
1932819328
if test "$with_python" = yes; then
1932919329
ac_save_CPPFLAGS=$CPPFLAGS
19330-
CPPFLAGS="$python_includespec $CPPFLAGS"
19330+
CPPFLAGS="$CPPFLAGS $python_includespec"
1933119331
ac_fn_c_check_header_mongrel "$LINENO" "Python.h" "ac_cv_header_Python_h" "$ac_includes_default"
1933219332
if test "x$ac_cv_header_Python_h" = xyes; then :
1933319333

configure.in

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,12 +1047,12 @@ if test "$with_libxml" = yes ; then
10471047
# Note the user could also set XML2_CFLAGS/XML2_LIBS directly
10481048
for pgac_option in $XML2_CFLAGS; do
10491049
case $pgac_option in
1050-
-I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
1050+
-I*|-D*) INCLUDES="$INCLUDES $pgac_option";;
10511051
esac
10521052
done
10531053
for pgac_option in $XML2_LIBS; do
10541054
case $pgac_option in
1055-
-L*) LDFLAGS="$LDFLAGS $pgac_option";;
1055+
-L*) LIBDIRS="$LIBDIRS $pgac_option";;
10561056
esac
10571057
done
10581058
fi
@@ -1961,7 +1961,7 @@ AC_CHECK_DECLS([strtoll, strtoull])
19611961

19621962
if test "$with_icu" = yes; then
19631963
ac_save_CPPFLAGS=$CPPFLAGS
1964-
CPPFLAGS="$ICU_CFLAGS $CPPFLAGS"
1964+
CPPFLAGS="$CPPFLAGS $ICU_CFLAGS"
19651965

19661966
# Verify we have ICU's header files
19671967
AC_CHECK_HEADER(unicode/ucol.h, [],
@@ -2320,7 +2320,7 @@ Use --without-tcl to disable building PL/Tcl.])
23202320
fi
23212321
# now that we have TCL_INCLUDE_SPEC, we can check for <tcl.h>
23222322
ac_save_CPPFLAGS=$CPPFLAGS
2323-
CPPFLAGS="$TCL_INCLUDE_SPEC $CPPFLAGS"
2323+
CPPFLAGS="$CPPFLAGS $TCL_INCLUDE_SPEC"
23242324
AC_CHECK_HEADER(tcl.h, [], [AC_MSG_ERROR([header file <tcl.h> is required for Tcl])])
23252325
CPPFLAGS=$ac_save_CPPFLAGS
23262326
fi
@@ -2357,7 +2357,7 @@ fi
23572357
# check for <Python.h>
23582358
if test "$with_python" = yes; then
23592359
ac_save_CPPFLAGS=$CPPFLAGS
2360-
CPPFLAGS="$python_includespec $CPPFLAGS"
2360+
CPPFLAGS="$CPPFLAGS $python_includespec"
23612361
AC_CHECK_HEADER(Python.h, [], [AC_MSG_ERROR([header file <Python.h> is required for Python])])
23622362
CPPFLAGS=$ac_save_CPPFLAGS
23632363
fi

src/Makefile.global.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ CPP = @CPP@
242242
CPPFLAGS = @CPPFLAGS@
243243
PG_SYSROOT = @PG_SYSROOT@
244244

245-
override CPPFLAGS := $(ICU_CFLAGS) $(CPPFLAGS)
245+
override CPPFLAGS += $(ICU_CFLAGS)
246246

247247
ifdef PGXS
248248
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/interfaces/libpq/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ NAME= pq
2121
SO_MAJOR_VERSION= 5
2222
SO_MINOR_VERSION= $(MAJORVERSION)
2323

24-
override CPPFLAGS := -DFRONTEND -DUNSAFE_STAT_OK -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port
24+
override CPPFLAGS := -DFRONTEND -DUNSAFE_STAT_OK -I$(srcdir) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port $(CPPFLAGS)
2525
ifneq ($(PORTNAME), win32)
2626
override CFLAGS += $(PTHREAD_CFLAGS)
2727
endif

src/pl/plpython/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ifeq ($(PORTNAME), win32)
1111
override python_libspec =
1212
endif
1313

14-
override CPPFLAGS := -I. -I$(srcdir) $(python_includespec) $(CPPFLAGS)
14+
override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS) $(python_includespec)
1515

1616
rpathdir = $(python_libdir)
1717

src/pl/tcl/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ top_builddir = ../../..
1111
include $(top_builddir)/src/Makefile.global
1212

1313

14-
override CPPFLAGS := -I. -I$(srcdir) $(TCL_INCLUDE_SPEC) $(CPPFLAGS)
14+
override CPPFLAGS := -I. -I$(srcdir) $(CPPFLAGS) $(TCL_INCLUDE_SPEC)
1515

1616
# On Windows, we don't link directly with the Tcl library; see below
1717
ifneq ($(PORTNAME), win32)

0 commit comments

Comments
 (0)