Add ffs() to porting library, needed by MinGW.
[wine.git] / tools / winapi_check / modules.pm
blob6f2b9906bfbe29e09b813d797a3f6d4c318711ef
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 package modules;
21 use strict;
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
24 require Exporter;
26 @ISA = qw(Exporter);
27 @EXPORT = qw();
28 @EXPORT_OK = qw($modules);
30 use vars qw($modules);
32 use config qw(
33 file_type files_skip
34 file_directory
35 get_c_files get_spec_files
36 $current_dir $wine_dir
37 $winapi_check_dir
39 use options qw($options);
40 use output qw($output);
42 sub import(@) {
43 $Exporter::ExportLevel++;
44 Exporter::import(@_);
45 $Exporter::ExportLevel--;
47 if (defined($modules)) {
48 return;
51 $modules = 'modules'->new;
54 sub get_spec_file_type($) {
55 my $file = shift;
57 my $module;
58 my $type;
60 $module = $file;
61 $module =~ s%^.*?([^/]+)\.spec$%$1%;
63 open(IN, "< $file") || die "$file: $!\n";
64 local $/ = "\n";
65 my $header = 1;
66 my $lookahead = 0;
67 while($lookahead || defined($_ = <IN>)) {
68 $lookahead = 0;
69 s/^\s*(.*?)\s*$/$1/;
70 s/^(.*?)\s*#.*$/$1/;
71 /^$/ && next;
73 if($header) {
74 if(/^\d+|@/) { $header = 0; $lookahead = 1; }
75 next;
78 if(/^(\d+|@)\s+pascal(?:16)?/) {
79 $type = "win16";
80 last;
83 close(IN);
85 if(!defined($type)) {
86 $type = "win32";
89 return ($type, $module);
92 sub find_spec_files($) {
93 my $self = shift;
95 my $dir2spec_file = \%{$self->{DIR2SPEC_FILE}};
96 my $spec_file2dir = \%{$self->{SPEC_FILE2DIR}};
98 $output->progress("modules");
100 my $spec_file_found = {};
101 my $allowed_dir;
102 my $spec_file;
104 my @spec_files = <{dlls/*/*.spec,dlls/*/*/*.spec}>;
106 foreach $spec_file (@spec_files) {
107 $spec_file =~ /(.*)\/.*\.spec/;
109 $allowed_dir = $1;
111 $$spec_file_found{$spec_file}++;
112 $$spec_file2dir{$spec_file}{$allowed_dir}++;
113 $$dir2spec_file{$allowed_dir}{$spec_file}++;
116 return $spec_file_found;
119 sub read_spec_files($$) {
120 my $self = shift;
122 my $spec_file_found = shift;
124 my $dir2spec_file = \%{$self->{DIR2SPEC_FILE}};
125 my $spec_files16 = \@{$self->{SPEC_FILES16}};
126 my $spec_files32 = \@{$self->{SPEC_FILES32}};
127 my $spec_file2module = \%{$self->{SPEC_FILE2MODULE}};
128 my $module2spec_file = \%{$self->{MODULE2SPEC_FILE}};
130 my @spec_files;
131 if($wine_dir eq ".") {
132 @spec_files = get_spec_files("winelib");
133 } else {
134 my %spec_files = ();
135 foreach my $dir ($options->directories) {
136 $dir = "$current_dir/$dir";
137 $dir =~ s%/\.$%%;
138 foreach my $spec_file (sort(keys(%{$$dir2spec_file{$dir}}))) {
139 $spec_files{$spec_file}++;
142 @spec_files = sort(keys(%spec_files));
145 @$spec_files16 = ();
146 @$spec_files32 = ();
147 foreach my $spec_file (@spec_files) {
148 (my $type, my $module) = get_spec_file_type("$wine_dir/$spec_file");
150 $$spec_file2module{$spec_file} = $module;
151 $$module2spec_file{$module} = $spec_file;
153 if($type eq "win16") {
154 push @$spec_files16, $spec_file;
155 } elsif($type eq "win32") {
156 push @$spec_files32, $spec_file;
157 } else {
158 $output->write("$spec_file: unknown type '$type'\n");
162 foreach my $spec_file (@spec_files) {
163 if(!$$spec_file_found{$spec_file} && $spec_file !~ m%tests/[^/]+$%) {
164 $output->write("modules: $spec_file: exists but is not specified\n");
169 sub new($) {
170 my $proto = shift;
171 my $class = ref($proto) || $proto;
172 my $self = {};
173 bless ($self, $class);
175 my $spec_file_found = $self->find_spec_files();
176 $self->read_spec_files($spec_file_found);
178 return $self;
181 sub all_modules($) {
182 my $self = shift;
184 my $module2spec_file = \%{$self->{MODULE2SPEC_FILE}};
186 return sort(keys(%$module2spec_file));
189 sub complete_modules($$) {
190 my $self = shift;
192 my $c_files = shift;
194 my %dirs;
196 foreach my $file (@$c_files) {
197 my $dir = file_directory("$current_dir/$file");
198 $dirs{$dir}++;
201 my @c_files = get_c_files("winelib");
202 @c_files = files_skip(@c_files);
203 foreach my $file (@c_files) {
204 my $dir = file_directory($file);
205 if(exists($dirs{$dir})) {
206 $dirs{$dir}--;
210 my @complete_modules = ();
211 foreach my $module ($self->all_modules) {
212 my $index = -1;
213 my @dirs = $self->allowed_dirs_for_module($module);
214 foreach my $dir (@dirs) {
215 if(exists($dirs{$dir}) && $dirs{$dir} == 0) {
216 $index++;
219 if($index == $#dirs) {
220 push @complete_modules, $module;
224 return @complete_modules;
227 sub is_allowed_module($$) {
228 my $self = shift;
230 my $module2spec_file = \%{$self->{MODULE2SPEC_FILE}};
232 my $module = shift;
234 return defined($$module2spec_file{$module});
237 sub is_allowed_module_in_file($$$) {
238 my $self = shift;
240 my $dir2spec_file = \%{$self->{DIR2SPEC_FILE}};
241 my $spec_file2module = \%{$self->{SPEC_FILE2MODULE}};
243 my $module = shift;
244 my $file = shift;
245 $file =~ s/^\.\///;
247 my $dir = $file;
248 $dir =~ s/\/[^\/]*$//;
250 if($dir =~ m%^include%) {
251 return 1;
254 foreach my $spec_file (sort(keys(%{$$dir2spec_file{$dir}}))) {
255 if($$spec_file2module{$spec_file} eq $module) {
256 return 1;
260 return 0;
263 sub allowed_modules_in_file($$) {
264 my $self = shift;
266 my $dir2spec_file = \%{$self->{DIR2SPEC_FILE}};
267 my $spec_file2module = \%{$self->{SPEC_FILE2MODULE}};
269 my $file = shift;
270 $file =~ s/^\.\///;
272 my $dir = $file;
273 $dir =~ s/\/[^\/]*$//;
275 my %allowed_modules = ();
276 foreach my $spec_file (sort(keys(%{$$dir2spec_file{$dir}}))) {
277 my $module = $$spec_file2module{$spec_file};
278 $allowed_modules{$module}++;
281 my $module = join(" & ", sort(keys(%allowed_modules)));
283 return $module;
286 sub allowed_dirs_for_module($$) {
287 my $self = shift;
289 my $module2spec_file = \%{$self->{MODULE2SPEC_FILE}};
290 my $spec_file2dir = \%{$self->{SPEC_FILE2DIR}};
292 my $module = shift;
294 my $spec_file = $$module2spec_file{$module};
296 return sort(keys(%{$$spec_file2dir{$spec_file}}));
299 sub allowed_spec_files16($) {
300 my $self = shift;
302 my $spec_files16 = \@{$self->{SPEC_FILES16}};
304 return @$spec_files16;
307 sub allowed_spec_files32($) {
308 my $self = shift;
310 my $spec_files32 = \@{$self->{SPEC_FILES32}};
312 return @$spec_files32;
315 sub found_module_in_dir($$$) {
316 my $self = shift;
318 my $module = shift;
319 my $dir = shift;
321 my $used_module_dirs = \%{$self->{USED_MODULE_DIRS}};
323 $dir = "$current_dir/$dir";
324 $dir =~ s%/\.$%%;
326 $$used_module_dirs{$module}{$dir}++;
329 sub global_report($) {
330 my $self = shift;
332 my $dir2spec_file = \%{$self->{DIR2SPEC_FILE}};
333 my $module2spec_file = \%{$self->{MODULE2SPEC_FILE}};
334 my $used_module_dirs = \%{$self->{USED_MODULE_DIRS}};
336 my @messages;
337 foreach my $dir ($options->directories) {
338 $dir = "$current_dir/$dir";
339 $dir =~ s%/\.$%%;
340 foreach my $module ($self->all_modules) {
341 if(!$$used_module_dirs{$module}{$dir}) {
342 my $spec_file = $$module2spec_file{$module};
343 push @messages, "modules: $spec_file: directory ($dir) is not used\n";
348 foreach my $message (sort(@messages)) {
349 $output->write($message);