From adba4b74710669cefe8e66cd8f586b47e6431f48 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 18 Jul 2022 12:32:26 -0700
Subject: [PATCH] Add output directory option to gen_node_support.pl

This is in preparation for building postgres with meson / ninja.

When building with meson, commands are run at the root of the build tree. Add
an option to put build output into the appropriate place. This can be utilized
by src/tools/msvc/ for a minor simplification, which also provides some
coverage for the new option.

Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
Discussion: https://postgr.es/m/5e216522-ba3c-f0e6-7f97-5276d0270029@enterprisedb.com
---
 src/backend/nodes/gen_node_support.pl | 29 +++++++++++++++++----------
 src/tools/msvc/Solution.pm            |  9 +++------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/backend/nodes/gen_node_support.pl b/src/backend/nodes/gen_node_support.pl
index 975e456ee2..f3309c3000 100644
--- a/src/backend/nodes/gen_node_support.pl
+++ b/src/backend/nodes/gen_node_support.pl
@@ -19,12 +19,19 @@ use strict;
 use warnings;
 
 use File::Basename;
+use Getopt::Long;
 
 use FindBin;
 use lib "$FindBin::RealBin/../catalog";
 
 use Catalog;    # for RenameTempFile
 
+my $output_path = '.';
+
+GetOptions(
+	'outdir:s'       => \$output_path)
+  or die "$0: wrong arguments";
+
 
 # Test whether first argument is element of the list in the second
 # argument
@@ -576,7 +583,7 @@ my $header_comment =
 # nodetags.h
 
 push @output_files, 'nodetags.h';
-open my $nt, '>', 'nodetags.h' . $tmpext or die $!;
+open my $nt, '>', "$output_path/nodetags.h$tmpext" or die "$output_path/nodetags.h$tmpext: $!";
 
 printf $nt $header_comment, 'nodetags.h';
 
@@ -620,13 +627,13 @@ foreach my $infile (sort @ARGV)
 # copyfuncs.c, equalfuncs.c
 
 push @output_files, 'copyfuncs.funcs.c';
-open my $cff, '>', 'copyfuncs.funcs.c' . $tmpext or die $!;
+open my $cff, '>', "$output_path/copyfuncs.funcs.c$tmpext" or die $!;
 push @output_files, 'equalfuncs.funcs.c';
-open my $eff, '>', 'equalfuncs.funcs.c' . $tmpext or die $!;
+open my $eff, '>', "$output_path/equalfuncs.funcs.c$tmpext" or die $!;
 push @output_files, 'copyfuncs.switch.c';
-open my $cfs, '>', 'copyfuncs.switch.c' . $tmpext or die $!;
+open my $cfs, '>', "$output_path/copyfuncs.switch.c$tmpext" or die $!;
 push @output_files, 'equalfuncs.switch.c';
-open my $efs, '>', 'equalfuncs.switch.c' . $tmpext or die $!;
+open my $efs, '>', "$output_path/equalfuncs.switch.c$tmpext" or die $!;
 
 printf $cff $header_comment, 'copyfuncs.funcs.c';
 printf $eff $header_comment, 'equalfuncs.funcs.c';
@@ -819,13 +826,13 @@ close $efs;
 # outfuncs.c, readfuncs.c
 
 push @output_files, 'outfuncs.funcs.c';
-open my $off, '>', 'outfuncs.funcs.c' . $tmpext or die $!;
+open my $off, '>', "$output_path/outfuncs.funcs.c$tmpext" or die $!;
 push @output_files, 'readfuncs.funcs.c';
-open my $rff, '>', 'readfuncs.funcs.c' . $tmpext or die $!;
+open my $rff, '>', "$output_path/readfuncs.funcs.c$tmpext" or die $!;
 push @output_files, 'outfuncs.switch.c';
-open my $ofs, '>', 'outfuncs.switch.c' . $tmpext or die $!;
+open my $ofs, '>', "$output_path/outfuncs.switch.c$tmpext" or die $!;
 push @output_files, 'readfuncs.switch.c';
-open my $rfs, '>', 'readfuncs.switch.c' . $tmpext or die $!;
+open my $rfs, '>', "$output_path/readfuncs.switch.c$tmpext" or die $!;
 
 printf $off $header_comment, 'outfuncs.funcs.c';
 printf $rff $header_comment, 'readfuncs.funcs.c';
@@ -1130,7 +1137,7 @@ close $rfs;
 # now rename the temporary files to their final names
 foreach my $file (@output_files)
 {
-	Catalog::RenameTempFile($file, $tmpext);
+	Catalog::RenameTempFile("$output_path/$file", $tmpext);
 }
 
 
@@ -1144,7 +1151,7 @@ END
 	{
 		foreach my $file (@output_files)
 		{
-			unlink($file . $tmpext);
+			unlink("$output_path/$file$tmpext");
 		}
 	}
 
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index a66b04c242..1e125aef94 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -865,15 +865,12 @@ EOF
 		  utils/rel.h
 		);
 
-		chdir('src/backend/nodes');
+		my @node_files = map { "src/include/$_" } @node_headers;
 
-		my @node_files = map { "../../../src/include/$_" } @node_headers;
-
-		system("perl gen_node_support.pl @node_files");
-		open(my $f, '>', 'node-support-stamp')
+		system("perl src/backend/nodes/gen_node_support.pl --outdir src/backend/nodes @node_files");
+		open(my $f, '>', 'src/backend/nodes/node-support-stamp')
 		  || confess "Could not touch node-support-stamp";
 		close($f);
-		chdir('../../..');
 	}
 
 	if (IsNewer(