3 # Copyright 2001 Patrik Stridvall
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 $0 =~ m%^(.*?/?tools)/winapi/winapi_extract$%;
24 require "$1/winapi/setup.pm";
28 &file_type &files_skip &files_filter &get_spec_files
29 $current_dir $wine_dir $winapi_dir $winapi_check_dir
31 use output qw($output);
32 use winapi_extract_options qw($options);
34 if($options->progress) {
35 $output->enable_progress;
37 $output->disable_progress;
44 use winapi qw($win16api $win32api @winapis);
50 if($options->spec_files || $options->winetest) {
53 foreach my $spec_file (get_spec_files("winelib")) {
56 my $module = $spec_file;
57 $module =~ s/^.*?([^\/]*)\.spec$/$1/;
61 open(IN, "< $wine_dir/$spec_file");
65 while($lookahead || defined($_ = <IN>)) {
68 s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begining and end of line
69 s/^(.*?)\s*#.*$/$1/; # remove comments
70 /^$/ && next; # skip empty lines
80 if(/^(@|\d+)\s+stdcall\s+(\w+)\s*\(\s*([^\)]*)\s*\)/) {
83 my @args = split(/\s+/, $3);
85 push @$entries, [$name, "undef", \@args];
90 $module2spec_file{$module} = $spec_file;
91 $module2entries{$module} = $entries;
97 sub documentation_specifications {
100 my @debug_channels = @{$function->debug_channels};
101 my $documentation = $function->documentation;
102 my $documentation_line = $function->documentation_line;
103 my $return_type = $function->return_type;
104 my $linkage = $function->linkage;
105 my $internal_name = $function->internal_name;
107 if($linkage eq "static") {
112 foreach (split(/\n/, $documentation)) {
113 if(/^\s*\*\s*(\S+)\s*[\(\[]\s*(\w+)\s*\.\s*(\S+)\s*[\)\]]/) {
114 my $external_name = $1;
118 if($ordinal eq "@") {
119 if(1 || !exists($specifications{$module}{unfixed}{$external_name})) {
120 $specifications{$module}{unfixed}{$external_name}{ordinal} = $ordinal;
121 $specifications{$module}{unfixed}{$external_name}{external_name} = $external_name;
122 $specifications{$module}{unfixed}{$external_name}{function} = $function;
124 $output->write("$external_name ($module.$ordinal) already exists\n");
126 } elsif($ordinal =~ /^\d+$/) {
127 if(1 || !exists($specifications{$module}{fixed}{$ordinal})) {
128 $specifications{$module}{fixed}{$ordinal}{ordinal} = $ordinal;
129 $specifications{$module}{fixed}{$ordinal}{external_name} = $external_name;
130 $specifications{$module}{fixed}{$ordinal}{function} = $function;
132 $output->write("$external_name ($module.$ordinal) already exists\n");
134 } elsif($ordinal eq "init") {
135 if(!exists($specifications{$module}{init})) {
136 $specifications{$module}{init}{function} = $function;
138 $output->write("$external_name ($module.$ordinal) already exists\n");
141 if(!exists($specifications{$module}{unknown}{$external_name})) {
142 $specifications{$module}{unknown}{$external_name}{ordinal} = $ordinal;
143 $specifications{$module}{unknown}{$external_name}{external_name} = $external_name;
144 $specifications{$module}{unknown}{$external_name}{function} = $function;
146 $output->write("$external_name ($module.$ordinal) already exists\n");
150 if($options->debug) {
151 $output->write("$external_name ($module.$ordinal)\n");
157 my %module_pseudo_stub;
159 sub statements_stub {
160 my $function = shift;
162 my $statements = $function->statements;
163 if(defined($statements) && $statements =~ /FIXME[^;]*stub/s) {
164 if($options->win16) {
165 my $external_name16 = $function->external_name16;
166 foreach my $module16 ($function->modules16) {
167 $module_pseudo_stub{$module16}{$external_name16}++;
170 if($options->win32) {
171 my $external_name32 = $function->external_name32;
172 foreach my $module32 ($function->modules32) {
173 $module_pseudo_stub{$module32}{$external_name32}++;
180 if($options->headers) {
181 @h_files = $options->h_files;
182 @h_files = files_skip(@h_files);
183 @h_files = files_filter("winelib", @h_files);
187 if(1 || $options->spec_files || $options->pseudo_stub_statistics) {
188 @c_files = $options->c_files;
189 @c_files = files_skip(@c_files);
190 @c_files = files_filter("winelib", @c_files);
194 my $progress_current = 0;
195 my $progress_max = scalar(@h_files) + scalar(@c_files);
197 foreach my $file (@h_files, @c_files) {
212 while(s/^.*?\n//) { $max_line++; }
213 if($_) { $max_line++; }
217 if (!$options->old) {
218 $parser = new c_parser($file);
220 $parser = new winapi_c_parser($file);
226 my $update_output = sub {
230 $progress .= "$file (file $progress_current of $progress_max)";
233 if(defined($function)) {
234 my $name = $function->name;
235 my $begin_line = $function->begin_line;
236 my $begin_column = $function->begin_column;
238 $progress .= ": function $name";
239 $prefix .= "$begin_line.$begin_column: function $name: ";
245 $progress .= ": line $line of $max_line";
248 $output->progress($progress);
249 $output->prefix($prefix);
254 my $found_function = sub {
257 my $name = $function->name;
258 $functions{$name} = $function;
263 if($options->stub_statistics) {
264 $old_function = 'winapi_function'->new;
266 $old_function = 'function'->new;
269 $function->file($file);
270 $old_function->debug_channels([]); # FIXME: Not complete
272 $old_function->documentation_line(0); # FIXME: Not complete
273 $old_function->documentation(""); # FIXME: Not complete
275 $old_function->function_line($function->begin_line());
276 $old_function->linkage($function->linkage);
277 $old_function->return_type($function->return_type);
278 $old_function->calling_convention($function->calling_convention);
279 $old_function->internal_name($function->name);
280 if (defined($function->argument_types)) {
281 $old_function->argument_types([@{$function->argument_types}]);
283 if (defined($function->argument_names)) {
284 $old_function->argument_names([@{$function->argument_names}]);
286 $old_function->argument_documentations([]); # FIXME: Not complete
287 $old_function->statements_line($function->statements_line);
288 $old_function->statements($function->statements);
290 if($options->spec_files || $options->winetest) {
291 documentation_specifications($old_function);
294 if($options->stub_statistics) {
295 statements_stub($old_function);
301 $parser->set_found_function_callback($found_function);
303 my $found_type = sub {
308 my $kind = $type->kind;
309 my $_name = $type->_name;
310 my $name = $type->name;
312 foreach my $field ($type->fields) {
313 (my $field_type, my $field_name) = @$field;
315 if ($options->struct) {
317 $output->write("$name:$field_type:$field_name\n");
319 $output->write("$kind $_name:$field_type:$field_name\n");
326 $parser->set_found_type_callback($found_type);
331 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
332 $output->write("can't parse file\n");
339 sub output_function {
343 my $external_name = shift;
344 my $function = shift;
346 my $internal_name = $function->internal_name;
349 my $calling_convention;
350 my $refargument_kinds;
351 if($type eq "win16") {
352 $return_kind = $function->return_kind16 || "undef";
353 $calling_convention = $function->calling_convention16 || "undef";
354 $refargument_kinds = $function->argument_kinds16;
355 } elsif($type eq "win32") {
356 $return_kind = $function->return_kind32 || "undef";
357 $calling_convention = $function->calling_convention32 || "undef";
358 $refargument_kinds = $function->argument_kinds32;
361 if(defined($refargument_kinds)) {
362 my @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
363 print OUT "$ordinal $calling_convention $external_name(@argument_kinds) $internal_name\n";
365 print OUT "$ordinal $calling_convention $external_name() $internal_name # FIXME: arguments undefined\n";
369 if($options->spec_files) {
370 foreach my $winapi (@winapis) {
371 my $type = $winapi->name;
373 if($type eq "win16" && !$options->win16) { next; }
374 if($type eq "win32" && !$options->win32) { next; }
376 foreach my $module ($winapi->all_modules) {
377 my $spec_file = $module2spec_file{$module};
379 if(!defined($spec_file) || !defined($type)) {
380 $output->write("$module: doesn't exist\n");
386 $output->progress("$spec_file");
387 open(OUT, "> $wine_dir/$spec_file");
389 if(exists($specifications{$module}{init})) {
390 my $function = $specifications{$module}{init}{function};
391 print OUT "init " . $function->internal_name . "\n";
396 if(exists($specifications{$module}{init})) {
397 my $function = $specifications{$module}{init}{function};
398 foreach my $debug_channel (@{$function->debug_channels}) {
399 $debug_channels{$debug_channel}++;
402 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
403 my $function = $specifications{$module}{fixed}{$ordinal}{function};
404 foreach my $debug_channel (@{$function->debug_channels}) {
405 $debug_channels{$debug_channel}++;
408 foreach my $name (sort(keys(%{$specifications{$module}{unfixed}}))) {
409 my $function = $specifications{$module}{unfixed}{$name}{function};
410 foreach my $debug_channel (@{$function->debug_channels}) {
411 $debug_channels{$debug_channel}++;
414 foreach my $name (sort(keys(%{$specifications{$module}{unknown}}))) {
415 my $function = $specifications{$module}{unknown}{$name}{function};
416 foreach my $debug_channel (@{$function->debug_channels}) {
417 $debug_channels{$debug_channel}++;
421 my @debug_channels = sort(keys(%debug_channels));
422 if($#debug_channels >= 0) {
423 print OUT "debug_channels (" . join(" ", @debug_channels) . ")\n";
433 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
434 my $entry = $specifications{$module}{unknown}{$external_name};
435 my $ordinal = $entry->{ordinal};
436 my $function = $entry->{function};
438 output_function(\*OUT, $type, $ordinal, $external_name, $function);
446 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
447 my $entry = $specifications{$module}{fixed}{$ordinal};
448 my $external_name = $entry->{external_name};
449 my $function = $entry->{function};
450 output_function(\*OUT, $type, $ordinal, $external_name, $function);
458 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
459 my $entry = $specifications{$module}{unfixed}{$external_name};
460 my $ordinal = $entry->{ordinal};
461 my $function = $entry->{function};
462 output_function(\*OUT, $type, $ordinal, $external_name, $function);
471 if($options->stub_statistics) {
472 foreach my $winapi (@winapis) {
473 my $type = $winapi->name;
475 if($type eq "win16" && !$options->win16) { next; }
476 if($type eq "win32" && !$options->win32) { next; }
479 foreach my $module ($winapi->all_modules) {
480 foreach my $external_name ($winapi->all_functions_in_module($module)) {
481 my $external_calling_convention =
482 $winapi->function_external_calling_convention_in_module($module, $external_name);
483 if($external_calling_convention !~ /^forward|stub$/) {
484 if($module_pseudo_stub{$module}{$external_name}) {
485 $external_calling_convention = "pseudo_stub";
487 } elsif($external_calling_convention eq "forward") {
488 (my $forward_module, my $forward_external_name) =
489 $winapi->function_forward_final_destination($module, $external_name);
491 my $forward_external_calling_convention =
492 $winapi->function_external_calling_convention_in_module($forward_module, $forward_external_name);
494 if(!defined($forward_external_calling_convention)) {
498 if($forward_external_calling_convention ne "stub" &&
499 $module_pseudo_stub{$forward_module}{$forward_external_name})
501 $forward_external_calling_convention = "pseudo_stub";
504 $external_calling_convention = "forward_$forward_external_calling_convention";
507 $module_counts{$module}{$external_calling_convention}++;
511 foreach my $module ($winapi->all_modules) {
512 my $pseudo_stubs = $module_counts{$module}{pseudo_stub} || 0;
513 my $real_stubs = $module_counts{$module}{stub} || 0;
514 my $forward_pseudo_stubs = $module_counts{$module}{forward_pseudo_stub} || 0;
515 my $forward_real_stubs = $module_counts{$module}{forward_stub} || 0;
519 foreach my $calling_convention (keys(%{$module_counts{$module}})) {
520 my $count = $module_counts{$module}{$calling_convention};
521 if($calling_convention =~ /^forward/) {
528 my $stubs = $real_stubs + $pseudo_stubs;
530 $output->write("*.c: $module: ");
531 $output->write("$stubs of $total functions are stubs ($real_stubs real, $pseudo_stubs pseudo) " .
532 "and $forwards are forwards\n");
536 my $forward_stubs = $forward_real_stubs + $forward_pseudo_stubs;
538 $output->write("*.c: $module: ");
539 $output->write("$forward_stubs of $forwards forwarded functions are stubs " .
540 "($forward_real_stubs real, $forward_pseudo_stubs pseudo)\n");
546 if($options->winetest) {
547 foreach my $module ($win32api->all_modules) {
550 my $package = $module;
551 $package =~ s/\.dll$//;
552 $package =~ s/\./_/g;
556 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
557 my $entry = $specifications{$module}{unknown}{$external_name};
558 push @entries, $entry;
561 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
562 my $entry = $specifications{$module}{fixed}{$ordinal};
563 push @entries, $entry;
566 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
567 my $entry = $specifications{$module}{unfixed}{$external_name};
568 push @entries, $entry;
572 foreach my $entry (@entries) {
573 my $external_name = $entry->{external_name};
574 my $ordinal = $entry->{ordinal};
575 my $function = $entry->{function};
577 my $return_kind = $function->return_kind32 || "undef";
578 my $calling_convention = $function->calling_convention32 || "undef";
579 my $refargument_kinds = $function->argument_kinds32;
582 if(defined($refargument_kinds)) {
583 @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
586 next if $calling_convention ne "stdcall";
587 next if $external_name eq "\@";
590 open(OUT, "> $wine_dir/programs/winetest/include/${package}.pm");
592 print OUT "package ${package};\n";
595 print OUT "use strict;\n";
598 print OUT "require Exporter;\n";
601 print OUT "use wine;\n";
602 print OUT "use vars qw(\@ISA \@EXPORT \@EXPORT_OK);\n";
605 print OUT "\@ISA = qw(Exporter);\n";
606 print OUT "\@EXPORT = qw();\n";
607 print OUT "\@EXPORT_OK = qw();\n";
610 print OUT "my \$module_declarations = {\n";
615 print OUT " \"\Q$external_name\E\" => [\"$return_kind\", [";
617 foreach my $argument_kind (@argument_kinds) {
621 print OUT "\"$argument_kind\"";
632 print OUT "&wine::declare(\"$module\",\%\$module_declarations);\n";
633 print OUT "push \@EXPORT, map { \"&\" . \$_; } sort(keys(\%\$module_declarations));\n";