summaryrefslogtreecommitdiffstats
path: root/source4/script/minimal_includes.pl
blob: 4203d00ac0df721548663979853f120dd5823e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/perl -w
# find a list of #include lines in C code that might not be needed
# usually called with something like this:
#    minimal_includes.pl `find . -name "*.c"`
# Andrew Tridgell <tridge@samba.org>

use strict;
use Data::Dumper;
use Getopt::Long;

my $opt_help = 0;
my $opt_remove = 0;
my $opt_skip_system = 0;
my $opt_waf = 0;

#####################################################################
# write a string into a file
sub FileSave($$)
{
    my($filename) = shift;
    my($v) = shift;
    local(*FILE);
    open(FILE, ">$filename") || die "can't open $filename";    
    print FILE $v;
    close(FILE);
}

sub load_lines($)
{
	my $fname = shift;
	my @lines = split(/^/m, `cat $fname`);
	return @lines;
}

sub save_lines($$)
{
	my $fname = shift;
	my $lines = shift;
	my $data = join('', @{$lines});
	FileSave($fname, $data);
}

sub test_compile($)
{
	my $fname = shift;
	my $obj;
	if ($opt_waf) {
		my $ret = `../buildtools/bin/waf $fname 2>&1`;
		return $ret
	}
	if ($fname =~ s/(.*)\..*$/$1.o/) {
		$obj = "$1.o";
	} else {
		return "NOT A C FILE";
	}
	unlink($obj);
	my $ret = `make $obj 2>&1`;
	if (!unlink("$obj")) {
		return "COMPILE FAILED";
	}
	return $ret;
}

sub test_include($$$$)
{
	my $fname = shift;
	my $lines = shift;
	my $i = shift;
	my $original = shift;
	my $line = $lines->[$i];
	my $testfname;

	$lines->[$i] = "";

	my $mname = $fname . ".misaved";

	unlink($mname);
	rename($fname, $mname) || die "failed to rename $fname";
	save_lines($fname, $lines);
	
	my $out = test_compile($fname);

	if ($out eq $original) {
		if ($opt_remove) {
			if ($opt_skip_system && 
			    $line =~ /system\//) {
				print "$fname: not removing system include $line\n";
			} else {
				print "$fname: removing $line\n";
				unlink($mname);
				return;
			}
		} else {
			print "$fname: might be able to remove $line\n";
		}
	}

	$lines->[$i] = $line;
	rename($mname, $fname) || die "failed to restore $fname";
}

sub process_file($)
{
	my $fname = shift;
	my @lines = load_lines($fname);
	my $num_lines = $#lines;

	my $original = test_compile($fname);

	if ($original eq "COMPILE FAILED") {
		print "Failed to compile $fname\n";
		return;
	}

	print "Processing $fname (with $num_lines lines)\n";
	
	my $if_level = 0;

	for (my $i=0;$i<=$num_lines;$i++) {
		my $line = $lines[$i];
		if ($line =~ /^\#\s*if/) {
			$if_level++;
		}
		if ($line =~ /^\#\s*endif/) {
			$if_level--;
		}
		if ($if_level == 0 &&
		    $line =~ /^\#\s*include/ && 
		    !($line =~ /needed/)) {
			test_include($fname, \@lines, $i, $original);
		}
	}
}


#########################################
# display help text
sub ShowHelp()
{
    print "
           minimise includes
           Copyright (C) tridge\@samba.org

	   Usage: minimal_includes.pl [options] <C files....>
	   
	   Options:
                 --help         show help
                 --remove       remove includes, don't just list them
                 --skip-system  don't remove system/ includes
                 --waf          use waf target conventions
";
}


# main program
GetOptions (
	    'h|help|?' => \$opt_help,
	    'remove' => \$opt_remove,
	    'skip-system' => \$opt_skip_system,
	    'waf' => \$opt_waf,
	    );

if ($opt_help) {
	ShowHelp();
	exit(0);
}

for (my $i=0;$i<=$#ARGV;$i++) {
	my $fname = $ARGV[$i];
	process_file($fname);
}