gitrev.sh: make it usable during buildkernel/world
[dragonfly.git] / tools / tools / kerninclude / kerninclude.sh
blob527b1eb4af65231e37cde78a1245b9fa26fbe3d1
1 #!/bin/sh
2 # ----------------------------------------------------------------------------
3 # "THE BEER-WARE LICENSE" (Revision 42):
4 # <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 # can do whatever you want with this stuff. If we meet some day, and you think
6 # this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 # ----------------------------------------------------------------------------
9 # $FreeBSD: src/tools/tools/kerninclude/kerninclude.sh,v 1.1.2.1 2001/03/05 12:18:27 kris Exp $
10 # $DragonFly: src/tools/tools/kerninclude/kerninclude.sh,v 1.2 2003/06/17 04:29:11 dillon Exp $
12 # This script tries to find #include statements which are not needed in
13 # the FreeBSD kernel tree.
16 set -e
18 # Base of the kernel sources you want to work on
19 cd /sys
21 # Set to true to start from scratch, false to resume
22 init=false
24 # Which kernels you want to check
25 kernels="LINT GENERIC GENERIC98"
27 NO_MODULES=yes
28 export NO_MODULES
30 if $init ; then
32 echo "Cleaning modules"
33 cd modules
34 make clean > /dev/null 2>&1
35 make cleandir > /dev/null 2>&1
36 make cleandir > /dev/null 2>&1
37 make clean > /dev/null 2>&1
38 make clean > /dev/null 2>&1
42 echo "Cleaning compile"
43 cd compile
44 ls | xargs rm -rf
49 echo "Cleaning temp files"
50 find . -name '*.h_' -print | xargs rm -f
51 find . -name '::*' -print | xargs rm -f
52 find . -name '*.o' -size 0 -print | xargs rm -f
55 echo "Configuring kernels"
57 cd i386/conf
58 make LINT
59 if $init ; then
60 config -r LINT
61 config -r GENERIC
62 else
63 config LINT
64 config GENERIC
68 cd pc98/conf
69 cp -f GENERIC GENERIC98
70 if $init ; then
71 config -r GENERIC98
72 else
73 config GENERIC98
77 for i in $kernels
80 echo "Compiling $i"
81 cd compile/$i
82 make > x.0 2>&1
83 tail -4 x.0
84 if [ ! -f kernel ] ; then
85 echo "Error: No $i kernel built"
86 exit 1
89 done
92 echo "Compiling modules"
93 cd modules
94 make > x.0 2>&1
97 # Generate the list of object files we want to check
98 # you can put a convenient grep right before the sort
99 # if you want just some specific subset of files checked
101 cd modules
102 for i in *
104 if [ -d $i ] ; then
105 ( cd $i ; ls *.o 2>/dev/null || true)
107 done
108 cd ../compile
109 for i in $kernels
111 ( cd $i ; ls *.o 2>/dev/null )
112 done
113 ) | sed '
114 /aicasm/d
115 /genassym/d
116 /vers.o/d
117 /setdef0.o/d
118 /setdef1.o/d
119 ' | sort -u > _
121 objlist=`cat _`
124 for o in $objlist
126 l=""
127 src=""
128 for k in $kernels
130 if [ ! -f compile/$k/$o ] ; then
131 continue;
133 l="$l compile/$k"
134 if [ "x$src" = "x" ] ; then
135 cd compile/$k
136 mv $o ${o}_
137 make -n $o > _
138 mv ${o}_ $o
139 src=compile/$k/`awk '$1 == "cc" {print $NF}' _`
140 cd ../..
141 if expr "x$src" : 'x.*\.c$' > /dev/null ; then
142 true
143 else
144 echo NO SRC $o
145 src=""
148 done
149 for m in modules/*
151 if [ ! -d $m -o ! -f $m/$o ] ; then
152 continue;
154 l="$l $m"
155 if [ "x$src" = "x" ] ; then
156 cd $m
157 mv $o ${o}_
158 make -n $o > _
159 mv ${o}_ $o
160 src=`awk '$1 == "cc" {print $NF}' _`
161 cd ../..
162 if expr "x$src" : 'x.*\.c$' > /dev/null ; then
163 if [ "`dirname $src`" = "." ] ; then
164 src="$m/$src"
166 true
167 else
168 echo NO SRC $o
169 src=""
172 done
173 if [ "x$src" = "x" ] ; then
174 echo "NO SOURCE $o"
175 continue
177 echo "OBJ $o"
178 echo " SRC $src"
180 grep -n '^[ ]*#[ ]*include' $src | sed '
181 s/^\([0-9]*\):[ ]*#[ ]*include[ ]*[<"]/\1 /
182 s/[">].*//
183 / opt_/d
184 ' | sort -rn | while read lin incl
186 S=""
187 echo " INCL $lin $incl"
188 cp $src ${src}_
190 # Check if we can compile without this #include line.
192 sed "${lin}s/.*//" ${src}_ > ${src}
193 for t in $l
195 cd $t
196 mv ${o} ${o}_
197 if make ${o} > _log 2>&1 ; then
198 if cmp -s ${o} ${o}_ ; then
199 echo " $t same object"
200 else
201 echo " $t changed object"
202 S=TAG
204 else
205 echo " $t used"
206 S=TAG
208 mv ${o}_ ${o}
209 cd ../..
210 if [ "x$S" != "x" ] ; then
211 break
213 done
214 if [ "x$S" != "x" ] ; then
215 mv ${src}_ ${src}
216 continue
219 # Check if this is because it is a nested #include
220 for t in $l
222 cd $t
223 rm -rf foo
224 mkdir -p foo/${incl}
225 rmdir foo/${incl}
226 touch foo/${incl}
227 mv ${o} ${o}_
228 if make INCLMAGIC=-Ifoo ${o} > _log2 2>&1 ; then
229 if cmp -s ${o} ${o}_ ; then
230 echo " $t still same object"
231 else
232 echo " $t changed object"
233 S=TAG
235 else
236 echo " $t nested include"
237 S=TAG
239 rm -rf foo
240 mv ${o}_ ${o}
241 cd ../..
242 if [ "x$S" != "x" ] ; then
243 break
245 done
246 if [ "x$S" != "x" ] ; then
247 mv ${src}_ ${src}
248 continue
251 # Check if this is because it is #ifdef'ed out
253 sed "${lin}s/.*/#error \"BARF\"/" ${src}_ > ${src}
254 for t in $l
256 cd $t
257 mv ${o} ${o}_
258 if make ${o} > /dev/null 2>&1 ; then
259 echo " $t line not read"
260 S=TAG
262 mv ${o}_ ${o}
263 cd ../..
264 if [ "x$S" != "x" ] ; then
265 break
267 done
269 mv ${src}_ ${src}
270 if [ "x$S" != "x" ] ; then
271 continue
274 # Check if the warnings changed.
276 for t in $l
278 cd $t
279 mv ${o} ${o}_
280 if make ${o} > _ref 2>&1 ; then
281 if cmp -s _ref _log ; then
282 echo " $t same warnings"
283 else
284 echo " $t changed warnings"
285 S=TAG
287 else
288 echo "ARGHH!!!"
289 exit 9
292 mv ${o}_ ${o}
293 cd ../..
294 if [ "x$S" != "x" ] ; then
295 break
297 done
298 if [ "x$S" != "x" ] ; then
299 continue
301 cp $src ${src}_
302 sed "${lin}d" ${src}_ > ${src}
303 rm ${src}_
304 touch _again
305 echo "BINGO $src $lin $incl $obj $l"
306 done
307 done