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/(.*)\.c$/$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 `/bin/mv -f $fname $fname.misaved` && die "failed to rename $fname";
71 save_lines
($fname, $lines);
73 my $out = test_compile
($fname);
75 if ($out eq $original) {
77 if ($opt_skip_system &&
78 $line =~ /system\//) {
79 print "$fname: not removing system include $line\n";
81 print "$fname: removing $line\n";
85 print "$fname: might be able to remove $line\n";
90 `/bin/mv -f $fname.misaved $fname` && die "failed to restore $fname";
96 my @lines = load_lines
($fname);
97 my $num_lines = $#lines;
99 my $original = test_compile
($fname);
101 if ($original eq "COMPILE FAILED") {
102 print "Failed to compile $fname\n";
106 print "Processing $fname (with $num_lines lines)\n";
110 for (my $i=0;$i<=$num_lines;$i++) {
111 my $line = $lines[$i];
112 if ($line =~ /^\#\s*if/) {
115 if ($line =~ /^\#\s*endif/) {
118 if ($if_level == 0 &&
119 $line =~ /^\#\s*include/ &&
120 !($line =~ /needed/)) {
121 test_include
($fname, \
@lines, $i, $original);
127 #########################################
133 Copyright (C) tridge\@samba.org
135 Usage: minimal_includes.pl [options] <C files....>
139 --remove remove includes, don't just list them
140 --skip-system don't remove system/ includes
147 'h|help|?' => \
$opt_help,
148 'remove' => \
$opt_remove,
149 'skip-system' => \
$opt_skip_system,
157 for (my $i=0;$i<=$#ARGV;$i++) {
158 my $fname = $ARGV[$i];
159 process_file
($fname);