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.
8 # This script makes no attempt to diagnose invalid or empty
9 # module-description files.
11 # Written by Jim Meyering
17 (my $VERSION = '$Revision: 1.3 $ ') =~ tr/[0-9].//cd;
18 (my $ME = $0) =~ s|.*/||;
20 use constant ST_INIT => 1;
21 use constant ST_FILES => 2;
22 use constant ST_DEPENDENTS => 3;
24 # Parse a module file (returning list of Files: names and
25 # list of dependent-modules.
26 # my ($file, $dep) = parse_module_file $module_file;
27 sub parse_module_file ($)
29 my ($module_file) = @_;
31 open FH, '<', $module_file
32 or die "$ME: can't open `$module_file' for reading: $!\n";
38 while (defined (my $line = <FH>))
40 if ($state eq ST_INIT)
42 if ($line =~ /^Files:$/)
46 elsif ($line =~ /^Depends-on:$/)
48 $state = ST_DEPENDENTS;
62 if ($state eq ST_FILES)
66 elsif ($state eq ST_DEPENDENTS)
74 # my @t = sort keys %file_set;
75 # print "files: @t\n";
76 # my @u = sort keys %dep_set;
77 # print "dependents: @u\n";
79 return (\%file_set, \%dep_set);
82 # Extract the set of files required for this module, including
83 # those required via dependent modules.
96 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
99 print $STREAM "Try `$ME --help' for more information.\n";
104 Usage: $ME [OPTIONS] FILE...
106 Read a module description file and derive the set of files
107 included directly by any .c or .h file listed in the `Files:' section.
108 Take the union of all such sets for any dependent modules.
109 Then, compare that set with the set derived from the names
110 listed in the various Files: sections.
114 --help display this help and exit
115 --version output version information and exit
122 sub find_included_lib_files ($)
127 my %special_non_dup = ( 'fnmatch_loop.c' => 1, 'regex.c' => 1 );
131 or die "$ME: can't open `$file' for reading: $!\n";
133 while (defined (my $line = <FH>))
135 # Ignore test-driver code at end of file.
136 $line =~ m!^\#if(def)? TEST_!
139 $line =~ m!^\s*\#\s*include\s+"!
144 exists $inc{$line} && ! exists $special_non_dup{$line}
145 and warn "$ME: $file: duplicate inclusion of $line\n";
156 # Exempt headers like unlocked-io.h that are `#include'd
157 # but not necessarily used.
158 'unlocked-io.h' => 1,
160 # Give gettext.h a free pass only when included from lib/error.c,
161 # since we've made that exception solely to make the error
162 # module easier to use -- at RMS's request.
163 'lib/error.c:gettext.h' => 1,
165 # The full-read module shares code with the full-write module.
166 'lib/full-write.c:full-read.h' => 1,
168 # The safe-write module shares code with the safe-read module.
169 'lib/safe-read.c:safe-write.h' => 1,
171 # The use of obstack.h in the hash module is conditional, off by default.
172 'lib/hash.c:obstack.h' => 1,
174 # The fts-lgpl module doesn't actually use fts-cycle.c and unistd-safer.h.
175 'lib/fts.c:fts-cycle.c' => 1,
176 'lib/fts.c:unistd-safer.h' => 1,
184 my %module_all_files;
192 exists $seen_module{$m}
194 $seen_module{$m} = 1;
195 my ($file, $dep) = parse_module_file $m;
197 foreach my $f (keys %$file)
199 $module_all_files{$f} = 1;
203 my @t = sort keys %module_all_files;
204 # warn "ALL files: @t\n";
206 # Derive from %module_all_files (by parsing the .c and .h files therein),
207 # the list of all #include'd files that reside in lib/.
208 foreach my $f (keys %module_all_files)
212 # FIXME: this is too naive
213 my $inc = find_included_lib_files "../$f";
214 foreach my $i (sort keys %$inc)
216 my $lib_file = "lib/$i";
217 exists $exempt_header{"$f:$i"}
218 || exists $exempt_header{$i}
220 !exists $module_all_files{$lib_file} && -f "../lib/$i"
221 and warn "$f: $i is `#include'd, but not "
222 . "listed in module's Files: section\n";
224 #my @t = sort keys %$inc;
225 #print "** $f: @t\n";
232 help => sub { usage 0 },
233 version => sub { print "$ME version $VERSION\n"; exit },
237 and (warn "$ME: missing FILE argument\n"), usage 1;
239 foreach my $module (@ARGV)
241 check_module $module;