3 # Copyright 2002 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;
47 use vars qw($win16api $win32api @winapis);
48 if ($options->spec_files || $options->implemented || $options->stub_statistics || $options->winetest) {
50 import winapi qw($win16api $win32api @winapis);
55 if($options->spec_files || $options->winetest) {
58 foreach my $spec_file (get_spec_files("winelib")) {
61 my $module = $spec_file;
62 $module =~ s/^.*?([^\/]*)\.spec$/$1/;
66 open(IN, "< $wine_dir/$spec_file");
70 while($lookahead || defined($_ = <IN>)) {
73 s/^\s*?(.*?)\s*$/$1/; # remove whitespace at begining and end of line
74 s/^(.*?)\s*#.*$/$1/; # remove comments
75 /^$/ && next; # skip empty lines
85 if(/^(@|\d+)\s+stdcall\s+(\w+)\s*\(\s*([^\)]*)\s*\)/) {
88 my @args = split(/\s+/, $3);
90 push @$entries, [$name, "undef", \@args];
95 $module2spec_file{$module} = $spec_file;
96 $module2entries{$module} = $entries;
102 sub documentation_specifications {
103 my $function = shift;
105 my @debug_channels = @{$function->debug_channels};
106 my $documentation = $function->documentation;
107 my $documentation_line = $function->documentation_line;
108 my $return_type = $function->return_type;
109 my $linkage = $function->linkage;
110 my $internal_name = $function->internal_name;
112 if($linkage eq "static") {
117 foreach (split(/\n/, $documentation)) {
118 if(/^\s*\*\s*(\S+)\s*[\(\[]\s*(\w+)\s*\.\s*(\S+)\s*[\)\]]/) {
119 my $external_name = $1;
123 if($ordinal eq "@") {
124 if(1 || !exists($specifications{$module}{unfixed}{$external_name})) {
125 $specifications{$module}{unfixed}{$external_name}{ordinal} = $ordinal;
126 $specifications{$module}{unfixed}{$external_name}{external_name} = $external_name;
127 $specifications{$module}{unfixed}{$external_name}{function} = $function;
129 $output->write("$external_name ($module.$ordinal) already exists\n");
131 } elsif($ordinal =~ /^\d+$/) {
132 if(1 || !exists($specifications{$module}{fixed}{$ordinal})) {
133 $specifications{$module}{fixed}{$ordinal}{ordinal} = $ordinal;
134 $specifications{$module}{fixed}{$ordinal}{external_name} = $external_name;
135 $specifications{$module}{fixed}{$ordinal}{function} = $function;
137 $output->write("$external_name ($module.$ordinal) already exists\n");
139 } elsif($ordinal eq "init") {
140 if(!exists($specifications{$module}{init})) {
141 $specifications{$module}{init}{function} = $function;
143 $output->write("$external_name ($module.$ordinal) already exists\n");
146 if(!exists($specifications{$module}{unknown}{$external_name})) {
147 $specifications{$module}{unknown}{$external_name}{ordinal} = $ordinal;
148 $specifications{$module}{unknown}{$external_name}{external_name} = $external_name;
149 $specifications{$module}{unknown}{$external_name}{function} = $function;
151 $output->write("$external_name ($module.$ordinal) already exists\n");
155 if($options->debug) {
156 $output->write("$external_name ($module.$ordinal)\n");
162 my %module_pseudo_stub;
164 sub statements_pseudo_stub {
165 my $function = shift;
168 my $statements = $function->statements;
169 if(defined($statements) && $statements =~ /FIXME[^;]*stub/s) {
170 if($options->win16) {
171 my $external_name16 = $function->external_name16;
172 foreach my $module16 ($function->modules16) {
173 $module_pseudo_stub{$module16}{$external_name16}++;
177 if($options->win32) {
178 my $external_name32 = $function->external_name32;
179 foreach my $module32 ($function->modules32) {
180 $module_pseudo_stub{$module32}{$external_name32}++;
190 if($options->headers) {
191 @h_files = $options->h_files;
192 @h_files = files_skip(@h_files);
193 @h_files = files_filter("winelib", @h_files);
197 if($options->spec_files || $options->pseudo_implemented || $options->pseudo_stub_statistics) {
198 @c_files = $options->c_files;
199 @c_files = files_skip(@c_files);
200 @c_files = files_filter("winelib", @c_files);
204 my $progress_current = 0;
205 my $progress_max = scalar(@h_files) + scalar(@c_files);
207 foreach my $file (@h_files, @c_files) {
222 while(s/^.*?\n//) { $max_line++; }
223 if($_) { $max_line++; }
227 if (!$options->old) {
228 $parser = new c_parser($file);
230 $parser = new winapi_c_parser($file);
236 my $update_output = sub {
240 $progress .= "$file (file $progress_current of $progress_max)";
243 if(defined($function)) {
244 my $name = $function->name;
245 my $begin_line = $function->begin_line;
246 my $begin_column = $function->begin_column;
248 $progress .= ": function $name";
249 $prefix .= "$begin_line.$begin_column: function $name: ";
255 $progress .= ": line $line of $max_line";
258 $output->progress($progress);
259 $output->prefix($prefix);
264 my $found_function = sub {
267 my $name = $function->name;
268 $functions{$name} = $function;
270 if ($function->statements) {
275 if($options->implemented || $options->stub_statistics) {
276 $old_function = 'winapi_function'->new;
278 $old_function = 'function'->new;
281 $old_function->file($function->file);
282 $old_function->debug_channels([]); # FIXME: Not complete
284 $old_function->documentation_line(0); # FIXME: Not complete
285 $old_function->documentation(""); # FIXME: Not complete
287 $old_function->function_line($function->begin_line());
288 $old_function->linkage($function->linkage);
289 $old_function->return_type($function->return_type);
290 $old_function->calling_convention($function->calling_convention);
291 $old_function->internal_name($function->name);
292 if (defined($function->argument_types)) {
293 $old_function->argument_types([@{$function->argument_types}]);
295 if (defined($function->argument_names)) {
296 $old_function->argument_names([@{$function->argument_names}]);
298 $old_function->argument_documentations([]); # FIXME: Not complete
299 $old_function->statements_line($function->statements_line);
300 $old_function->statements($function->statements);
302 if($options->spec_files || $options->winetest) {
303 documentation_specifications($old_function);
306 if ($function->statements) {
314 if ($options->pseudo_implemented || $options->pseudo_stub_statistics) {
315 $pseudo_stub = statements_pseudo_stub($old_function);
318 my $module = $old_function->module;
319 my $external_name = $old_function->external_name;
320 my $statements = $old_function->statements;
321 if ($options->pseudo_implemented && $module && $external_name && $statements) {
322 my @external_names = split(/\s*&\s*/, $external_name);
323 my @modules = split(/\s*&\s*/, $module);
326 while(defined(my $external_name = shift @external_names) &&
327 defined(my $module = shift @modules))
330 $output->write("$module.$external_name: pseudo implemented\n");
332 $output->write("$module.$external_name: implemented\n");
337 $parser->set_found_function_callback($found_function);
339 my $found_line = sub {
344 $parser->set_found_line_callback($found_line);
346 my $found_type = sub {
351 my $kind = $type->kind;
352 my $_name = $type->_name;
353 my $name = $type->name;
355 foreach my $field ($type->fields) {
356 my $field_type_name = $field->type_name;
357 my $field_name = $field->name;
359 if ($options->struct && $kind =~ /^(?:struct|union)$/) {
361 $output->write("$name:$field_type_name:$field_name\n");
363 $output->write("$kind $_name:$field_type_name:$field_name\n");
370 $parser->set_found_type_callback($found_type);
375 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
376 $output->write("can't parse file\n");
384 if($options->implemented && !$options->pseudo_implemented) {
385 foreach my $winapi (@winapis) {
386 my $type = $winapi->name;
388 if($type eq "win16" && !$options->win16) { next; }
389 if($type eq "win32" && !$options->win32) { next; }
391 foreach my $module ($winapi->all_modules) {
392 foreach my $external_name ($winapi->all_functions_in_module($module)) {
393 my $external_calling_convention =
394 $winapi->function_external_calling_convention_in_module($module, $external_name);
396 if($external_calling_convention eq "forward") {
397 (my $forward_module, my $forward_external_name) =
398 $winapi->function_forward_final_destination($module, $external_name);
400 my $forward_external_calling_convention =
401 $winapi->function_external_calling_convention_in_module($forward_module, $forward_external_name);
403 if(!defined($forward_external_calling_convention)) {
407 $external_calling_convention = $forward_external_calling_convention;
410 if ($external_calling_convention ne "stub") {
411 $output->write("*.spec: $module.$external_name: implemented\n");
418 sub output_function {
422 my $external_name = shift;
423 my $function = shift;
425 my $internal_name = $function->internal_name;
428 my $calling_convention;
429 my $refargument_kinds;
430 if($type eq "win16") {
431 $return_kind = $function->return_kind16 || "undef";
432 $calling_convention = $function->calling_convention16 || "undef";
433 $refargument_kinds = $function->argument_kinds16;
434 } elsif($type eq "win32") {
435 $return_kind = $function->return_kind32 || "undef";
436 $calling_convention = $function->calling_convention32 || "undef";
437 $refargument_kinds = $function->argument_kinds32;
440 if(defined($refargument_kinds)) {
441 my @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
442 print OUT "$ordinal $calling_convention $external_name(@argument_kinds) $internal_name\n";
444 print OUT "$ordinal $calling_convention $external_name() $internal_name # FIXME: arguments undefined\n";
448 if($options->spec_files) {
449 foreach my $winapi (@winapis) {
450 my $type = $winapi->name;
452 if($type eq "win16" && !$options->win16) { next; }
453 if($type eq "win32" && !$options->win32) { next; }
455 foreach my $module ($winapi->all_modules) {
456 my $spec_file = $module2spec_file{$module};
458 if(!defined($spec_file) || !defined($type)) {
459 $output->write("$module: doesn't exist\n");
465 $output->progress("$spec_file");
466 open(OUT, "> $wine_dir/$spec_file");
468 if(exists($specifications{$module}{init})) {
469 my $function = $specifications{$module}{init}{function};
470 print OUT "init " . $function->internal_name . "\n";
475 if(exists($specifications{$module}{init})) {
476 my $function = $specifications{$module}{init}{function};
477 foreach my $debug_channel (@{$function->debug_channels}) {
478 $debug_channels{$debug_channel}++;
481 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
482 my $function = $specifications{$module}{fixed}{$ordinal}{function};
483 foreach my $debug_channel (@{$function->debug_channels}) {
484 $debug_channels{$debug_channel}++;
487 foreach my $name (sort(keys(%{$specifications{$module}{unfixed}}))) {
488 my $function = $specifications{$module}{unfixed}{$name}{function};
489 foreach my $debug_channel (@{$function->debug_channels}) {
490 $debug_channels{$debug_channel}++;
493 foreach my $name (sort(keys(%{$specifications{$module}{unknown}}))) {
494 my $function = $specifications{$module}{unknown}{$name}{function};
495 foreach my $debug_channel (@{$function->debug_channels}) {
496 $debug_channels{$debug_channel}++;
500 my @debug_channels = sort(keys(%debug_channels));
501 if($#debug_channels >= 0) {
502 print OUT "debug_channels (" . join(" ", @debug_channels) . ")\n";
512 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
513 my $entry = $specifications{$module}{unknown}{$external_name};
514 my $ordinal = $entry->{ordinal};
515 my $function = $entry->{function};
517 output_function(\*OUT, $type, $ordinal, $external_name, $function);
525 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
526 my $entry = $specifications{$module}{fixed}{$ordinal};
527 my $external_name = $entry->{external_name};
528 my $function = $entry->{function};
529 output_function(\*OUT, $type, $ordinal, $external_name, $function);
537 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
538 my $entry = $specifications{$module}{unfixed}{$external_name};
539 my $ordinal = $entry->{ordinal};
540 my $function = $entry->{function};
541 output_function(\*OUT, $type, $ordinal, $external_name, $function);
550 if($options->stub_statistics) {
551 foreach my $winapi (@winapis) {
552 my $type = $winapi->name;
554 if($type eq "win16" && !$options->win16) { next; }
555 if($type eq "win32" && !$options->win32) { next; }
558 foreach my $module ($winapi->all_modules) {
559 foreach my $external_name ($winapi->all_functions_in_module($module)) {
560 my $external_calling_convention =
561 $winapi->function_external_calling_convention_in_module($module, $external_name);
562 if($external_calling_convention !~ /^forward|stub$/) {
563 if($module_pseudo_stub{$module}{$external_name}) {
564 $external_calling_convention = "pseudo_stub";
566 } elsif($external_calling_convention eq "forward") {
567 (my $forward_module, my $forward_external_name) =
568 $winapi->function_forward_final_destination($module, $external_name);
570 my $forward_external_calling_convention =
571 $winapi->function_external_calling_convention_in_module($forward_module, $forward_external_name);
573 if(!defined($forward_external_calling_convention)) {
577 if($forward_external_calling_convention ne "stub" &&
578 $module_pseudo_stub{$forward_module}{$forward_external_name})
580 $forward_external_calling_convention = "pseudo_stub";
583 $external_calling_convention = "forward_$forward_external_calling_convention";
586 $module_counts{$module}{$external_calling_convention}++;
590 foreach my $module ($winapi->all_modules) {
591 my $pseudo_stubs = $module_counts{$module}{pseudo_stub} || 0;
592 my $real_stubs = $module_counts{$module}{stub} || 0;
593 my $forward_pseudo_stubs = $module_counts{$module}{forward_pseudo_stub} || 0;
594 my $forward_real_stubs = $module_counts{$module}{forward_stub} || 0;
598 foreach my $calling_convention (keys(%{$module_counts{$module}})) {
599 my $count = $module_counts{$module}{$calling_convention};
600 if($calling_convention =~ /^forward/) {
607 my $stubs = $real_stubs + $pseudo_stubs;
609 $output->write("*.c: $module: ");
610 $output->write("$stubs of $total functions are stubs ($real_stubs real, $pseudo_stubs pseudo) " .
611 "and $forwards are forwards\n");
615 my $forward_stubs = $forward_real_stubs + $forward_pseudo_stubs;
617 $output->write("*.c: $module: ");
618 $output->write("$forward_stubs of $forwards forwarded functions are stubs " .
619 "($forward_real_stubs real, $forward_pseudo_stubs pseudo)\n");
625 if($options->winetest) {
626 foreach my $module ($win32api->all_modules) {
629 my $package = $module;
630 $package =~ s/\.dll$//;
631 $package =~ s/\./_/g;
635 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
636 my $entry = $specifications{$module}{unknown}{$external_name};
637 push @entries, $entry;
640 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
641 my $entry = $specifications{$module}{fixed}{$ordinal};
642 push @entries, $entry;
645 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
646 my $entry = $specifications{$module}{unfixed}{$external_name};
647 push @entries, $entry;
651 foreach my $entry (@entries) {
652 my $external_name = $entry->{external_name};
653 my $ordinal = $entry->{ordinal};
654 my $function = $entry->{function};
656 my $return_kind = $function->return_kind32 || "undef";
657 my $calling_convention = $function->calling_convention32 || "undef";
658 my $refargument_kinds = $function->argument_kinds32;
661 if(defined($refargument_kinds)) {
662 @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
665 next if $calling_convention ne "stdcall";
666 next if $external_name eq "\@";
669 open(OUT, "> $wine_dir/programs/winetest/include/${package}.pm");
671 print OUT "package ${package};\n";
674 print OUT "use strict;\n";
677 print OUT "require Exporter;\n";
680 print OUT "use wine;\n";
681 print OUT "use vars qw(\@ISA \@EXPORT \@EXPORT_OK);\n";
684 print OUT "\@ISA = qw(Exporter);\n";
685 print OUT "\@EXPORT = qw();\n";
686 print OUT "\@EXPORT_OK = qw();\n";
689 print OUT "my \$module_declarations = {\n";
694 print OUT " \"\Q$external_name\E\" => [\"$return_kind\", [";
696 foreach my $argument_kind (@argument_kinds) {
700 print OUT "\"$argument_kind\"";
711 print OUT "&wine::declare(\"$module\",\%\$module_declarations);\n";
712 print OUT "push \@EXPORT, map { \"&\" . \$_; } sort(keys(\%\$module_declarations));\n";