From 290bf24a89a075f3380c7e9af03d357475718887 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 4 Nov 2025 03:09:22 +1300
Subject: [PATCH v4 3/4] meson: Try to find libintl without -Dextra_XXX.

Since libintl is the only dependency that Meson can't usually find via
pkg-config on typical non-Linux Unix systems, let's try a bit harder to
find it with existing clues.

We typically find msgfmt in $PATH, so we can plausibly guess that the
library and headers are installed in the same prefix, and then try that
before giving up and requiring a manual path configuration.

This revealed some missing dependency declarations previously covered by
the catch-all extra_includes_dir, added here.

XXX Is this a terrible idea for some reason?
---
 meson.build                            | 34 +++++++++++++++++++++-----
 src/backend/jit/llvm/meson.build       |  5 +++-
 src/interfaces/libpq-oauth/meson.build |  1 +
 src/port/meson.build                   |  6 ++---
 4 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/meson.build b/meson.build
index 0f61ff6a700..2690d7a7b2b 100644
--- a/meson.build
+++ b/meson.build
@@ -2973,6 +2973,7 @@ cdata.set_quoted('PG_VERSION_STR',
 
 nlsopt = get_option('nls')
 libintl = not_found_dep
+libintl_include_dirs = []
 
 if not nlsopt.disabled()
   # otherwise there'd be lots of
@@ -2982,7 +2983,7 @@ if not nlsopt.disabled()
 
   # meson 0.59 has this wrapped in dependency('intl')
   if (msgfmt.found() and
-      cc.check_header('libintl.h', required: nlsopt,
+      cc.check_header('libintl.h',
         args: test_c_args, include_directories: postgres_inc))
 
     # in libc
@@ -2990,13 +2991,34 @@ if not nlsopt.disabled()
       libintl = declare_dependency()
     else
       libintl = cc.find_library('intl',
-        has_headers: ['libintl.h'], required: nlsopt,
+        has_headers: ['libintl.h'],
         header_include_directories: postgres_inc,
         dirs: test_lib_d)
     endif
   endif
 
+  if (msgfmt.found() and not libintl.found())
+    # libintl doesn't provide libintl.pc, but since we found its companion
+    # binary msgfmt we have a solid clue where to look, assuming standard
+    # layout within the install prefix.
+    guess_libintl_prefix = fs.parent(fs.parent(msgfmt.full_path()))
+    guess_libintl_lib_d = guess_libintl_prefix / 'lib'
+    guess_libintl_include = guess_libintl_prefix / 'include'
+    guess_libintl_include_dirs = include_directories(guess_libintl_include)
+    libintl_lib = cc.find_library('intl',
+      has_headers: ['libintl.h'],
+      header_include_directories: guess_libintl_include_dirs,
+      dirs: guess_libintl_lib_d)
+    if libintl_lib.found()
+      libintl = declare_dependency(dependencies: libintl_lib,
+                                   include_directories: guess_libintl_include_dirs)
+      libintl_include_dirs += guess_libintl_include # needed for llvmjit_types.bc
+    endif
+  endif
+
   if libintl.found()
+    cc.check_header('libintl.h', required: nlsopt,
+      args: test_c_args, include_directories: postgres_inc, dependencies: libintl)
     i18n = import('i18n')
     cdata.set('ENABLE_NLS', 1)
   endif
@@ -3220,14 +3242,14 @@ subdir('config')
 frontend_port_code = declare_dependency(
   compile_args: ['-DFRONTEND'],
   include_directories: [postgres_inc],
-  dependencies: os_deps,
+  dependencies: [os_deps, libintl]
 )
 
 backend_port_code = declare_dependency(
   compile_args: ['-DBUILDING_DLL'],
   include_directories: [postgres_inc],
   sources: [errcodes], # errcodes.h is needed due to use of ereport
-  dependencies: os_deps,
+  dependencies: [os_deps, libintl]
 )
 
 subdir('src/port')
@@ -3236,14 +3258,14 @@ frontend_common_code = declare_dependency(
   compile_args: ['-DFRONTEND'],
   include_directories: [postgres_inc],
   sources: generated_headers_stamp,
-  dependencies: [os_deps, zlib, zstd, lz4],
+  dependencies: [os_deps, libintl, zlib, zstd, lz4],
 )
 
 backend_common_code = declare_dependency(
   compile_args: ['-DBUILDING_DLL'],
   include_directories: [postgres_inc],
   sources: generated_headers_stamp,
-  dependencies: [os_deps, zlib, zstd],
+  dependencies: [os_deps, libintl, zlib, zstd],
 )
 
 subdir('src/common')
diff --git a/src/backend/jit/llvm/meson.build b/src/backend/jit/llvm/meson.build
index 805fbd69006..bcc5efed8a2 100644
--- a/src/backend/jit/llvm/meson.build
+++ b/src/backend/jit/llvm/meson.build
@@ -32,7 +32,7 @@ endif
 llvmjit = shared_module('llvmjit',
   llvmjit_sources,
   kwargs: pg_mod_args + {
-    'dependencies': pg_mod_args['dependencies'] + [llvm],
+    'dependencies': pg_mod_args['dependencies'] + [llvm, libintl],
     'cpp_args': pg_mod_args['cpp_args'] + llvm.get_variable(configtool: 'cxxflags').split(),
   }
 )
@@ -69,6 +69,9 @@ bitcode_cflags += '-I@BUILD_ROOT@/src/include'
 bitcode_cflags += '-I@BUILD_ROOT@/src/backend/utils/misc'
 bitcode_cflags += '-I@SOURCE_ROOT@/src/include'
 
+foreach d : libintl_include_dirs
+  bitcode_cflags += ['-I' + d]
+endforeach
 
 # Note this is intentionally not installed to bitcodedir, as it's not for
 # inlining
diff --git a/src/interfaces/libpq-oauth/meson.build b/src/interfaces/libpq-oauth/meson.build
index 505e1671b86..9d7fa1bc67c 100644
--- a/src/interfaces/libpq-oauth/meson.build
+++ b/src/interfaces/libpq-oauth/meson.build
@@ -29,6 +29,7 @@ libpq_oauth_st = static_library('libpq-oauth',
     frontend_stlib_code,
     libpq_oauth_deps,
     ssl, # libpq-int.h includes OpenSSL headers
+    gssapi, # and gssapi
   ],
   kwargs: default_lib_args,
 )
diff --git a/src/port/meson.build b/src/port/meson.build
index fc7b059fee5..9c4f5459e0a 100644
--- a/src/port/meson.build
+++ b/src/port/meson.build
@@ -154,14 +154,14 @@ endif
 pgport = {}
 pgport_variants = {
   '_srv': internal_lib_args + {
-    'dependencies': [backend_port_code],
+    'dependencies': [backend_port_code, libintl],
   },
   '': default_lib_args + {
-    'dependencies': [frontend_port_code],
+    'dependencies': [frontend_port_code, libintl],
   },
   '_shlib': default_lib_args + {
     'pic': true,
-    'dependencies': [frontend_port_code],
+    'dependencies': [frontend_port_code, libintl],
   },
 }
 
-- 
2.51.1

