Add a FIXME comment.
[gnulib.git] / check-module
1 #!/usr/bin/perl -w
2 # Read a module description file and derive the set of files
3 # included directly by any .c or .h file listed in the `Files:' section.
4 # Take the union of all such sets for any dependent modules.
5 # Then, compare that set with the set derived from the names
6 # listed in the various Files: sections.
7
8 # This script makes no attempt to diagnose invalid or empty
9 # module-description files.
10
11 # Written by Jim Meyering
12
13 # FIXME:
14 # for each .m4 file listed in the Files: section(s)
15 # parse it for AC_LIBSOURCES directives, and accumulate the set
16 # of files `required' via all AC_LIBSOURCES.
17 # If this set is not empty, ensure that it contains
18 # the same (.c and .h only?) files as are listed in the Files: sections.
19
20 use strict;
21 use Getopt::Long;
22 #use Coda;
23
24 (my $VERSION = '$Revision: 1.4 $ ') =~ tr/[0-9].//cd;
25 (my $ME = $0) =~ s|.*/||;
26
27 use constant ST_INIT => 1;
28 use constant ST_FILES => 2;
29 use constant ST_DEPENDENTS => 3;
30
31 # Parse a module file (returning list of Files: names and
32 # list of dependent-modules.
33 # my ($file, $dep) = parse_module_file $module_file;
34 sub parse_module_file ($)
35 {
36   my ($module_file) = @_;
37
38   open FH, '<', $module_file
39     or die "$ME: can't open `$module_file' for reading: $!\n";
40
41   my %file_set;
42   my %dep_set;
43
44   my $state = ST_INIT;
45   while (defined (my $line = <FH>))
46     {
47       if ($state eq ST_INIT)
48         {
49           if ($line =~ /^Files:$/)
50             {
51               $state = ST_FILES;
52             }
53           elsif ($line =~ /^Depends-on:$/)
54             {
55               $state = ST_DEPENDENTS;
56             }
57         }
58       else
59         {
60           chomp $line;
61           $line =~ s/^\s+//;
62           $line =~ s/\s+$//;
63           if ( ! $line)
64             {
65               $state = ST_INIT;
66               next;
67             }
68
69           if ($state eq ST_FILES)
70             {
71               $file_set{$line} = 1;
72             }
73           elsif ($state eq ST_DEPENDENTS)
74             {
75               $dep_set{$line} = 1;
76             }
77         }
78     }
79   close FH;
80
81   # my @t = sort keys %file_set;
82   # print "files: @t\n";
83   # my @u = sort keys %dep_set;
84   # print "dependents: @u\n";
85
86   return (\%file_set, \%dep_set);
87 }
88
89 # Extract the set of files required for this module, including
90 # those required via dependent modules.
91
92 # Files:
93 # lib/stat.c
94 # m4/stat.m4
95 # lib/foo.h
96 #
97 # Depends-on:
98 # some-other-module
99
100 sub usage ($)
101 {
102   my ($exit_code) = @_;
103   my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
104   if ($exit_code != 0)
105     {
106       print $STREAM "Try `$ME --help' for more information.\n";
107     }
108   else
109     {
110       print $STREAM <<EOF;
111 Usage: $ME [OPTIONS] FILE...
112
113 Read a module description file and derive the set of files
114 included directly by any .c or .h file listed in the `Files:' section.
115 Take the union of all such sets for any dependent modules.
116 Then, compare that set with the set derived from the names
117 listed in the various Files: sections.
118
119 OPTIONS:
120
121    --help             display this help and exit
122    --version          output version information and exit
123
124 EOF
125     }
126   exit $exit_code;
127 }
128
129 sub find_included_lib_files ($)
130 {
131   my ($file) = @_;
132
133   # Special cases...
134   my %special_non_dup = ( 'fnmatch_loop.c' => 1, 'regex.c' => 1 );
135
136   my %inc;
137   open FH, '<', $file
138     or die "$ME: can't open `$file' for reading: $!\n";
139
140   while (defined (my $line = <FH>))
141     {
142       # Ignore test-driver code at end of file.
143       $line =~ m!^\#if(def)? TEST_!
144         and last;
145
146       $line =~ m!^\s*\#\s*include\s+"!
147         or next;
148       $line =~ s///;
149       chomp $line;
150       $line =~ s/".*//;
151       exists $inc{$line} && ! exists $special_non_dup{$line}
152         and warn "$ME: $file: duplicate inclusion of $line\n";
153
154       $inc{$line} = 1;
155     }
156   close FH;
157
158   return \%inc;
159 }
160
161 my %exempt_header =
162   (
163    # Exempt headers like unlocked-io.h that are `#include'd
164    # but not necessarily used.
165    'unlocked-io.h' => 1,
166
167    # Give gettext.h a free pass only when included from lib/error.c,
168    # since we've made that exception solely to make the error
169    # module easier to use -- at RMS's request.
170    'lib/error.c:gettext.h' => 1,
171
172    # The full-read module shares code with the full-write module.
173    'lib/full-write.c:full-read.h' => 1,
174
175    # The safe-write module shares code with the safe-read module.
176    'lib/safe-read.c:safe-write.h' => 1,
177
178    # The use of obstack.h in the hash module is conditional, off by default.
179    'lib/hash.c:obstack.h' => 1,
180
181    # The fts-lgpl module doesn't actually use fts-cycle.c and unistd-safer.h.
182    'lib/fts.c:fts-cycle.c' => 1,
183    'lib/fts.c:unistd-safer.h' => 1,
184   );
185
186 sub check_module ($)
187 {
188   my @m = @_;
189
190   my %file;
191   my %module_all_files;
192   my %dep;
193   my %seen_module;
194
195   while (@m)
196     {
197       my $m = pop @m;
198       # warn "M: $m\n";
199       exists $seen_module{$m}
200         and next;
201       $seen_module{$m} = 1;
202       my ($file, $dep) = parse_module_file $m;
203       push @m, keys %$dep;
204       foreach my $f (keys %$file)
205         {
206           $module_all_files{$f} = 1;
207         }
208     }
209
210   my @t = sort keys %module_all_files;
211   # warn "ALL files: @t\n";
212
213   # Derive from %module_all_files (by parsing the .c and .h files therein),
214   # the list of all #include'd files that reside in lib/.
215   foreach my $f (keys %module_all_files)
216     {
217       $f =~ /\.[ch]$/
218         or next;
219       # FIXME: this is too naive
220       my $inc = find_included_lib_files "../$f";
221       foreach my $i (sort keys %$inc)
222         {
223           my $lib_file = "lib/$i";
224           exists $exempt_header{"$f:$i"}
225             || exists $exempt_header{$i}
226               and next;
227           !exists $module_all_files{$lib_file} && -f "../lib/$i"
228             and warn "$f: $i is `#include'd, but not "
229               . "listed in module's Files: section\n";
230         }
231       #my @t = sort keys %$inc;
232       #print "** $f: @t\n";
233     }
234 }
235
236 {
237   GetOptions
238     (
239      help => sub { usage 0 },
240      version => sub { print "$ME version $VERSION\n"; exit },
241     ) or usage 1;
242
243   @ARGV < 1
244     and (warn "$ME: missing FILE argument\n"), usage 1;
245
246   foreach my $module (@ARGV)
247     {
248       check_module $module;
249     }
250
251   exit 0;
252 }