xref: /freebsd/sys/conf/kern.post.mk (revision aa0a1e58f0189b0fde359a8bda032887e72057fa)
1# $FreeBSD$
2
3# Part of a unified Makefile for building kernels.  This part includes all
4# the definitions that need to be after all the % directives except %RULES
5# and ones that act like they are part of %RULES.
6#
7# Most make variables should not be defined in this file.  Instead, they
8# should be defined in the kern.pre.mk so that port makefiles can
9# override or augment them.
10
11# In case the config had a makeoptions DESTDIR...
12.if defined(DESTDIR)
13MKMODULESENV+=	DESTDIR="${DESTDIR}"
14.endif
15SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;}
16MKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
17
18.if defined(CONF_CFLAGS)
19MKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
20.endif
21
22.if defined(WITH_CTF)
23MKMODULESENV+=	WITH_CTF="${WITH_CTF}"
24.endif
25
26.MAIN: all
27
28.for target in all clean cleandepend cleandir clobber depend install \
29    obj reinstall tags
30${target}: kernel-${target}
31.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
32${target}: modules-${target}
33modules-${target}:
34	cd $S/modules; ${MKMODULESENV} ${MAKE} \
35	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
36.endif
37.endfor
38
39# Handle out of tree ports
40.if !defined(NO_MODULES) && defined(PORTS_MODULES)
41PORTSMODULESENV=SYSDIR=${SYSDIR}
42.for __target in all install reinstall clean
43${__target}: ports-${__target}
44ports-${__target}:
45.for __i in ${PORTS_MODULES}
46	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
47.endfor
48.endfor
49.endif
50
51.ORDER: kernel-install modules-install
52
53kernel-all: ${KERNEL_KO} ${KERNEL_EXTRA}
54
55kernel-cleandir: kernel-clean kernel-cleandepend
56
57kernel-clobber:
58	find . -maxdepth 1 ! -type d ! -name version -delete
59
60kernel-obj:
61
62.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
63modules: modules-all
64
65.if !defined(NO_MODULES_OBJ)
66modules-all modules-depend: modules-obj
67.endif
68.endif
69
70.if !defined(DEBUG)
71FULLKERNEL=	${KERNEL_KO}
72.else
73FULLKERNEL=	${KERNEL_KO}.debug
74${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.symbols
75	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.symbols\
76	    ${FULLKERNEL} ${.TARGET}
77${KERNEL_KO}.symbols: ${FULLKERNEL}
78	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
79install.debug reinstall.debug: gdbinit
80	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
81
82# Install gdbinit files for kernel debugging.
83gdbinit:
84	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
85	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
86	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
87.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
88	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
89	    ${.CURDIR}/gdbinit.machine
90.endif
91.endif
92
93${FULLKERNEL}: ${SYSTEM_DEP} vers.o
94	@rm -f ${.TARGET}
95	@echo linking ${.TARGET}
96	${SYSTEM_LD}
97	@${SYSTEM_CTFMERGE}
98.if !defined(DEBUG)
99	${OBJCOPY} --strip-debug ${.TARGET}
100.endif
101	${SYSTEM_LD_TAIL}
102.if defined(MFS_IMAGE)
103	@sh ${S}/tools/embed_mfs.sh ${FULLKERNEL} ${MFS_IMAGE}
104.endif
105
106.if !exists(${.OBJDIR}/.depend)
107${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
108.endif
109
110LNFILES=	${CFILES:T:S/.c$/.ln/}
111
112.for mfile in ${MFILES}
113# XXX the low quality .m.o rules gnerated by config are normally used
114# instead of the .m.c rules here.
115${mfile:T:S/.m$/.c/}: ${mfile}
116	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
117${mfile:T:S/.m$/.h/}: ${mfile}
118	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
119.endfor
120
121kernel-clean:
122	rm -f *.o *.so *.So *.ko *.s eddep errs \
123	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.symbols \
124	    linterrs makelinks tags vers.c \
125	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
126	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
127	    ${CLEAN}
128
129lint: ${LNFILES}
130	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
131	    tee -a linterrs
132
133# This is a hack.  BFD "optimizes" away dynamic mode if there are no
134# dynamic references.  We could probably do a '-Bforcedynamic' mode like
135# in the a.out ld.  For now, this works.
136HACK_EXTRA_FLAGS?= -shared
137hack.So: Makefile
138	:> hack.c
139	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
140	rm -f hack.c
141
142# This rule stops ./assym.s in .depend from causing problems.
143./assym.s: assym.s
144
145assym.s: $S/kern/genassym.sh genassym.o
146	NM='${NM}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
147
148genassym.o: $S/$M/$M/genassym.c
149	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
150
151${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
152
153kernel-depend: .depend
154# The argument list can be very long, so use make -V and xargs to
155# pass it to mkdep.
156SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
157	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
158	${MFILES:T:S/.m$/.h/}
159.depend: .PRECIOUS ${SRCS}
160	rm -f .newdep
161	${MAKE} -V CFILES -V SYSTEM_CFILES -V GEN_CFILES | \
162	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
163	${MAKE} -V SFILES | \
164	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
165	rm -f .depend
166	mv .newdep .depend
167
168_ILINKS= machine
169.if ${MACHINE} != ${MACHINE_CPUARCH}
170_ILINKS+= ${MACHINE_CPUARCH}
171.endif
172.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
173_ILINKS+= x86
174.endif
175
176# Ensure that the link exists without depending on it when it exists.
177.for _link in ${_ILINKS}
178.if !exists(${.OBJDIR}/${_link})
179${SRCS}: ${_link}
180.endif
181.endfor
182
183${_ILINKS}:
184	@case ${.TARGET} in \
185	machine) \
186		path=${S}/${MACHINE}/include ;; \
187	*) \
188		path=${S}/${.TARGET}/include ;; \
189	esac ; \
190	${ECHO} ${.TARGET} "->" $$path ; \
191	ln -s $$path ${.TARGET}
192
193# .depend needs include links so we remove them only together.
194kernel-cleandepend:
195	rm -f .depend ${_ILINKS}
196
197links:
198	egrep '#if' ${CFILES} | sed -f $S/conf/defines | \
199	    sed -e 's/:.*//' -e 's/\.c/.o/' | sort -u > dontlink
200	${MAKE} -V CFILES | tr -s ' ' '\12' | sed 's/\.c/.o/' | \
201	    sort -u | comm -23 - dontlink | \
202	    sed 's,../.*/\(.*.o\),rm -f \1;ln -s ../GENERIC/\1 \1,' > makelinks
203	sh makelinks; rm -f dontlink
204
205kernel-tags:
206	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
207	sh $S/conf/systags.sh
208
209kernel-install:
210	@if [ ! -f ${KERNEL_KO} ] ; then \
211		echo "You must build a kernel first." ; \
212		exit 1 ; \
213	fi
214.if exists(${DESTDIR}${KODIR})
215	-thiskernel=`sysctl -n kern.bootfile` ; \
216	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
217		chflags -R noschg ${DESTDIR}${KODIR} ; \
218		rm -rf ${DESTDIR}${KODIR} ; \
219	else \
220		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
221			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
222			rm -rf ${DESTDIR}${KODIR}.old ; \
223		fi ; \
224		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
225		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
226	fi
227.endif
228	mkdir -p ${DESTDIR}${KODIR}
229	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
230.if defined(DEBUG) && !defined(INSTALL_NODEBUG)
231	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
232.endif
233.if defined(KERNEL_EXTRA_INSTALL)
234	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}
235.endif
236
237
238
239kernel-reinstall:
240	@-chflags -R noschg ${DESTDIR}${KODIR}
241	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
242.if defined(DEBUG) && !defined(INSTALL_NODEBUG)
243	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
244.endif
245
246config.o env.o hints.o vers.o vnode_if.o:
247	${NORMAL_C}
248	@[ -z "${CTFCONVERT}" -o -n "${NO_CTF}" ] || ${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
249
250config.ln env.ln hints.ln vers.ln vnode_if.ln:
251	${NORMAL_LINT}
252
253vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
254	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
255
256vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
257	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
258
259vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
260    $S/kern/vnode_if.src
261vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
262	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
263vnode_if_newproto.h:
264	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
265vnode_if_typedef.h:
266	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
267
268# XXX strictly, everything depends on Makefile because changes to ${PROF}
269# only appear there, but we don't handle that.
270
271.include "kern.mk"
272