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;
47 use vars qw($win16api $win32api @winapis);
48 if ($options->spec_files || $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_stub {
165 my $function = shift;
167 my $statements = $function->statements;
168 if(defined($statements) && $statements =~ /FIXME[^;]*stub/s) {
169 if($options->win16) {
170 my $external_name16 = $function->external_name16;
171 foreach my $module16 ($function->modules16) {
172 $module_pseudo_stub{$module16}{$external_name16}++;
175 if($options->win32) {
176 my $external_name32 = $function->external_name32;
177 foreach my $module32 ($function->modules32) {
178 $module_pseudo_stub{$module32}{$external_name32}++;
185 if($options->headers) {
186 @h_files = $options->h_files;
187 @h_files = files_skip(@h_files);
188 @h_files = files_filter("winelib", @h_files);
192 if(1 || $options->spec_files || $options->pseudo_stub_statistics) {
193 @c_files = $options->c_files;
194 @c_files = files_skip(@c_files);
195 @c_files = files_filter("winelib", @c_files);
199 my $progress_current = 0;
200 my $progress_max = scalar(@h_files) + scalar(@c_files);
202 foreach my $file (@h_files, @c_files) {
217 while(s/^.*?\n//) { $max_line++; }
218 if($_) { $max_line++; }
222 if (!$options->old) {
223 $parser = new c_parser($file);
225 $parser = new winapi_c_parser($file);
231 my $update_output = sub {
235 $progress .= "$file (file $progress_current of $progress_max)";
238 if(defined($function)) {
239 my $name = $function->name;
240 my $begin_line = $function->begin_line;
241 my $begin_column = $function->begin_column;
243 $progress .= ": function $name";
244 $prefix .= "$begin_line.$begin_column: function $name: ";
250 $progress .= ": line $line of $max_line";
253 $output->progress($progress);
254 $output->prefix($prefix);
259 my $found_function = sub {
262 my $name = $function->name;
263 $functions{$name} = $function;
265 if ($function->statements) {
270 if($options->stub_statistics) {
271 $old_function = 'winapi_function'->new;
273 $old_function = 'function'->new;
276 $function->file($file);
277 $old_function->debug_channels([]); # FIXME: Not complete
279 $old_function->documentation_line(0); # FIXME: Not complete
280 $old_function->documentation(""); # FIXME: Not complete
282 $old_function->function_line($function->begin_line());
283 $old_function->linkage($function->linkage);
284 $old_function->return_type($function->return_type);
285 $old_function->calling_convention($function->calling_convention);
286 $old_function->internal_name($function->name);
287 if (defined($function->argument_types)) {
288 $old_function->argument_types([@{$function->argument_types}]);
290 if (defined($function->argument_names)) {
291 $old_function->argument_names([@{$function->argument_names}]);
293 $old_function->argument_documentations([]); # FIXME: Not complete
294 $old_function->statements_line($function->statements_line);
295 $old_function->statements($function->statements);
297 if($options->spec_files || $options->winetest) {
298 documentation_specifications($old_function);
301 if($options->stub_statistics) {
302 statements_stub($old_function);
305 if ($function->statements) {
312 $parser->set_found_function_callback($found_function);
314 my $found_line = sub {
319 $parser->set_found_line_callback($found_line);
321 my $found_type = sub {
326 my $kind = $type->kind;
327 my $_name = $type->_name;
328 my $name = $type->name;
330 foreach my $field ($type->fields) {
331 my $field_type_name = $field->type_name;
332 my $field_name = $field->name;
334 if ($options->struct) {
336 $output->write("$name:$field_type_name:$field_name\n");
338 $output->write("$kind $_name:$field_type_name:$field_name\n");
345 $parser->set_found_type_callback($found_type);
350 if(!$parser->parse_c_file(\$_, \$line, \$column)) {
351 $output->write("can't parse file\n");
358 sub output_function {
362 my $external_name = shift;
363 my $function = shift;
365 my $internal_name = $function->internal_name;
368 my $calling_convention;
369 my $refargument_kinds;
370 if($type eq "win16") {
371 $return_kind = $function->return_kind16 || "undef";
372 $calling_convention = $function->calling_convention16 || "undef";
373 $refargument_kinds = $function->argument_kinds16;
374 } elsif($type eq "win32") {
375 $return_kind = $function->return_kind32 || "undef";
376 $calling_convention = $function->calling_convention32 || "undef";
377 $refargument_kinds = $function->argument_kinds32;
380 if(defined($refargument_kinds)) {
381 my @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
382 print OUT "$ordinal $calling_convention $external_name(@argument_kinds) $internal_name\n";
384 print OUT "$ordinal $calling_convention $external_name() $internal_name # FIXME: arguments undefined\n";
388 if($options->spec_files) {
389 foreach my $winapi (@winapis) {
390 my $type = $winapi->name;
392 if($type eq "win16" && !$options->win16) { next; }
393 if($type eq "win32" && !$options->win32) { next; }
395 foreach my $module ($winapi->all_modules) {
396 my $spec_file = $module2spec_file{$module};
398 if(!defined($spec_file) || !defined($type)) {
399 $output->write("$module: doesn't exist\n");
405 $output->progress("$spec_file");
406 open(OUT, "> $wine_dir/$spec_file");
408 if(exists($specifications{$module}{init})) {
409 my $function = $specifications{$module}{init}{function};
410 print OUT "init " . $function->internal_name . "\n";
415 if(exists($specifications{$module}{init})) {
416 my $function = $specifications{$module}{init}{function};
417 foreach my $debug_channel (@{$function->debug_channels}) {
418 $debug_channels{$debug_channel}++;
421 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
422 my $function = $specifications{$module}{fixed}{$ordinal}{function};
423 foreach my $debug_channel (@{$function->debug_channels}) {
424 $debug_channels{$debug_channel}++;
427 foreach my $name (sort(keys(%{$specifications{$module}{unfixed}}))) {
428 my $function = $specifications{$module}{unfixed}{$name}{function};
429 foreach my $debug_channel (@{$function->debug_channels}) {
430 $debug_channels{$debug_channel}++;
433 foreach my $name (sort(keys(%{$specifications{$module}{unknown}}))) {
434 my $function = $specifications{$module}{unknown}{$name}{function};
435 foreach my $debug_channel (@{$function->debug_channels}) {
436 $debug_channels{$debug_channel}++;
440 my @debug_channels = sort(keys(%debug_channels));
441 if($#debug_channels >= 0) {
442 print OUT "debug_channels (" . join(" ", @debug_channels) . ")\n";
452 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
453 my $entry = $specifications{$module}{unknown}{$external_name};
454 my $ordinal = $entry->{ordinal};
455 my $function = $entry->{function};
457 output_function(\*OUT, $type, $ordinal, $external_name, $function);
465 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
466 my $entry = $specifications{$module}{fixed}{$ordinal};
467 my $external_name = $entry->{external_name};
468 my $function = $entry->{function};
469 output_function(\*OUT, $type, $ordinal, $external_name, $function);
477 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
478 my $entry = $specifications{$module}{unfixed}{$external_name};
479 my $ordinal = $entry->{ordinal};
480 my $function = $entry->{function};
481 output_function(\*OUT, $type, $ordinal, $external_name, $function);
490 if($options->stub_statistics) {
491 foreach my $winapi (@winapis) {
492 my $type = $winapi->name;
494 if($type eq "win16" && !$options->win16) { next; }
495 if($type eq "win32" && !$options->win32) { next; }
498 foreach my $module ($winapi->all_modules) {
499 foreach my $external_name ($winapi->all_functions_in_module($module)) {
500 my $external_calling_convention =
501 $winapi->function_external_calling_convention_in_module($module, $external_name);
502 if($external_calling_convention !~ /^forward|stub$/) {
503 if($module_pseudo_stub{$module}{$external_name}) {
504 $external_calling_convention = "pseudo_stub";
506 } elsif($external_calling_convention eq "forward") {
507 (my $forward_module, my $forward_external_name) =
508 $winapi->function_forward_final_destination($module, $external_name);
510 my $forward_external_calling_convention =
511 $winapi->function_external_calling_convention_in_module($forward_module, $forward_external_name);
513 if(!defined($forward_external_calling_convention)) {
517 if($forward_external_calling_convention ne "stub" &&
518 $module_pseudo_stub{$forward_module}{$forward_external_name})
520 $forward_external_calling_convention = "pseudo_stub";
523 $external_calling_convention = "forward_$forward_external_calling_convention";
526 $module_counts{$module}{$external_calling_convention}++;
530 foreach my $module ($winapi->all_modules) {
531 my $pseudo_stubs = $module_counts{$module}{pseudo_stub} || 0;
532 my $real_stubs = $module_counts{$module}{stub} || 0;
533 my $forward_pseudo_stubs = $module_counts{$module}{forward_pseudo_stub} || 0;
534 my $forward_real_stubs = $module_counts{$module}{forward_stub} || 0;
538 foreach my $calling_convention (keys(%{$module_counts{$module}})) {
539 my $count = $module_counts{$module}{$calling_convention};
540 if($calling_convention =~ /^forward/) {
547 my $stubs = $real_stubs + $pseudo_stubs;
549 $output->write("*.c: $module: ");
550 $output->write("$stubs of $total functions are stubs ($real_stubs real, $pseudo_stubs pseudo) " .
551 "and $forwards are forwards\n");
555 my $forward_stubs = $forward_real_stubs + $forward_pseudo_stubs;
557 $output->write("*.c: $module: ");
558 $output->write("$forward_stubs of $forwards forwarded functions are stubs " .
559 "($forward_real_stubs real, $forward_pseudo_stubs pseudo)\n");
565 if($options->winetest) {
566 foreach my $module ($win32api->all_modules) {
569 my $package = $module;
570 $package =~ s/\.dll$//;
571 $package =~ s/\./_/g;
575 foreach my $external_name (sort(keys(%{$specifications{$module}{unknown}}))) {
576 my $entry = $specifications{$module}{unknown}{$external_name};
577 push @entries, $entry;
580 foreach my $ordinal (sort {$a <=> $b} keys(%{$specifications{$module}{fixed}})) {
581 my $entry = $specifications{$module}{fixed}{$ordinal};
582 push @entries, $entry;
585 foreach my $external_name (sort(keys(%{$specifications{$module}{unfixed}}))) {
586 my $entry = $specifications{$module}{unfixed}{$external_name};
587 push @entries, $entry;
591 foreach my $entry (@entries) {
592 my $external_name = $entry->{external_name};
593 my $ordinal = $entry->{ordinal};
594 my $function = $entry->{function};
596 my $return_kind = $function->return_kind32 || "undef";
597 my $calling_convention = $function->calling_convention32 || "undef";
598 my $refargument_kinds = $function->argument_kinds32;
601 if(defined($refargument_kinds)) {
602 @argument_kinds = map { $_ || "undef"; } @$refargument_kinds;
605 next if $calling_convention ne "stdcall";
606 next if $external_name eq "\@";
609 open(OUT, "> $wine_dir/programs/winetest/include/${package}.pm");
611 print OUT "package ${package};\n";
614 print OUT "use strict;\n";
617 print OUT "require Exporter;\n";
620 print OUT "use wine;\n";
621 print OUT "use vars qw(\@ISA \@EXPORT \@EXPORT_OK);\n";
624 print OUT "\@ISA = qw(Exporter);\n";
625 print OUT "\@EXPORT = qw();\n";
626 print OUT "\@EXPORT_OK = qw();\n";
629 print OUT "my \$module_declarations = {\n";
634 print OUT " \"\Q$external_name\E\" => [\"$return_kind\", [";
636 foreach my $argument_kind (@argument_kinds) {
640 print OUT "\"$argument_kind\"";
651 print OUT "&wine::declare(\"$module\",\%\$module_declarations);\n";
652 print OUT "push \@EXPORT, map { \"&\" . \$_; } sort(keys(\%\$module_declarations));\n";