xref: /freebsd/sys/conf/kern.post.mk (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
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 ports (as defined by the user) that build kernel modules
40.if !defined(NO_MODULES) && defined(PORTS_MODULES)
41#
42# The ports tree needs some environment variables defined to match the new kernel
43#
44# Ports search for some dependencies in PATH, so add the location of the installed files
45LOCALBASE?=	/usr/local
46# SRC_BASE is how the ports tree refers to the location of the base source files
47.if !defined(SRC_BASE)
48SRC_BASE!=	realpath "${SYSDIR:H}/"
49.endif
50# OSVERSION is used by some ports to determine build options
51.if !defined(OSRELDATE)
52# Definition copied from src/Makefile.inc1
53OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
54		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
55.endif
56# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
57WRKDIRPREFIX?=	${MAKEOBJDIRPREFIX}${SRC_BASE}/sys/${KERNCONF}
58PORTSMODULESENV=\
59	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
60	SRC_BASE=${SRC_BASE} \
61	OSVERSION=${OSRELDATE} \
62	WRKDIRPREFIX=${WRKDIRPREFIX}
63
64# The WRKDIR needs to be cleaned before building, and trying to change the target
65# with a :C pattern below results in install -> instclean
66all:
67.for __i in ${PORTS_MODULES}
68	@${ECHO} "===> Ports module ${__i} (all)"
69	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean all
70.endfor
71
72.for __target in install reinstall clean
73${__target}: ports-${__target}
74ports-${__target}:
75.for __i in ${PORTS_MODULES}
76	@${ECHO} "===> Ports module ${__i} (${__target})"
77	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
78.endfor
79.endfor
80.endif
81
82.ORDER: kernel-install modules-install
83
84kernel-all: ${KERNEL_KO} ${KERNEL_EXTRA}
85
86kernel-cleandir: kernel-clean kernel-cleandepend
87
88kernel-clobber:
89	find . -maxdepth 1 ! -type d ! -name version -delete
90
91kernel-obj:
92
93.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
94modules: modules-all
95
96.if !defined(NO_MODULES_OBJ)
97modules-all modules-depend: modules-obj
98.endif
99.endif
100
101.if !defined(DEBUG)
102FULLKERNEL=	${KERNEL_KO}
103.else
104FULLKERNEL=	${KERNEL_KO}.debug
105${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.symbols
106	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.symbols\
107	    ${FULLKERNEL} ${.TARGET}
108${KERNEL_KO}.symbols: ${FULLKERNEL}
109	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
110install.debug reinstall.debug: gdbinit
111	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
112
113# Install gdbinit files for kernel debugging.
114gdbinit:
115	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
116	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
117	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
118.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
119	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
120	    ${.CURDIR}/gdbinit.machine
121.endif
122.endif
123
124${FULLKERNEL}: ${SYSTEM_DEP} vers.o ${MFS_IMAGE}
125	@rm -f ${.TARGET}
126	@echo linking ${.TARGET}
127	${SYSTEM_LD}
128.if ${MK_CTF} != "no"
129	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
130	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
131.endif
132.if !defined(DEBUG)
133	${OBJCOPY} --strip-debug ${.TARGET}
134.endif
135	${SYSTEM_LD_TAIL}
136.if defined(MFS_IMAGE)
137	sh ${S}/tools/embed_mfs.sh ${FULLKERNEL} ${MFS_IMAGE}
138.endif
139
140.if !exists(${.OBJDIR}/.depend)
141${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
142.endif
143
144LNFILES=	${CFILES:T:S/.c$/.ln/}
145
146.for mfile in ${MFILES}
147# XXX the low quality .m.o rules gnerated by config are normally used
148# instead of the .m.c rules here.
149${mfile:T:S/.m$/.c/}: ${mfile}
150	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
151${mfile:T:S/.m$/.h/}: ${mfile}
152	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
153.endfor
154
155kernel-clean:
156	rm -f *.o *.so *.So *.ko *.s eddep errs \
157	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.symbols \
158	    linterrs tags vers.c \
159	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
160	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
161	    ${CLEAN}
162
163lint: ${LNFILES}
164	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
165	    tee -a linterrs
166
167# This is a hack.  BFD "optimizes" away dynamic mode if there are no
168# dynamic references.  We could probably do a '-Bforcedynamic' mode like
169# in the a.out ld.  For now, this works.
170HACK_EXTRA_FLAGS?= -shared
171hack.So: Makefile
172	:> hack.c
173	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
174	rm -f hack.c
175
176# This rule stops ./assym.s in .depend from causing problems.
177./assym.s: assym.s
178
179assym.s: $S/kern/genassym.sh genassym.o
180	NM='${NM}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
181
182genassym.o: $S/$M/$M/genassym.c
183	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
184
185${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
186
187# We have "special" -I include paths for zfs/dtrace files in 'depend'.
188CFILES_NOCDDL=	${CFILES:N*/cddl/*:N*fs/nfsclient/nfs_clkdtrace*}
189SFILES_NOCDDL=	${SFILES:N*/cddl/*}
190CFILES_CDDL=	${CFILES:M*/cddl/*}
191SFILES_CDDL=	${SFILES:M*/cddl/*}
192
193kernel-depend: .depend
194# The argument list can be very long, so use make -V and xargs to
195# pass it to mkdep.
196SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
197	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
198	${MFILES:T:S/.m$/.h/}
199.depend: .PRECIOUS ${SRCS}
200	rm -f .newdep
201	${MAKE} -V CFILES_NOCDDL -V SYSTEM_CFILES -V GEN_CFILES | \
202	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
203	${MAKE} -V CFILES_CDDL | \
204	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${ZFS_CFLAGS} ${FBT_CFLAGS} ${DTRACE_CFLAGS}
205	${MAKE} -V SFILES_NOCDDL | \
206	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
207	${MAKE} -V SFILES_CDDL | \
208	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ZFS_ASM_CFLAGS}
209	rm -f .depend
210	mv .newdep .depend
211
212_ILINKS= machine
213.if ${MACHINE} != ${MACHINE_CPUARCH}
214_ILINKS+= ${MACHINE_CPUARCH}
215.endif
216.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
217_ILINKS+= x86
218.endif
219
220# Ensure that the link exists without depending on it when it exists.
221.for _link in ${_ILINKS}
222.if !exists(${.OBJDIR}/${_link})
223${SRCS} ${CLEAN:M*.o}: ${_link}
224.endif
225.endfor
226
227${_ILINKS}:
228	@case ${.TARGET} in \
229	machine) \
230		path=${S}/${MACHINE}/include ;; \
231	*) \
232		path=${S}/${.TARGET}/include ;; \
233	esac ; \
234	${ECHO} ${.TARGET} "->" $$path ; \
235	ln -s $$path ${.TARGET}
236
237# .depend needs include links so we remove them only together.
238kernel-cleandepend:
239	rm -f .depend ${_ILINKS}
240
241kernel-tags:
242	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
243	sh $S/conf/systags.sh
244
245kernel-install:
246	@if [ ! -f ${KERNEL_KO} ] ; then \
247		echo "You must build a kernel first." ; \
248		exit 1 ; \
249	fi
250.if exists(${DESTDIR}${KODIR})
251	-thiskernel=`sysctl -n kern.bootfile` ; \
252	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
253		chflags -R noschg ${DESTDIR}${KODIR} ; \
254		rm -rf ${DESTDIR}${KODIR} ; \
255	else \
256		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
257			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
258			rm -rf ${DESTDIR}${KODIR}.old ; \
259		fi ; \
260		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
261		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
262	fi
263.endif
264	mkdir -p ${DESTDIR}${KODIR}
265	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
266.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
267	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
268.endif
269.if defined(KERNEL_EXTRA_INSTALL)
270	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}
271.endif
272
273
274
275kernel-reinstall:
276	@-chflags -R noschg ${DESTDIR}${KODIR}
277	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
278.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
279	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.symbols ${DESTDIR}${KODIR}
280.endif
281
282config.o env.o hints.o vers.o vnode_if.o:
283	${NORMAL_C}
284	${NORMAL_CTFCONVERT}
285
286config.ln env.ln hints.ln vers.ln vnode_if.ln:
287	${NORMAL_LINT}
288
289vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
290	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
291
292vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
293	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
294
295vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
296    $S/kern/vnode_if.src
297vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
298	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
299vnode_if_newproto.h:
300	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
301vnode_if_typedef.h:
302	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
303
304# XXX strictly, everything depends on Makefile because changes to ${PROF}
305# only appear there, but we don't handle that.
306
307.include "kern.mk"
308