s3-nmbd: Fix bug #7875
[Samba.git] / source4 / script / minimal_includes.pl
blob2bcbd1152a2c07166e98af901756198028614128
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/(.*)\.c$/$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 `/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) {
76 if ($opt_remove) {
77 if ($opt_skip_system &&
78 $line =~ /system\//) {
79 print "$fname: not removing system include $line\n";
80 } else {
81 print "$fname: removing $line\n";
82 return;
84 } else {
85 print "$fname: might be able to remove $line\n";
89 $lines->[$i] = $line;
90 `/bin/mv -f $fname.misaved $fname` && die "failed to restore $fname";
93 sub process_file($)
95 my $fname = shift;
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";
103 return;
106 print "Processing $fname (with $num_lines lines)\n";
108 my $if_level = 0;
110 for (my $i=0;$i<=$num_lines;$i++) {
111 my $line = $lines[$i];
112 if ($line =~ /^\#\s*if/) {
113 $if_level++;
115 if ($line =~ /^\#\s*endif/) {
116 $if_level--;
118 if ($if_level == 0 &&
119 $line =~ /^\#\s*include/ &&
120 !($line =~ /needed/)) {
121 test_include($fname, \@lines, $i, $original);
127 #########################################
128 # display help text
129 sub ShowHelp()
131 print "
132 minimise includes
133 Copyright (C) tridge\@samba.org
135 Usage: minimal_includes.pl [options] <C files....>
137 Options:
138 --help show help
139 --remove remove includes, don't just list them
140 --skip-system don't remove system/ includes
145 # main program
146 GetOptions (
147 'h|help|?' => \$opt_help,
148 'remove' => \$opt_remove,
149 'skip-system' => \$opt_skip_system,
152 if ($opt_help) {
153 ShowHelp();
154 exit(0);
157 for (my $i=0;$i<=$#ARGV;$i++) {
158 my $fname = $ARGV[$i];
159 process_file($fname);