From 0bf3e5ff87631940642fed0f71b89b547b83d642 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Wed, 12 Mar 2025 10:44:46 +0300
Subject: [PATCH v7 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': [
                <custom_target for scan.c>,
                <custom_target for gram.c>
        ]
      }
    ]
  }
]
```

Author: Diego Fronza <diego.fronza@percona.com>
Author: Nazir Bilal Yavuz <byavuz81@gmail.com>
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 29726c1ab4f..389c75f8081 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 276ecbd6d74..aec0276f308 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 874aa749aa6..add472a0cd8 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 b0601498865..71ab1164960 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 b1dcab93e70..080f59988e9 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]]}
-- 
2.50.1

