aboutsummaryrefslogtreecommitdiff
path: root/tools/tools/kerninclude/kerninclude.sh
blob: 1efe4c278fc2ca3434484a33686a348f40d1d911 (plain) (blame)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/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.
#

set -e

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

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

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

NO_MODULES=yes
export NO_MODULES

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
	)
fi

(
echo "Cleaning temp files"
find . -name '*.h_' -print | xargs rm -f
find . -name '::*' -print | xargs rm -f
find . -name '*.o' -size 0 -print | xargs rm -f
)

echo "Configuring kernels"
(
	cd i386/conf
	make LINT
	if $init ; then
		rm -rf ../../compile/LINT ../../compile/GENERIC
	fi
	config LINT
	config GENERIC
)
(
	cd pc98/conf
	cp -f GENERIC GENERIC98
	if $init ; then
		rm -rf ../../compile/GENERIC98
	fi
	config GENERIC98
)

for i in $kernels
do
	(
	echo "Compiling $i"
	cd compile/$i
	make > x.0 2>&1
	tail -4 x.0
	if [ ! -f kernel ] ; then
		echo "Error: No $i kernel built"
		exit 1
	fi
	)
done

(
echo "Compiling modules"
cd modules
make > x.0 2>&1 
)

# 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
' | sort -u > _

objlist=`cat _`


for o in $objlist
do
	l=""
	src=""
	for k in $kernels
	do
		if [ ! -f compile/$k/$o ] ; then
			continue;
		fi
		l="$l compile/$k"
		if [ "x$src" = "x" ] ; then
			cd compile/$k
			mv $o ${o}_
			make -n $o > _
			mv ${o}_ $o
			src=compile/$k/`awk '$1 == "cc" {print $NF}' _`
			cd ../..
			if expr "x$src" : 'x.*\.c$' > /dev/null ; then
				true
			else
				echo NO SRC $o
				src=""
			fi
		fi
	done
	for m in modules/*
	do
		if [ ! -d $m -o ! -f $m/$o ] ; then
			continue;
		fi
		l="$l $m"
		if [ "x$src" = "x" ] ; then
			cd $m
			mv $o ${o}_
			make -n $o > _
			mv ${o}_ $o
			src=`awk '$1 == "cc" {print $NF}' _`
			cd ../..
			if expr "x$src" : 'x.*\.c$' > /dev/null ; then
				if [ "`dirname $src`" = "." ] ; then
					src="$m/$src"
				fi
				true
			else
				echo NO SRC $o
				src=""
			fi
		fi
	done
	if [ "x$src" = "x" ] ; then
		echo "NO SOURCE $o"
		continue
	fi
	echo "OBJ	$o"
	echo "	SRC	$src"

	grep -n '^[ 	]*#[ 	]*include' $src | sed '
	s/^\([0-9]*\):[ 	]*#[ 	]*include[ 	]*[<"]/\1 /
	s/[">].*//
	/ opt_/d
	' | sort -rn | while read lin incl
	do
		S=""
		echo "		INCL	$lin	$incl"
		cp $src ${src}_

		# Check if we can compile without this #include line.

		sed "${lin}s/.*//" ${src}_ > ${src}
		for t in $l
		do
			cd $t
			mv ${o} ${o}_
			if make ${o} > _log 2>&1 ; then
				if cmp -s ${o} ${o}_ ; then
					echo "			$t	same object"
				else
					echo "			$t	changed object"
					S=TAG
				fi
			else
				echo "			$t	used"
				S=TAG
			fi
			mv ${o}_ ${o}
			cd ../..
			if [ "x$S" != "x" ] ; then
				break
			fi
		done
		if [ "x$S" != "x" ] ; then
			mv ${src}_ ${src}
			continue
		fi

		# Check if this is because it is a nested #include
		for t in $l
		do
			cd $t
			rm -rf foo
			mkdir -p foo/${incl}
			rmdir foo/${incl}
			touch foo/${incl}
			mv ${o} ${o}_
			if make INCLMAGIC=-Ifoo ${o} > _log2 2>&1 ; then
				if cmp -s ${o} ${o}_ ; then
					echo "			$t	still same object"
				else
					echo "			$t	changed object"
					S=TAG
				fi
			else
				echo "			$t	nested include"
				S=TAG
			fi
			rm -rf foo
			mv ${o}_ ${o}
			cd ../..
			if [ "x$S" != "x" ] ; then
				break
			fi
		done
		if [ "x$S" != "x" ] ; then
			mv ${src}_ ${src}
			continue
		fi

		# Check if this is because it is #ifdef'ed out

		sed "${lin}s/.*/#error \"BARF\"/" ${src}_ > ${src}
		for t in $l
		do
			cd $t
			mv ${o} ${o}_
			if make ${o} > /dev/null 2>&1 ; then
				echo "			$t	line not read"
				S=TAG
			fi
			mv ${o}_ ${o}
			cd ../..
			if [ "x$S" != "x" ] ; then
				break
			fi
		done

		mv ${src}_ ${src}
		if [ "x$S" != "x" ] ; then
			continue
		fi

		# Check if the warnings changed.

		for t in $l
		do
			cd $t
			mv ${o} ${o}_
			if make ${o} > _ref 2>&1 ; then
				if cmp -s _ref _log ; then
					echo "			$t	same warnings"
				else
					echo "			$t	changed warnings"
					S=TAG
				fi
			else
				echo "ARGHH!!!"
				exit 9
			fi
					
			mv ${o}_ ${o}
			cd ../..
			if [ "x$S" != "x" ] ; then
				break
			fi
		done
		if [ "x$S" != "x" ] ; then
			continue
		fi
		cp $src ${src}_
		sed "${lin}d" ${src}_ > ${src}
		rm ${src}_
		touch _again
		echo "BINGO $src $lin $incl $obj $l"
	done
done