From aa64b9ffc2b6aed9057568d616ab9e6c43af4b16 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 v12 6/7] 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>
        ],
        'extra_depends' : [
          ...,
          ...,
        ],
      }
    ]
  }
]
```

Author: Diego Fronza <diego.fronza@percona.com>
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/bootstrap/meson.build   |  5 +++++
 src/backend/meson.build             |  2 ++
 src/backend/parser/meson.build      |  8 ++++++++
 src/backend/replication/meson.build | 10 ++++++++++
 src/backend/utils/fmgr/meson.build  |  1 +
 5 files changed, 26 insertions(+)

diff --git a/src/backend/bootstrap/meson.build b/src/backend/bootstrap/meson.build
index 2f9115fc97c..b2fffdb4276 100644
--- a/src/backend/bootstrap/meson.build
+++ b/src/backend/bootstrap/meson.build
@@ -18,8 +18,13 @@ bootparse = custom_target('bootparse',
   input: 'bootparse.y',
   kwargs: bison_kw,
 )
+bc_generated_backend_sources += {
+  'srcfiles': [bootscanner],
+  'extra_depends': [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 2877127954b..102006ccd35 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 = []
 
 var_bitcode_cflags = []
@@ -172,6 +173,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 86c09b29ec2..a8d65eb1898 100644
--- a/src/backend/parser/meson.build
+++ b/src/backend/parser/meson.build
@@ -43,6 +43,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 ce9be4117ad..b68a339c9d4 100644
--- a/src/backend/replication/meson.build
+++ b/src/backend/replication/meson.build
@@ -24,8 +24,13 @@ repl_gram = custom_target('repl_gram',
   input: 'repl_gram.y',
   kwargs: bison_kw,
 )
+bc_generated_backend_sources += {
+  'srcfiles': [repl_scanner],
+  'extra_depends': [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',
@@ -39,8 +44,13 @@ syncrep_gram = custom_target('syncrep_gram',
   input: 'syncrep_gram.y',
   kwargs: bison_kw,
 )
+bc_generated_backend_sources += {
+  'srcfiles': [syncrep_scanner],
+  'extra_depends': [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 3a90deba979..71dad370d1d 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.47.3

