Skip to content

Commit 70df2df

Browse files
committed
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. As it's easier to pass in a library anyway, do so. 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: Andres Freund <andres@anarazel.de> Reviewed-By: Peter Eisentraut <peter.eisentraut@enterprisedb.com> Discussion: https://postgr.es/m/20220809071055.rgikv3qn74ypnnbb@awork3.anarazel.de Discussion: https://postgr.es/m/7dae5979-c6c0-cec5-7a36-76a85aa8053d@enterprisedb.com
1 parent 1091b48 commit 70df2df

File tree

2 files changed

+49
-22
lines changed

2 files changed

+49
-22
lines changed

src/tools/msvc/MSBuildProject.pm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ sub WriteItemDefinitionGroup
312312

313313
my $targetmachine =
314314
$self->{platform} eq 'Win32' ? 'MachineX86' : 'MachineX64';
315+
my $arch =
316+
$self->{platform} eq 'Win32' ? 'x86' : 'x86_64';
315317

316318
my $includes = join ';', @{ $self->{includes} }, "";
317319

@@ -380,7 +382,7 @@ EOF
380382
print $f <<EOF;
381383
<PreLinkEvent>
382384
<Message>Generate DEF file</Message>
383-
<Command>perl src\\tools\\msvc\\gendef.pl $cfgname\\$self->{name} $self->{platform}</Command>
385+
<Command>perl src\\tools\\msvc\\gendef.pl --arch $arch --deffile $cfgname\\$self->{name}\\$self->{name}.def $cfgname\\$self->{name}</Command>
384386
</PreLinkEvent>
385387
EOF
386388
}

src/tools/msvc/gendef.pl

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
use strict;
55
use warnings;
6-
use List::Util qw(max);
6+
use List::Util qw(min);
7+
use Getopt::Long;
78

89
my @def;
910

@@ -112,7 +113,7 @@ sub extract_syms
112113

113114
sub writedef
114115
{
115-
my ($deffile, $platform, $def) = @_;
116+
my ($deffile, $arch, $def) = @_;
116117
open(my $fh, '>', $deffile) || die "Could not write to $deffile\n";
117118
print $fh "EXPORTS\n";
118119
foreach my $f (sort keys %{$def})
@@ -121,7 +122,7 @@ sub writedef
121122

122123
# Strip the leading underscore for win32, but not x64
123124
$f =~ s/^_//
124-
unless ($platform eq "x64");
125+
unless ($arch eq "x86_64");
125126

126127
# Emit just the name if it's a function symbol, or emit the name
127128
# decorated with the DATA option for variables.
@@ -141,40 +142,64 @@ sub writedef
141142

142143
sub usage
143144
{
144-
die( "Usage: gendef.pl <modulepath> <platform>\n"
145-
. " modulepath: path to dir with obj files, no trailing slash"
146-
. " platform: Win32 | x64");
145+
die("Usage: gendef.pl --arch <arch> --deffile <deffile> --tempdir <tempdir> files-or-directories\n"
146+
. " arch: x86 | x86_64\n"
147+
. " deffile: path of the generated file\n"
148+
. " tempdir: directory for temporary files\n"
149+
. " files or directories: object files or directory containing object files\n"
150+
);
147151
}
148152

149-
usage()
150-
unless scalar(@ARGV) == 2
151-
&& ( ($ARGV[0] =~ /\\([^\\]+$)/)
152-
&& ($ARGV[1] eq 'Win32' || $ARGV[1] eq 'x64'));
153-
my $defname = uc $1;
154-
my $deffile = "$ARGV[0]/$defname.def";
155-
my $platform = $ARGV[1];
153+
my $arch;
154+
my $deffile;
155+
my $tempdir = '.';
156+
157+
GetOptions(
158+
'arch:s' => \$arch,
159+
'deffile:s' => \$deffile,
160+
'tempdir:s' => \$tempdir,) or usage();
161+
162+
usage("arch: $arch")
163+
unless ($arch eq 'x86' || $arch eq 'x86_64');
164+
165+
my @files;
166+
167+
foreach my $in (@ARGV)
168+
{
169+
if (-d $in)
170+
{
171+
push @files, glob "$in/*.obj";
172+
}
173+
else
174+
{
175+
push @files, $in;
176+
}
177+
}
156178

157179
# if the def file exists and is newer than all input object files, skip
158180
# its creation
159181
if (-f $deffile
160-
&& (-M $deffile > max(map { -M } <$ARGV[0]/*.obj>)))
182+
&& (-M $deffile < min(map { -M } @files)))
161183
{
162-
print "Not re-generating $defname.DEF, file already exists.\n";
184+
print "Not re-generating $deffile, file already exists.\n";
163185
exit(0);
164186
}
165187

166-
print "Generating $defname.DEF from directory $ARGV[0], platform $platform\n";
188+
print "Generating $deffile in tempdir $tempdir\n";
167189

168190
my %def = ();
169191

170-
my $symfile = "$ARGV[0]/all.sym";
171-
my $tmpfile = "$ARGV[0]/tmp.sym";
172-
system("dumpbin /symbols /out:$tmpfile $ARGV[0]/*.obj >NUL")
173-
&& die "Could not call dumpbin";
192+
my $symfile = "$tempdir/all.sym";
193+
my $tmpfile = "$tempdir/tmp.sym";
194+
mkdir($tempdir) unless -d $tempdir;
195+
196+
my $cmd = "dumpbin /nologo /symbols /out:$tmpfile " . join(' ', @files);
197+
198+
system($cmd) && die "Could not call dumpbin";
174199
rename($tmpfile, $symfile);
175200
extract_syms($symfile, \%def);
176201
print "\n";
177202

178-
writedef($deffile, $platform, \%def);
203+
writedef($deffile, $arch, \%def);
179204

180205
print "Generated " . scalar(keys(%def)) . " symbols\n";

0 commit comments

Comments
 (0)