From ba982266d6dfa6b867c0a0fc5273716b5afea340 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sat, 27 Aug 2022 09:52:03 -0700 Subject: [PATCH 1/6] meson: Add postgresql-extension.pc for building extension libraries This should work with several other buildsystems. TODO: Docs Author: Andres Freund Author: Nazir Bilal Yavuz Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- src/backend/meson.build | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/src/backend/meson.build b/src/backend/meson.build index b831a541652b..dd903591e349 100644 --- a/src/backend/meson.build +++ b/src/backend/meson.build @@ -188,6 +188,116 @@ pg_test_mod_args = pg_mod_args + { +############################################################### +# Define a .pc file that can be used to build server extensions +############################################################### + +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 + + +# Extension modules should likely also use -fwrapv etc. But it it's a bit odd +# to expose it to a .pc file? +pg_ext_cflags_warn = pg_ext_cflags + cflags_warn +pg_ext_cflags += cflags + +# Directories for extensions to install into +# XXX: more needed +pg_ext_vars += 'pkglibdir=${prefix}/@0@'.format(dir_lib_pkg) +pg_ext_vars += 'dir_mod=${pkglibdir}' +pg_ext_vars += 'dir_data=${prefix}/@0@'.format(dir_data_extension) +pg_ext_vars += 'dir_include=${prefix}/@0@'.format(dir_include_extension) +pg_ext_vars += 'dir_doc=${prefix}/@0@'.format(dir_doc_extension) +pg_ext_vars += 'dir_bitcode=${prefix}/@0@'.format(dir_bitcode) +# referenced on some platforms, via mod_link_with_dir +pg_ext_vars += 'bindir=${prefix}/@0@'.format(dir_bin) + +# XXX: 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 != '') + + pg_ext_vars_inst += 'mod_link_with=@0@'.format(link_with_inst) + 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 to build extensions +pkgconfig.generate( + name: 'postgresql-extension', + description: 'PostgreSQL Extension 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: 'postgresql-extension-warnings', + description: 'PostgreSQL Extension Support - Compiler Warnings', + requires: 'postgresql-extension', + 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. From ab657b3393692f578f5528a4a2474c6f7ca7e421 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Thu, 27 Feb 2025 17:45:31 +0300 Subject: [PATCH 2/6] meson: Test building extensions by using postgresql-extension.pc The 'test_meson_extensions' pyton wrapper is added to run these tests. It compiles and builds extensions at ${build}/testrun/meson_extensions/${extension_name} path. The tests for building amcheck, auth_delay and postgres_fdw extensions are added. These are also examples of how to build extensions by using postgresql-extension.pc. Author: Andres Freund Author: Nazir Bilal Yavuz Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- meson.build | 32 +++++++++ src/test/modules/meson.build | 1 + .../modules/test_meson_extensions/meson.build | 3 + .../amcheck/meson.build | 28 ++++++++ .../auth_delay/meson.build | 17 +++++ .../test_pkg_config_extensions/meson.build | 24 +++++++ .../postgres_fdw/meson.build | 30 ++++++++ src/tools/ci/test_meson_extensions | 70 +++++++++++++++++++ 8 files changed, 205 insertions(+) create mode 100644 src/test/modules/test_meson_extensions/meson.build create mode 100644 src/test/modules/test_meson_extensions/test_pkg_config_extensions/amcheck/meson.build create mode 100644 src/test/modules/test_meson_extensions/test_pkg_config_extensions/auth_delay/meson.build create mode 100644 src/test/modules/test_meson_extensions/test_pkg_config_extensions/meson.build create mode 100644 src/test/modules/test_meson_extensions/test_pkg_config_extensions/postgres_fdw/meson.build create mode 100644 src/tools/ci/test_meson_extensions diff --git a/meson.build b/meson.build index c1e17aa30405..c520986ba720 100644 --- a/meson.build +++ b/meson.build @@ -3049,6 +3049,7 @@ nls_targets = [] # Define the tests to distribute them to the correct test styles later test_deps = [] tests = [] +meson_extension_tests = [] # Default options for targets @@ -3612,6 +3613,37 @@ sys.exit(sp.returncode) suite: ['setup']) +# it seems freebsd doesn't use libdir for pkgconfig path +if host_system == 'freebsd' + pkgconf_installdir = dir_prefix / 'libdata' / 'pkgconfig' +else + pkgconf_installdir = dir_prefix / dir_lib / 'pkgconfig' +endif +test_pkg_conf_file = files('src/tools/ci/test_meson_extensions') + +foreach test : meson_extension_tests + if test['kind'] not in ['pkg_config'] + error('unknown kind @0@ of test in @1@'.format(test['kind'], test['sd'])) + endif + + test_group = 'meson_@0@_extensions'.format(test['kind']) + + test(test_group / test['name'], + test_pkg_conf_file, + args: [ + '--meson', meson_bin.path(), + '--meson_args', meson_args, + '--test_dir', test['sd'], + '--test_out_dir', test_result_dir / 'meson_extensions' / test['name'], + '--builddir', meson.build_root(), + '--pkg_conf_path', get_option('pkg_config_path'), + '--', + cc.cmd_array(), + ], + suite: test_group, + ) + +endforeach ############################################################### # Test Generation diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build index 14fc761c4cfa..5c8049aab1fa 100644 --- a/src/test/modules/meson.build +++ b/src/test/modules/meson.build @@ -30,6 +30,7 @@ subdir('test_integerset') subdir('test_json_parser') subdir('test_lfind') subdir('test_lwlock_tranches') +subdir('test_meson_extensions') subdir('test_misc') subdir('test_oat_hooks') subdir('test_parser') diff --git a/src/test/modules/test_meson_extensions/meson.build b/src/test/modules/test_meson_extensions/meson.build new file mode 100644 index 000000000000..06cf5d555df7 --- /dev/null +++ b/src/test/modules/test_meson_extensions/meson.build @@ -0,0 +1,3 @@ +# Copyright (c) 2022-2025, PostgreSQL Global Development Group + +subdir('test_pkg_config_extensions') diff --git a/src/test/modules/test_meson_extensions/test_pkg_config_extensions/amcheck/meson.build b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/amcheck/meson.build new file mode 100644 index 000000000000..482a543eb86a --- /dev/null +++ b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/amcheck/meson.build @@ -0,0 +1,28 @@ +# Copyright (c) 2022-2025, PostgreSQL Global Development Group + +project('amcheck', 'c') + +amcheck_path = '../../../../../../contrib/amcheck/' + +amcheck_sources = files( + amcheck_path / 'verify_heapam.c', + amcheck_path / 'verify_nbtree.c', +) + +pg_ext = dependency('postgresql-extension-warnings') + +amcheck = shared_module('amcheck', + amcheck_sources, + dependencies: pg_ext, + install_dir: pg_ext.get_variable(pkgconfig: 'dir_mod'), +) + +install_data( + amcheck_path / 'amcheck.control', + amcheck_path / 'amcheck--1.0.sql', + amcheck_path / 'amcheck--1.0--1.1.sql', + amcheck_path / 'amcheck--1.1--1.2.sql', + amcheck_path / 'amcheck--1.2--1.3.sql', + amcheck_path / 'amcheck--1.3--1.4.sql', + install_dir: pg_ext.get_variable(pkgconfig: 'dir_data'), +) diff --git a/src/test/modules/test_meson_extensions/test_pkg_config_extensions/auth_delay/meson.build b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/auth_delay/meson.build new file mode 100644 index 000000000000..98ad24cc1832 --- /dev/null +++ b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/auth_delay/meson.build @@ -0,0 +1,17 @@ +# Copyright (c) 2022-2025, PostgreSQL Global Development Group + +project('auth_delay', 'c') + +auth_delay_path = '../../../../../../contrib/auth_delay/' + +auth_delay_sources = files( + auth_delay_path / 'auth_delay.c', +) + +pg_ext = dependency('postgresql-extension-warnings') + +auth_delay = shared_module('auth_delay', + auth_delay_sources, + dependencies: pg_ext, + install_dir: pg_ext.get_variable(pkgconfig: 'dir_mod'), +) diff --git a/src/test/modules/test_meson_extensions/test_pkg_config_extensions/meson.build b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/meson.build new file mode 100644 index 000000000000..dae94a384a11 --- /dev/null +++ b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/meson.build @@ -0,0 +1,24 @@ +# Copyright (c) 2022-2025, PostgreSQL Global Development Group + +# pkgconfig is not available on Windows, so skip it. +if host_machine.system() == 'windows' + subdir_done() +endif + +meson_extension_tests += { + 'name': 'amcheck', + 'kind': 'pkg_config', + 'sd': meson.current_source_dir() / 'amcheck', +} + +meson_extension_tests += { + 'name': 'auth_delay', + 'kind': 'pkg_config', + 'sd': meson.current_source_dir() / 'auth_delay', +} + +meson_extension_tests += { + 'name': 'postgres_fdw', + 'kind': 'pkg_config', + 'sd': meson.current_source_dir() / 'postgres_fdw', +} diff --git a/src/test/modules/test_meson_extensions/test_pkg_config_extensions/postgres_fdw/meson.build b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/postgres_fdw/meson.build new file mode 100644 index 000000000000..6d561e3789b8 --- /dev/null +++ b/src/test/modules/test_meson_extensions/test_pkg_config_extensions/postgres_fdw/meson.build @@ -0,0 +1,30 @@ +# Copyright (c) 2022-2025, PostgreSQL Global Development Group + +project('auth_delay', 'c') + +postgres_fdw_path = '../../../../../../contrib/postgres_fdw/' + +postgres_fdw_sources = files( + postgres_fdw_path / 'connection.c', + postgres_fdw_path / 'deparse.c', + postgres_fdw_path / 'option.c', + postgres_fdw_path / 'postgres_fdw.c', + postgres_fdw_path / 'shippable.c', +) + +pg_ext = dependency('postgresql-extension-warnings') +libpq = dependency('libpq') + +postgres_fdw = shared_module('postgres_fdw', + postgres_fdw_sources, + dependencies: [pg_ext, libpq], + install_dir: pg_ext.get_variable(pkgconfig: 'dir_mod'), +) + +install_data( + postgres_fdw_path / 'postgres_fdw.control', + postgres_fdw_path / 'postgres_fdw--1.0.sql', + postgres_fdw_path / 'postgres_fdw--1.0--1.1.sql', + postgres_fdw_path / 'postgres_fdw--1.1--1.2.sql', + install_dir: pg_ext.get_variable(pkgconfig: 'dir_data'), +) diff --git a/src/tools/ci/test_meson_extensions b/src/tools/ci/test_meson_extensions new file mode 100644 index 000000000000..50358121f493 --- /dev/null +++ b/src/tools/ci/test_meson_extensions @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 + +import argparse +import os +import shutil +import subprocess + +parser = argparse.ArgumentParser() + +parser.add_argument('--meson', help='path to meson binary', + type=str, required=True) +parser.add_argument('--meson_args', help='args of meson binary', + type=str, nargs='*', required=False) +parser.add_argument('--test_dir', help='test source directory', + type=str, required=True) +parser.add_argument('--test_out_dir', help='test output directory', + type=str, required=True) +parser.add_argument('--builddir', help='meson build directory', + type=str, required=True) +parser.add_argument('--pkg_conf_path', + help='PKG_CONF_PATH from surrounding meson build', + type=str, nargs='?', const='', required=False) +parser.add_argument('c_args', help='c_args from surrounding meson build', + nargs='*') + +args = parser.parse_args() + +meson_bin = args.meson +meson_args = ' '.join(args.meson_args) +test_source_dir = args.test_dir +test_out_dir = args.test_out_dir +build_dir = args.builddir +pkg_conf_path = args.pkg_conf_path +c_args = ' '.join(args.c_args) + +exit_code = 0 + +def remove_duplicates(duplicate_str): + words = duplicate_str.split() + return ' '.join(sorted(set(words), key=words.index)) + + +def run_tests(pkg_conf_path_local, message): + print('\n{}\n{}\n'.format('#' * 60, message), flush=True) + + env = {**os.environ, } + env['PKG_CONFIG_PATH'] = '{}:{}:{}'.format( + pkg_conf_path_local, pkg_conf_path, env.get('PKG_CONFIG_PATH', ''), + ).strip(': ') + env['CC'] = '{} {}'.format( + c_args, env.get('CC', ''), + ) + env['CC'] = remove_duplicates(env['CC']) + + # Clear the build directory beforehand. + if os.path.exists(test_out_dir): + shutil.rmtree(test_out_dir) + + if meson_args: + meson_setup_command = [meson_bin, meson_args, 'setup', test_out_dir] + else: + meson_setup_command = [meson_bin, 'setup', test_out_dir] + + meson_compile_command = ['meson', 'compile', '-C', test_out_dir, '-v'] + + subprocess.run(meson_setup_command, env=env, cwd=test_source_dir, check=True) + subprocess.run(meson_compile_command, cwd=test_source_dir, check=True) + +run_tests(os.path.join(build_dir, 'meson-uninstalled'), + message='Testing postgresql-extension-warnings-uninstalled') From c1c69ba82c05f21a9006d6c0c117c551a2775487 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Thu, 6 Mar 2025 17:46:57 +0300 Subject: [PATCH 3/6] meson: [WIP] Add docs for postgresql-extension.pc Author: Andres Freund Author: Nazir Bilal Yavuz Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- doc/src/sgml/acronyms.sgml | 2 +- doc/src/sgml/extend.sgml | 101 +++++++++++++++++++++++-------------- doc/src/sgml/jit.sgml | 2 +- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/doc/src/sgml/acronyms.sgml b/doc/src/sgml/acronyms.sgml index 2f906e9f018d..57f49c06a19e 100644 --- a/doc/src/sgml/acronyms.sgml +++ b/doc/src/sgml/acronyms.sgml @@ -579,7 +579,7 @@ PGXS - PostgreSQL Extension System + PostgreSQL Extension System diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml index 63c5ec6d1eb9..6d9fe5e96ad4 100644 --- a/doc/src/sgml/extend.sgml +++ b/doc/src/sgml/extend.sgml @@ -1426,7 +1426,7 @@ include $(PGXS) This makefile relies on PGXS, which is described - in . The command make install + in . The command make install will install the control and script files into the correct directory as reported by pg_config. @@ -1439,21 +1439,26 @@ include $(PGXS) - + Extension Building Infrastructure - - pgxs - - If you are thinking about distributing your PostgreSQL extension modules, setting up a portable build system for them can be fairly difficult. Therefore the PostgreSQL installation provides a build - infrastructure for extensions, called PGXS, so - that simple extension modules can be built simply against an - already installed server. PGXS is mainly intended + infrastructure for extensions, called PGXS + () and + its meson counterpart postgresql-extension.pc + (). + + + + + + PGXS + + PGXS is mainly intended for extensions that include C code, although it can be used for pure-SQL extensions too. Note that PGXS is not intended to be a universal build system framework that can be used @@ -1493,7 +1498,7 @@ include $(PGXS) Set one of these three variables to specify what is built: - + MODULES @@ -1503,7 +1508,7 @@ include $(PGXS) - + MODULE_big @@ -1513,7 +1518,7 @@ include $(PGXS) - + PROGRAM @@ -1527,7 +1532,7 @@ include $(PGXS) The following variables can also be set: - + EXTENSION @@ -1539,7 +1544,7 @@ include $(PGXS) - + MODULEDIR @@ -1552,7 +1557,7 @@ include $(PGXS) - + DATA @@ -1561,7 +1566,7 @@ include $(PGXS) - + DATA_built @@ -1572,7 +1577,7 @@ include $(PGXS) - + DATA_TSEARCH @@ -1582,7 +1587,7 @@ include $(PGXS) - + DOCS @@ -1592,7 +1597,7 @@ include $(PGXS) - + HEADERS HEADERS_built @@ -1608,7 +1613,7 @@ include $(PGXS) - + HEADERS_$MODULE HEADERS_built_$MODULE @@ -1634,7 +1639,7 @@ include $(PGXS) - + SCRIPTS @@ -1644,7 +1649,7 @@ include $(PGXS) - + SCRIPTS_built @@ -1655,7 +1660,7 @@ include $(PGXS) - + REGRESS @@ -1664,7 +1669,7 @@ include $(PGXS) - + REGRESS_OPTS @@ -1673,7 +1678,7 @@ include $(PGXS) - + ISOLATION @@ -1682,7 +1687,7 @@ include $(PGXS) - + ISOLATION_OPTS @@ -1692,7 +1697,7 @@ include $(PGXS) - + TAP_TESTS @@ -1701,7 +1706,7 @@ include $(PGXS) - + NO_INSTALL @@ -1711,7 +1716,7 @@ include $(PGXS) - + NO_INSTALLCHECK @@ -1720,7 +1725,7 @@ include $(PGXS) - + EXTRA_CLEAN @@ -1729,7 +1734,7 @@ include $(PGXS) - + PG_CPPFLAGS @@ -1738,7 +1743,7 @@ include $(PGXS) - + PG_CFLAGS @@ -1747,7 +1752,7 @@ include $(PGXS) - + PG_CXXFLAGS @@ -1756,7 +1761,7 @@ include $(PGXS) - + PG_LDFLAGS @@ -1765,7 +1770,7 @@ include $(PGXS) - + PG_LIBS @@ -1774,7 +1779,7 @@ include $(PGXS) - + SHLIB_LINK @@ -1783,7 +1788,7 @@ include $(PGXS) - + PG_CONFIG @@ -1929,4 +1934,26 @@ make VPATH=/path/to/extension/source/tree install + + postgresql-extension.pc + + + When Postgres is built by using meson, it generates + postgresql-extension.pc pkg-config file. Extension + libraries can use this file like PGXS + (). + + To use the postgresql-extension.pc infrastructure for + your extension, you must write a simple meson.build file. In the + meson.build file, you need to include the + postgresql-extension.pc pkg-config file. Here is an + example that builds an extension module named isbn_issn, consisting of a + shared library containing some C code, an extension control file, an SQL + script, an include file (only needed if other modules might need to access + the extension functions without going via SQL), and a documentation text + file: + + + + diff --git a/doc/src/sgml/jit.sgml b/doc/src/sgml/jit.sgml index 44e18bf1a6f0..81a4644a97d6 100644 --- a/doc/src/sgml/jit.sgml +++ b/doc/src/sgml/jit.sgml @@ -223,7 +223,7 @@ SET of types C and internal, as well as operators based on such functions. To do so for functions in extensions, the definitions of those functions need to be made available. - When using PGXS to build an extension + When using PGXS to build an extension against a server that has been compiled with LLVM JIT support, the relevant files will be built and installed automatically. From 3e1dcab094469a8c4123febaea78a7a773a13cb7 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Fri, 7 Mar 2025 12:10:58 +0300 Subject: [PATCH 4/6] meson: Add architecture for LLVM bitcode emission This commit adds suport for bitcode emission for both normal and generated source files (processed by bison, flex, etc). These bitcode files are installed into $pkglibdir/bitcode/ directory if the LLVM is found. New variable `bitcode_modules` is introduced to generate bitcode files. All required information is gathered in this variable. Then, this variable is processed by the main meson LLVM bitcode emission scripts: src/backend/jit/llvm/bitcode/meson.build -> src/tools/irlink. An example of a possible structure of bitcode_modules is: ``` bitcode_modules = [ { 'name': '...', 'target': ..., 'srcfiles': [ '...', '...', ], 'additional_flags': [ '-I...', '-I...', ], 'gen_srcfiles': [ { 'srcfiles': [ , , ], 'additional_flags': [ '-I...', '-I...', ] } ] } ] ``` Author: Andres Freund Author: Nazir Bilal Yavuz Author: Diego Fronza Reviewed-by: Diego Fronza Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- meson.build | 21 ++++++++ src/backend/jit/llvm/bitcode/meson.build | 69 ++++++++++++++++++++++++ src/backend/jit/llvm/meson.build | 31 +++++++---- src/backend/meson.build | 6 +++ src/tools/irlink | 25 +++++++++ 5 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 src/backend/jit/llvm/bitcode/meson.build create mode 100644 src/tools/irlink diff --git a/meson.build b/meson.build index c520986ba720..c658441532c5 100644 --- a/meson.build +++ b/meson.build @@ -851,6 +851,8 @@ if add_languages('cpp', required: llvmopt, native: false) # Some distros put LLVM and clang in different paths, so fallback to # find via PATH, too. clang = find_program(llvm_binpath / 'clang', 'clang', required: true) + llvm_lto = find_program(llvm_binpath / 'llvm-lto', required: true) + irlink = find_program('src/tools/irlink', native: true) endif elif llvmopt.auto() message('llvm requires a C++ compiler') @@ -3051,6 +3053,11 @@ test_deps = [] tests = [] meson_extension_tests = [] +# List of object files + source files to generated LLVM IR for inlining. +# Each element is a hash of: +# {'target': target, 'srcfiles': ..., 'additional_flags': ...}. +bitcode_modules = [] + # Default options for targets @@ -3374,6 +3381,11 @@ subdir('src/interfaces/ecpg/test') subdir('doc/src/sgml') +# generate bitcode for JIT inlining after giving contrib modules etc a chance +# to add themselves to bitcode_modules[] +subdir('src/backend/jit/llvm/bitcode', if_found: llvm) + + generated_sources_ac += {'': ['GNUmakefile']} # After processing src/test, add test_install_libs to the testprep_targets @@ -4003,6 +4015,15 @@ summary( section: 'Programs', ) +if llvm.found() + summary( + { + 'clang': clang, + }, + section: 'Programs', + ) +endif + summary( { 'bonjour': bonjour, diff --git a/src/backend/jit/llvm/bitcode/meson.build b/src/backend/jit/llvm/bitcode/meson.build new file mode 100644 index 000000000000..546e4d8b8986 --- /dev/null +++ b/src/backend/jit/llvm/bitcode/meson.build @@ -0,0 +1,69 @@ +# Copyright (c) 2022-2024, PostgreSQL Global Development Group +# +# emit LLVM bitcode for JIT inlining + +assert(llvm.found()) + +foreach bitcode_module : bitcode_modules + bitcode_targets = [] + bitcode_obj = bitcode_module['target'] + bitcode_cflags_local = bitcode_cflags + bitcode_module.get('additional_flags', []) + bitcode_name = bitcode_module.get('name', bitcode_obj.name()) + + foreach srcfile : bitcode_module['srcfiles'] + if meson.version().version_compare('>=0.59') + srcfilename = fs.parent(srcfile) / fs.name(srcfile) + else + srcfilename = '@0@'.format(srcfile) + endif + + targetname = '@0@_@1@.bc'.format( + bitcode_name, + srcfilename.underscorify(), + ) + bitcode_targets += custom_target( + targetname, + depends: [bitcode_obj], + input: [srcfile], + output: targetname, + command: [llvm_irgen_command, bitcode_cflags_local], + install: true, + install_dir: dir_bitcode, + ) + endforeach + + # Process generated sources, which may include custom compilation flags. + foreach gen_sources: bitcode_module.get('gen_sources', []) + bitcode_cflags_gen_local = bitcode_cflags_local + gen_sources.get('additional_flags', []) + + foreach srcfile: gen_sources['srcfiles'] + # Generated sources are stored in some folder under meson.build_root()/**, + # remove the build prefix from the string. + srcfilename = srcfile.full_path().split(meson.build_root() + '/')[1] + + targetname = '@0@_@1@.bc'.format( + bitcode_name, + srcfilename.underscorify(), + ) + bitcode_targets += custom_target( + targetname, + depends: [bitcode_obj], + input: [srcfile], + output: targetname, + command: [llvm_irgen_command, bitcode_cflags_gen_local], + install: true, + install_dir: dir_bitcode, + ) + endforeach + endforeach + + index_name = '@0@.index.bc'.format(bitcode_name) + bitcode_index = custom_target('@0@'.format(bitcode_name), + output: index_name, + input: bitcode_targets, + command: [irlink, '--lto', llvm_lto, '--outdir', '@OUTDIR@', '--index', index_name, '@INPUT@'], + install: true, + install_dir: dir_bitcode, + ) + backend_targets += bitcode_index +endforeach diff --git a/src/backend/jit/llvm/meson.build b/src/backend/jit/llvm/meson.build index 805fbd690067..2dd922e573b8 100644 --- a/src/backend/jit/llvm/meson.build +++ b/src/backend/jit/llvm/meson.build @@ -42,21 +42,22 @@ backend_targets += llvmjit # Define a few bits and pieces used here and elsewhere to generate bitcode -llvm_irgen_args = [ - '-c', '-o', '@OUTPUT@', '@INPUT@', +llvm_irgen_command = [] +if ccache.found() + llvm_irgen_command += ccache +endif + +llvm_irgen_command += [ + clang, + '-c', '-o', '@OUTPUT0@', '@INPUT0@', '-flto=thin', '-emit-llvm', - '-MD', '-MQ', '@OUTPUT@', '-MF', '@DEPFILE@', '-O2', '-Wno-ignored-attributes', '-Wno-empty-body', + '-Wno-unknown-warning-option', + '-Wno-compound-token-split-by-macro', ] - -if ccache.found() - llvm_irgen_command = ccache - llvm_irgen_args = [clang.full_path()] + llvm_irgen_args -else - llvm_irgen_command = clang -endif +llvm_irgen_dep_args = ['-MD', '-MQ', '@OUTPUT0@', '-MF', '@DEPFILE@'] # XXX: Need to determine proper version of the function cflags for clang @@ -73,7 +74,7 @@ bitcode_cflags += '-I@SOURCE_ROOT@/src/include' # Note this is intentionally not installed to bitcodedir, as it's not for # inlining llvmjit_types = custom_target('llvmjit_types.bc', - command: [llvm_irgen_command] + llvm_irgen_args + bitcode_cflags, + command: llvm_irgen_command + llvm_irgen_dep_args + bitcode_cflags, input: 'llvmjit_types.c', output: 'llvmjit_types.bc', depends: [postgres], @@ -82,3 +83,11 @@ llvmjit_types = custom_target('llvmjit_types.bc', depfile: '@BASENAME@.c.bc.d', ) backend_targets += llvmjit_types + +# Figure out -I's needed to build all postgres code, including all its +# dependencies +pkg_config = find_program(['pkg-config', 'pkgconf'], required: true) +r = run_command(pkg_config, + ['--cflags-only-I', meson.build_root() / 'meson-uninstalled/postgresql-extension-uninstalled.pc'], + check: true) +bitcode_cflags += r.stdout().split() diff --git a/src/backend/meson.build b/src/backend/meson.build index dd903591e349..276ecbd6d74d 100644 --- a/src/backend/meson.build +++ b/src/backend/meson.build @@ -140,6 +140,12 @@ postgres = executable('postgres', backend_targets += postgres +bitcode_modules += { + 'name': 'postgres', + 'target': postgres_lib, + 'srcfiles': backend_sources, +} + pg_mod_c_args = cflags_mod pg_mod_cpp_args = cxxflags_mod pg_mod_link_args = ldflags_sl + ldflags_mod diff --git a/src/tools/irlink b/src/tools/irlink new file mode 100644 index 000000000000..793c0abf91a4 --- /dev/null +++ b/src/tools/irlink @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import argparse +import os +import shutil +import subprocess +import sys + +parser = argparse.ArgumentParser( + description='generate PostgreSQL JIT IR module') + +parser.add_argument('--index', type=str, required=True) +parser.add_argument('--lto', type=str, required=True) +parser.add_argument('--outdir', type=str, required=True) +parser.add_argument('INPUT', type=str, nargs='+') + +args = parser.parse_args() + +file_names = [os.path.basename(f) for f in args.INPUT] +command = [args.lto, + '-thinlto', '-thinlto-action=thinlink', + '-o', args.index] + file_names +res = subprocess.run(command, cwd=args.outdir) + +exit(res.returncode) From 651f210fb785f5e8159439458b10faef04f03f93 Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Wed, 12 Mar 2025 10:49:18 +0300 Subject: [PATCH 5/6] meson: Add LLVM bitcode emissions for contrib libraries The libraries which the bitcode files will be generated in are selected manually. Author: Andres Freund Author: Nazir Bilal Yavuz Author: Diego Fronza Reviewed-by: Diego Fronza Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- contrib/bloom/meson.build | 5 +++++ contrib/bool_plperl/meson.build | 9 +++++++++ contrib/btree_gin/meson.build | 5 +++++ contrib/btree_gist/meson.build | 5 +++++ contrib/citext/meson.build | 5 +++++ contrib/cube/meson.build | 13 +++++++++++++ contrib/dict_int/meson.build | 5 +++++ contrib/dict_xsyn/meson.build | 5 +++++ contrib/earthdistance/meson.build | 6 ++++++ contrib/fuzzystrmatch/meson.build | 9 +++++++++ contrib/hstore/meson.build | 7 +++++++ contrib/hstore_plperl/meson.build | 10 ++++++++++ contrib/hstore_plpython/meson.build | 12 ++++++++++++ contrib/intarray/meson.build | 5 +++++ contrib/isn/meson.build | 5 +++++ contrib/jsonb_plperl/meson.build | 8 ++++++++ contrib/jsonb_plpython/meson.build | 10 ++++++++++ contrib/lo/meson.build | 5 +++++ contrib/ltree/meson.build | 10 ++++++++++ contrib/ltree_plpython/meson.build | 11 +++++++++++ contrib/pg_buffercache/meson.build | 5 +++++ contrib/pg_freespacemap/meson.build | 5 +++++ contrib/pg_logicalinspect/meson.build | 5 +++++ contrib/pg_surgery/meson.build | 4 ++++ contrib/pg_trgm/meson.build | 5 +++++ contrib/pgcrypto/meson.build | 4 ++++ contrib/seg/meson.build | 12 ++++++++++++ contrib/spi/meson.build | 20 ++++++++++++++++++++ contrib/sslinfo/meson.build | 5 +++++ contrib/tablefunc/meson.build | 5 +++++ contrib/tcn/meson.build | 5 +++++ contrib/tsm_system_rows/meson.build | 5 +++++ contrib/tsm_system_time/meson.build | 5 +++++ contrib/unaccent/meson.build | 5 +++++ contrib/uuid-ossp/meson.build | 5 +++++ contrib/xml2/meson.build | 5 +++++ meson.build | 1 + src/interfaces/libpq/meson.build | 3 +++ src/pl/plperl/meson.build | 1 + src/pl/plpython/meson.build | 1 + 40 files changed, 256 insertions(+) diff --git a/contrib/bloom/meson.build b/contrib/bloom/meson.build index 695712a455e6..1090c2815325 100644 --- a/contrib/bloom/meson.build +++ b/contrib/bloom/meson.build @@ -28,6 +28,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': bloom, + 'srcfiles': bloom_sources, +} + tests += { 'name': 'bloom', 'sd': meson.current_source_dir(), diff --git a/contrib/bool_plperl/meson.build b/contrib/bool_plperl/meson.build index f489d99044ce..1758adb74896 100644 --- a/contrib/bool_plperl/meson.build +++ b/contrib/bool_plperl/meson.build @@ -37,6 +37,15 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': bool_plperl, + 'srcfiles': bool_plperl_sources, + 'additional_flags': [ + '-I@0@'.format(plperl_dir), + perl_ccflags + ] +} + tests += { 'name': 'bool_plperl', 'sd': meson.current_source_dir(), diff --git a/contrib/btree_gin/meson.build b/contrib/btree_gin/meson.build index ece0a716973c..e92e52822b0e 100644 --- a/contrib/btree_gin/meson.build +++ b/contrib/btree_gin/meson.build @@ -26,6 +26,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': btree_gin, + 'srcfiles': btree_gin_sources, +} + tests += { 'name': 'btree_gin', 'sd': meson.current_source_dir(), diff --git a/contrib/btree_gist/meson.build b/contrib/btree_gist/meson.build index f4fa9574f1fd..2649d5a7ffb6 100644 --- a/contrib/btree_gist/meson.build +++ b/contrib/btree_gist/meson.build @@ -54,6 +54,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': btree_gist, + 'srcfiles': btree_gist_sources, +} + tests += { 'name': 'btree_gist', 'sd': meson.current_source_dir(), diff --git a/contrib/citext/meson.build b/contrib/citext/meson.build index 7fff34f23685..fa07ff72be4b 100644 --- a/contrib/citext/meson.build +++ b/contrib/citext/meson.build @@ -30,6 +30,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': citext, + 'srcfiles': citext_sources, +} + tests += { 'name': 'citext', 'sd': meson.current_source_dir(), diff --git a/contrib/cube/meson.build b/contrib/cube/meson.build index fd3c057f4692..a7649a74a1ff 100644 --- a/contrib/cube/meson.build +++ b/contrib/cube/meson.build @@ -3,6 +3,7 @@ cube_sources = files( 'cube.c', ) +bc_cube_sources = cube_sources cube_scan = custom_target('cubescan', input: 'cubescan.l', @@ -11,6 +12,7 @@ cube_scan = custom_target('cubescan', ) generated_sources += cube_scan cube_sources += cube_scan +bc_cube_gen_sources = [{'srcfiles': [cube_scan]}] cube_parse = custom_target('cubeparse', input: 'cubeparse.y', @@ -18,6 +20,7 @@ cube_parse = custom_target('cubeparse', ) generated_sources += cube_parse.to_list() cube_sources += cube_parse +bc_cube_gen_sources += {'srcfiles': [cube_parse[0]]} if host_system == 'windows' cube_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ @@ -48,6 +51,16 @@ install_headers( install_dir: dir_include_extension / 'cube', ) +cube_dir = meson.current_source_dir() +bitcode_modules += { + 'target': cube, + 'srcfiles': bc_cube_sources, + 'gen_sources': bc_cube_gen_sources, + 'additional_flags': [ + '-I@0@'.format(cube_dir), + ] +} + tests += { 'name': 'cube', 'sd': meson.current_source_dir(), diff --git a/contrib/dict_int/meson.build b/contrib/dict_int/meson.build index ab41588547bc..894c8a9c94d0 100644 --- a/contrib/dict_int/meson.build +++ b/contrib/dict_int/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': dict_int, + 'srcfiles': dict_int_sources, +} + tests += { 'name': 'dict_int', 'sd': meson.current_source_dir(), diff --git a/contrib/dict_xsyn/meson.build b/contrib/dict_xsyn/meson.build index 93f41b1f963e..d4bd4a6ef5a8 100644 --- a/contrib/dict_xsyn/meson.build +++ b/contrib/dict_xsyn/meson.build @@ -29,6 +29,11 @@ install_data( } ) +bitcode_modules += { + 'target': dict_xsyn, + 'srcfiles': dict_xsyn_sources, +} + tests += { 'name': 'dict_xsyn', 'sd': meson.current_source_dir(), diff --git a/contrib/earthdistance/meson.build b/contrib/earthdistance/meson.build index a5f8d1a3ffa0..c101359b1186 100644 --- a/contrib/earthdistance/meson.build +++ b/contrib/earthdistance/meson.build @@ -24,6 +24,12 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': earthdistance, + 'srcfiles': earthdistance_sources, +} + + tests += { 'name': 'earthdistance', 'sd': meson.current_source_dir(), diff --git a/contrib/fuzzystrmatch/meson.build b/contrib/fuzzystrmatch/meson.build index f52daa4ea1cf..fc5e88d0c925 100644 --- a/contrib/fuzzystrmatch/meson.build +++ b/contrib/fuzzystrmatch/meson.build @@ -5,6 +5,7 @@ fuzzystrmatch_sources = files( 'dmetaphone.c', 'fuzzystrmatch.c', ) +bc_fuzzystrmatch_sources = fuzzystrmatch_sources daitch_mokotoff_h = custom_target('daitch_mokotoff', input: 'daitch_mokotoff_header.pl', @@ -35,6 +36,14 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': fuzzystrmatch, + 'srcfiles': bc_fuzzystrmatch_sources, + 'additional_flags': [ + '-I@0@'.format(meson.current_build_dir()) + ] +} + tests += { 'name': 'fuzzystrmatch', 'sd': meson.current_source_dir(), diff --git a/contrib/hstore/meson.build b/contrib/hstore/meson.build index 39622d1024de..344a620273fd 100644 --- a/contrib/hstore/meson.build +++ b/contrib/hstore/meson.build @@ -43,6 +43,13 @@ install_headers( install_dir: dir_include_extension / 'hstore', ) +# some libraries include "hstore/hstore.h" instead of "hstore.h" +hstore_dir_up = join_paths(meson.current_source_dir(), '..') +bitcode_modules += { + 'target': hstore, + 'srcfiles': hstore_sources, +} + tests += { 'name': 'hstore', 'sd': meson.current_source_dir(), diff --git a/contrib/hstore_plperl/meson.build b/contrib/hstore_plperl/meson.build index 60b8ad23569b..b689b55e1c98 100644 --- a/contrib/hstore_plperl/meson.build +++ b/contrib/hstore_plperl/meson.build @@ -37,6 +37,16 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': hstore_plperl, + 'srcfiles': hstore_plperl_sources, + 'additional_flags': [ + '-I@0@'.format(hstore_dir_up), + '-I@0@'.format(plperl_dir), + perl_ccflags, + ] +} + tests += { 'name': 'hstore_plperl', 'sd': meson.current_source_dir(), diff --git a/contrib/hstore_plpython/meson.build b/contrib/hstore_plpython/meson.build index ea8e20a377be..d68de3dc46be 100644 --- a/contrib/hstore_plpython/meson.build +++ b/contrib/hstore_plpython/meson.build @@ -30,6 +30,18 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': hstore_plpython, + 'srcfiles': hstore_plpython_sources, + 'additional_flags': [ + '-I@0@'.format(hstore_dir_up), + '-DPLPYTHON_LIBNAME="plpython3"', + '-I@0@'.format(python3_inc_dir), + '-I@0@'.format(plpython_dir), + perl_ccflags, + ] +} + hstore_plpython_regress = [ 'hstore_plpython' ] diff --git a/contrib/intarray/meson.build b/contrib/intarray/meson.build index fae9add981dc..3d7da0c9c9aa 100644 --- a/contrib/intarray/meson.build +++ b/contrib/intarray/meson.build @@ -33,6 +33,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': intarray, + 'srcfiles': intarray_sources, +} + tests += { 'name': 'intarray', 'sd': meson.current_source_dir(), diff --git a/contrib/isn/meson.build b/contrib/isn/meson.build index 39cf781684ee..71dc8cf1a76c 100644 --- a/contrib/isn/meson.build +++ b/contrib/isn/meson.build @@ -30,6 +30,11 @@ install_headers( install_dir: dir_include_extension / 'isn', ) +bitcode_modules += { + 'target': isn, + 'srcfiles': isn_sources, +} + tests += { 'name': 'isn', 'sd': meson.current_source_dir(), diff --git a/contrib/jsonb_plperl/meson.build b/contrib/jsonb_plperl/meson.build index 95a9a7bc0824..febafc73a99b 100644 --- a/contrib/jsonb_plperl/meson.build +++ b/contrib/jsonb_plperl/meson.build @@ -37,6 +37,14 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': jsonb_plperl, + 'srcfiles': jsonb_plperl_sources, + 'additional_flags': [ + '-I@0@'.format(plperl_dir), + perl_ccflags, + ] +} tests += { 'name': 'jsonb_plperl', diff --git a/contrib/jsonb_plpython/meson.build b/contrib/jsonb_plpython/meson.build index 5fe80483e587..6be922212724 100644 --- a/contrib/jsonb_plpython/meson.build +++ b/contrib/jsonb_plpython/meson.build @@ -30,6 +30,16 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': jsonb_plpython, + 'srcfiles': jsonb_plpython_sources, + 'additional_flags': [ + '-DPLPYTHON_LIBNAME="plpython3"', + '-I@0@'.format(python3_inc_dir), + '-I@0@'.format(plpython_dir), + ] +} + jsonb_plpython_regress = [ 'jsonb_plpython' ] diff --git a/contrib/lo/meson.build b/contrib/lo/meson.build index 2d78907ba12a..1222faa91692 100644 --- a/contrib/lo/meson.build +++ b/contrib/lo/meson.build @@ -24,6 +24,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': lo, + 'srcfiles': lo_sources, +} + tests += { 'name': 'lo', 'sd': meson.current_source_dir(), diff --git a/contrib/ltree/meson.build b/contrib/ltree/meson.build index f9b063028391..cda86935544a 100644 --- a/contrib/ltree/meson.build +++ b/contrib/ltree/meson.build @@ -41,6 +41,16 @@ install_headers( install_dir: dir_include_extension / 'ltree', ) +ltree_dir = meson.current_source_dir() +ltree_dir_up = join_paths(ltree_dir, '..') +bitcode_modules += { + 'target': ltree, + 'srcfiles': ltree_sources, + 'additional_flags': [ + '-I@0@'.format(ltree_dir) + ] +} + tests += { 'name': 'ltree', 'sd': meson.current_source_dir(), diff --git a/contrib/ltree_plpython/meson.build b/contrib/ltree_plpython/meson.build index a37732c486b9..74b285da05c5 100644 --- a/contrib/ltree_plpython/meson.build +++ b/contrib/ltree_plpython/meson.build @@ -30,6 +30,17 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': ltree_plpython, + 'srcfiles': ltree_plpython_sources, + 'additional_flags': [ + '-I@0@'.format(ltree_dir_up), + '-DPLPYTHON_LIBNAME="plpython3"', + '-I@0@'.format(python3_inc_dir), + '-I@0@'.format(plpython_dir), + ] +} + ltree_plpython_regress = [ 'ltree_plpython' ] diff --git a/contrib/pg_buffercache/meson.build b/contrib/pg_buffercache/meson.build index 7cd039a1df9c..65dcc6906018 100644 --- a/contrib/pg_buffercache/meson.build +++ b/contrib/pg_buffercache/meson.build @@ -28,6 +28,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pg_buffercache, + 'srcfiles': pg_buffercache_sources, +} + tests += { 'name': 'pg_buffercache', 'sd': meson.current_source_dir(), diff --git a/contrib/pg_freespacemap/meson.build b/contrib/pg_freespacemap/meson.build index ff8eda3580e5..120def1ad2e7 100644 --- a/contrib/pg_freespacemap/meson.build +++ b/contrib/pg_freespacemap/meson.build @@ -25,6 +25,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pg_freespacemap, + 'srcfiles': pg_freespacemap_sources, +} + tests += { 'name': 'pg_freespacemap', 'sd': meson.current_source_dir(), diff --git a/contrib/pg_logicalinspect/meson.build b/contrib/pg_logicalinspect/meson.build index 5c0528a8c63e..1b4f46277ed9 100644 --- a/contrib/pg_logicalinspect/meson.build +++ b/contrib/pg_logicalinspect/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pg_logicalinspect, + 'srcfiles': pg_logicalinspect_sources, +} + tests += { 'name': 'pg_logicalinspect', 'sd': meson.current_source_dir(), diff --git a/contrib/pg_surgery/meson.build b/contrib/pg_surgery/meson.build index c6cfa9c4694a..9d7199392c76 100644 --- a/contrib/pg_surgery/meson.build +++ b/contrib/pg_surgery/meson.build @@ -22,6 +22,10 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pg_surgery, + 'srcfiles': pg_surgery_sources, +} tests += { 'name': 'pg_surgery', diff --git a/contrib/pg_trgm/meson.build b/contrib/pg_trgm/meson.build index a31aa5c574d3..408e287172e7 100644 --- a/contrib/pg_trgm/meson.build +++ b/contrib/pg_trgm/meson.build @@ -32,6 +32,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pg_trgm, + 'srcfiles': pg_trgm_sources, +} + tests += { 'name': 'pg_trgm', 'sd': meson.current_source_dir(), diff --git a/contrib/pgcrypto/meson.build b/contrib/pgcrypto/meson.build index 7d5ef9b6d323..65d94174f7e0 100644 --- a/contrib/pgcrypto/meson.build +++ b/contrib/pgcrypto/meson.build @@ -100,6 +100,10 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': pgcrypto, + 'srcfiles': pgcrypto_sources, +} tests += { 'name': 'pgcrypto', diff --git a/contrib/seg/meson.build b/contrib/seg/meson.build index e331e0972306..371cd272a065 100644 --- a/contrib/seg/meson.build +++ b/contrib/seg/meson.build @@ -3,6 +3,7 @@ seg_sources = files( 'seg.c', ) +bc_seg_sources = seg_sources seg_scan = custom_target('segscan', input: 'segscan.l', @@ -11,6 +12,7 @@ seg_scan = custom_target('segscan', ) generated_sources += seg_scan seg_sources += seg_scan +bc_seg_gen_sources = [{'srcfiles': [seg_scan]}] seg_parse = custom_target('segparse', input: 'segparse.y', @@ -18,6 +20,7 @@ seg_parse = custom_target('segparse', ) generated_sources += seg_parse.to_list() seg_sources += seg_parse +bc_seg_gen_sources += {'srcfiles': [seg_parse[0]]} if host_system == 'windows' seg_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ @@ -47,6 +50,15 @@ install_headers( install_dir: dir_include_extension / 'seg', ) +bitcode_modules += { + 'target': seg, + 'srcfiles': bc_seg_sources, + 'gen_sources': bc_seg_gen_sources, + 'additional_flags': [ + '-I@0@'.format(meson.current_source_dir()), + ] +} + tests += { 'name': 'seg', 'sd': meson.current_source_dir(), diff --git a/contrib/spi/meson.build b/contrib/spi/meson.build index 3832a91019a4..c8216fcbcd09 100644 --- a/contrib/spi/meson.build +++ b/contrib/spi/meson.build @@ -24,6 +24,11 @@ install_data('autoinc.example', kwargs: contrib_doc_args, ) +bitcode_modules += { + 'target': autoinc, + 'srcfiles': autoinc_sources, +} + insert_username_sources = files( 'insert_username.c', @@ -51,6 +56,11 @@ install_data('insert_username.example', kwargs: contrib_doc_args, ) +bitcode_modules += { + 'target': insert_username, + 'srcfiles': insert_username_sources, +} + moddatetime_sources = files( 'moddatetime.c', @@ -78,6 +88,11 @@ install_data('moddatetime.example', kwargs: contrib_doc_args, ) +bitcode_modules += { + 'target': moddatetime, + 'srcfiles': moddatetime_sources, +} + # this is needed for the regression tests; # comment out if you want a quieter refint package for other uses @@ -108,6 +123,11 @@ install_data('refint.example', kwargs: contrib_doc_args, ) +bitcode_modules += { + 'target': refint, + 'srcfiles': refint_sources, +} + tests += { 'name': 'spi', 'sd': meson.current_source_dir(), diff --git a/contrib/sslinfo/meson.build b/contrib/sslinfo/meson.build index 4c513759200e..5750d783b925 100644 --- a/contrib/sslinfo/meson.build +++ b/contrib/sslinfo/meson.build @@ -29,3 +29,8 @@ install_data( 'sslinfo.control', kwargs: contrib_data_args, ) + +bitcode_modules += { + 'target': sslinfo, + 'srcfiles': sslinfo_sources, +} diff --git a/contrib/tablefunc/meson.build b/contrib/tablefunc/meson.build index ee67272ec0ac..6c26ffafddfe 100644 --- a/contrib/tablefunc/meson.build +++ b/contrib/tablefunc/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': tablefunc, + 'srcfiles': tablefunc_sources, +} + tests += { 'name': 'tablefunc', 'sd': meson.current_source_dir(), diff --git a/contrib/tcn/meson.build b/contrib/tcn/meson.build index 6ffb136af904..df71e8a91f97 100644 --- a/contrib/tcn/meson.build +++ b/contrib/tcn/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': tcn, + 'srcfiles': tcn_sources, +} + tests += { 'name': 'tcn', 'sd': meson.current_source_dir(), diff --git a/contrib/tsm_system_rows/meson.build b/contrib/tsm_system_rows/meson.build index b8cece3d80f5..1ea74cce949d 100644 --- a/contrib/tsm_system_rows/meson.build +++ b/contrib/tsm_system_rows/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': tsm_system_rows, + 'srcfiles': tsm_system_rows_sources, +} + tests += { 'name': 'tsm_system_rows', 'sd': meson.current_source_dir(), diff --git a/contrib/tsm_system_time/meson.build b/contrib/tsm_system_time/meson.build index 8a143a8f8e6f..1f6b98d3ec46 100644 --- a/contrib/tsm_system_time/meson.build +++ b/contrib/tsm_system_time/meson.build @@ -22,6 +22,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': tsm_system_time, + 'srcfiles': tsm_system_time_sources, +} + tests += { 'name': 'tsm_system_time', 'sd': meson.current_source_dir(), diff --git a/contrib/unaccent/meson.build b/contrib/unaccent/meson.build index 33d4649bae15..1f5d120e3d83 100644 --- a/contrib/unaccent/meson.build +++ b/contrib/unaccent/meson.build @@ -28,6 +28,11 @@ install_data( install_dir: dir_data / 'tsearch_data' ) +bitcode_modules += { + 'target': unaccent, + 'srcfiles': unaccent_sources, +} + # XXX: Implement downlo tests += { 'name': 'unaccent', diff --git a/contrib/uuid-ossp/meson.build b/contrib/uuid-ossp/meson.build index 982f27c085fc..8d21bb0a9965 100644 --- a/contrib/uuid-ossp/meson.build +++ b/contrib/uuid-ossp/meson.build @@ -29,6 +29,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': uuid_ossp, + 'srcfiles': uuid_ossp_sources, +} + tests += { 'name': 'uuid-ossp', 'sd': meson.current_source_dir(), diff --git a/contrib/xml2/meson.build b/contrib/xml2/meson.build index 08d3c3b8e308..b23a8fe0cd76 100644 --- a/contrib/xml2/meson.build +++ b/contrib/xml2/meson.build @@ -32,6 +32,11 @@ install_data( kwargs: contrib_data_args, ) +bitcode_modules += { + 'target': xml2, + 'srcfiles': xml2_sources, +} + tests += { 'name': 'xml2', 'sd': meson.current_source_dir(), diff --git a/meson.build b/meson.build index c658441532c5..d18ce4df9480 100644 --- a/meson.build +++ b/meson.build @@ -1331,6 +1331,7 @@ if not pyopt.disabled() python3_inst = pm.find_installation(python.full_path(), required: pyopt) if python3_inst.found() python3_dep = python3_inst.dependency(embed: true, required: pyopt) + python3_inc_dir = python3_inst.get_variable('INCLUDEPY') # Remove this check after we depend on Meson >= 1.1.0 if not cc.check_header('Python.h', dependencies: python3_dep, required: pyopt, include_directories: postgres_inc) python3_dep = not_found_dep diff --git a/src/interfaces/libpq/meson.build b/src/interfaces/libpq/meson.build index a74e885b169d..c3ceab1dd70a 100644 --- a/src/interfaces/libpq/meson.build +++ b/src/interfaces/libpq/meson.build @@ -49,6 +49,9 @@ libpq_c_args = ['-DSO_MAJOR_VERSION=5'] # The OAuth implementation differs depending on the type of library being built. libpq_so_c_args = ['-DUSE_DYNAMIC_OAUTH'] +# libpq-fe.h is needed to generate bitcode for some extensions +libpq_dir = meson.current_source_dir() + # Not using both_libraries() here as # 1) resource files should only be in the shared library # 2) we want the .pc file to include a dependency to {pgport,common}_static for diff --git a/src/pl/plperl/meson.build b/src/pl/plperl/meson.build index 7c4081c3460e..06e853c1ac72 100644 --- a/src/pl/plperl/meson.build +++ b/src/pl/plperl/meson.build @@ -37,6 +37,7 @@ foreach n : ['SPI', 'Util'] plperl_sources += xs_c endforeach +plperl_dir = meson.current_source_dir() plperl_inc = include_directories('.') if host_system == 'windows' diff --git a/src/pl/plpython/meson.build b/src/pl/plpython/meson.build index 709e5932a93a..e2c19771d85b 100644 --- a/src/pl/plpython/meson.build +++ b/src/pl/plpython/meson.build @@ -29,6 +29,7 @@ plpython_sources += custom_target('spiexceptions.h', # FIXME: need to duplicate import library ugliness? plpython_inc = include_directories('.') +plpython_dir = meson.current_source_dir() if host_system == 'windows' plpython_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ From 20299b1fc0044dfc094cf7b2b32ad242169fc06a Mon Sep 17 00:00:00 2001 From: Nazir Bilal Yavuz Date: Wed, 12 Mar 2025 10:44:46 +0300 Subject: [PATCH 6/6] meson: Add LLVM bitcode emission for backend sources Since generated backend sources may have their own compilation flags and must also be included in the postgres.index.bc, the way to make it work with current code was to create a new variable, called `bc_generated_backend_sources`, which is a list of dictionaries, each one having an optional 'additional_flags' and a `srclist` pointing to the list of custom_target generated sources. An example of a possible structure of bitcode_modules which is processed by the main meson llvm bitcode emission file src/backend/jit/llvm/bitcode/meson.build: ``` bitcode_modules = [ { 'name': 'postgres', 'target': postgres_lib, 'src_file': backend_sources, 'gen_srcfiles': [ { 'additional_flags': [ '-I/path/postgresl/src/backend/parser', '-I/path/postgresl/build/src/backend/parser', ], 'srcfiles': [ , ] } ] } ] ``` Author: Diego Fronza Author: Nazir Bilal Yavuz Discussion: https://postgr.es/m/206b001d-1884-4081-bd02-bed5c92f02ba%40eisentraut.org --- src/backend/bootstrap/meson.build | 2 ++ src/backend/meson.build | 2 ++ src/backend/parser/meson.build | 8 ++++++++ src/backend/replication/meson.build | 4 ++++ src/backend/utils/fmgr/meson.build | 1 + 5 files changed, 17 insertions(+) diff --git a/src/backend/bootstrap/meson.build b/src/backend/bootstrap/meson.build index 29726c1ab4ff..389c75f80817 100644 --- a/src/backend/bootstrap/meson.build +++ b/src/backend/bootstrap/meson.build @@ -13,6 +13,7 @@ bootscanner = custom_target('bootscanner', ) generated_sources += bootscanner boot_parser_sources += bootscanner +bc_generated_backend_sources += {'srcfiles': [bootscanner]} bootparse = custom_target('bootparse', input: 'bootparse.y', @@ -20,6 +21,7 @@ bootparse = custom_target('bootparse', ) generated_sources += bootparse.to_list() boot_parser_sources += bootparse +bc_generated_backend_sources += {'srcfiles': [bootparse[0]]} boot_parser = static_library('boot_parser', boot_parser_sources, diff --git a/src/backend/meson.build b/src/backend/meson.build index 276ecbd6d74d..aec0276f3082 100644 --- a/src/backend/meson.build +++ b/src/backend/meson.build @@ -5,6 +5,7 @@ backend_sources = [] backend_link_with = [pgport_srv, common_srv] generated_backend_sources = [] +bc_generated_backend_sources = [] post_export_backend_sources = [] subdir('access') @@ -144,6 +145,7 @@ bitcode_modules += { 'name': 'postgres', 'target': postgres_lib, 'srcfiles': backend_sources, + 'gen_sources': bc_generated_backend_sources, } pg_mod_c_args = cflags_mod diff --git a/src/backend/parser/meson.build b/src/backend/parser/meson.build index 874aa749aa69..add472a0cd89 100644 --- a/src/backend/parser/meson.build +++ b/src/backend/parser/meson.build @@ -42,6 +42,14 @@ backend_parser = custom_target('gram', generated_sources += backend_parser.to_list() parser_sources += backend_parser +bc_generated_backend_sources += { + 'additional_flags': [ + '-I@0@'.format(meson.current_build_dir()), + '-I@0@'.format(meson.current_source_dir()), + ], + 'srcfiles': [backend_scanner, backend_parser[0]], +} + parser = static_library('parser', parser_sources, dependencies: [backend_code], diff --git a/src/backend/replication/meson.build b/src/backend/replication/meson.build index b0601498865b..71ab1164960c 100644 --- a/src/backend/replication/meson.build +++ b/src/backend/replication/meson.build @@ -19,6 +19,7 @@ repl_scanner = custom_target('repl_scanner', ) generated_sources += repl_scanner repl_parser_sources += repl_scanner +bc_generated_backend_sources += {'srcfiles': [repl_scanner]} repl_gram = custom_target('repl_gram', input: 'repl_gram.y', @@ -26,6 +27,7 @@ repl_gram = custom_target('repl_gram', ) generated_sources += repl_gram.to_list() repl_parser_sources += repl_gram +bc_generated_backend_sources += {'srcfiles': [repl_gram[0]]} syncrep_scanner = custom_target('syncrep_scanner', input: 'syncrep_scanner.l', @@ -34,6 +36,7 @@ syncrep_scanner = custom_target('syncrep_scanner', ) generated_sources += syncrep_scanner repl_parser_sources += syncrep_scanner +bc_generated_backend_sources += {'srcfiles': [syncrep_scanner]} syncrep_gram = custom_target('syncrep_gram', input: 'syncrep_gram.y', @@ -41,6 +44,7 @@ syncrep_gram = custom_target('syncrep_gram', ) generated_sources += syncrep_gram.to_list() repl_parser_sources += syncrep_gram +bc_generated_backend_sources += {'srcfiles': [syncrep_gram[0]]} repl_parser = static_library('repl_parser', repl_parser_sources, diff --git a/src/backend/utils/fmgr/meson.build b/src/backend/utils/fmgr/meson.build index b1dcab93e709..080f59988e90 100644 --- a/src/backend/utils/fmgr/meson.build +++ b/src/backend/utils/fmgr/meson.build @@ -8,3 +8,4 @@ backend_sources += files( # fmgrtab.c generated_backend_sources += fmgrtab_target[2] +bc_generated_backend_sources += {'srcfiles': [fmgrtab_target[2]]}