From 3265dd20336852e2e2a975717111dc363efa6e09 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Tue, 17 Feb 2026 15:56:52 +0100
Subject: [PATCH] Add headerscheck support for meson

headerscheck depends on Makefile.global, which in the meson build only
contained the minimal configuration required for PGXS to work.
Headerscheck requires a few more configuration parameters, so these are
also properly generated now.

Additionally this add two new test targets for meson, making sure that
headerscheck is executed automatically with the entire test suite, or
manually when requested with:

meson test --suite headerscheck
meson test --suite headerscheck-c++
---
 src/makefiles/meson.build       | 49 +++++++++++++++++++++++++++++----
 src/meson.build                 |  2 ++
 src/tools/pginclude/meson.build | 38 +++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 5 deletions(-)
 create mode 100644 src/tools/pginclude/meson.build

diff --git a/src/makefiles/meson.build b/src/makefiles/meson.build
index 77f7a729cc2..fad059ea643 100644
--- a/src/makefiles/meson.build
+++ b/src/makefiles/meson.build
@@ -153,8 +153,6 @@ pgxs_bins = {
 }
 
 pgxs_empty = [
-  'ICU_CFLAGS', # needs to be added, included by public server headers
-
   # hard to see why we'd need these ones?
   'ZIC',
   'TCLSH',
@@ -183,14 +181,14 @@ pgxs_empty = [
   'PG_TEST_EXTRA',
   'DTRACEFLAGS', # only server has dtrace probes
 
-  'perl_archlibexp', 'perl_embed_ccflags', 'perl_embed_ldflags', 'perl_includespec', 'perl_privlibexp',
-  'python_additional_libs', 'python_includespec', 'python_libdir', 'python_libspec', 'python_majorversion', 'python_version',
+  'perl_archlibexp', 'perl_embed_ccflags', 'perl_embed_ldflags', 'perl_privlibexp',
+  'python_additional_libs', 'python_libdir', 'python_libspec', 'python_majorversion', 'python_version',
 
   # possible that some of these are referenced explicitly in pgxs makefiles?
   # For now not worth it.
   'TCL_INCLUDE_SPEC', 'TCL_LIBS', 'TCL_LIB_SPEC', 'TCL_SHARED_BUILD',
 
-  'LLVM_CFLAGS', 'LLVM_CPPFLAGS', 'LLVM_CXXFLAGS', 'LLVM_LIBS',
+  'LLVM_CFLAGS', 'LLVM_CXXFLAGS', 'LLVM_LIBS',
 
   'LDAP_LIBS_BE', 'LDAP_LIBS_FE',
 
@@ -272,3 +270,44 @@ endforeach
 foreach d, v : pgxs_deps
   pgxs_cdata.set('with_@0@'.format(d), v.found() ? 'yes' : 'no')
 endforeach
+
+# These variables are not needed by PGXS itself, but are used by the
+# headerscheck script (src/tools/pginclude/headerscheck) which reads
+# Makefile.global to obtain compiler flags for checking header
+# standalone compilability.
+
+_icu_cflags = ''
+if icu.found()
+  _pkgconf = find_program('pkg-config', required: false)
+  if _pkgconf.found()
+    _r = run_command(_pkgconf, '--cflags', 'icu-uc', check: false)
+    if _r.returncode() == 0
+      _icu_cflags = _r.stdout().strip()
+    endif
+  endif
+endif
+pgxs_cdata.set('ICU_CFLAGS', _icu_cflags)
+
+_llvm_cppflags = ''
+if llvm.found()
+  _llvm_cppflags = llvm.get_variable(configtool: 'cppflags',
+                                     default_value: '')
+endif
+pgxs_cdata.set('LLVM_CPPFLAGS', _llvm_cppflags)
+
+_perl_includespec = ''
+if perl_dep.found()
+  _perl_includespec = ' '.join(perl_ccflags)
+endif
+pgxs_cdata.set('perl_includespec', _perl_includespec)
+
+_python_includespec = ''
+if python3_dep.found()
+  _r = run_command(python, '-c',
+    'import sysconfig; print("-I" + sysconfig.get_path("include"))',
+    check: false)
+  if _r.returncode() == 0
+    _python_includespec = _r.stdout().strip()
+  endif
+endif
+pgxs_cdata.set('python_includespec', _python_includespec)
diff --git a/src/meson.build b/src/meson.build
index b57d4a5c259..8663ffba7b3 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -15,6 +15,8 @@ subdir('interfaces')
 
 subdir('tools/pg_bsd_indent')
 
+subdir('tools/pginclude')
+
 
 ### Generate a Makefile.global that's complete enough for PGXS to work.
 #
diff --git a/src/tools/pginclude/meson.build b/src/tools/pginclude/meson.build
new file mode 100644
index 00000000000..2268b3f2166
--- /dev/null
+++ b/src/tools/pginclude/meson.build
@@ -0,0 +1,38 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# Register headerscheck tests.
+#
+# The headerscheck script reads compiler flags from src/Makefile.global,
+# which meson populates via src/makefiles/meson.build (the same approach
+# used for PGXS support).
+#
+# An alternative approach would be to implement the header checks
+# natively in meson (discovering headers at configure time, generating
+# test source files, and compiling them via static_library targets).
+# That would integrate more tightly with meson's dependency system, but
+# would add significant complexity to the build files compared to
+# reusing the existing shell script.  It could be considered in the
+# future if the make build is no longer needed.
+#
+# Run selectively with: meson test --suite headerscheck
+
+headerscheck_script = find_program('headerscheck')
+
+test('headerscheck',
+  headerscheck_script,
+  args: [meson.project_source_root(), meson.project_build_root()],
+  depends: [generated_backend_headers_stamp],
+  timeout: 600,
+  suite: ['headerscheck'],
+)
+
+if have_cxx
+  test('headerscheck-c++',
+    headerscheck_script,
+    args: ['--cplusplus',
+           meson.project_source_root(), meson.project_build_root()],
+    depends: [generated_backend_headers_stamp],
+    timeout: 600,
+    suite: ['headerscheck'],
+  )
+endif
-- 
2.43.0

