From 44a7e1227fd4a57472b9dffe69f15813e0b2734c Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Sat, 29 Nov 2025 19:25:20 +0300
Subject: [PATCH v3 1/3] meson: Add {python|perl}_includespec to the
 Makefile.global

---
 src/makefiles/meson.build | 7 +++++--
 meson.build               | 9 +++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/makefiles/meson.build b/src/makefiles/meson.build
index 0def244c901..3c732a94653 100644
--- a/src/makefiles/meson.build
+++ b/src/makefiles/meson.build
@@ -77,6 +77,9 @@ pgxs_kv = {
   'STRIP_STATIC_LIB': ' '.join(strip_static_cmd),
   'STRIP_SHARED_LIB': ' '.join(strip_shared_cmd),
 
+  'python_includespec': python_includespec,
+  'perl_includespec': perl_includespec,
+
   # these seem to be standard these days
   'MKDIR_P': 'mkdir -p',
   'LN_S': 'ln -s',
@@ -181,8 +184,8 @@ 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.
diff --git a/meson.build b/meson.build
index 6e7ddd74683..0a68ebf1598 100644
--- a/meson.build
+++ b/meson.build
@@ -1176,6 +1176,7 @@ endif
 # Library: Perl (for plperl)
 ###############################################################
 
+perl_includespec = ''
 perlopt = get_option('plperl')
 perl_dep = not_found_dep
 perlversion = ''
@@ -1202,6 +1203,7 @@ if not perlopt.disabled()
     useshrplib = run_command(perl_conf_cmd, 'useshrplib', check: true).stdout()
 
     perl_inc_dir = '@0@/CORE'.format(archlibexp)
+    perl_includespec = '-I@0@'.format(perl_inc_dir)
 
     if perlversion.version_compare('< 5.14')
       perl_may_work = false
@@ -1220,6 +1222,7 @@ if not perlopt.disabled()
     if not fs.is_file('@0@/perl.h'.format(perl_inc_dir)) and \
        fs.is_file('@0@@1@/perl.h'.format(pg_sysroot, perl_inc_dir))
       perl_ccflags = ['-iwithsysroot', perl_inc_dir]
+      perl_includespec = '-iwithsysroot @0@/CORE'.format(archlibexp)
     endif
 
     # check compiler finds header
@@ -1322,6 +1325,7 @@ endif
 # Library: Python (for plpython)
 ###############################################################
 
+python_includespec = ''
 pyopt = get_option('plpython')
 python3_dep = not_found_dep
 if not pyopt.disabled()
@@ -1334,6 +1338,11 @@ if not pyopt.disabled()
       python3_dep = not_found_dep
     endif
   endif
+
+  if python3_dep.found()
+    command = [python, '-c', 'import sysconfig; print("-I" + sysconfig.get_config_var("INCLUDEPY"))']
+    python_includespec = run_command(command, check: true).stdout().strip()
+  endif
 endif
 
 
-- 
2.51.0

