From b5fd87aa2f38001c81547346f576f9d599d57b18 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Tue, 9 Aug 2022 00:29:57 -0700
Subject: [PATCH v11 3/9] meson: prereq: Extend gendef.pl in preparation for
 meson

The main issue with using gendef.pl as is for meson is that with meson the
filenames are a bit longer, exceeding the max commandline length when calling
dumpbin with all objects. It's easier to pass a library in anyway.

The .def file location, input and temporary file location need to be tunable
as well.

This also fixes a bug in gendef.pl: The logic when to regenerate was broken
and never avoid regenerating.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/tools/msvc/MSBuildProject.pm |  4 +-
 src/tools/msvc/gendef.pl         | 72 ++++++++++++++++++++++----------
 2 files changed, 54 insertions(+), 22 deletions(-)

diff --git a/src/tools/msvc/MSBuildProject.pm b/src/tools/msvc/MSBuildProject.pm
index 62acdda3a19..3581fa4a123 100644
--- a/src/tools/msvc/MSBuildProject.pm
+++ b/src/tools/msvc/MSBuildProject.pm
@@ -312,6 +312,8 @@ sub WriteItemDefinitionGroup
 
 	my $targetmachine =
 	  $self->{platform} eq 'Win32' ? 'MachineX86' : 'MachineX64';
+	my $arch =
+	  $self->{platform} eq 'Win32' ? 'x86' : 'x86_64';
 
 	my $includes = join ';', @{ $self->{includes} }, "";
 
@@ -381,7 +383,7 @@ EOF
 		print $f <<EOF;
     <PreLinkEvent>
       <Message>Generate DEF file</Message>
-      <Command>perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name} $self->{platform}</Command>
+      <Command>perl src\\tools\\msvc\\gendef.pl --arch $arch --deffile $cfgname\\$self->{name}\\$self->{name}.def $cfgname\\$self->{name}</Command>
     </PreLinkEvent>
 EOF
 	}
diff --git a/src/tools/msvc/gendef.pl b/src/tools/msvc/gendef.pl
index b8c514a8311..1e25c0e4d38 100644
--- a/src/tools/msvc/gendef.pl
+++ b/src/tools/msvc/gendef.pl
@@ -4,7 +4,8 @@
 use strict;
 use warnings;
 use 5.8.0;
-use List::Util qw(max);
+use List::Util qw(min);
+use Getopt::Long;
 
 my @def;
 
@@ -113,7 +114,7 @@ sub extract_syms
 
 sub writedef
 {
-	my ($deffile, $platform, $def) = @_;
+	my ($deffile, $arch, $def) = @_;
 	open(my $fh, '>', $deffile) || die "Could not write to $deffile\n";
 	print $fh "EXPORTS\n";
 	foreach my $f (sort keys %{$def})
@@ -122,7 +123,7 @@ sub writedef
 
 		# Strip the leading underscore for win32, but not x64
 		$f =~ s/^_//
-		  unless ($platform eq "x64");
+		  unless ($arch eq "x86_64");
 
 		# Emit just the name if it's a function symbol, or emit the name
 		# decorated with the DATA option for variables.
@@ -142,40 +143,69 @@ sub writedef
 
 sub usage
 {
-	die(    "Usage: gendef.pl <modulepath> <platform>\n"
-		  . "    modulepath: path to dir with obj files, no trailing slash"
-		  . "    platform: Win32 | x64");
+	my $add = shift;
+
+	die(    "Usage: gendef.pl --arch <arch> --deffile <deffile> --tempdir <tempdir> files or directories\n"
+		  . "    arch: x86 | x86_64\n"
+		  . "    outfile: \n"
+		  . "    tempdir: directory for temporary objects\n"
+		  . "    $add\n"
+	  );
 }
 
-usage()
-  unless scalar(@ARGV) == 2
-  && ( ($ARGV[0] =~ /\\([^\\]+$)/)
-	&& ($ARGV[1] eq 'Win32' || $ARGV[1] eq 'x64'));
-my $defname  = uc $1;
-my $deffile  = "$ARGV[0]/$defname.def";
-my $platform = $ARGV[1];
+my $arch;
+my $deffile;
+my $tempdir = '.';
+
+GetOptions(
+	'arch:s'   => \$arch,
+	'deffile:s'    => \$deffile,
+	'tempdir:s' => \$tempdir,
+  ) or die 'bluebeard';
+
+usage("arch: $arch") unless
+  ($arch eq 'x86' || $arch eq 'x86_64');
+
+print join(' ', @ARGV)."\n";
+
+my @files;
+
+foreach my $in (@ARGV)
+{
+	if (-d $in)
+	{
+		push @files, <$in/*.obj>;
+	}
+	else
+	{
+		push @files, $in;
+	}
+}
 
 # if the def file exists and is newer than all input object files, skip
 # its creation
 if (-f $deffile
-	&& (-M $deffile > max(map { -M } <$ARGV[0]/*.obj>)))
+	&& (-M $deffile < min(map { -M } @files)))
 {
-	print "Not re-generating $defname.DEF, file already exists.\n";
+	print "Not re-generating $deffile, file already exists.\n";
 	exit(0);
 }
 
-print "Generating $defname.DEF from directory $ARGV[0], platform $platform\n";
+print STDERR "Generating $deffile in tempdir $tempdir\n";
 
 my %def = ();
 
-my $symfile = "$ARGV[0]/all.sym";
-my $tmpfile = "$ARGV[0]/tmp.sym";
-system("dumpbin /symbols /out:$tmpfile $ARGV[0]/*.obj >NUL")
-  && die "Could not call dumpbin";
+my $symfile = "$tempdir/all.sym";
+my $tmpfile = "$tempdir/tmp.sym";
+mkdir($tempdir) unless -d $tempdir;
+
+my $cmd = "dumpbin /symbols /out:$tmpfile ".join(' ', @files);
+
+system($cmd) && die "Could not call dumpbin";
 rename($tmpfile, $symfile);
 extract_syms($symfile, \%def);
 print "\n";
 
-writedef($deffile, $platform, \%def);
+writedef($deffile, $arch, \%def);
 
 print "Generated " . scalar(keys(%def)) . " symbols\n";
-- 
2.37.0.3.g30cc8d0f14

