From 3c0caccb8d9686538385cbb617a73a2d9b2476bd Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 8 Mar 2021 13:47:39 -0800
Subject: [PATCH v3 04/17] meson: prereq: output and depencency tracking work.

---
 src/backend/utils/misc/Makefile  |  5 ++++-
 src/backend/utils/misc/guc.c     |  2 +-
 src/bin/initdb/initdb.c          |  5 +++--
 src/bin/psql/Makefile            |  4 ++--
 src/bin/psql/create_help.pl      | 16 ++++++++++++----
 src/tools/msvc/MSBuildProject.pm |  9 +++++++--
 src/tools/msvc/Mkvcbuild.pm      |  3 +++
 src/tools/msvc/Solution.pm       |  2 +-
 src/tools/msvc/pgflex.pl         |  4 ++--
 9 files changed, 35 insertions(+), 15 deletions(-)

diff --git a/src/backend/utils/misc/Makefile b/src/backend/utils/misc/Makefile
index 1d5327cf644..14861fd96b2 100644
--- a/src/backend/utils/misc/Makefile
+++ b/src/backend/utils/misc/Makefile
@@ -37,8 +37,11 @@ endif
 
 include $(top_srcdir)/src/backend/common.mk
 
+guc-file.c.h: guc-file.l
+	flex -o $@ $<
+
 # guc-file is compiled as part of guc
-guc.o: guc-file.c
+guc.o: guc-file.c.h
 
 # Note: guc-file.c is not deleted by 'make clean',
 # since we want to ship it in distribution tarballs.
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index d2ce4a84506..3786ae11a08 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -12559,4 +12559,4 @@ check_default_with_oids(bool *newval, void **extra, GucSource source)
 	return true;
 }
 
-#include "guc-file.c"
+#include "guc-file.c.h"
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 1ed4808d53f..9067a06e58a 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1368,8 +1368,9 @@ bootstrap_template1(void)
 
 	if (strcmp(headerline, *bki_lines) != 0)
 	{
-		pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
-					 bki_file, PG_VERSION);
+		pg_log_error("input file \"%s\" does not belong to PostgreSQL %s (expect %s, is %s)",
+					 bki_file, PG_VERSION,
+					 headerline, *bki_lines);
 		fprintf(stderr,
 				_("Check your installation or specify the correct path "
 				  "using the option -L.\n"));
diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index d00881163c0..3851da1c8ef 100644
--- a/src/bin/psql/Makefile
+++ b/src/bin/psql/Makefile
@@ -56,7 +56,7 @@ sql_help.c: sql_help.h
 	touch $@
 
 sql_help.h: create_help.pl $(wildcard $(REFDOCDIR)/*.sgml)
-	$(PERL) $< $(REFDOCDIR) $*
+	$(PERL) $< $(REFDOCDIR) . $*
 
 psqlscanslash.c: FLEXFLAGS = -Cfe -p -p
 psqlscanslash.c: FLEX_NO_BACKUP=yes
@@ -81,7 +81,7 @@ clean distclean:
 # files removed here are supposed to be in the distribution tarball,
 # so do not clean them in the clean/distclean rules
 maintainer-clean: distclean
-	rm -f sql_help.h sql_help.c psqlscanslash.c
+	rm -f sql_help.h sql_help.c sql_help.dep psqlscanslash.c
 
 check:
 	$(prove_check)
diff --git a/src/bin/psql/create_help.pl b/src/bin/psql/create_help.pl
index 83324239740..40eb6ac2d3f 100644
--- a/src/bin/psql/create_help.pl
+++ b/src/bin/psql/create_help.pl
@@ -23,9 +23,12 @@ use strict;
 use warnings;
 
 my $docdir = $ARGV[0] or die "$0: missing required argument: docdir\n";
-my $hfile = $ARGV[1] . '.h'
+my $outdir = $ARGV[1] or die "$0: missing required argument: outdir\n";
+
+my $hfile = $ARGV[2] . '.h'
   or die "$0: missing required argument: output file\n";
-my $cfile = $ARGV[1] . '.c';
+my $cfile = $ARGV[2] . '.c';
+my $depfile = $ARGV[2] . '.dep';
 
 my $hfilebasename;
 if ($hfile =~ m!.*/([^/]+)$!)
@@ -43,10 +46,12 @@ $define =~ s/\W/_/g;
 
 opendir(DIR, $docdir)
   or die "$0: could not open documentation source dir '$docdir': $!\n";
-open(my $hfile_handle, '>', $hfile)
+open(my $hfile_handle, '>', $outdir . '/' . $hfile)
   or die "$0: could not open output file '$hfile': $!\n";
-open(my $cfile_handle, '>', $cfile)
+open(my $cfile_handle, '>', $outdir . '/' . $cfile)
   or die "$0: could not open output file '$cfile': $!\n";
+open(my $depfile_handle, '>', $outdir . '/' . $depfile)
+  or die "$0: could not open output file '$depfile': $!\n";
 
 print $hfile_handle "/*
  * *** Do not change this file by hand. It is automatically
@@ -98,6 +103,8 @@ foreach my $file (sort readdir DIR)
 	my ($cmdid, @cmdnames, $cmddesc, $cmdsynopsis);
 	$file =~ /\.sgml$/ or next;
 
+	print $depfile_handle "$cfile $hfile: $docdir/$file\n";
+
 	open(my $fh, '<', "$docdir/$file") or next;
 	my $filecontent = join('', <$fh>);
 	close $fh;
@@ -216,4 +223,5 @@ print $hfile_handle "
 
 close $cfile_handle;
 close $hfile_handle;
+close $depfile_handle;
 closedir DIR;
diff --git a/src/tools/msvc/MSBuildProject.pm b/src/tools/msvc/MSBuildProject.pm
index fdd22e89eb2..036e44fcb83 100644
--- a/src/tools/msvc/MSBuildProject.pm
+++ b/src/tools/msvc/MSBuildProject.pm
@@ -211,14 +211,19 @@ EOF
 			}
 			else    #if ($grammarFile =~ /\.l$/)
 			{
+				if ($outputFile eq 'src/backend/utils/misc/guc-file.c')
+				{
+					$outputFile = 'src/backend/utils/misc/guc-file.c.h';
+				}
+
 				print $f <<EOF;
     <CustomBuild Include="$grammarFile">
       <Message Condition="'\$(Configuration)|\$(Platform)'=='Debug|$self->{platform}'">Running flex on $grammarFile</Message>
-      <Command Condition="'\$(Configuration)|\$(Platform)'=='Debug|$self->{platform}'">perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile"</Command>
+      <Command Condition="'\$(Configuration)|\$(Platform)'=='Debug|$self->{platform}'">perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile" "$outputFile"</Command>
       <AdditionalInputs Condition="'\$(Configuration)|\$(Platform)'=='Debug|$self->{platform}'">%(AdditionalInputs)</AdditionalInputs>
       <Outputs Condition="'\$(Configuration)|\$(Platform)'=='Debug|$self->{platform}'">$outputFile;%(Outputs)</Outputs>
       <Message Condition="'\$(Configuration)|\$(Platform)'=='Release|$self->{platform}'">Running flex on $grammarFile</Message>
-      <Command Condition="'\$(Configuration)|\$(Platform)'=='Release|$self->{platform}'">perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile"</Command>
+      <Command Condition="'\$(Configuration)|\$(Platform)'=='Release|$self->{platform}'">perl "src\\tools\\msvc\\pgflex.pl" "$grammarFile" "$outputFile"</Command>
       <AdditionalInputs Condition="'\$(Configuration)|\$(Platform)'=='Release|$self->{platform}'">%(AdditionalInputs)</AdditionalInputs>
       <Outputs Condition="'\$(Configuration)|\$(Platform)'=='Release|$self->{platform}'">$outputFile;%(Outputs)</Outputs>
     </CustomBuild>
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 4362bd44fd1..b8e62c6d3f7 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -330,6 +330,7 @@ sub mkvcbuild
 	$pgregress_ecpg->AddFile('src/test/regress/pg_regress.c');
 	$pgregress_ecpg->AddIncludeDir('src/port');
 	$pgregress_ecpg->AddIncludeDir('src/test/regress');
+	$pgregress_ecpg->AddDefine('DLSUFFIX=".dll"');
 	$pgregress_ecpg->AddDefine('HOST_TUPLE="i686-pc-win32vc"');
 	$pgregress_ecpg->AddLibrary('ws2_32.lib');
 	$pgregress_ecpg->AddDirResourceFile('src/interfaces/ecpg/test');
@@ -345,6 +346,7 @@ sub mkvcbuild
 	$isolation_tester->AddIncludeDir('src/port');
 	$isolation_tester->AddIncludeDir('src/test/regress');
 	$isolation_tester->AddIncludeDir('src/interfaces/libpq');
+	$isolation_tester->AddDefine('DLSUFFIX=".dll"');
 	$isolation_tester->AddDefine('HOST_TUPLE="i686-pc-win32vc"');
 	$isolation_tester->AddLibrary('ws2_32.lib');
 	$isolation_tester->AddDirResourceFile('src/test/isolation');
@@ -356,6 +358,7 @@ sub mkvcbuild
 	$pgregress_isolation->AddFile('src/test/regress/pg_regress.c');
 	$pgregress_isolation->AddIncludeDir('src/port');
 	$pgregress_isolation->AddIncludeDir('src/test/regress');
+	$pgregress_isolation->AddDefine('DLSUFFIX=".dll"');
 	$pgregress_isolation->AddDefine('HOST_TUPLE="i686-pc-win32vc"');
 	$pgregress_isolation->AddLibrary('ws2_32.lib');
 	$pgregress_isolation->AddDirResourceFile('src/test/isolation');
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 165a93987ac..721f690ae46 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -688,7 +688,7 @@ sub GenerateFiles
 	{
 		print "Generating sql_help.h...\n";
 		chdir('src/bin/psql');
-		system("perl create_help.pl ../../../doc/src/sgml/ref sql_help");
+		system("perl create_help.pl ../../../doc/src/sgml/ref . sql_help");
 		chdir('../../..');
 	}
 
diff --git a/src/tools/msvc/pgflex.pl b/src/tools/msvc/pgflex.pl
index 0728b85d4de..19f26ff213f 100644
--- a/src/tools/msvc/pgflex.pl
+++ b/src/tools/msvc/pgflex.pl
@@ -29,6 +29,8 @@ unless ($verparts[0] == 2
 }
 
 my $input = shift;
+my $output = shift;
+
 if ($input !~ /\.l$/)
 {
 	print "Input must be a .l file\n";
@@ -40,8 +42,6 @@ elsif (!-e $input)
 	exit 1;
 }
 
-(my $output = $input) =~ s/\.l$/.c/;
-
 # get flex flags from make file
 my $makefile = dirname($input) . "/Makefile";
 my ($mf, $make);
-- 
2.23.0.385.gbc12974a89

