summaryrefslogtreecommitdiffstats
path: root/tools/tools/kerninclude/kerninclude.sh
blob: 45d4d1f0bc41d91ebed167198096664a4a16178e (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/sh
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
# ----------------------------------------------------------------------------
#
# $FreeBSD$
#
# This script tries to find #include statements which are not needed in
# the FreeBSD kernel tree.
#
# For each include file on the tasklist (set in $includes right below)
#    For each object file (see around line 170 for how these are selected)
#	For each kernel (set in $kernels right below) and all modules
#	   if the object exists
#	      figure out the sourcefile
#	      if the sourcefile doesn't contain "#include $include"
#	          continue
#	      if object can be compile without $include existing
#	          continue /* probably protected by #ifdef something */
#	      if object can't be compile with empty file for $include 
#	          continue /* needed something/
#	      if the compiler warnings/errors were different than normal
#	          continue /* needed something/
#	      if the resulting object were different than normal
#	          continue /* needed something */
#             /* didn't need this include */
#	      remove $include from source file
#
# Takes about 12h to run on a PII/400
#
# NOTE: /usr/include is mucked about with!!
#

set -e

# Base of the kernel sources you want to work on
cd /some/sourcetree/sys

# Set to true to start from scratch, false to resume
init=true

# Which kernels you want to check
kernels="LINT GENERIC GENERIC98"

# Which includes you want to check
includes="*/*.h i386/*/*.h dev/*/*.h cam/scsi/*.h ufs/*/*.h pc98/*/*.h netatm/*/*.h i4b/*/*.h"

check_it ()
{
	if [ -f ::$2 ] ; then
		if grep "#[ 	]*include[ 	].$1." ::$2 > /dev/null; then
			src=`ls -l ::$2 | awk '{print $11}'`
		else
			echo " -"
			exit 0
		fi
	else
		rm -f $2
		make -n $2 > _0 2>&1 || true
		src=`awk '$1 == "cc" {print $NF}' _0`
		if expr "x$src" : 'x.*\.c$' > /dev/null ; then
			ln -s $src ::$2
		else
			echo " not C source"
			# don't create $2, we don't care about it.
			exit 0
		fi
		if grep "#[ 	]*include[ 	].$1." $src > /dev/null; then
			true
		else
			echo " -"
			touch $2
			exit 0
		fi
	fi
	rm ../../$1
	rm -f $2
	if [ -f /usr/include/$1 ] ; then
		mv /usr/include/$1 /usr/include/${1}_
		make $2 > _0 2>&1 || true
		mv /usr/include/${1}_ /usr/include/$1
	else
		make $2 > _0 2>&1 || true
	fi
	echo > ../../$1
	if [ -f $2 ] ; then
		echo " no read"
		cp ../../${1}_ ../../$1
		exit 0
	fi

	make $2 > _1 2>&1 || true

	cp ../../${1}_ ../../$1
	
	if [ ! -f $2 ] ; then
		echo " compile error"
		touch $2
		exit 0
	fi
	m2=`md5 < $2`

	rm $2
	make $2 > _0 2>&1 || true
	if [ ! -f $2 ] ; then
		echo "$2 reference compile failed"
		touch $2
		cat _0
		exit 0
	fi
	m1=`md5 < $2`

	if cmp _0 _1 > /dev/null 2>&1 ; then
		true
	else
		echo " warnings changed"
		exit 0
	fi

	if [ $m1 != $m2 ] ; then
		echo " MD5 changed"
		exit 0
	fi
}


if $init ; then
	(
	echo "Cleaning modules"
	cd modules
	make clean > /dev/null 2>&1
	make cleandir > /dev/null 2>&1
	make cleandir > /dev/null 2>&1
	make clean > /dev/null 2>&1
	make clean > /dev/null 2>&1
	)

	(
	echo "Cleaning compile"
	cd compile
	ls | grep -v CVS | xargs rm -rf
	)

	( 
	echo "Configuring kernels"
	for d in i386/conf pc98/conf
	do
		(
		cd $d
		for i in $kernels
		do
			if [ ! -f $i ] ; then
				continue
			fi
			if [ $i = "LINT" ] ; then
				config -r -p $i
			else
				config -r $i
			fi
		done
		)
	done
	)

	for i in $kernels
	do
		(
		echo "Compiling $i"
		cd compile/$i
		rm -f ::*
		make -k > x.0 2>&1
		tail -4 x.0
		)
	done

	(
	echo "Compiling modules"
	cd modules
	rm -f */::*
	make -k > x.0 2>&1 || true
	)
fi

# Generate the list of object files we want to check
# you can put a convenient grep right before the sort
# if you want just some specific subset of files checked
(
cd modules
for i in *
do
	if [ -d $i -a $i != CVS ] ; then
		( cd $i ; ls *.o 2>/dev/null || true)
	fi
done
cd ../compile
for i in $kernels
do
	( cd $i ; ls *.o 2>/dev/null )
done
) | sed '
/aicasm/d	
/genassym/d
/vers.o/d
/setdef0.o/d
/setdef1.o/d
/::/d
' | sort -u > _

objlist=`cat _`

find . -name '*.h_' -print | xargs rm -f

for incl in $includes
do
	if [ ! -f ${incl} ] ; then
		continue
	fi
	if [ ! -f ${incl}_ ] ; then
		cp $incl ${incl}_
	fi
	for obj in $objlist
	do
		(
		echo -n "$incl $obj:"
		src=""

		cd compile 
		for i in $kernels
		do
			cd $i

			if [ ! -f $obj ] ; then
				cd ..
				continue
			fi
			echo -n " [$i]"
			check_it $incl $obj
			cd ..
		done
		cd ..
		cd modules
		for d in */$obj
		do
			if [ ! -f $d ] ; then
				continue
			fi
			b=`dirname $d`
			echo -n " [$b]"
			cd $b
			check_it $incl $obj 
			cd ..
		done
		cd ..
		if [ "x$src" = "x" ] ; then
			echo " -"
			exit 0
		fi
		echo -n " ($src)"
		echo " BINGO!"
		echo "$incl $src" >> _incl
		(
		cd compile/LINT
		grep -v "#[ 	]*include[ 	]*.$incl." $src > ${src}_
		mv ${src}_ $src
		)
		)
	done
done

OpenPOWER on IntegriCloud