On Sat, Nov 1, 2025 at 10:13 AM Thomas Munro <[email protected]> wrote:
> On Sat, Nov 1, 2025 at 7:14 AM Heikki Linnakangas <[email protected]> wrote:
> > However, I'm worried that we'll soon break it again. The new rule is
> > apparently "include libpq first", but we have no way of enforcing it.
>
> Hmm, my meson-fu is weak, but there must surely be some way to declare
> that you want libpq and have it automatically put things in the right
> order, will look into that...

If we really don't want every site that depends on both libpq and
frontend_code to have to remember to respect that order when declaring
dependencies, then we could instead change frontend_code to force
libpq_inc to appear in its include_directories before postgres_inc (=
where extra_include_dirs comes from).  This one-liner fixes the build
on my system:

 # for frontend binaries
 frontend_code = declare_dependency(
-  include_directories: [postgres_inc],
+  include_directories: [libpq_inc, postgres_inc],

That feels a little odd, because libpq is not really a dependency of
frontend_code, and not all frontend_code users also use libpq (though
almost all do).  Does this have any unwanted side-effects?  Is there a
better way to do this in a central place?

There are two other places that already have those two in
include_directories already, so their order should surely be flipped
to match, they just didn't happen to break on my system (I guess by
luck, ie not accessing APIs that changed incompatibly since the
version in my system-installed libpq headers).
From 25d830c8dab734349758286605bd2d1de643ed86 Mon Sep 17 00:00:00 2001
From: Thomas Munro <[email protected]>
Date: Sat, 1 Nov 2025 17:10:11 +1300
Subject: [PATCH v2] meson: Fix libpq header inclusion order.

Make sure that libpq_inc always comes before postgres_inc, since the
latter includes -Dextra_include_dirs.  We want to find the in-tree libpq
headers before any copy that might happen to be installed on the system
in that location.

Discussion: https://postgr.es/m/CA%2BhUKGJU9kJiSTwxvrDxqrDFw8VvsX9ZbZRL%3DqP%3DKXuFX_XH2A%40mail.gmail.com
---
 meson.build                             | 7 ++++++-
 src/fe_utils/meson.build                | 2 +-
 src/interfaces/ecpg/preproc/meson.build | 2 +-
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/meson.build b/meson.build
index 0f61ff6a700..2017b76a836 100644
--- a/meson.build
+++ b/meson.build
@@ -3301,7 +3301,12 @@ subdir('src/interfaces/libpq-oauth')
 
 # for frontend binaries
 frontend_code = declare_dependency(
-  include_directories: [postgres_inc],
+  # Not all code that depends on frontend_code also depends on libpq, but by
+  # pulling in its headers before postgres_inc we avoid exposing headers from a
+  # system-installed copy of libpq found in -Dextra_include_dirs, without
+  # requiring every frontend binary that depends on both to declare them in the
+  # right order.
+  include_directories: [libpq_inc, postgres_inc],
   link_with: [fe_utils, common_static, pgport_static],
   sources: generated_headers_stamp,
   dependencies: [os_deps, libintl],
diff --git a/src/fe_utils/meson.build b/src/fe_utils/meson.build
index ddac3c3a658..0a65b973779 100644
--- a/src/fe_utils/meson.build
+++ b/src/fe_utils/meson.build
@@ -32,7 +32,7 @@ fe_utils_sources += psqlscan
 fe_utils = static_library('libpgfeutils',
   fe_utils_sources,
   c_pch: pch_postgres_fe_h,
-  include_directories: [postgres_inc, libpq_inc],
+  include_directories: [libpq_inc, postgres_inc],
   c_args: host_system == 'windows' ? ['-DFD_SETSIZE=1024'] : [],
   dependencies: frontend_common_code,
   kwargs: default_lib_args,
diff --git a/src/interfaces/ecpg/preproc/meson.build b/src/interfaces/ecpg/preproc/meson.build
index aa948efc0dc..c754d885b7b 100644
--- a/src/interfaces/ecpg/preproc/meson.build
+++ b/src/interfaces/ecpg/preproc/meson.build
@@ -78,7 +78,7 @@ endif
 
 ecpg_exe = executable('ecpg',
   ecpg_sources,
-  include_directories: ['.', ecpg_inc, postgres_inc, libpq_inc],
+  include_directories: ['.', ecpg_inc, libpq_inc, postgres_inc],
   c_pch: pch_postgres_fe_h,
   dependencies: [frontend_code],
   kwargs: default_bin_args,
-- 
2.51.1

Reply via email to