s4:tortore/rpc/spoolss.c: fix compiler warning
[Samba/nascimento.git] / source4 / script / minimal_includes.pl
blob990ff00025f5aeb5e0d058212ba3e447ecb1e11f
1 #!/usr/bin/perl -w
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>
7 use strict;
8 use Data::Dumper;
9 use Getopt::Long;
11 my $opt_help = 0;
12 my $opt_remove = 0;
13 my $opt_skip_system = 0;
15 #####################################################################
16 # write a string into a file
17 sub FileSave($$)
19 my($filename) = shift;
20 my($v) = shift;
21 local(*FILE);
22 open(FILE, ">$filename") || die "can't open $filename";
23 print FILE $v;
24 close(FILE);
27 sub load_lines($)
29 my $fname = shift;
30 my @lines = split(/^/m, `cat $fname`);
31 return @lines;
34 sub save_lines($$)
36 my $fname = shift;
37 my $lines = shift;
38 my $data = join('', @{$lines});
39 FileSave($fname, $data);
42 sub test_compile($)
44 my $fname = shift;
45 my $obj;
46 if ($fname =~ s/(.*)\..*$/$1.o/) {
47 $obj = "$1.o";
48 } else {
49 return "NOT A C FILE";
51 unlink($obj);
52 my $ret = `make $obj 2>&1`;
53 if (!unlink("$obj")) {
54 return "COMPILE FAILED";
56 return $ret;
59 sub test_include($$$$)
61 my $fname = shift;
62 my $lines = shift;
63 my $i = shift;
64 my $original = shift;
65 my $line = $lines->[$i];
66 my $testfname;
68 $lines->[$i] = "";
70 my $mname = $fname . ".misaved";
72 unlink($mname);
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) {
79 if ($opt_remove) {
80 if ($opt_skip_system &&
81 $line =~ /system\//) {
82 print "$fname: not removing system include $line\n";
83 } else {
84 print "$fname: removing $line\n";
85 unlink($mname);
86 return;
88 } else {
89 print "$fname: might be able to remove $line\n";
93 $lines->[$i] = $line;
94 rename($mname, $fname) || die "failed to restore $fname";
97 sub process_file($)
99 my $fname = shift;
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";
107 return;
110 print "Processing $fname (with $num_lines lines)\n";
112 my $if_level = 0;
114 for (my $i=0;$i<=$num_lines;$i++) {
115 my $line = $lines[$i];
116 if ($line =~ /^\#\s*if/) {
117 $if_level++;
119 if ($line =~ /^\#\s*endif/) {
120 $if_level--;
122 if ($if_level == 0 &&
123 $line =~ /^\#\s*include/ &&
124 !($line =~ /needed/)) {
125 test_include($fname, \@lines, $i, $original);
131 #########################################
132 # display help text
133 sub ShowHelp()
135 print "
136 minimise includes
137 Copyright (C) tridge\@samba.org
139 Usage: minimal_includes.pl [options] <C files....>
141 Options:
142 --help show help
143 --remove remove includes, don't just list them
144 --skip-system don't remove system/ includes
149 # main program
150 GetOptions (
151 'h|help|?' => \$opt_help,
152 'remove' => \$opt_remove,
153 'skip-system' => \$opt_skip_system,
156 if ($opt_help) {
157 ShowHelp();
158 exit(0);
161 for (my $i=0;$i<=$#ARGV;$i++) {
162 my $fname = $ARGV[$i];
163 process_file($fname);