From 5a8ccc68b0efa846d83e72b88f07ca47cf8d4846 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 27 Aug 2022 09:52:03 -0700
Subject: [PATCH v15 1/5] meson: Add postgresql-$version-llvm-jit-bitcode.pc

This .pc file collects the cflags (in particular the include directories)
needed to compile server code, so that the LLVM JIT bitcode emission can
reuse them. It is versioned and intentionally scoped to JIT bitcode only,
since there is not yet agreement on a stable, general extension-build
interface.

Author: Andres Freund <andres@anarazel.de>
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
Reviewed-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org
---
 src/backend/meson.build | 129 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/src/backend/meson.build b/src/backend/meson.build
index f737d799c61..94b2418ed53 100644
--- a/src/backend/meson.build
+++ b/src/backend/meson.build
@@ -213,6 +213,135 @@ pg_test_mod_args = pg_mod_args + {
 
 
 
+###############################################################
+# Define a .pc file used when emitting LLVM JIT bitcode
+###############################################################
+
+# Versioned name, so we don't claim a too-general name before there's
+# agreement on a stable interface for building extensions.
+pg_bitcode_pc_name = 'postgresql-@0@-llvm-jit-bitcode'.format(pg_version_major)
+
+pg_ext_vars = []
+pg_ext_vars_inst = []
+pg_ext_vars_uninst = []
+
+pg_ext_cflags = pg_mod_c_args + cppflags
+pg_ext_libs = [backend_mod_deps, thread_dep, ldflags, ldflags_mod]
+pg_ext_subdirs = ['']
+
+# Compute directories to add include directories to the .pc files for.
+# This is a bit more complicated due to port/win32 etc.
+i = 0
+foreach incdir : postgres_inc_d
+  if fs.is_absolute(incdir)
+    # an absolute path from -Dextra_include_dirs
+    pg_ext_cflags += '-I@0@'.format(incdir)
+    continue
+  elif incdir.startswith('src/include')
+    subincdir = dir_include_pkg_rel / 'server' / incdir.split('src/include/').get(1, '')
+  else
+    subincdir = ''
+  endif
+  pg_ext_subdirs += subincdir
+
+  # Add directories in source / build dir containing headers to cflags for the
+  # -uninstalled.pc. Older versions of pkg-config complain if a referenced
+  # variable is not defined, so we emit an empty one for the installed .pc
+  # file.
+  pg_ext_vars += [
+    'build_inc@0@=""'.format(i),
+    'src_inc@0@=""'.format(i),
+  ]
+  pg_ext_vars_uninst += [
+    'build_inc@0@=-I${prefix}/@1@'.format(i, incdir),
+    'src_inc@0@=-I${srcdir}/@1@'.format(i, incdir),
+  ]
+  pg_ext_cflags += [
+    '${build_inc@0@}'.format(i),
+    '${src_inc@0@}'.format(i)
+  ]
+
+  i += 1
+endforeach
+
+
+# We need to have these flags inside the .pc file but it is not very nice
+# since these flags (-fwrapv for example) change the behavior.
+pg_ext_cflags_warn = pg_ext_cflags + cflags_warn
+pg_ext_cflags += cflags
+
+# Directories for extensions to install into
+# TODO: more might be needed
+#
+# These directories can be configured as absolute paths (e.g. Debian sets
+# bindir/libdir/etc. to absolute paths). In that case they must not be
+# prefixed with ${prefix}, or we would end up with doubled paths.
+foreach var : [
+  ['pkglibdir', dir_lib_pkg],
+  ['dir_data', dir_data_extension],
+  ['dir_include', dir_include_extension],
+  ['dir_doc', dir_doc_extension],
+  ['dir_bitcode', dir_bitcode],
+  # referenced on some platforms, via mod_link_with_dir
+  ['bindir', dir_bin],
+]
+  if fs.is_absolute(var[1])
+    pg_ext_vars += '@0@=@1@'.format(var[0], var[1])
+  else
+    pg_ext_vars += '@0@=${prefix}/@1@'.format(var[0], var[1])
+  endif
+endforeach
+pg_ext_vars += 'dir_mod=${pkglibdir}'
+
+# TODO: Define variables making it easy to define tests, too
+
+# Some platforms need linker flags to link with binary, they are the same
+# between building with meson and .pc file, except that we have have to
+# reference a variable to make it work for both normal and -uninstalled .pc
+# files.
+if mod_link_args_fmt.length() != 0
+  assert(link_with_inst != '')
+  assert(link_with_uninst != '')
+
+  # We define mod_link_with_dir as bindir in MacOS but there is already bindir
+  # variable in pg_ext_vars, meson gives warning if we define it again.
+  if not link_with_inst.startswith('${bindir}')
+    pg_ext_vars_inst += 'mod_link_with=@0@'.format(link_with_inst)
+  endif
+  pg_ext_vars_uninst += 'mod_link_with=@0@'.format(link_with_uninst)
+
+  foreach el : mod_link_args_fmt
+    pg_ext_libs += el.format('${mod_link_with}')
+  endforeach
+endif
+
+# main .pc used to emit LLVM JIT bitcode
+pkgconfig.generate(
+  name: pg_bitcode_pc_name,
+  description: 'PostgreSQL LLVM JIT Bitcode Support',
+  url: pg_url,
+
+  subdirs: pg_ext_subdirs,
+  libraries: pg_ext_libs,
+  extra_cflags: pg_ext_cflags,
+
+  variables: pg_ext_vars + pg_ext_vars_inst,
+  uninstalled_variables: pg_ext_vars + pg_ext_vars_uninst,
+)
+
+# a .pc depending on the above, but with all our warnings enabled
+pkgconfig.generate(
+  name: pg_bitcode_pc_name + '-warnings',
+  description: 'PostgreSQL LLVM JIT Bitcode Support with compiler warnings the same as core code',
+  requires: pg_bitcode_pc_name,
+  url: pg_url,
+  extra_cflags: pg_ext_cflags_warn,
+
+  variables: pg_ext_vars + pg_ext_vars_inst,
+  uninstalled_variables: pg_ext_vars + pg_ext_vars_uninst,
+)
+
+
 # Shared modules that, on some system, link against the server binary. Only
 # enter these after we defined the server build.
 
-- 
2.47.3

