From 0f81674db8b399d8b733b63f0e1a4c5ed97634e3 Mon Sep 17 00:00:00 2001 From: Aidar Imamov Date: Wed, 14 Oct 2026 17:58:21 +0300 Subject: [PATCH v3] meson: Expose OpenSSL and ICU compiler flags to PGXS and pg_config Extensions built with PGXS that include libpq/libpq-be.h also include , so they need OpenSSL's preprocessor flags. The autoconf build provides those through CPPFLAGS, and ICU's through ICU_CFLAGS, which Makefile.global adds to CPPFLAGS. The meson build provided neither, so such extensions failed to compile when the library lives in a non-default prefix, e.g. Homebrew on macOS. Meson has no API to retrieve a dependency's compile arguments, so ask the tool that found the dependency. For pkg-config dependencies, run pkg-config --cflags with the same program and environment that Meson uses (pkg_config_path option, sys_root and pkg_config_libdir properties), querying icu-uc and icu-i18n together like configure does, and fail if that doesn't work. For CMake dependencies, use the include directories that Meson's CMake wrapper records in PACKAGE_INCLUDE_DIRS, which works for Find modules and package config files alike, and warn if there are none. Dependencies found with method 'system' don't need any flags; extra_include_dirs used by cc.find_library() not reaching PGXS is a more general, separate gap. The flags go where Makefile.global and autoconf expect them: OpenSSL in CPPFLAGS, ICU in ICU_CFLAGS. As with autoconf, pg_config --cppflags includes both. --- meson.build | 83 +++++++++++++++++++++++++++++++++++++++ src/common/meson.build | 2 +- src/include/meson.build | 6 ++- src/makefiles/meson.build | 4 +- 4 files changed, 91 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index d05634fef0d..191dbfa5cb6 100644 --- a/meson.build +++ b/meson.build @@ -3346,6 +3346,89 @@ endif +############################################################### +# Dependency compiler flags for PGXS and pg_config +############################################################### + +# Meson passes a dependency's compiler flags to our own targets directly, but +# extensions built with PGXS or using pg_config --cppflags need them spelled +# out, as the autoconf build does: OpenSSL is part of CPPFLAGS +# (libpq/libpq-be.h includes ), and ICU goes into ICU_CFLAGS. +# Meson doesn't expose a dependency's compile arguments, so ask the tool that +# found the dependency. +# +# Dependencies found by other methods get no flags here. Method 'system' is +# only chosen if the headers are on the compiler's default search path. +# Libraries found with cc.find_library() may rely on extra_include_dirs, which +# isn't passed on to PGXS at all; that is a separate, more general gap. + +# Run pkg-config the way Meson does for host dependencies (same program lookup, +# same environment), so that we get the flags of the .pc files Meson used. +pkg_config = find_program('pkg-config', required: false) +pkg_config_env = environment() +pkg_config_env.set('PKG_CONFIG_PATH', get_option('pkg_config_path')) +pkg_config_sysroot = meson.get_external_property('sys_root', '') +if pkg_config_sysroot != '' + pkg_config_env.set('PKG_CONFIG_SYSROOT_DIR', pkg_config_sysroot) +endif +pkg_config_libdir = [] + meson.get_external_property('pkg_config_libdir', []) +if pkg_config_libdir.length() > 0 + pkg_config_env.set('PKG_CONFIG_LIBDIR', pkg_config_libdir) +endif + +# For each dependency: the dependency objects and its pkg-config modules, +# queried together like configure's PKG_CHECK_MODULES does. +pgxs_dep_cflags = {} +foreach dep_name, dep_info : { + 'openssl': [[ssl], ['openssl']], + 'icu': [[icu, icu_i18n], ['icu-uc', 'icu-i18n']], + } + dep_objs = dep_info[0] + dep_flags = [] + + if not dep_objs[0].found() + # nothing to do + elif dep_objs[0].type_name() == 'pkgconfig' + pc_modules = dep_info[1] + if not pkg_config.found() + error('pkg-config not found, but needed to determine compiler flags for @0@'.format(dep_name)) + endif + pc_cflags = run_command(pkg_config, '--cflags', pc_modules, + env: pkg_config_env, check: false) + if pc_cflags.returncode() != 0 + error('pkg-config --cflags @0@ failed:\n@1@'.format( + ' '.join(pc_modules), pc_cflags.stderr())) + endif + # Use the output verbatim, as configure does, keeping pkg-config's quoting + if pc_cflags.stdout().strip() != '' + dep_flags += pc_cflags.stdout().strip() + endif + elif dep_objs[0].type_name() == 'cmake' + # PACKAGE_INCLUDE_DIRS is set by Meson's CMake dependency wrapper from the + # package's _INCLUDE_DIRS, _INCLUDES or _INCLUDE_DIR + # variable, for both Find modules and package config files. Those + # variables can't be queried directly: e.g. FindOpenSSL sets + # OPENSSL_INCLUDE_DIR only via find_path(), which Meson's CMake trace + # doesn't capture. + foreach dep : dep_objs + foreach incdir_raw : dep.get_variable(cmake: 'PACKAGE_INCLUDE_DIRS', + default_value: '').split(';') + incdir = incdir_raw.strip() + if incdir != '' and '-I' + incdir not in dep_flags + dep_flags += '-I' + incdir + endif + endforeach + endforeach + if dep_flags.length() == 0 + warning('could not determine include directories of CMake dependency @0@, PGXS builds may not find its headers'.format(dep_name)) + endif + endif + + pgxs_dep_cflags += {dep_name: dep_flags} +endforeach + + + ############################################################### # Build ############################################################### diff --git a/src/common/meson.build b/src/common/meson.build index fc89a173346..594380266b7 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -78,7 +78,7 @@ ryu_cflags += cflags_no_decl_after_statement config_info_sources = files('config_info.c',) config_info_cflags = [ '-DVAL_CC="@0@"'.format(var_cc), - '-DVAL_CPPFLAGS="@0@"'.format(var_cppflags), + '-DVAL_CPPFLAGS="@0@"'.format(var_cppflags_pg_config), '-DVAL_CFLAGS="@0@"'.format(var_cflags), '-DVAL_CFLAGS_SL="@0@"'.format(var_cflags_sl), '-DVAL_LDFLAGS="@0@"'.format(var_ldflags), diff --git a/src/include/meson.build b/src/include/meson.build index 7d734d92dab..37915393744 100644 --- a/src/include/meson.build +++ b/src/include/meson.build @@ -47,7 +47,11 @@ else var_cxx = 'g++' var_cxxflags = '' endif -var_cppflags = ' '.join(cppflags) +var_cppflags = ' '.join(cppflags + pgxs_dep_cflags['openssl']) +# pg_config --cppflags also includes ICU_CFLAGS, as with autoconf, where +# Makefile.global adds $(ICU_CFLAGS) to CPPFLAGS +var_cppflags_pg_config = ' '.join( + cppflags + pgxs_dep_cflags['openssl'] + pgxs_dep_cflags['icu']) 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 d0430b3519e..1822cb0e128 100644 --- a/src/makefiles/meson.build +++ b/src/makefiles/meson.build @@ -80,6 +80,8 @@ pgxs_kv = { 'perl_includespec': perl_includespec, 'python_includespec': python_includespec, + 'ICU_CFLAGS': ' '.join(pgxs_dep_cflags['icu']), + # these seem to be standard these days 'MKDIR_P': 'mkdir -p', 'LN_S': 'ln -s', @@ -158,8 +160,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