From 8acdd51fa7a6cfb86dd5886be71ff173c6bafbe8 Mon Sep 17 00:00:00 2001
From: David Rowley <dgrowley@gmail.com>
Date: Tue, 27 Jul 2021 22:02:36 +1200
Subject: [PATCH v9 2/6] Adjust MSVC build scripts to parse Makefiles for
 defines

This adjusts the MSVC build scripts to look at the compile flags mentioned
in the Makefile to look for -D arguments in order to determine which
constants should be defined in the Visual Studio build.
---
 contrib/ltree/crc32.c       | 10 +++++++++-
 contrib/ltree/ltree.h       | 10 +++++++++-
 src/tools/msvc/Mkvcbuild.pm | 21 ++++++++++++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/contrib/ltree/crc32.c b/contrib/ltree/crc32.c
index 8fed3346e8..b035706c05 100644
--- a/contrib/ltree/crc32.c
+++ b/contrib/ltree/crc32.c
@@ -9,7 +9,15 @@
 
 #include "postgres.h"
 
-#ifdef LOWER_NODE
+/*
+ * Below we ignore the fact that LOWER_NODE is defined when compiling with
+ * MSVC.  The reason for this is that earlier versions of the MSVC build
+ * scripts failed to define LOWER_NODE.  More recent version of the MSVC
+ * build scripts parse makefiles which results in LOWER_NODE now being
+ * defined.  We check for _MSC_VER here so as not to break pg_upgrade when
+ * upgrading from versions MSVC versions where LOWER_NODE was not defined.
+ */
+#if defined(LOWER_NODE) && !defined(_MSC_VER)
 #include <ctype.h>
 #define TOLOWER(x)	tolower((unsigned char) (x))
 #else
diff --git a/contrib/ltree/ltree.h b/contrib/ltree/ltree.h
index dc68a0c212..8c10384503 100644
--- a/contrib/ltree/ltree.h
+++ b/contrib/ltree/ltree.h
@@ -90,7 +90,15 @@ typedef struct
 #define LQL_NOT		0x10		/* level has '!' (NOT) prefix */
 #define LQL_COUNT	0x20		/* level is non-'*' and has repeat counts */
 
-#ifdef LOWER_NODE
+/*
+ * Below we ignore the fact that LOWER_NODE is defined when compiling with
+ * MSVC.  The reason for this is that earlier versions of the MSVC build
+ * scripts failed to define LOWER_NODE.  More recent version of the MSVC
+ * build scripts parse makefiles which results in LOWER_NODE now being
+ * defined.  We check for _MSC_VER here so as not to break pg_upgrade when
+ * upgrading from versions MSVC versions where LOWER_NODE was not defined.
+ */
+#if defined(LOWER_NODE) && !defined(_MSC_VER)
 #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME ) ) == 0 )
 #else
 #define FLG_CANLOOKSIGN(x) ( ( (x) & ( LQL_NOT | LVAR_ANYEND | LVAR_SUBLEXEME | LVAR_INCASE ) ) == 0 )
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 4c36e482fb..dbe2b4c5ac 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -35,7 +35,7 @@ my $libpq;
 my @unlink_on_exit;
 
 # Set of variables for modules in contrib/ and src/test/modules/
-my $contrib_defines = { 'refint' => 'REFINT_VERBOSE' };
+my $contrib_defines = {};
 my @contrib_uselibpq =
   ('dblink', 'oid2name', 'postgres_fdw', 'vacuumlo', 'libpq_pipeline');
 my @contrib_uselibpgport   = ('libpq_pipeline', 'oid2name', 'vacuumlo');
@@ -964,6 +964,7 @@ sub AddContrib
 	my $subdir = shift;
 	my $n      = shift;
 	my $mf     = Project::read_file("$subdir/$n/Makefile");
+	my @projects;
 
 	if ($mf =~ /^MODULE_big\s*=\s*(.*)$/mg)
 	{
@@ -971,6 +972,7 @@ sub AddContrib
 		my $proj = $solution->AddProject($dn, 'dll', 'contrib', "$subdir/$n");
 		$proj->AddReference($postgres);
 		AdjustContribProj($proj);
+		push @projects, $proj;
 	}
 	elsif ($mf =~ /^MODULES\s*=\s*(.*)$/mg)
 	{
@@ -982,18 +984,35 @@ sub AddContrib
 			$proj->AddFile("$subdir/$n/$filename");
 			$proj->AddReference($postgres);
 			AdjustContribProj($proj);
+			push @projects, $proj;
 		}
 	}
 	elsif ($mf =~ /^PROGRAM\s*=\s*(.*)$/mg)
 	{
 		my $proj = $solution->AddProject($1, 'exe', 'contrib', "$subdir/$n");
 		AdjustContribProj($proj);
+		push @projects, $proj;
 	}
 	else
 	{
 		croak "Could not determine contrib module type for $n\n";
 	}
 
+	# Process custom compiler flags
+	if ($mf =~ /^PG_CPPFLAGS\s*=\s*(.*)$/mg || $mf =~ /^override\s*CPPFLAGS\s*(?:[\+\:])?=\s*(.*)$/mg)
+	{
+		foreach my $flag (split /\s+/, $1)
+		{
+			if ($flag =~ /^-D(.*)$/)
+			{
+				foreach my $proj (@projects)
+				{
+					$proj->AddDefine($1);
+				}
+			}
+		}
+	}
+
 	# Are there any output data files to build?
 	GenerateContribSqlFiles($n, $mf);
 	return;
-- 
2.21.0.windows.1

