From: Aidar Imamov <imamovaj22@gmail.com>
Subject: [PATCH] meson: expose OpenSSL and ICU include dirs to PGXS-built
 extensions

Some external dependencies have headers that are included by public
server headers (e.g. libpq-be.h includes openssl/ssl.h).  Extensions
built with PGXS that include such server headers need the dependency's
include directory on the compiler search path.  The autoconf build
provides this via CPPFLAGS for OpenSSL and via ICU_CFLAGS for ICU, but
the meson build did not: OpenSSL's include path stayed inside the
dependency object, and ICU_CFLAGS was stubbed out in pgxs_empty.

Extract the OpenSSL include directory from the ssl dependency and append
it to var_cppflags, and populate ICU_CFLAGS from the icu dependency
instead of leaving it empty.  On systems where OpenSSL or ICU is
installed in a non-default prefix (e.g. Homebrew on macOS) this is
required for extensions to compile at all.
---
 src/include/meson.build   | 15 ++++++++++++++-
 src/makefiles/meson.build | 21 +++++++++++++++++++--
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/src/include/meson.build b/src/include/meson.build
index 7d734d92dab..a7b7f71cea0 100644
--- a/src/include/meson.build
+++ b/src/include/meson.build
@@ -47,7 +47,20 @@ else
   var_cxx = 'g++'
   var_cxxflags = ''
 endif
-var_cppflags = ' '.join(cppflags)
+# libpq-be.h, a public server header, includes openssl/ssl.h.  Extensions
+# built with PGXS that include such headers need the OpenSSL include directory
+# on the compiler search path.  The autoconf build gets this from CPPFLAGS
+# passed to configure; meson tracks it in the dependency object, so extract
+# it here so that it ends up in CPPFLAGS as well.
+openssl_cppflags = []
+if ssl.found()
+  openssl_incdir = ssl.get_variable(pkgconfig: 'includedir', default_value: '')
+  if openssl_incdir != ''
+    openssl_cppflags += '-I' + openssl_incdir
+  endif
+endif
+
+var_cppflags = ' '.join(cppflags + openssl_cppflags)
 var_cflags_sl = ' '.join(cc.get_supported_arguments('-fPIC'))
 # explicitly add -Wl,--as-needed, normally added by meson, but we want it for
 # PGXS compatibility
diff --git a/src/makefiles/meson.build b/src/makefiles/meson.build
index 2401025d1cd..0da5352fca2 100644
--- a/src/makefiles/meson.build
+++ b/src/makefiles/meson.build
@@ -137,6 +137,25 @@ else
   }
 endif
 
+# ICU headers are included by public server headers, so extensions that
+# include those need the ICU include directory on the compiler search path.
+# Makefile.global adds $(ICU_CFLAGS) to CPPFLAGS, so populate it here, as the
+# autoconf build does via pkg-config.
+icu_cflags = ''
+if icu.found()
+  icu_incdir = icu.get_variable(pkgconfig: 'includedir',
+                                cmake: 'ICU_INCLUDE_DIRS',
+                                default_value: '')
+  if icu_incdir != ''
+    # cmake may return a ';'-separated list; take the first entry
+    icu_cflags = '-I' + icu_incdir.split(';')[0].strip()
+  endif
+endif
+
+pgxs_kv += {
+  'ICU_CFLAGS': icu_cflags,
+}
+
 pgxs_bins = {
   'AR':
     find_program(['ar'], native: true, required: false),
@@ -156,8 +175,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',
-- 
2.47.1

