1#!/usr/bin/perl 2 3$| = 1; 4 5sub writefile($@){ 6 my ($file, @lines) = @_; 7 8 sleep(1); 9 open(F, ">$file") || die "open >$file: $!"; 10 print F @lines; 11 close(F); 12} 13 14# Cut out #include lines that don't contribute anything. 15for($i=0; $i<@ARGV; $i++){ 16 $file = $ARGV[$i]; 17 if(!open(F, $file)){ 18 print STDERR "open $file: $!\n"; 19 next; 20 } 21 @lines = <F>; 22 close(F); 23 24 $obj = "$file.o"; 25 $obj =~ s/\.c\.o$/.o/; 26 system("touch $file"); 27 28 if(system("make CC='gcc -Werror' $obj >/dev/null 2>\&1") != 0){ 29 print STDERR "make $obj failed: $rv\n"; 30 next; 31 } 32 33 system("cp $file =$file"); 34 for($j=@lines-1; $j>=0; $j--){ 35 if($lines[$j] =~ /^#include/){ 36 $old = $lines[$j]; 37 $lines[$j] = "/* CUT-H */\n"; 38 writefile($file, @lines); 39 if(system("make CC='gcc -Werror' $obj >/dev/null 2>\&1") != 0){ 40 $lines[$j] = $old; 41 }else{ 42 print STDERR "$file $old"; 43 } 44 } 45 } 46 writefile($file, grep {!/CUT-H/} @lines); 47 system("rm =$file"); 48} 49