pty: Activate the signature wrapper of forkpty.
[gnulib.git] / check-module
1 #!/usr/bin/perl -w
2 # Check a gnulib module.
3
4 # Copyright (C) 2005-2007, 2009-2013 Free Software Foundation, Inc.
5
6 # This file is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 # Read a module description file and derive the set of files
21 # included directly by any .c or .h file listed in the 'Files:' section.
22 # Take the union of all such sets for any dependent modules.
23 # Then, compare that set with the set derived from the names
24 # listed in the various Files: sections.
25
26 # This script makes no attempt to diagnose invalid or empty
27 # module-description files.
28
29 # Written by Jim Meyering
30
31 # FIXME:
32 # for each .m4 file listed in the Files: section(s)
33 # parse it for AC_LIBSOURCES directives, and accumulate the set
34 # of files "required" via all AC_LIBSOURCES.
35 # If this set is not empty, ensure that it contains
36 # the same (.c and .h only?) files as are listed in the Files: sections.
37
38 use strict;
39 use Getopt::Long;
40 use File::Basename;
41 #use Coda;
42
43 my $COPYRIGHT_NOTICE = "Copyright (C) 2006 Free Software Foundation, Inc.\n".
44 "This is free software.  You may redistribute copies of it under the terms of\n".
45 "the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n".
46 "There is NO WARRANTY, to the extent permitted by law.\n";
47
48 (my $VERSION = '$Revision: 1.8 $ ') =~ tr/[0-9].//cd;
49 (my $ME = $0) =~ s|.*/||;
50
51 use constant ST_INIT => 1;
52 use constant ST_FILES => 2;
53 use constant ST_DEPENDENTS => 3;
54
55 # Parse a module file (returning list of Files: names and
56 # list of dependent-modules.
57 # my ($file, $dep) = parse_module_file $module_file;
58 sub parse_module_file ($)
59 {
60   my ($module_file) = @_;
61
62   open FH, '<', $module_file
63     or die "$ME: can't open '$module_file' for reading: $!\n";
64
65   my %file_set;
66   my %dep_set;
67
68   my $state = ST_INIT;
69   while (defined (my $line = <FH>))
70     {
71       if ($state eq ST_INIT)
72         {
73           if ($line =~ /^Files:$/)
74             {
75               $state = ST_FILES;
76             }
77           elsif ($line =~ /^Depends-on:$/)
78             {
79               $state = ST_DEPENDENTS;
80             }
81         }
82       else
83         {
84           chomp $line;
85           $line =~ s/^\s+//;
86           $line =~ s/\s+$//;
87           if ( ! $line)
88             {
89               $state = ST_INIT;
90               next;
91             }
92
93           if ($state eq ST_FILES)
94             {
95               $file_set{$line} = 1;
96             }
97           elsif ($state eq ST_DEPENDENTS)
98             {
99               $dep_set{$line} = 1;
100               (my $base = $module_file) =~ s,.*/,,;
101               $line eq $base
102                 and die "$ME: module $module_file depends on itself\n";
103             }
104         }
105     }
106   close FH;
107
108   # my @t = sort keys %file_set;
109   # print "files: @t\n";
110   # my @u = sort keys %dep_set;
111   # print "dependents: @u\n";
112
113   return (\%file_set, \%dep_set);
114 }
115
116 # Extract the set of files required for this module, including
117 # those required via dependent modules.
118
119 # Files:
120 # lib/stat.c
121 # m4/stat.m4
122 # lib/foo.h
123 #
124 # Depends-on:
125 # some-other-module
126
127 sub usage ($)
128 {
129   my ($exit_code) = @_;
130   my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
131   if ($exit_code != 0)
132     {
133       print $STREAM "Try '$ME --help' for more information.\n";
134     }
135   else
136     {
137       print $STREAM <<EOF;
138 Usage: $ME [OPTIONS] FILE...
139
140 Read a module description file and derive the set of files
141 included directly by any .c or .h file listed in the 'Files:' section.
142 Take the union of all such sets for any dependent modules.
143 Then, compare that set with the set derived from the names
144 listed in the various Files: sections.
145
146 OPTIONS:
147
148    --help             display this help and exit
149    --version          output version information and exit
150
151 EOF
152     }
153   exit $exit_code;
154 }
155
156 sub find_included_lib_files ($)
157 {
158   my ($file) = @_;
159
160   # Special cases...
161   my %special_non_dup = ( 'fnmatch_loop.c' => 1,
162                           'regex.c' => 1, 'at-func.c' => 1,
163                           'vasnprintf.c' => 1
164                         );
165   my %dup_include_ok;
166   $dup_include_ok{'vasnprintf.c'}{'isnand-nolibm.h'} = 1;
167   $dup_include_ok{'vasnprintf.c'}{'isnanl-nolibm.h'} = 1;
168   $dup_include_ok{'vasnprintf.c'}{'fpucw.h'} = 1;
169   $dup_include_ok{'gen-uni-tables.c'}{'3level.h'} = 1;
170   $dup_include_ok{'csharpexec.c'}{'classpath.c'} = 1;
171   $dup_include_ok{'csharpexec.c'}{'classpath.h'} = 1;
172
173   my %inc;
174   open FH, '<', $file
175     or die "$ME: can't open '$file' for reading: $!\n";
176
177   while (defined (my $line = <FH>))
178     {
179       # Ignore test-driver code at end of file.
180       $line =~ m!^\#if(def)? TEST_!
181         and last;
182
183       $line =~ m!^\s*\#\s*include\s+"!
184         or next;
185       $line =~ s///;
186       chomp $line;
187       $line =~ s/".*//;
188       exists $inc{$line} && ! exists $special_non_dup{$line}
189           && ! exists $dup_include_ok{basename $file}{$line}
190         and warn "$ME: $file: duplicate inclusion of $line\n";
191
192       $inc{$line} = 1;
193     }
194   close FH;
195
196   return \%inc;
197 }
198
199 my %exempt_header =
200   (
201    # Exempt headers like unlocked-io.h that are '#include'd
202    # but not necessarily used.
203    'unlocked-io.h' => 1,
204
205    # Give gettext.h a free pass only when included from lib/error.c,
206    # since we've made that exception solely to make the error
207    # module easier to use -- at RMS's request.
208    'lib/error.c:gettext.h' => 1,
209
210    # The full-read module shares code with the full-write module.
211    'lib/full-write.c:full-read.h' => 1,
212
213    # The safe-write module shares code with the safe-read module.
214    'lib/safe-read.c:safe-write.h' => 1,
215
216    # The use of obstack.h in the hash module is conditional, off by default.
217    'lib/hash.c:obstack.h' => 1,
218
219    # C files in the gc module have conditional includes.
220    'lib/gc-gnulib.c:des.h' => 1,
221    'lib/gc-gnulib.c:arcfour.h' => 1,
222    'lib/gc-gnulib.c:arctwo.h' => 1,
223    'lib/gc-gnulib.c:md2.h' => 1,
224    'lib/gc-gnulib.c:md4.h' => 1,
225    'lib/gc-gnulib.c:md5.h' => 1,
226    'lib/gc-gnulib.c:rijndael.h' => 1,
227    'lib/gc-gnulib.c:sha1.h' => 1,
228    'lib/gc-gnulib.c:rijndael-api-fst.h' => 1,
229    'lib/gc-gnulib.c:hmac.h' => 1,
230    'lib/gc-libgcrypt.c:md2.h' => 1,
231   );
232
233 sub check_module ($)
234 {
235   my @m = @_;
236
237   my %file;
238   my %module_all_files;
239   my %dep;
240   my %seen_module;
241
242   while (@m)
243     {
244       my $m = pop @m;
245       # warn "M: $m\n";
246       exists $seen_module{$m}
247         and next;
248       $seen_module{$m} = 1;
249       my ($file, $dep) = parse_module_file $m;
250       push @m, keys %$dep;
251       foreach my $f (keys %$file)
252         {
253           $module_all_files{$f} = 1;
254         }
255     }
256
257   my @t = sort keys %module_all_files;
258   # warn "ALL files: @t\n";
259
260   # Derive from %module_all_files (by parsing the .c and .h files therein),
261   # the list of all #include'd files that reside in lib/.
262   foreach my $f (keys %module_all_files)
263     {
264       $f =~ /\.[ch]$/
265         or next;
266       # FIXME: this is too naive
267       my $inc = find_included_lib_files "../$f";
268       foreach my $i (sort keys %$inc)
269         {
270           my $lib_file = "lib/$i";
271           exists $exempt_header{"$f:$i"}
272             || exists $exempt_header{$i}
273               and next;
274           !exists $module_all_files{$lib_file} && -f "../lib/$i"
275             and warn "$f: $i is '#include'd, but not "
276               . "listed in module's Files: section\n";
277         }
278       #my @t = sort keys %$inc;
279       #print "** $f: @t\n";
280     }
281 }
282
283 {
284   GetOptions
285     (
286      help => sub { usage 0 },
287      version => sub { print "$ME version $VERSION\n$COPYRIGHT_NOTICE"; exit },
288     ) or usage 1;
289
290   @ARGV < 1
291     and (warn "$ME: missing FILE argument\n"), usage 1;
292
293   foreach my $module (@ARGV)
294     {
295       check_module $module;
296     }
297
298   exit 0;
299 }