import new version from samba
authorAndrew Tridgell <tridge@samba.org>
Thu, 8 Oct 2009 04:52:24 +0000 (15:52 +1100)
committerAndrew Tridgell <tridge@samba.org>
Thu, 8 Oct 2009 04:52:24 +0000 (15:52 +1100)
minimal_includes.pl

index f9d80e2..d64409e 100755 (executable)
@@ -10,6 +10,7 @@ use Getopt::Long;
 
 my $opt_help = 0;
 my $opt_remove = 0;
+my $opt_skip_system = 0;
 
 #####################################################################
 # write a string into a file
@@ -38,23 +39,18 @@ sub save_lines($$)
        FileSave($fname, $data);
 }
 
-sub obj_name($)
-{
-    my $fname = shift;
-    $fname =~ s/(.*)\.c$/$1.o/;
-    return $fname;
-}
-
 sub test_compile($)
 {
        my $fname = shift;
-       my $obj = obj_name($fname);
-       if ($obj eq $fname) {
+       my $obj;
+       if ($fname =~ s/(.*)\..*$/$1.o/) {
+               $obj = "$1.o";
+       } else {
                return "NOT A C FILE";
        }
        unlink($obj);
-       my $ret = `make $obj 2>&1 && md5sum $obj`;
-       if (!stat("$obj")) {
+       my $ret = `make $obj 2>&1`;
+       if (!unlink("$obj")) {
                return "COMPILE FAILED";
        }
        return $ret;
@@ -67,25 +63,31 @@ sub test_include($$$$)
        my $i = shift;
        my $original = shift;
        my $line = $lines->[$i];
+       my $testfname;
 
        $lines->[$i] = "";
-       save_lines($fname, $lines);
 
-       my $out = test_compile($fname);
+       `/bin/mv -f $fname $fname.misaved` && die "failed to rename $fname";
+       save_lines($fname, $lines);
        
+       my $out = test_compile($fname);
+
        if ($out eq $original) {
                if ($opt_remove) {
-                       print "$fname: removing $line";
-                       return;
+                       if ($opt_skip_system && 
+                           $line =~ /system\//) {
+                               print "$fname: not removing system include $line\n";
+                       } else {
+                               print "$fname: removing $line\n";
+                               return;
+                       }
+               } else {
+                       print "$fname: might be able to remove $line\n";
                }
-               print "$fname: might be able to remove $line";
-       } else {
-#              print "OUT:  $out\n";
-#              print "ORIG: $original\n";
        }
 
        $lines->[$i] = $line;
-       save_lines($fname, $lines);
+       `/bin/mv -f $fname.misaved $fname` && die "failed to restore $fname";
 }
 
 sub process_file($)
@@ -94,14 +96,10 @@ sub process_file($)
        my @lines = load_lines($fname);
        my $num_lines = $#lines;
 
-       if (!stat(obj_name($fname))) {
-           return;
-       }
-       
        my $original = test_compile($fname);
 
        if ($original eq "COMPILE FAILED") {
-#              print "Failed to compile $fname\n";
+               print "Failed to compile $fname\n";
                return;
        }
 
@@ -137,8 +135,9 @@ sub ShowHelp()
           Usage: minimal_includes.pl [options] <C files....>
           
           Options:
-                 --help       show help
-                 --remove     remove includes, don't just list them
+                 --help         show help
+                 --remove       remove includes, don't just list them
+                 --skip-system  don't remove system/ includes
 ";
 }
 
@@ -147,6 +146,7 @@ sub ShowHelp()
 GetOptions (
            'h|help|?' => \$opt_help,
            'remove' => \$opt_remove,
+           'skip-system' => \$opt_skip_system,
            );
 
 if ($opt_help) {