2011-05-03 10:02:25 +00:00
#!/usr/bin/perl -w
#
# Update spec files across dlls that share an implementation
#
# Copyright 2011 Alexandre Julliard
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
use strict;
my %funcs;
my $group_head;
my @dll_groups =
(
[
"msvcrt",
2012-12-06 12:23:23 +00:00
"msvcirt",
2011-05-03 10:02:25 +00:00
"msvcrt40",
"msvcrt20",
],
[
"msvcrt",
"msvcp90",
"msvcp100",
2013-03-30 12:48:49 +00:00
"msvcp110",
2014-11-11 17:35:39 +00:00
"msvcp120",
2015-11-03 21:54:34 +00:00
"msvcp140",
2011-05-03 10:02:25 +00:00
"msvcp71",
2013-01-16 13:36:40 +00:00
"msvcp80",
2011-05-03 10:02:25 +00:00
"msvcp70",
"msvcp60",
],
2014-12-10 08:44:44 +00:00
[
"msvcr120",
"msvcr120_app",
2016-08-03 23:57:08 +00:00
"concrt140",
2014-12-10 08:44:44 +00:00
],
2015-08-26 06:44:42 +00:00
[
"ucrtbase",
2015-10-29 12:30:08 +00:00
"vcruntime140",
2015-08-26 06:44:42 +00:00
],
2014-12-10 13:44:04 +00:00
[
"msvcp120",
"msvcp120_app",
],
2021-11-19 18:47:18 +00:00
[
"msvcp140",
"msvcp_win",
],
2018-02-27 04:21:25 +00:00
[
"d3d10",
"d3d10_1",
],
2011-05-03 09:13:08 +00:00
[
"d3dx10_43",
"d3dx10_42",
"d3dx10_41",
"d3dx10_40",
"d3dx10_39",
"d3dx10_38",
"d3dx10_37",
"d3dx10_36",
"d3dx10_35",
"d3dx10_34",
"d3dx10_33",
],
[
"xinput1_3",
2013-09-03 19:07:29 +00:00
"xinput1_4",
2011-05-03 09:13:08 +00:00
"xinput1_2",
"xinput1_1",
"xinput9_1_0",
],
2012-03-18 18:30:10 +00:00
[
"vcomp",
2016-08-09 02:56:29 +00:00
"vcomp140",
2015-04-28 18:08:47 +00:00
"vcomp120",
2015-07-31 17:00:00 +00:00
"vcomp110",
2012-03-18 18:30:10 +00:00
"vcomp100",
2012-09-16 15:10:20 +00:00
"vcomp90",
2012-03-18 18:30:10 +00:00
],
2012-12-17 10:37:45 +00:00
[
"advapi32",
2022-02-15 14:55:46 +00:00
"sechost",
2018-03-13 13:44:31 +00:00
],
2016-11-19 10:56:38 +00:00
[
"netapi32",
2020-11-02 18:19:27 +00:00
"srvcli",
2016-11-19 10:56:38 +00:00
],
2012-12-20 07:50:01 +00:00
[
"ole32",
2016-09-19 19:16:26 +00:00
"iprop",
2012-12-20 07:50:01 +00:00
],
2018-04-25 01:44:02 +00:00
[
"secur32",
2022-02-16 15:07:33 +00:00
"security",
2018-04-25 01:44:02 +00:00
"sspicli",
],
2014-02-04 13:55:23 +00:00
[
"gdi32",
2020-11-09 10:04:04 +00:00
"usp10"
2014-02-04 13:55:23 +00:00
],
2016-01-22 06:51:00 +00:00
[
"bthprops.cpl",
"irprops.cpl",
],
2016-04-01 23:36:45 +00:00
[
"sfc_os",
"sfc",
],
2016-05-26 05:21:09 +00:00
[
"bcrypt",
"ncrypt",
2021-10-22 10:40:08 +00:00
"cng.sys",
2016-05-26 05:21:09 +00:00
],
2017-03-07 15:01:20 +00:00
[
"ntoskrnl.exe",
"hal",
],
2019-07-11 19:23:43 +00:00
[
"mscoree",
"mscorwks",
],
2021-06-02 19:40:16 +00:00
[
"sppc",
"slc",
],
2011-05-03 10:02:25 +00:00
);
my $update_flags = 0;
my $show_duplicates = 0;
foreach my $arg (@ARGV)
{
if ($arg eq "-f") { $update_flags = 1; }
elsif ($arg eq "-d") { $show_duplicates = 1; }
}
2016-07-06 06:32:16 +00:00
# update a file if changed
sub update_file($$)
2011-05-03 10:02:25 +00:00
{
my $file = shift;
2016-07-06 06:32:16 +00:00
my $new = shift;
open FILE, ">$file.new" or die "cannot create $file.new";
print FILE $new;
close FILE;
rename "$file.new", "$file";
print "$file updated\n";
2011-05-03 10:02:25 +00:00
}
2023-11-16 13:07:08 +00:00
# update a file if changed
sub output_file($$)
{
my $file = shift;
my $new = shift;
my $old = "";
if (open FILE, "<$file")
{
local $/ = undef;
$old .= <FILE>;
close FILE;
}
update_file( $file, $new ) if $old ne $new;
}
2011-05-03 10:02:25 +00:00
# parse a spec file line
sub parse_line($$$)
{
2014-02-06 10:19:16 +00:00
my ($name, $line, $str) = @_;
2011-05-03 10:02:25 +00:00
2014-02-06 10:19:16 +00:00
if ($str =~ /^\s*(\@|\d+)\s+(stdcall|cdecl|varargs|thiscall|stub|extern)\s+((?:-\S+\s+)*)([A-Za-z0-9_\@\$?]+)(?:\s*(\([^)]*\)))?(?:\s+([A-Za-z0-9_\@\$?.]+))?(\s*\#.*)?/)
2011-05-03 10:02:25 +00:00
{
return ( "ordinal" => $1, "callconv" => $2, "flags" => $3, "name" => $4, "args" => $5 || "",
"target" => $6 || $4, "comment" => $7, "spec" => $name );
}
2014-02-06 10:19:16 +00:00
return () if $str =~ /^\s*$/;
return () if $str =~ /^\s*\#/;
2011-05-03 10:02:25 +00:00
printf STDERR "$name.spec:$line: error: Unrecognized line $_\n";
}
sub read_spec_file($)
{
my $name = shift;
my $file = "dlls/$name/$name.spec";
my %stubs;
open SPEC, "<$file" or die "cannot open $file";
while (<SPEC>)
{
chomp;
my %descr = parse_line( $name, $., $_ );
next unless %descr;
my $func = $descr{name};
2021-11-28 15:19:34 +00:00
if (defined $funcs{$func})
{
my %update = %{$funcs{$func}};
next if $update{ordinal} ne $descr{ordinal} or $update{callconv} ne $descr{callconv} or $update{args} ne $descr{args};
my $arch = $1 if $update{flags} =~ /-arch=(\S+)/;
my $new_arch = $1 if $descr{flags} =~ /-arch=(\S+)/;
next if !defined $arch or !defined $new_arch;
if (($arch eq "win32" and $new_arch eq "win64") or ($arch eq "win64" and $new_arch eq "win32"))
{
$funcs{$func}{flags} =~ s/-arch=\S+\s+//;
next;
}
$funcs{$func}{flags} =~ s/-arch=$arch/-arch=$arch,$new_arch/;
next;
}
2017-03-24 03:11:50 +00:00
next if $func eq "@";
2011-05-03 10:02:25 +00:00
$funcs{$func} = \%descr;
}
close SPEC;
}
sub update_spec_file($)
{
my $name = shift;
my $file = "dlls/$name/$name.spec";
my %stubs;
2016-07-06 06:32:16 +00:00
my ($old, $new);
2011-05-03 10:02:25 +00:00
open SPEC, "<$file" or die "cannot open $file";
while (<SPEC>)
{
2016-07-06 06:32:16 +00:00
$old .= $_;
2011-05-03 10:02:25 +00:00
chomp;
my $commented_out = 0;
my %descr = parse_line( $name, $., $_ );
if (!%descr)
{
# check for commented out exports
if (/^\s*\#\s*((?:\@|\d+)\s+)?((?:extern|stub|stdcall|cdecl|varargs|thiscall)\s+.*)/)
{
$commented_out = 1;
%descr = parse_line( $name, $., ($1 || "\@ ") . $2 );
}
}
goto done unless %descr;
my $func = $descr{name};
if (!defined $funcs{$func})
{
2019-09-26 17:39:08 +00:00
$funcs{$func} = \%descr unless $commented_out || $name =~ /-/;
2011-05-03 10:02:25 +00:00
goto done;
}
my %parent = %{$funcs{$func}};
goto done if $parent{spec} eq $descr{spec}; # the definition is in this spec file
2011-09-14 14:29:35 +00:00
goto done if $descr{comment} && $descr{comment} =~ /don't forward/;
2011-05-04 11:45:36 +00:00
if ($descr{callconv} ne "stub" && $descr{target} !~ /\./ && !$commented_out)
2011-05-03 10:02:25 +00:00
{
printf "%s:%u: note: %s already defined in %s\n", $file, $., $func, $parent{spec} if $show_duplicates;
goto done;
}
2012-12-19 13:36:24 +00:00
my $flags = $descr{flags};
if ($parent{callconv} ne "stub" || $update_flags)
{
$flags = $parent{flags};
$flags =~ s/-ordinal\s*// if $descr{ordinal} eq "@";
2014-02-06 10:19:16 +00:00
$flags =~ s/-noname\s*// if $descr{ordinal} eq "@";
2019-06-21 10:08:44 +00:00
$flags =~ s/-import\s*//;
2013-03-14 12:57:33 +00:00
if ($descr{flags} =~ /-private/) # preserve -private flag
{
$flags = "-private " . $flags unless $flags =~ /-private/;
}
2012-12-19 13:36:24 +00:00
}
2011-05-03 10:02:25 +00:00
2011-05-03 11:13:15 +00:00
if ($parent{callconv} ne "stub" || $parent{args})
2011-05-03 10:02:25 +00:00
{
my $callconv = $parent{callconv} ne "stub" ? $parent{callconv} :
2015-08-26 06:44:41 +00:00
$parent{spec} =~ /(msvc|ucrtbase)/ ? "cdecl" : "stdcall"; # hack
2011-06-22 22:02:30 +00:00
$_ = sprintf "$descr{ordinal} %s %s%s", $callconv, $flags, $func;
2011-05-03 10:02:25 +00:00
if ($parent{target} =~ /$group_head\./) # use the same forward as parent if possible
{
$_ .= sprintf "%s %s", $parent{args}, $parent{target};
}
else
{
$_ .= sprintf "%s %s.%s", $parent{args}, $parent{spec}, $func;
}
}
else
{
2011-06-22 22:02:30 +00:00
$_ = sprintf "$descr{ordinal} stub %s%s", $flags, $func;
2011-05-03 10:02:25 +00:00
}
$_ .= $descr{comment} || "";
done:
2016-07-06 06:32:16 +00:00
$new .= "$_\n";
2011-05-03 10:02:25 +00:00
}
close SPEC;
2016-07-06 06:32:16 +00:00
update_file( $file, $new ) if $old ne $new;
2011-05-03 10:02:25 +00:00
}
2023-11-16 13:07:08 +00:00
sub get_args_size($)
{
my $args = shift;
my $ret32 = 0;
my $ret64 = 0;
if ($args =~ /^\((.*)\)$/)
{
my @args = split /\s+/, $1;
$ret64 += 8 * scalar @args;
map { $ret32 += ($_ eq "int64") ? 8 : 4; } @args;
}
return ($ret32, $ret64);
}
sub get_syscalls_str(@)
{
my @syscalls = sort { $a->[0] cmp $b->[0] } @_;
my $ret = "";
for (my $i = 0; $i < @syscalls; $i++)
{
my ($name, $args) = @{$syscalls[$i]};
$ret .= sprintf " \\\n SYSCALL_ENTRY( 0x%04x, %s, %u )", $i, $name, $args;
}
return $ret . "\n";
}
sub read_syscalls($)
{
my $spec = shift;
my @syscalls32 = ();
my @syscalls64 = ();
%funcs = ();
read_spec_file( $spec );
foreach my $func (keys %funcs)
{
my $descr = $funcs{$func};
next unless $descr->{flags} =~ /-syscall/;
next if $descr->{target} ne $func && defined $funcs{$descr->{target}};
my ($args32, $args64) = get_args_size( $funcs{$func}->{args} );
push @syscalls32, [ $func, $args32 ] unless $descr->{flags} =~ /-arch=win64/;
push @syscalls64, [ $func, $args64 ] unless $descr->{flags} =~ /-arch=win32/;
}
return (\@syscalls32, \@syscalls64);
}
sub update_syscalls($$)
{
my ($spec, $file) = @_;
my ($syscalls32, $syscalls64) = read_syscalls( $spec );
output_file( $file,
"/* Automatically generated by tools/make_specfiles */\n" .
"\n#define ALL_SYSCALLS32" . get_syscalls_str( @{$syscalls32} ) .
"\n#define ALL_SYSCALLS64" . get_syscalls_str( @{$syscalls64} ));
}
2011-05-03 10:02:25 +00:00
sub sync_spec_files(@)
{
%funcs = ();
$group_head = shift;
read_spec_file( $group_head );
foreach my $spec (@_) { update_spec_file($spec); }
}
foreach my $group (@dll_groups)
{
sync_spec_files( @{$group} );
}
2023-11-16 13:07:08 +00:00
update_syscalls( "ntdll", "dlls/ntdll/ntsyscalls.h" );
update_syscalls( "win32u", "dlls/win32u/win32syscalls.h" );