Move bki file pre-processing from initdb - part 1 - initdb->genbki.pl

Started by Krishnakumar Rabout 2 years ago2 messages
#1Krishnakumar R
kksrcv001@gmail.com
1 attachment(s)

Hi,

As per discussion in [1]/messages/by-id/CAPMWgZ9TCByVjpfdsgyte4rx=YsrAttYay2xDK4UN4Lm=-wJMQ@mail.gmail.com splitting the patch. Part1 moves replacement
logic in initdb of NAMEDATALEN, FLOAT8PASSBYVAL, SIZEOF_VOID_P,
ALIGNOF_POINTER to compile time via genbki.pl.

--
Thanks and regards,
Krishnakumar (KK).
[Microsoft]

[1]: /messages/by-id/CAPMWgZ9TCByVjpfdsgyte4rx=YsrAttYay2xDK4UN4Lm=-wJMQ@mail.gmail.com

Attachments:

v4-0001-Move-some-BKI-token-replacement-from-initdb-to-co.patchtext/x-patch; charset=US-ASCII; name=v4-0001-Move-some-BKI-token-replacement-from-initdb-to-co.patchDownload
From 2b968de3dd559aa31095214f96773569ebec8b21 Mon Sep 17 00:00:00 2001
From: "Krishnakumar R (KK)" <kksrcv001@gmail.com>
Date: Mon, 4 Dec 2023 01:25:20 -0800
Subject: [PATCH v4] Move some BKI token replacement from initdb to compile
 time via genbki.pl.

Here are some details:
- NAMEDATALEN, FLOAT8PASSBYVAL, SIZEOF_VOID_P, ALIGNOF_POINTER are replaced
  during compilation from genbki.pl by reading header files.
- SIZEOF_VOID_P is available only after configuration (in pg_config.h).
  A new parameter include-conf had to be added to genbki to point to header files
  generated after configuration.
---
 src/backend/catalog/Makefile    |  2 +-
 src/backend/catalog/genbki.pl   | 34 ++++++++++++++++++++++++++++++++-
 src/bin/initdb/initdb.c         | 12 ------------
 src/include/catalog/meson.build |  1 +
 src/tools/msvc/Solution.pm      |  2 +-
 5 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/src/backend/catalog/Makefile b/src/backend/catalog/Makefile
index ec7b6f5362..9859b52662 100644
--- a/src/backend/catalog/Makefile
+++ b/src/backend/catalog/Makefile
@@ -167,7 +167,7 @@ generated-header-symlinks: $(top_builddir)/src/include/catalog/header-stamp
 # instead is cheating a bit, but it will achieve the goal of updating the
 # version number when it changes.
 bki-stamp: genbki.pl Catalog.pm $(POSTGRES_BKI_SRCS) $(POSTGRES_BKI_DATA) $(top_srcdir)/configure.ac $(top_srcdir)/src/include/access/transam.h
-	$(PERL) $< --include-path=$(top_srcdir)/src/include/ \
+	$(PERL) $< --include-path=$(top_srcdir)/src/include/ --include-conf=$(top_builddir)/src/include/ \
 		--set-version=$(MAJORVERSION) $(POSTGRES_BKI_SRCS)
 	touch $@
 
diff --git a/src/backend/catalog/genbki.pl b/src/backend/catalog/genbki.pl
index 380bc23c82..f7c8390e6d 100644
--- a/src/backend/catalog/genbki.pl
+++ b/src/backend/catalog/genbki.pl
@@ -25,13 +25,15 @@ use Catalog;
 my $output_path = '';
 my $major_version;
 my $include_path;
+my $include_conf;
 
 my $num_errors = 0;
 
 GetOptions(
 	'output:s' => \$output_path,
 	'set-version:s' => \$major_version,
-	'include-path:s' => \$include_path) || usage();
+	'include-path:s' => \$include_path,
+	'include-conf:s' => \$include_conf) || usage();
 
 # Sanity check arguments.
 die "No input files.\n" unless @ARGV;
@@ -39,6 +41,7 @@ die "--set-version must be specified.\n" unless $major_version;
 die "Invalid version string: $major_version\n"
   unless $major_version =~ /^\d+$/;
 die "--include-path must be specified.\n" unless $include_path;
+die "--include-conf must be specified.\n" unless $include_conf;
 
 # Make sure paths end with a slash.
 if ($output_path ne '' && substr($output_path, -1) ne '/')
@@ -180,6 +183,12 @@ my $FirstUnpinnedObjectId =
 # Hash of next available OID, indexed by catalog name.
 my %GenbkiNextOids;
 
+my $NameDataLen=Catalog::FindDefinedSymbol('pg_config_manual.h', $include_path,
+	'NAMEDATALEN');
+my $SizeOfPointer=Catalog::FindDefinedSymbol('pg_config.h', $include_conf,
+	'SIZEOF_VOID_P');
+my $Float8PassByVal=$SizeOfPointer >= 8 ? "true": "false";
+my $AlignOfPointer=$SizeOfPointer == 4 ? "i" : "d";
 
 # Fetch some special data that we will substitute into the output file.
 # CAUTION: be wary about what symbols you substitute into the .bki file here!
@@ -634,6 +643,23 @@ EOM
 			my $symbol = form_pg_type_symbol($bki_values{typname});
 			$bki_values{oid_symbol} = $symbol
 			  if defined $symbol;
+
+			if ($bki_values{typlen} eq  'NAMEDATALEN')
+			{
+				$bki_values{typlen} = $NameDataLen;
+			}
+			if ($bki_values{typlen} eq  'SIZEOF_POINTER')
+			{
+				$bki_values{typlen} = $SizeOfPointer;
+			}
+			if ($bki_values{typalign} eq  'ALIGNOF_POINTER')
+			{
+				$bki_values{typalign} = $AlignOfPointer;
+			}
+			if ($bki_values{typbyval} eq  'FLOAT8PASSBYVAL')
+			{
+				$bki_values{typbyval} = $Float8PassByVal;
+			}
 		}
 
 		# Write to postgres.bki
@@ -812,6 +838,11 @@ sub gen_pg_attribute
 			  ($row{attnotnull} eq 't'
 				  && ($row{attlen} eq 'NAMEDATALEN' || $row{attlen} > 0));
 
+			if ($row{attnotnull} eq 't' && ($row{attlen} eq 'NAMEDATALEN'))
+			{
+				$row{attlen} = $NameDataLen;
+			}
+
 			# If it's bootstrapped, put an entry in postgres.bki.
 			print_bki_insert(\%row, $schema) if $table->{bootstrap};
 
@@ -1106,6 +1137,7 @@ Options:
     --output         Output directory (default '.')
     --set-version    PostgreSQL version number for initdb cross-check
     --include-path   Include path in source tree
+    --include-conf   Include file path generated after configuration
 
 genbki.pl generates postgres.bki and symbol definition
 headers from specially formatted header files and .dat
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 0c6f5ceb0a..54ec8f8421 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1498,18 +1498,6 @@ bootstrap_template1(void)
 
 	/* Substitute for various symbols used in the BKI file */
 
-	sprintf(buf, "%d", NAMEDATALEN);
-	bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
-
-	sprintf(buf, "%d", (int) sizeof(Pointer));
-	bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
-
-	bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
-							  (sizeof(Pointer) == 4) ? "i" : "d");
-
-	bki_lines = replace_token(bki_lines, "FLOAT8PASSBYVAL",
-							  FLOAT8PASSBYVAL ? "true" : "false");
-
 	bki_lines = replace_token(bki_lines, "POSTGRES",
 							  escape_quotes_bki(username));
 
diff --git a/src/include/catalog/meson.build b/src/include/catalog/meson.build
index dcb3c5f766..eb9cdc889c 100644
--- a/src/include/catalog/meson.build
+++ b/src/include/catalog/meson.build
@@ -123,6 +123,7 @@ generated_catalog_headers = custom_target('generated_catalog_headers',
     perl,
     files('../../backend/catalog/genbki.pl'),
     '--include-path=@SOURCE_ROOT@/src/include',
+    '--include-conf=@BUILD_ROOT@/src/include',
     '--set-version=' + pg_version_major.to_string(),
     '--output=@OUTDIR@', '@INPUT@'
   ],
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index a980b51f5d..35769d4777 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -781,7 +781,7 @@ EOF
 		chdir('src/backend/catalog');
 		my $bki_srcs = join(' ../../../src/include/catalog/', @bki_srcs);
 		system(
-			"perl genbki.pl --include-path ../../../src/include/ --set-version=$majorver $bki_srcs"
+			"perl genbki.pl --include-path ../../../src/include/ --include-conf ../../../src/include/ --set-version=$majorver $bki_srcs"
 		);
 		open(my $f, '>', 'bki-stamp')
 		  || confess "Could not touch bki-stamp";
-- 
2.40.1

#2John Naylor
johncnaylorls@gmail.com
In reply to: Krishnakumar R (#1)
Re: Move bki file pre-processing from initdb - part 1 - initdb->genbki.pl

On Mon, Dec 4, 2023 at 5:03 PM Krishnakumar R <kksrcv001@gmail.com> wrote:

Hi,

As per discussion in [1] splitting the patch. Part1 moves replacement
logic in initdb of NAMEDATALEN, FLOAT8PASSBYVAL, SIZEOF_VOID_P,
ALIGNOF_POINTER to compile time via genbki.pl.

Hi Krishnakumar,

Note this comment in genbki.pl:

# Fetch some special data that we will substitute into the output file.
# CAUTION: be wary about what symbols you substitute into the .bki file here!
# It's okay to substitute things that are expected to be really constant
# within a given Postgres release, such as fixed OIDs. Do not substitute
# anything that could depend on platform or configuration. (The right place
# to handle those sorts of things is in initdb.c's bootstrap_template1().)

The premise of this patch is to invalidate this comment, so we need to
show that it's not needed any more. With commit 721856ff24 to remove
distprep, the biggest obstacle is out of the way, I think. If all else
goes well, this comment will need to be removed.

Also some cosmetic review:

+my $include_conf;

This is referring to a path, a different one than the include path. I
suggest "config_path" or something like that. Since the script now
uses both paths, I wonder if we can just call them "source" and
"build" paths...

+ if ($row{attnotnull} eq 't' && ($row{attlen} eq 'NAMEDATALEN'))
+ {
+ $row{attlen} = $NameDataLen;
+ }

The check for $row{attnotnull} must be a copy-paste-o.

+my $Float8PassByVal=$SizeOfPointer >= 8 ? "true": "false";

This is copied from the C source, but it's spelled 't' / 'f' in the
bki file, so I'm mildly astonished it still seems to work.