Use libwine and libwine_unicode directly from their build directory
[wine] / tools / winapi_check / winapi_options.pm
1 package winapi_options;
2
3 use strict;
4
5 sub parser_comma_list {
6     my $prefix = shift;
7     my $value = shift;
8     if(defined($prefix) && $prefix eq "no") {
9         return { active => 0, filter => 0, hash => {} };
10     } elsif(defined($value)) {
11         my %names;
12         for my $name (split /,/, $value) {
13             $names{$name} = 1;
14         }
15         return { active => 1, filter => 1, hash => \%names };
16     } else {
17         return { active => 1, filter => 0, hash => {} };
18     }
19 }
20
21 my %options = (
22     "debug" => { default => 0, description => "debug mode" },
23     "help" => { default => 0, description => "help mode" },
24     "verbose" => { default => 0, description => "verbose mode" },
25
26     "progress" => { default => 1, description => "show progress" },
27
28     "win16" => { default => 1, description => "Win16 checking" },
29     "win32" => { default => 1, description => "Win32 checking" },
30
31     "shared" =>  { default => 0, description => "show shared functions between Win16 and Win32" },
32     "shared-segmented" =>  { default => 0, description => "segmented shared functions between Win16 and Win32 checking" },
33
34     "config" => { default => 1, parent => "local", description => "check configuration include consistancy" },
35     "config-unnessary" => { default => 0, parent => "config", description => "check for unnessary #include \"config.h\"" },
36
37     "spec-mismatch" => { default => 0, description => "spec file mismatch checking" },
38
39     "local" =>  { default => 1, description => "local checking" },
40     "module" => { 
41         default => { active => 1, filter => 0, hash => {} },
42         parent => "local",
43         parser => \&parser_comma_list,
44         description => "module filter"
45     },
46
47     "argument" => { default => 1, parent => "local", description => "argument checking" },
48     "argument-count" => { default => 1, parent => "argument", description => "argument count checking" },
49     "argument-forbidden" => {
50         default => { active => 1, filter => 0, hash => {} },
51         parent => "argument",
52         parser => \&parser_comma_list,
53         description => "argument forbidden checking"
54     },
55     "argument-kind" => {
56         default => { active => 1, filter => 1, hash => { double => 1 } },
57         parent => "argument",
58         parser => \&parser_comma_list,
59         description => "argument kind checking"
60     },
61     "calling-convention" => { default => 1, parent => "local", description => "calling convention checking" },
62     "calling-convention-win16" => { default => 0, parent => "calling-convention", description => "calling convention checking (Win16)" },
63     "calling-convention-win32" => { default => 1, parent => "calling-convention", description => "calling convention checking (Win32)" },
64     "misplaced" => { default => 1, parent => "local", description => "check for misplaced functions" },
65     "statements"  => { default => 0, parent => "local", description => "check for statements inconsistances" },
66     "cross-call" => { default => 0, parent => "statements",  description => "check for cross calling functions" },
67     "cross-call-win32-win16" => { 
68         default => 0, parent => "cross-call", description => "check for cross calls between win32 and win16"
69      },
70     "cross-call-unicode-ascii" => { 
71         default => 0, parent => "cross-call", description => "check for cross calls between Unicode and ASCII" 
72      },
73     "debug-messages" => { default => 0, parent => "statements", description => "check for debug messages inconsistances" },
74     "documentation" => { default => 1, parent => "local", description => "check for documentation inconsistances\n" },
75     "documentation-width" => { default => 0, parent => "documentation", description => "check for documentation width inconsistances\n" },
76     "documentation-arguments" => { default => 0, parent => "documentation", description => "check for arguments documentation inconsistances\n" },
77     "prototype" => { default => 0, parent => ["local", "headers"], description => "prototype checking" },
78
79     "global" => { default => 1, description => "global checking" },
80     "declared" => { default => 1, parent => "global", description => "declared checking" },
81     "implemented" => { default => 1, parent => "local", description => "implemented checking" },
82     "implemented-win32" => { default => 0, parent => "implemented", description => "implemented as win32 checking" },
83     "include" => { default => 1, parent => "global", description => "include checking" },
84     "headers" => { default => 0, parent => "global", description => "headers checking" },
85     "headers-duplicated" => { default => 0, parent => "headers", description => "duplicated function declarations checking" },
86     "headers-misplaced" => { default => 0, parent => "headers", description => "misplaced function declarations checking" },
87     "stubs" => { default => 0, parent => "global", description => "stubs checking" }
88 );
89
90 my %short_options = (
91     "d" => "debug",
92     "?" => "help",
93     "v" => "verbose"
94 );
95
96 sub new {
97     my $proto = shift;
98     my $class = ref($proto) || $proto;
99     my $self  = {};
100     bless ($self, $class);
101
102     my $output = \${$self->{OUTPUT}};
103
104     $$output = shift;
105     my $refarguments = shift;
106     my $wine_dir = shift;
107
108     $self->options_set("default");
109
110     my $c_files = \@{$self->{C_FILES}};
111     my $h_files = \@{$self->{H_FILES}};
112     my $module = \${$self->{MODULE}};
113     my $global = \${$self->{GLOBAL}};
114
115     my @files;
116
117     if($wine_dir eq ".") {
118         $$global = 1;
119     } else {
120         $$global = 0;
121     }
122
123     while(defined($_ = shift @$refarguments)) {
124         if(/^--(all|none)$/) {
125             $self->options_set("$1");
126             next;
127         } elsif(/^-([^=]*)(=(.*))?$/) {
128             my $name;
129             my $value;
130             if(defined($2)) {
131                 $name = $1;
132                 $value = $3;
133             } else {
134                 $name = $1;
135             }
136             
137             if($name =~ /^([^-].*)$/) {
138                 $name = $short_options{$1};
139             } else {
140                 $name =~ s/^-(.*)$/$1/;
141             }
142                    
143             my $prefix;
144             if(defined($name) && $name =~ /^no-(.*)$/) {
145                 $name = $1;
146                 $prefix = "no";
147                 if(defined($value)) {
148                     $$output->write("options with prefix 'no' can't take parameters\n");
149
150                     return undef;
151                 }
152             }
153
154             my $option;
155             if(defined($name)) {
156                 $option = $options{$name};
157             }
158
159             if(defined($option)) {
160                 my $key = $$option{key};
161                 my $parser = $$option{parser};
162                 my $refvalue = \${$self->{$key}};
163                 my @parents = ();
164                 
165                 if(defined($$option{parent})) {
166                     if(ref($$option{parent}) eq "ARRAY") {
167                         @parents = @{$$option{parent}};
168                     } else {
169                         @parents = $$option{parent};
170                     }
171                 }
172
173                 if(defined($parser)) { 
174                     $$refvalue = &$parser($prefix,$value);
175                 } else {
176                     if(defined($value)) {
177                         $$refvalue = $value;
178                     } elsif(!defined($prefix)) {
179                         $$refvalue = 1;
180                     } else {
181                         $$refvalue = 0;
182                     }
183                 }
184
185                 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
186                     while($#parents >= 0) {
187                         my @old_parents = @parents;
188                         @parents = ();
189                         foreach my $parent (@old_parents) {
190                             my $parentkey = $options{$parent}{key};
191                             my $refparentvalue = \${$self->{$parentkey}};
192                             
193                             $$refparentvalue = 1;
194
195                             if(defined($options{$parent}{parent})) {
196                                 if(ref($options{$parent}{parent}) eq "ARRAY") {
197                                     push @parents, @{$options{$parent}{parent}};
198                                 } else {
199                                     push @parents, $options{$parent}{parent};
200                                 }
201                             }
202                         }
203                     }
204                 }
205                 next;
206             }    
207         }
208         
209         if(/^--module-dlls$/) {
210             my @dirs = `cd dlls && find . -type d ! -name CVS`;
211             my %names;
212             for my $dir (@dirs) {
213                 chomp $dir;
214                 $dir =~ s/^\.\/(.*)$/$1/;
215                 next if $dir eq "";
216                 $names{$dir} = 1;
217             }
218             $$module = { active => 1, filter => 1, hash => \%names };
219         }       
220         elsif(/^-(.*)$/) {
221             $$output->write("unknown option: $_\n"); 
222
223             return undef;
224         } else {
225             if(!-e $_) {
226                 $$output->write("$_: no such file or directory\n");
227
228                 return undef;
229             }
230
231             push @files, $_;
232         }
233     }
234
235     if($self->help) {
236         return $self;
237     }
238
239     my @paths = ();
240     my @c_files = ();
241     my @h_files = ();
242     foreach my $file (@files) {
243         if($file =~ /\.c$/) {
244             push @c_files, $file;
245         } elsif($file =~ /\.h$/) {
246             push @h_files, $file;
247         } else {
248             push @paths, $file;
249         }
250     }
251
252     if($#c_files == -1 && $#h_files == -1 &&
253        ($#paths == -1 || ($#paths == 0 && $paths[0] eq $wine_dir)))
254     {
255         @paths = ".";
256         push @h_files, "$wine_dir/include";
257     } else {
258         $$global = 0;
259     }
260
261     if($#paths != -1 || $#c_files != -1) {
262         my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
263         my %found;
264         @$c_files = sort(map {
265             s/^\.\/(.*)$/$1/;
266             if(defined($found{$_}) || /glue\.c|spec\.c$/) {
267                 ();
268             } else {
269                 $found{$_}++;
270                 $_;
271             }
272         } split(/\n/, `$c_command`));
273     }
274
275     if($#h_files != -1) {
276         my $h_command = "find " . join(" ", @h_files) . " -name \\*.h";
277         my %found;
278
279         @$h_files = sort(map {
280             s/^\.\/(.*)$/$1/;
281             if(defined($found{$_})) {
282                 ();
283             } else {
284                 $found{$_}++;
285                 $_;
286             }
287         } split(/\n/, `$h_command`));
288     }
289     return $self;
290 }
291
292 sub DESTROY {
293 }
294
295 sub options_set {
296     my $self = shift;
297
298     local $_ = shift;
299     for my $name (sort(keys(%options))) {
300         my $option = $options{$name};
301         my $key = uc($name);
302         $key =~ tr/-/_/;
303         $$option{key} = $key;
304         my $refvalue = \${$self->{$key}};
305
306         if(/^default$/) {
307             $$refvalue = $$option{default};
308         } elsif(/^all$/) {
309             if($name !~ /^help|debug|verbose|module$/) {
310                 if(ref($$refvalue) ne "HASH") {
311                     $$refvalue = 1;
312                 } else {
313                     $$refvalue = { active => 1, filter => 0, hash => {} };
314                 }
315             }
316         } elsif(/^none$/) {
317             if($name !~ /^help|debug|verbose|module$/) {
318                 if(ref($$refvalue) ne "HASH") {
319                     $$refvalue = 0;
320                 } else {
321                     $$refvalue = { active => 0, filter => 0, hash => {} };
322                 }
323             }
324         }
325     }
326 }
327
328 sub show_help {
329     my $self = shift;
330
331     my $maxname = 0;
332     for my $name (sort(keys(%options))) {
333         if(length($name) > $maxname) {
334             $maxname = length($name);
335         }
336     }
337
338     print "usage: winapi-check [--help] [<files>]\n";
339     print "\n";
340     for my $name (sort(keys(%options))) {
341         my $option = $options{$name};
342         my $description = $$option{description};
343         my $default = $$option{default};
344         my $current = ${$self->{$$option{key}}};
345
346         my $value = $current;
347         
348         my $output;
349         if(ref($value) ne "HASH") {
350             if($value) {
351                 $output = "--no-$name";
352             } else {
353                 $output = "--$name";
354             }
355         } else {
356             if($value->{active}) {
357                 $output = "--[no-]$name\[=<value>]";
358             } else {
359                 $output = "--$name\[=<value>]";
360             }
361         }
362
363         print "$output";
364         for (0..(($maxname - length($name) + 17) - (length($output) - length($name) + 1))) { print " "; }
365         if(ref($value) ne "HASH") {
366             if($value) {
367                 print "Disable ";
368             } else {
369                 print "Enable ";
370             }    
371         } else {
372             if($value->{active}) {
373                 print "(Disable) ";
374             } else {
375                 print "Enable ";
376             }
377         }
378         if($default == $current) {
379             print "$description (default)\n";
380         } else {
381             print "$description\n";
382         }    
383     }
384 }
385
386 sub AUTOLOAD {
387     my $self = shift;
388
389     my $name = $winapi_options::AUTOLOAD;
390     $name =~ s/^.*::(.[^:]*)$/\U$1/;
391
392     my $refvalue = $self->{$name};
393     if(!defined($refvalue)) {
394         die "<internal>: winapi_options.pm: member $name does not exists\n"; 
395     }
396
397     if(ref($$refvalue) ne "HASH") {
398         return $$refvalue;
399     } else {
400         return $$refvalue->{active};
401     }
402 }
403
404 sub c_files { my $self = shift; return @{$self->{C_FILES}}; }
405
406 sub h_files { my $self = shift; return @{$self->{H_FILES}}; }
407
408 sub report_module {
409     my $self = shift;
410     my $refvalue = $self->{MODULE};
411     
412     my $name = shift;
413
414     if(defined($name)) {
415         return $$refvalue->{active} && (!$$refvalue->{filter} || $$refvalue->{hash}->{$name}); 
416     } else {
417         return 0;
418     } 
419 }
420
421 sub report_argument_forbidden {
422     my $self = shift;   
423     my $refargument_forbidden = $self->{ARGUMENT_FORBIDDEN};
424
425     my $type = shift;
426
427     return $$refargument_forbidden->{active} && (!$$refargument_forbidden->{filter} || $$refargument_forbidden->{hash}->{$type}); 
428 }
429
430 sub report_argument_kind {
431     my $self = shift;
432     my $refargument_kind = $self->{ARGUMENT_KIND};
433
434     my $kind = shift;
435
436     return $$refargument_kind->{active} && (!$$refargument_kind->{filter} || $$refargument_kind->{hash}->{$kind}); 
437
438 }
439
440 1;
441