From e37951875d0a92d473df61930c3517d5d5fa32ee Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Thu, 14 Jul 2022 08:37:16 -0700
Subject: [PATCH v10 02/16] meson: prereq: psql: Output dir and dependency
 generation for sql_help

---
 src/bin/psql/Makefile       |  2 +-
 src/bin/psql/create_help.pl | 44 ++++++++++++++++++++++++-------------
 src/tools/msvc/Solution.pm  |  5 ++---
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index 58ec4a89b49..d38775af467 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) $< --docdir $(REFDOCDIR) --basename $*
 
 psqlscanslash.c: FLEXFLAGS = -Cfe -p -p
 psqlscanslash.c: FLEX_NO_BACKUP=yes
diff --git a/src/bin/psql/create_help.pl b/src/bin/psql/create_help.pl
index 1a9836cbcc0..ba9a49cff04 100644
--- a/src/bin/psql/create_help.pl
+++ b/src/bin/psql/create_help.pl
@@ -21,21 +21,24 @@
 
 use strict;
 use warnings;
+use Getopt::Long;
 
-my $docdir = $ARGV[0] or die "$0: missing required argument: docdir\n";
-my $hfile = $ARGV[1] . '.h'
-  or die "$0: missing required argument: output file\n";
-my $cfile = $ARGV[1] . '.c';
+my $docdir        = '';
+my $outdir        = '.';
+my $depfile       = '';
+my $hfilebasename = '';
 
-my $hfilebasename;
-if ($hfile =~ m!.*/([^/]+)$!)
-{
-	$hfilebasename = $1;
-}
-else
-{
-	$hfilebasename = $hfile;
-}
+GetOptions(
+	'docdir=s'   => \$docdir,
+	'outdir=s'   => \$outdir,
+	'basename=s' => \$hfilebasename,
+	'depfile=s'  => \$depfile,) or die "$0: wrong arguments";
+
+$docdir        or die "$0: missing required argument: docdir\n";
+$hfilebasename or die "$0: missing required argument: basename\n";
+
+my $hfile = $hfilebasename . '.h';
+my $cfile = $hfilebasename . '.c';
 
 my $define = $hfilebasename;
 $define =~ tr/a-z/A-Z/;
@@ -43,11 +46,18 @@ $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";
 
+my $depfile_handle;
+if ($depfile)
+{
+	open($depfile_handle, '>', $depfile)
+	  or die "$0: could not open output file '$depfile': $!\n";
+}
+
 print $hfile_handle "/*
  * *** Do not change this file by hand. It is automatically
  * *** generated from the DocBook documentation.
@@ -98,6 +108,9 @@ foreach my $file (sort readdir DIR)
 	my ($cmdid, @cmdnames, $cmddesc, $cmdsynopsis);
 	$file =~ /\.sgml$/ or next;
 
+	print $depfile_handle "$outdir/$cfile $outdir/$hfile: $docdir/$file\n"
+	  if ($depfile);
+
 	open(my $fh, '<', "$docdir/$file") or next;
 	my $filecontent = join('', <$fh>);
 	close $fh;
@@ -216,4 +229,5 @@ print $hfile_handle "
 
 close $cfile_handle;
 close $hfile_handle;
+close $depfile_handle if ($depfile);
 closedir DIR;
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index fa32dc371dc..37bd5c0db69 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -691,9 +691,8 @@ sub GenerateFiles
 	if (IsNewer('src/bin/psql/sql_help.h', 'src/bin/psql/create_help.pl'))
 	{
 		print "Generating sql_help.h...\n";
-		chdir('src/bin/psql');
-		system("perl create_help.pl ../../../doc/src/sgml/ref sql_help");
-		chdir('../../..');
+		my $psql = 'src/bin/psql';
+		system("perl $psql/create_help.pl --docdir doc/src/sgml/ref --outdir $psql --basename sql_help");
 	}
 
 	if (IsNewer('src/common/kwlist_d.h', 'src/include/parser/kwlist.h'))
-- 
2.37.0.3.g30cc8d0f14

