psql: Output dir and dependency generation for sql_help
authorAndres Freund <[email protected]>
Mon, 18 Jul 2022 18:57:31 +0000 (11:57 -0700)
committerAndres Freund <[email protected]>
Mon, 18 Jul 2022 19:24:12 +0000 (12:24 -0700)
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.

To deal with dependencies to the variable set of input files to this script,
add an option to generate a dependency file (which meson / ninja can consume).

Reviewed-by: Peter Eisentraut <[email protected]>
Discussion: https://postgr.es/m/5e216522-ba3c-f0e6-7f97-5276d0270029@enterprisedb.com

src/bin/psql/Makefile
src/bin/psql/create_help.pl
src/tools/msvc/Solution.pm

index 58ec4a89b49e79b51c4482e48623c6ac64273c56..d38775af467fcb5b25c09acd3b4e4d571c2c0771 100644 (file)
@@ -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
index 1a9836cbcc04658beda8475fc76a2dc98d1132fc..ba9a49cff048340c4708bbae9bae41915fffd6f8 100644 (file)
 
 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;
index f2427008df69196108e232930b50649b65a8417e..840f251343cde6fc4fab3bcec4204113419cd97d 100644 (file)
@@ -692,9 +692,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'))