make_specfiles 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/usr/bin/perl -w
  2. #
  3. # Update spec files across dlls that share an implementation
  4. #
  5. # Copyright 2011 Alexandre Julliard
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. #
  21. use strict;
  22. my %funcs;
  23. my $group_head;
  24. my @dll_groups =
  25. (
  26. [
  27. "msvcrt",
  28. "msvcirt",
  29. "msvcrt40",
  30. "msvcrt20",
  31. ],
  32. [
  33. "msvcrt",
  34. "msvcp90",
  35. "msvcp100",
  36. "msvcp110",
  37. "msvcp120",
  38. "msvcp140",
  39. "msvcp71",
  40. "msvcp80",
  41. "msvcp70",
  42. "msvcp60",
  43. ],
  44. [
  45. "msvcr120",
  46. "msvcr120_app",
  47. "concrt140",
  48. ],
  49. [
  50. "ucrtbase",
  51. "vcruntime140",
  52. ],
  53. [
  54. "msvcp120",
  55. "msvcp120_app",
  56. ],
  57. [
  58. "msvcp140",
  59. "msvcp_win",
  60. ],
  61. [
  62. "d3d10",
  63. "d3d10_1",
  64. ],
  65. [
  66. "d3dx10_43",
  67. "d3dx10_42",
  68. "d3dx10_41",
  69. "d3dx10_40",
  70. "d3dx10_39",
  71. "d3dx10_38",
  72. "d3dx10_37",
  73. "d3dx10_36",
  74. "d3dx10_35",
  75. "d3dx10_34",
  76. "d3dx10_33",
  77. ],
  78. [
  79. "xinput1_3",
  80. "xinput1_4",
  81. "xinput1_2",
  82. "xinput1_1",
  83. "xinput9_1_0",
  84. ],
  85. [
  86. "vcomp",
  87. "vcomp140",
  88. "vcomp120",
  89. "vcomp110",
  90. "vcomp100",
  91. "vcomp90",
  92. ],
  93. [
  94. "advapi32",
  95. "sechost",
  96. ],
  97. [
  98. "netapi32",
  99. "srvcli",
  100. ],
  101. [
  102. "ole32",
  103. "iprop",
  104. ],
  105. [
  106. "secur32",
  107. "security",
  108. "sspicli",
  109. ],
  110. [
  111. "gdi32",
  112. "usp10"
  113. ],
  114. [
  115. "bthprops.cpl",
  116. "irprops.cpl",
  117. ],
  118. [
  119. "sfc_os",
  120. "sfc",
  121. ],
  122. [
  123. "bcrypt",
  124. "ncrypt",
  125. "cng.sys",
  126. ],
  127. [
  128. "ntoskrnl.exe",
  129. "hal",
  130. ],
  131. [
  132. "mscoree",
  133. "mscorwks",
  134. ],
  135. [
  136. "sppc",
  137. "slc",
  138. ],
  139. );
  140. my $update_flags = 0;
  141. my $show_duplicates = 0;
  142. foreach my $arg (@ARGV)
  143. {
  144. if ($arg eq "-f") { $update_flags = 1; }
  145. elsif ($arg eq "-d") { $show_duplicates = 1; }
  146. }
  147. # update a file if changed
  148. sub update_file($$)
  149. {
  150. my $file = shift;
  151. my $new = shift;
  152. open FILE, ">$file.new" or die "cannot create $file.new";
  153. print FILE $new;
  154. close FILE;
  155. rename "$file.new", "$file";
  156. print "$file updated\n";
  157. }
  158. # parse a spec file line
  159. sub parse_line($$$)
  160. {
  161. my ($name, $line, $str) = @_;
  162. 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*\#.*)?/)
  163. {
  164. return ( "ordinal" => $1, "callconv" => $2, "flags" => $3, "name" => $4, "args" => $5 || "",
  165. "target" => $6 || $4, "comment" => $7, "spec" => $name );
  166. }
  167. return () if $str =~ /^\s*$/;
  168. return () if $str =~ /^\s*\#/;
  169. printf STDERR "$name.spec:$line: error: Unrecognized line $_\n";
  170. }
  171. sub read_spec_file($)
  172. {
  173. my $name = shift;
  174. my $file = "dlls/$name/$name.spec";
  175. my %stubs;
  176. open SPEC, "<$file" or die "cannot open $file";
  177. while (<SPEC>)
  178. {
  179. chomp;
  180. my %descr = parse_line( $name, $., $_ );
  181. next unless %descr;
  182. my $func = $descr{name};
  183. if (defined $funcs{$func})
  184. {
  185. my %update = %{$funcs{$func}};
  186. next if $update{ordinal} ne $descr{ordinal} or $update{callconv} ne $descr{callconv} or $update{args} ne $descr{args};
  187. my $arch = $1 if $update{flags} =~ /-arch=(\S+)/;
  188. my $new_arch = $1 if $descr{flags} =~ /-arch=(\S+)/;
  189. next if !defined $arch or !defined $new_arch;
  190. if (($arch eq "win32" and $new_arch eq "win64") or ($arch eq "win64" and $new_arch eq "win32"))
  191. {
  192. $funcs{$func}{flags} =~ s/-arch=\S+\s+//;
  193. next;
  194. }
  195. $funcs{$func}{flags} =~ s/-arch=$arch/-arch=$arch,$new_arch/;
  196. next;
  197. }
  198. next if $func eq "@";
  199. $funcs{$func} = \%descr;
  200. }
  201. close SPEC;
  202. }
  203. sub update_spec_file($)
  204. {
  205. my $name = shift;
  206. my $file = "dlls/$name/$name.spec";
  207. my %stubs;
  208. my ($old, $new);
  209. open SPEC, "<$file" or die "cannot open $file";
  210. while (<SPEC>)
  211. {
  212. $old .= $_;
  213. chomp;
  214. my $commented_out = 0;
  215. my %descr = parse_line( $name, $., $_ );
  216. if (!%descr)
  217. {
  218. # check for commented out exports
  219. if (/^\s*\#\s*((?:\@|\d+)\s+)?((?:extern|stub|stdcall|cdecl|varargs|thiscall)\s+.*)/)
  220. {
  221. $commented_out = 1;
  222. %descr = parse_line( $name, $., ($1 || "\@ ") . $2 );
  223. }
  224. }
  225. goto done unless %descr;
  226. my $func = $descr{name};
  227. if (!defined $funcs{$func})
  228. {
  229. $funcs{$func} = \%descr unless $commented_out || $name =~ /-/;
  230. goto done;
  231. }
  232. my %parent = %{$funcs{$func}};
  233. goto done if $parent{spec} eq $descr{spec}; # the definition is in this spec file
  234. goto done if $descr{comment} && $descr{comment} =~ /don't forward/;
  235. if ($descr{callconv} ne "stub" && $descr{target} !~ /\./ && !$commented_out)
  236. {
  237. printf "%s:%u: note: %s already defined in %s\n", $file, $., $func, $parent{spec} if $show_duplicates;
  238. goto done;
  239. }
  240. my $flags = $descr{flags};
  241. if ($parent{callconv} ne "stub" || $update_flags)
  242. {
  243. $flags = $parent{flags};
  244. $flags =~ s/-ordinal\s*// if $descr{ordinal} eq "@";
  245. $flags =~ s/-noname\s*// if $descr{ordinal} eq "@";
  246. $flags =~ s/-import\s*//;
  247. if ($descr{flags} =~ /-private/) # preserve -private flag
  248. {
  249. $flags = "-private " . $flags unless $flags =~ /-private/;
  250. }
  251. }
  252. if ($parent{callconv} ne "stub" || $parent{args})
  253. {
  254. my $callconv = $parent{callconv} ne "stub" ? $parent{callconv} :
  255. $parent{spec} =~ /(msvc|ucrtbase)/ ? "cdecl" : "stdcall"; # hack
  256. $_ = sprintf "$descr{ordinal} %s %s%s", $callconv, $flags, $func;
  257. if ($parent{target} =~ /$group_head\./) # use the same forward as parent if possible
  258. {
  259. $_ .= sprintf "%s %s", $parent{args}, $parent{target};
  260. }
  261. else
  262. {
  263. $_ .= sprintf "%s %s.%s", $parent{args}, $parent{spec}, $func;
  264. }
  265. }
  266. else
  267. {
  268. $_ = sprintf "$descr{ordinal} stub %s%s", $flags, $func;
  269. }
  270. $_ .= $descr{comment} || "";
  271. done:
  272. $new .= "$_\n";
  273. }
  274. close SPEC;
  275. update_file( $file, $new ) if $old ne $new;
  276. }
  277. sub sync_spec_files(@)
  278. {
  279. %funcs = ();
  280. $group_head = shift;
  281. read_spec_file( $group_head );
  282. foreach my $spec (@_) { update_spec_file($spec); }
  283. }
  284. foreach my $group (@dll_groups)
  285. {
  286. sync_spec_files( @{$group} );
  287. }