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>
13 my $opt_skip_system = 0;
16 #####################################################################
17 # write a string into a file
20 my($filename) = shift;
23 open(FILE
, ">$filename") || die "can't open $filename";
31 my @lines = split(/^/m, `cat $fname`);
39 my $data = join('', @
{$lines});
40 FileSave
($fname, $data);
48 my $ret = `../buildtools/bin/waf $fname 2>&1`;
51 if ($fname =~ s/(.*)\..*$/$1.o/) {
54 return "NOT A C FILE";
57 my $ret = `make $obj 2>&1`;
58 if (!unlink("$obj")) {
59 return "COMPILE FAILED";
64 sub test_include
($$$$)
70 my $line = $lines->[$i];
75 my $mname = $fname . ".misaved";
78 rename($fname, $mname) || die "failed to rename $fname";
79 save_lines
($fname, $lines);
81 my $out = test_compile
($fname);
83 if ($out eq $original) {
85 if ($opt_skip_system &&
86 $line =~ /system\//) {
87 print "$fname: not removing system include $line\n";
89 print "$fname: removing $line\n";
94 print "$fname: might be able to remove $line\n";
99 rename($mname, $fname) || die "failed to restore $fname";
105 my @lines = load_lines
($fname);
106 my $num_lines = $#lines;
108 my $original = test_compile
($fname);
110 if ($original eq "COMPILE FAILED") {
111 print "Failed to compile $fname\n";
115 print "Processing $fname (with $num_lines lines)\n";
119 for (my $i=0;$i<=$num_lines;$i++) {
120 my $line = $lines[$i];
121 if ($line =~ /^\#\s*if/) {
124 if ($line =~ /^\#\s*endif/) {
127 if ($if_level == 0 &&
128 $line =~ /^\#\s*include/ &&
129 !($line =~ /needed/)) {
130 test_include
($fname, \
@lines, $i, $original);
136 #########################################
142 Copyright (C) tridge\@samba.org
144 Usage: minimal_includes.pl [options] <C files....>
148 --remove remove includes, don't just list them
149 --skip-system don't remove system/ includes
150 --waf use waf target conventions
157 'h|help|?' => \
$opt_help,
158 'remove' => \
$opt_remove,
159 'skip-system' => \
$opt_skip_system,
168 for (my $i=0;$i<=$#ARGV;$i++) {
169 my $fname = $ARGV[$i];
170 process_file
($fname);