2 # find a list of #include lines in C code that might not be needed
3 # usually called with something like this:
4 # minimal_includes.pl `find . -name "*.c"`
5 # Andrew Tridgell <tridge@samba.org>
14 #####################################################################
15 # write a string into a file
18 my($filename) = shift;
21 open(FILE
, ">$filename") || die "can't open $filename";
29 my @lines = split(/^/m, `cat $fname`);
37 my $data = join('', @
{$lines});
38 FileSave
($fname, $data);
45 if ($fname =~ s/(.*)\.c$/$1.o/) {
48 return "NOT A C FILE";
51 my $ret = `make $obj 2>&1`;
52 if (!unlink("$obj")) {
53 return "COMPILE FAILED";
58 sub test_include
($$$$)
64 my $line = $lines->[$i];
67 save_lines
("_testcompile.c", $lines);
69 my $out = test_compile
("_testcompile.c");
70 $out =~ s/_testcompile.c/$fname/g;
72 if ($out eq $original) {
74 print "$fname: removing $line\n";
75 save_lines
($fname, $lines);
78 print "$fname: might be able to remove $line\n";
82 unlink("_testcompile.c");
88 my @lines = load_lines
($fname);
89 my $num_lines = $#lines;
91 my $original = test_compile
($fname);
93 if ($original eq "COMPILE FAILED") {
94 print "Failed to compile $fname\n";
98 print "Processing $fname (with $num_lines lines)\n";
102 for (my $i=0;$i<=$num_lines;$i++) {
103 my $line = $lines[$i];
104 if ($line =~ /^\#\s*if/) {
107 if ($line =~ /^\#\s*endif/) {
110 if ($if_level == 0 &&
111 $line =~ /^\#\s*include/ &&
112 !($line =~ /needed/)) {
113 test_include
($fname, \
@lines, $i, $original);
119 #########################################
125 Copyright (C) tridge\@samba.org
127 Usage: minimal_includes.pl [options] <C files....>
131 --remove remove includes, don't just list them
138 'h|help|?' => \
$opt_help,
139 'remove' => \
$opt_remove,
147 for (my $i=0;$i<=$#ARGV;$i++) {
148 my $fname = $ARGV[$i];
149 process_file
($fname);