From c5c1e55d8237291a1e7ff68ca2306b1c21875e05 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 20 Jun 2024 00:33:49 -0700
Subject: [PATCH v2 06/10] meson: nls: Handle intl requiring iconv

This is e.g. the case when intl is a) not part of libc and b) statically
linked.

Author:
Reviewed-by:
Discussion: https://postgr.es/m/
Backpatch:
---
 meson.build | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/meson.build b/meson.build
index 04d5877212a..e4bfd105f4c 100644
--- a/meson.build
+++ b/meson.build
@@ -2757,19 +2757,45 @@ if not nlsopt.disabled()
   # warnings if not found.
   msgfmt = find_program('msgfmt', required: nlsopt, native: true)
 
+  intl_link_test = '''
+        #include <libintl.h>
+        int main(void)
+        {
+            return ngettext == 0;
+        }
+  '''
+
   # meson 0.59 has this wrapped in dependency('intl')
   if (msgfmt.found() and
       cc.check_header('libintl.h', required: nlsopt,
         args: test_c_args, include_directories: postgres_inc))
 
-    # in libc
     if cc.has_function('ngettext')
+      # gettext is part of libc
       libintl = declare_dependency()
     else
-      libintl = cc.find_library('intl',
-        has_headers: ['libintl.h'], required: nlsopt,
-        header_include_directories: postgres_inc,
-        dirs: test_lib_d)
+      # gettext is in a dedicated library
+      libintl_int = cc.find_library('intl',
+        required: nlsopt, disabler: true, dirs: test_lib_d,
+        has_headers: ['libintl.h'], header_include_directories: postgres_inc)
+
+      # On some platforms/configurations one needs to link to both intl and
+      # iconv. Test if that's the case.
+      if cc.links(intl_link_test,
+          name: 'ngettext', dependencies: libintl_int,
+          include_directories: postgres_inc, args: test_c_args)
+        # all good, libintl works without iconv
+        libintl = libintl_int
+      else
+        # need iconv as well
+        libiconv = cc.find_library('iconv',
+          required: nlsopt, disabler: true, dirs: test_lib_d)
+        if cc.links(intl_link_test,
+             name: 'ngettext', dependencies: [libintl_int, libiconv],
+             include_directories: postgres_inc, args: test_c_args)
+          libintl = declare_dependency(dependencies: [libintl_int, libiconv])
+        endif
+      endif
     endif
   endif
 
-- 
2.44.0.279.g3bd955d269

