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
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.
24 (my $VERSION = '$Revision: 1.4 $ ') =~ tr/[0-9].//cd;
25 (my $ME = $0) =~ s|.*/||;
27 use constant ST_INIT => 1;
28 use constant ST_FILES => 2;
29 use constant ST_DEPENDENTS => 3;
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 ($)
36 my ($module_file) = @_;
38 open FH, '<', $module_file
39 or die "$ME: can't open `$module_file' for reading: $!\n";
45 while (defined (my $line = <FH>))
47 if ($state eq ST_INIT)
49 if ($line =~ /^Files:$/)
53 elsif ($line =~ /^Depends-on:$/)
55 $state = ST_DEPENDENTS;
69 if ($state eq ST_FILES)
73 elsif ($state eq ST_DEPENDENTS)
81 # my @t = sort keys %file_set;
82 # print "files: @t\n";
83 # my @u = sort keys %dep_set;
84 # print "dependents: @u\n";
86 return (\%file_set, \%dep_set);
89 # Extract the set of files required for this module, including
90 # those required via dependent modules.
102 my ($exit_code) = @_;
103 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
106 print $STREAM "Try `$ME --help' for more information.\n";
111 Usage: $ME [OPTIONS] FILE...
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.
121 --help display this help and exit
122 --version output version information and exit
129 sub find_included_lib_files ($)
134 my %special_non_dup = ( 'fnmatch_loop.c' => 1, 'regex.c' => 1 );
138 or die "$ME: can't open `$file' for reading: $!\n";
140 while (defined (my $line = <FH>))
142 # Ignore test-driver code at end of file.
143 $line =~ m!^\#if(def)? TEST_!
146 $line =~ m!^\s*\#\s*include\s+"!
151 exists $inc{$line} && ! exists $special_non_dup{$line}
152 and warn "$ME: $file: duplicate inclusion of $line\n";
163 # Exempt headers like unlocked-io.h that are `#include'd
164 # but not necessarily used.
165 'unlocked-io.h' => 1,
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,
172 # The full-read module shares code with the full-write module.
173 'lib/full-write.c:full-read.h' => 1,
175 # The safe-write module shares code with the safe-read module.
176 'lib/safe-read.c:safe-write.h' => 1,
178 # The use of obstack.h in the hash module is conditional, off by default.
179 'lib/hash.c:obstack.h' => 1,
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,
191 my %module_all_files;
199 exists $seen_module{$m}
201 $seen_module{$m} = 1;
202 my ($file, $dep) = parse_module_file $m;
204 foreach my $f (keys %$file)
206 $module_all_files{$f} = 1;
210 my @t = sort keys %module_all_files;
211 # warn "ALL files: @t\n";
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)
219 # FIXME: this is too naive
220 my $inc = find_included_lib_files "../$f";
221 foreach my $i (sort keys %$inc)
223 my $lib_file = "lib/$i";
224 exists $exempt_header{"$f:$i"}
225 || exists $exempt_header{$i}
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";
231 #my @t = sort keys %$inc;
232 #print "** $f: @t\n";
239 help => sub { usage 0 },
240 version => sub { print "$ME version $VERSION\n"; exit },
244 and (warn "$ME: missing FILE argument\n"), usage 1;
246 foreach my $module (@ARGV)
248 check_module $module;