kern.post.mk revision 147155
1110566Smike# $FreeBSD: head/sys/conf/kern.post.mk 147155 2005-06-09 05:38:18Z imp $
2110566Smike
3141379Sdas# Part of a unified Makefile for building kernels.  This part includes all
4110566Smike# the definitions that need to be after all the % directives except %RULES
5110566Smike# and ones that act like they are part of %RULES.
6110566Smike#
7110566Smike# Most make variables should not be defined in this file.  Instead, they
8110566Smike# should be defined in the kern.pre.mk so that port makefiles can
9110566Smike# override or augment them.
10110566Smike
11110566Smike# In case the config had a makeoptions DESTDIR...
12110566Smike.if defined(DESTDIR)
13110566SmikeMKMODULESENV+=	DESTDIR="${DESTDIR}"
14110566Smike.endif
15110566SmikeMKMODULESENV+=	KERNBUILDDIR="${.CURDIR}"
16110566Smike
17110566Smike.MAIN: all
18110566Smike
19110566Smike.for target in all clean cleandepend cleandir clobber depend install \
20110566Smike    obj reinstall tags
21110566Smike${target}: kernel-${target}
22110566Smike.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
23110566Smike${target}: modules-${target}
24110566Smikemodules-${target}:
25110566Smike	cd $S/modules; ${MKMODULESENV} ${MAKE} \
26110566Smike	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
27110566Smike.endif
28110566Smike.endfor
29110566Smike
30110566Smike# Handle out of tree ports 
31110566Smike.if !defined(NO_MODULES) && defined(PORTS_MODULES)
32110566Smike.for _dir in ${.CURDIR}/../../..
33110566Smike.if !defined(SYSDIR) && exists(${_dir}/kern/)
34110566SmikeSYSDIR=	${_dir}
35110566Smike.endif
36110566Smike.endfor
37110566SmikePORTSMODULESENV=SYSDIR=${SYSDIR}
38110566Smike.for __target in all install reinstall clean
39110566Smike${__target}: ports-${__target}
40110566Smikeports-${__target}:
41110566Smike.for __i in ${PORTS_MODULES}
42110566Smike	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
43110566Smike.endfor
44110566Smike.endfor
45110566Smike.endif
46110566Smike
47110566Smike.ORDER: kernel-install modules-install
48110566Smike
49110566Smikekernel-all: ${KERNEL_KO}
50110566Smike
51110566Smikekernel-cleandir: kernel-clean
52110566Smike
53110566Smikekernel-clobber:
54110566Smike	find . -maxdepth 1 ! -type d ! -name version -delete
55110566Smike
56110566Smikekernel-obj:
57110566Smike
58110566Smike.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
59110566Smikemodules: modules-all
60110566Smike
61110566Smike.if !defined(NO_MODULES_OBJ)
62110566Smikemodules-all modules-depend: modules-obj
63110566Smike.endif
64110566Smike.endif
65110566Smike
66110566Smike.if !defined(DEBUG)
67110566SmikeFULLKERNEL=	${KERNEL_KO}
68110566Smike.else
69110566SmikeFULLKERNEL=	${KERNEL_KO}.debug
70110566Smike${KERNEL_KO}: ${FULLKERNEL}
71110566Smike	${OBJCOPY} --strip-debug ${FULLKERNEL} ${KERNEL_KO}
72110566Smikeinstall.debug reinstall.debug: gdbinit
73110566Smike	cd ${.CURDIR}; ${MAKE} -DINSTALL_DEBUG ${.TARGET:R}
74110566Smike
75110566Smike# Install gdbinit files for kernel debugging.
76110566Smikegdbinit:
77110566Smike	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
78110566Smike	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
79110566Smike	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
80110566Smike.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_ARCH})
81110566Smike	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_ARCH} \
82110566Smike	    ${.CURDIR}/gdbinit.machine
83110566Smike.endif
84110566Smike.endif
85110566Smike
86110566Smike${FULLKERNEL}: ${SYSTEM_DEP} vers.o
87110566Smike	@rm -f ${.TARGET}
88110566Smike	@echo linking ${.TARGET}
89110566Smike	${SYSTEM_LD}
90110566Smike.if !defined(DEBUG)
91110566Smike	${OBJCOPY} --strip-debug ${.TARGET}
92110566Smike.endif
93110566Smike	${SYSTEM_LD_TAIL}
94
95.if !exists(${.OBJDIR}/.depend)
96${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
97.endif
98
99LNFILES=	${CFILES:T:S/.c$/.ln/}
100
101.for mfile in ${MFILES}
102# XXX the low quality .m.o rules gnerated by config are normally used
103# instead of the .m.c rules here.
104${mfile:T:S/.m$/.c/}: ${mfile}
105	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
106${mfile:T:S/.m$/.h/}: ${mfile}
107	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
108.endfor
109
110kernel-clean:
111	rm -f *.o *.so *.So *.ko *.s eddep errs \
112	    ${FULLKERNEL} ${KERNEL_KO} linterrs makelinks tags vers.c \
113	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
114	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
115	    ${CLEAN}
116
117lint: ${LNFILES}
118	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
119	    tee -a linterrs
120
121# This is a hack.  BFD "optimizes" away dynamic mode if there are no
122# dynamic references.  We could probably do a '-Bforcedynamic' mode like
123# in the a.out ld.  For now, this works.
124HACK_EXTRA_FLAGS?= -shared
125hack.So: Makefile
126	touch hack.c
127	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
128	rm -f hack.c
129
130# This rule stops ./assym.s in .depend from causing problems.
131./assym.s: assym.s
132
133assym.s: $S/kern/genassym.sh genassym.o
134	NM='${NM}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
135
136genassym.o: $S/$M/$M/genassym.c
137	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
138
139${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
140
141kernel-depend: .depend
142# The argument list can be very long, so use make -V and xargs to
143# pass it to mkdep.
144.depend: assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
145	    ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
146	    ${MFILES:T:S/.m$/.h/}
147	rm -f .newdep
148	${MAKE} -V CFILES -V SYSTEM_CFILES -V GEN_CFILES | \
149	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
150	${MAKE} -V SFILES | \
151	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
152	rm -f .depend
153	mv .newdep .depend
154
155kernel-cleandepend:
156	rm -f .depend
157
158links:
159	egrep '#if' ${CFILES} | sed -f $S/conf/defines | \
160	    sed -e 's/:.*//' -e 's/\.c/.o/' | sort -u > dontlink
161	${MAKE} -V CFILES | tr -s ' ' '\12' | sed 's/\.c/.o/' | \
162	    sort -u | comm -23 - dontlink | \
163	    sed 's,../.*/\(.*.o\),rm -f \1;ln -s ../GENERIC/\1 \1,' > makelinks
164	sh makelinks; rm -f dontlink
165
166kernel-tags:
167	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
168	sh $S/conf/systags.sh
169	rm -f tags1
170	sed -e 's,      ../,    ,' tags > tags1
171
172.if ${MACHINE_ARCH} != "ia64"
173.if exists(${DESTDIR}/boot)
174kernel-install-check:
175	@if [ ! -f ${DESTDIR}/boot/device.hints ] ; then \
176		echo "You must set up a ${DESTDIR}/boot/device.hints file first." ; \
177		exit 1 ; \
178	fi
179	@if [ x"`grep device.hints ${DESTDIR}/boot/defaults/loader.conf ${DESTDIR}/boot/loader.conf`" = "x" ]; then \
180		echo "You must activate /boot/device.hints in loader.conf." ; \
181		exit 1 ; \
182	fi
183
184kernel-install: kernel-install-check
185.endif
186.endif
187
188kernel-install:
189	@if [ ! -f ${FULLKERNEL} ] ; then \
190		echo "You must build a kernel first." ; \
191		exit 1 ; \
192	fi
193.if exists(${DESTDIR}${KODIR})
194	-thiskernel=`sysctl -n kern.bootfile` ; \
195	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
196		chflags -R noschg ${DESTDIR}${KODIR} ; \
197		rm -rf ${DESTDIR}${KODIR} ; \
198	else \
199		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
200			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
201			rm -rf ${DESTDIR}${KODIR}.old ; \
202		fi ; \
203		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
204		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
205	fi
206.endif
207	mkdir -p ${DESTDIR}${KODIR}
208.if defined(DEBUG) && defined(INSTALL_DEBUG)
209	${INSTALL} -p -m 555 -o root -g wheel ${FULLKERNEL} ${DESTDIR}${KODIR}
210.else
211	${INSTALL} -p -m 555 -o root -g wheel ${KERNEL_KO} ${DESTDIR}${KODIR}
212.endif
213
214kernel-reinstall:
215	@-chflags -R noschg ${DESTDIR}${KODIR}
216.if defined(DEBUG) && defined(INSTALL_DEBUG)
217	${INSTALL} -p -m 555 -o root -g wheel ${FULLKERNEL} ${DESTDIR}${KODIR}
218.else
219	${INSTALL} -p -m 555 -o root -g wheel ${KERNEL_KO} ${DESTDIR}${KODIR}
220.endif
221
222config.o env.o hints.o vers.o vnode_if.o:
223	${NORMAL_C}
224
225config.ln env.ln hints.ln vers.ln vnode_if.ln:
226	${NORMAL_LINT}
227
228vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
229	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
230
231vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
232	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
233
234vnode_if.h: $S/tools/vnode_if.awk $S/kern/vnode_if.src
235	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
236	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
237	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
238
239# XXX strictly, everything depends on Makefile because changes to ${PROF}
240# only appear there, but we don't handle that.
241
242.include "kern.mk"
243