From 25d830c8dab734349758286605bd2d1de643ed86 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
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

