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;
15 #####################################################################
16 # write a string into a file
19 my($filename) = shift;
22 open(FILE
, ">$filename") || die "can't open $filename";
30 my @lines = split(/^/m, `cat $fname`);
38 my $data = join('', @
{$lines});
39 FileSave
($fname, $data);
46 if ($fname =~ s/(.*)\..*$/$1.o/) {
49 return "NOT A C FILE";
52 my $ret = `make $obj 2>&1`;
53 if (!unlink("$obj")) {
54 return "COMPILE FAILED";
59 sub test_include
($$$$)
65 my $line = $lines->[$i];
70 my $mname = $fname . ".misaved";
73 rename($fname, $mname) || die "failed to rename $fname";
74 save_lines
($fname, $lines);
76 my $out = test_compile
($fname);
78 if ($out eq $original) {
80 if ($opt_skip_system &&
81 $line =~ /system\//) {
82 print "$fname: not removing system include $line\n";
84 print "$fname: removing $line\n";
89 print "$fname: might be able to remove $line\n";
94 rename($mname, $fname) || die "failed to restore $fname";
100 my @lines = load_lines
($fname);
101 my $num_lines = $#lines;
103 my $original = test_compile
($fname);
105 if ($original eq "COMPILE FAILED") {
106 print "Failed to compile $fname\n";
110 print "Processing $fname (with $num_lines lines)\n";
114 for (my $i=0;$i<=$num_lines;$i++) {
115 my $line = $lines[$i];
116 if ($line =~ /^\#\s*if/) {
119 if ($line =~ /^\#\s*endif/) {
122 if ($if_level == 0 &&
123 $line =~ /^\#\s*include/ &&
124 !($line =~ /needed/)) {
125 test_include
($fname, \
@lines, $i, $original);
131 #########################################
137 Copyright (C) tridge\@samba.org
139 Usage: minimal_includes.pl [options] <C files....>
143 --remove remove includes, don't just list them
144 --skip-system don't remove system/ includes
151 'h|help|?' => \
$opt_help,
152 'remove' => \
$opt_remove,
153 'skip-system' => \
$opt_skip_system,
161 for (my $i=0;$i<=$#ARGV;$i++) {
162 my $fname = $ARGV[$i];
163 process_file
($fname);