make_specfiles 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. "cryptbase",
  99. "advapi32",
  100. ],
  101. [
  102. "netapi32",
  103. "srvcli",
  104. ],
  105. [
  106. "ole32",
  107. "iprop",
  108. ],
  109. [
  110. "secur32",
  111. "security",
  112. "sspicli",
  113. ],
  114. [
  115. "gdi32",
  116. "usp10"
  117. ],
  118. [
  119. "bthprops.cpl",
  120. "irprops.cpl",
  121. ],
  122. [
  123. "sfc_os",
  124. "sfc",
  125. ],
  126. [
  127. "bcrypt",
  128. "ncrypt",
  129. "cng.sys",
  130. ],
  131. [
  132. "ntoskrnl.exe",
  133. "hal",
  134. ],
  135. [
  136. "mscoree",
  137. "mscorwks",
  138. ],
  139. [
  140. "sppc",
  141. "slc",
  142. ],
  143. );
  144. my $update_flags = 0;
  145. my $show_duplicates = 0;
  146. foreach my $arg (@ARGV)
  147. {
  148. if ($arg eq "-f") { $update_flags = 1; }
  149. elsif ($arg eq "-d") { $show_duplicates = 1; }
  150. }
  151. # update a file if changed
  152. sub update_file($$)
  153. {
  154. my $file = shift;
  155. my $new = shift;
  156. open FILE, ">$file.new" or die "cannot create $file.new";
  157. print FILE $new;
  158. close FILE;
  159. rename "$file.new", "$file";
  160. print "$file updated\n";
  161. }
  162. # parse a spec file line
  163. sub parse_line($$$)
  164. {
  165. my ($name, $line, $str) = @_;
  166. 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*\#.*)?/)
  167. {
  168. return ( "ordinal" => $1, "callconv" => $2, "flags" => $3, "name" => $4, "args" => $5 || "",
  169. "target" => $6 || $4, "comment" => $7, "spec" => $name );
  170. }
  171. return () if $str =~ /^\s*$/;
  172. return () if $str =~ /^\s*\#/;
  173. printf STDERR "$name.spec:$line: error: Unrecognized line $_\n";
  174. }
  175. sub read_spec_file($)
  176. {
  177. my $name = shift;
  178. my $file = "dlls/$name/$name.spec";
  179. my %stubs;
  180. open SPEC, "<$file" or die "cannot open $file";
  181. while (<SPEC>)
  182. {
  183. chomp;
  184. my %descr = parse_line( $name, $., $_ );
  185. next unless %descr;
  186. my $func = $descr{name};
  187. if (defined $funcs{$func})
  188. {
  189. my %update = %{$funcs{$func}};
  190. next if $update{ordinal} ne $descr{ordinal} or $update{callconv} ne $descr{callconv} or $update{args} ne $descr{args};
  191. my $arch = $1 if $update{flags} =~ /-arch=(\S+)/;
  192. my $new_arch = $1 if $descr{flags} =~ /-arch=(\S+)/;
  193. next if !defined $arch or !defined $new_arch;
  194. if (($arch eq "win32" and $new_arch eq "win64") or ($arch eq "win64" and $new_arch eq "win32"))
  195. {
  196. $funcs{$func}{flags} =~ s/-arch=\S+\s+//;
  197. next;
  198. }
  199. $funcs{$func}{flags} =~ s/-arch=$arch/-arch=$arch,$new_arch/;
  200. next;
  201. }
  202. next if $func eq "@";
  203. $funcs{$func} = \%descr;
  204. }
  205. close SPEC;
  206. }
  207. sub update_spec_file($)
  208. {
  209. my $name = shift;
  210. my $file = "dlls/$name/$name.spec";
  211. my %stubs;
  212. my ($old, $new);
  213. open SPEC, "<$file" or die "cannot open $file";
  214. while (<SPEC>)
  215. {
  216. $old .= $_;
  217. chomp;
  218. my $commented_out = 0;
  219. my %descr = parse_line( $name, $., $_ );
  220. if (!%descr)
  221. {
  222. # check for commented out exports
  223. if (/^\s*\#\s*((?:\@|\d+)\s+)?((?:extern|stub|stdcall|cdecl|varargs|thiscall)\s+.*)/)
  224. {
  225. $commented_out = 1;
  226. %descr = parse_line( $name, $., ($1 || "\@ ") . $2 );
  227. }
  228. }
  229. goto done unless %descr;
  230. my $func = $descr{name};
  231. if (!defined $funcs{$func})
  232. {
  233. $funcs{$func} = \%descr unless $commented_out || $name =~ /-/;
  234. goto done;
  235. }
  236. my %parent = %{$funcs{$func}};
  237. goto done if $parent{spec} eq $descr{spec}; # the definition is in this spec file
  238. goto done if $descr{comment} && $descr{comment} =~ /don't forward/;
  239. if ($descr{callconv} ne "stub" && $descr{target} !~ /\./ && !$commented_out)
  240. {
  241. printf "%s:%u: note: %s already defined in %s\n", $file, $., $func, $parent{spec} if $show_duplicates;
  242. goto done;
  243. }
  244. my $flags = $descr{flags};
  245. if ($parent{callconv} ne "stub" || $update_flags)
  246. {
  247. $flags = $parent{flags};
  248. $flags =~ s/-ordinal\s*// if $descr{ordinal} eq "@";
  249. $flags =~ s/-noname\s*// if $descr{ordinal} eq "@";
  250. $flags =~ s/-import\s*//;
  251. if ($descr{flags} =~ /-private/) # preserve -private flag
  252. {
  253. $flags = "-private " . $flags unless $flags =~ /-private/;
  254. }
  255. }
  256. if ($parent{callconv} ne "stub" || $parent{args})
  257. {
  258. my $callconv = $parent{callconv} ne "stub" ? $parent{callconv} :
  259. $parent{spec} =~ /(msvc|ucrtbase)/ ? "cdecl" : "stdcall"; # hack
  260. $_ = sprintf "$descr{ordinal} %s %s%s", $callconv, $flags, $func;
  261. if ($parent{target} =~ /$group_head\./) # use the same forward as parent if possible
  262. {
  263. $_ .= sprintf "%s %s", $parent{args}, $parent{target};
  264. }
  265. else
  266. {
  267. $_ .= sprintf "%s %s.%s", $parent{args}, $parent{spec}, $func;
  268. }
  269. }
  270. else
  271. {
  272. $_ = sprintf "$descr{ordinal} stub %s%s", $flags, $func;
  273. }
  274. $_ .= $descr{comment} || "";
  275. done:
  276. $new .= "$_\n";
  277. }
  278. close SPEC;
  279. update_file( $file, $new ) if $old ne $new;
  280. }
  281. sub sync_spec_files(@)
  282. {
  283. %funcs = ();
  284. $group_head = shift;
  285. read_spec_file( $group_head );
  286. foreach my $spec (@_) { update_spec_file($spec); }
  287. }
  288. foreach my $group (@dll_groups)
  289. {
  290. sync_spec_files( @{$group} );
  291. }