xref: /freebsd/sys/conf/kern.post.mk (revision 6e3875ebcf0e768332735f8306bf4625b30ab61c)
19644b987SBruce Evans
29644b987SBruce Evans# Part of a unified Makefile for building kernels.  This part includes all
39644b987SBruce Evans# the definitions that need to be after all the % directives except %RULES
49644b987SBruce Evans# and ones that act like they are part of %RULES.
541c8eb30SWarner Losh#
699fd86afSWarner Losh# Most make variables should not be defined in this file.  Instead, they
799fd86afSWarner Losh# should be defined in the kern.pre.mk so that port makefiles can
8714b6aa6SWarner Losh# override or augment them.
941c8eb30SWarner Losh
10af44a263SKyle Evans.if defined(DTS) || defined(DTSO) || defined(FDT_DTS_FILE)
11dc463613SKyle Evans.include "dtb.build.mk"
12dc463613SKyle Evans
13dc463613SKyle EvansKERNEL_EXTRA+=	${DTB} ${DTBO}
14dc463613SKyle EvansCLEAN+=		${DTB} ${DTBO}
15dc463613SKyle Evans
16dc463613SKyle Evanskernel-install: _dtbinstall
17dc463613SKyle Evans.ORDER: beforeinstall _dtbinstall
18dc463613SKyle Evans.endif
19dc463613SKyle Evans
2089e50d0eSPoul-Henning Kamp# In case the config had a makeoptions DESTDIR...
2189e50d0eSPoul-Henning Kamp.if defined(DESTDIR)
2289e50d0eSPoul-Henning KampMKMODULESENV+=	DESTDIR="${DESTDIR}"
2389e50d0eSPoul-Henning Kamp.endif
242dcffa9cSBryan DrewerySYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
258859442eSJohn BaldwinMKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
269f7b089eSMatt MacyMKMODULESENV+=  MODULE_TIED=yes
2789e50d0eSPoul-Henning Kamp
2862769330SAlexander Leidinger.if defined(CONF_CFLAGS)
2962769330SAlexander LeidingerMKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
3062769330SAlexander Leidinger.endif
3162769330SAlexander Leidinger
3221edb039SAlexander Leidinger.if defined(WITH_CTF)
3321edb039SAlexander LeidingerMKMODULESENV+=	WITH_CTF="${WITH_CTF}"
3421edb039SAlexander Leidinger.endif
3521edb039SAlexander Leidinger
3624221389SAndrew Turner.if !empty(KCSAN_ENABLED)
37849aef49SAndrew TurnerMKMODULESENV+=	KCSAN_ENABLED="yes"
38849aef49SAndrew Turner.endif
39849aef49SAndrew Turner
405426539cSMatt Macy.if defined(GCOV_CFLAGS)
415426539cSMatt MacyMKMODULESENV+=	GCOV_CFLAGS="${GCOV_CFLAGS}"
425426539cSMatt Macy.endif
435426539cSMatt Macy
440f2d88d1SKonstantin Belousov.if !empty(COMPAT_FREEBSD32_ENABLED)
450f2d88d1SKonstantin BelousovMKMODULESENV+=	COMPAT_FREEBSD32_ENABLED="yes"
460f2d88d1SKonstantin Belousov.endif
470f2d88d1SKonstantin Belousov
4805117b57SEd Maste# Allow overriding the kernel debug directory, so kernel and user debug may be
4905117b57SEd Maste# installed in different directories. Setting it to "" restores the historical
5005117b57SEd Maste# behavior of installing debug files in the kernel directory.
5105117b57SEd MasteKERN_DEBUGDIR?=	${DEBUGDIR}
5205117b57SEd Maste
5373b893a7SRuslan Ermilov.MAIN: all
5441c8eb30SWarner Losh
55cd785c1bSJohn Baldwin.if !defined(NO_MODULES)
56cd785c1bSJohn Baldwin# Default prefix used for modules installed from ports
57cd785c1bSJohn BaldwinLOCALBASE?=	/usr/local
58cd785c1bSJohn Baldwin
59cd785c1bSJohn BaldwinLOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
60cd785c1bSJohn Baldwin
61cd785c1bSJohn Baldwin# Default to installing all modules installed by ports unless overridden
62cd785c1bSJohn Baldwin# by the user.
639b82c664SJohn Baldwin.if !defined(LOCAL_MODULES) && exists(${LOCAL_MODULES_DIR})
64cd785c1bSJohn BaldwinLOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
65cd785c1bSJohn Baldwin.endif
66cd785c1bSJohn Baldwin.endif
67cd785c1bSJohn Baldwin
68ab404da2SRuslan Ermilov.for target in all clean cleandepend cleandir clobber depend install \
696d75a7a8SBryan Drewery    ${_obj} reinstall tags
7073b893a7SRuslan Ermilov${target}: kernel-${target}
71cd785c1bSJohn Baldwin.if !defined(NO_MODULES)
7273b893a7SRuslan Ermilov${target}: modules-${target}
7373b893a7SRuslan Ermilovmodules-${target}:
74cd785c1bSJohn Baldwin.if !defined(MODULES_WITH_WORLD) && exists($S/modules)
75ab404da2SRuslan Ermilov	cd $S/modules; ${MKMODULESENV} ${MAKE} \
76ab404da2SRuslan Ermilov	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
7773b893a7SRuslan Ermilov.endif
78cd785c1bSJohn Baldwin.for module in ${LOCAL_MODULES}
799b82c664SJohn Baldwin	@${ECHODIR} "===> ${module} (${target:S/^reinstall$/install/:S/^clobber$/cleandir/})"
809b82c664SJohn Baldwin	@cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
819b82c664SJohn Baldwin	    DIRPRFX="${module}/" \
82cd785c1bSJohn Baldwin	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
83cd785c1bSJohn Baldwin.endfor
84cd785c1bSJohn Baldwin.endif
8573b893a7SRuslan Ermilov.endfor
8673b893a7SRuslan Ermilov
8724a5cfd2SDoug Barton# Handle ports (as defined by the user) that build kernel modules
889f74b222SWarner Losh.if !defined(NO_MODULES) && defined(PORTS_MODULES)
8924a5cfd2SDoug Barton#
9024a5cfd2SDoug Barton# The ports tree needs some environment variables defined to match the new kernel
9124a5cfd2SDoug Barton#
9224a5cfd2SDoug Barton# SRC_BASE is how the ports tree refers to the location of the base source files
9324a5cfd2SDoug Barton.if !defined(SRC_BASE)
9458bcfe41SBryan DrewerySRC_BASE=	${SYSDIR:H:tA}
9524a5cfd2SDoug Barton.endif
9624a5cfd2SDoug Barton# OSVERSION is used by some ports to determine build options
9724a5cfd2SDoug Barton.if !defined(OSRELDATE)
9824a5cfd2SDoug Barton# Definition copied from src/Makefile.inc1
9924a5cfd2SDoug BartonOSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
10024a5cfd2SDoug Barton		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
10124a5cfd2SDoug Barton.endif
10224a5cfd2SDoug Barton# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
103cd785c1bSJohn Baldwin#
104cd785c1bSJohn Baldwin# Ports search for some dependencies in PATH, so add the location of the
105cd785c1bSJohn Baldwin# installed files
1060a201eeaSIan LeporeWRKDIRPREFIX?=	${.OBJDIR}
10724a5cfd2SDoug BartonPORTSMODULESENV=\
108786c8050SBryan Drewery	env \
109786c8050SBryan Drewery	-u CC \
110786c8050SBryan Drewery	-u CXX \
111786c8050SBryan Drewery	-u CPP \
112283f8a5cSJason A. Harmening	-u MAKESYSPATH \
11328da5c6dSJason A. Harmening	-u MK_AUTO_OBJ \
11450160862SBryan Drewery	-u MAKEOBJDIR \
11528da5c6dSJason A. Harmening	MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
116283f8a5cSJason A. Harmening	SYSDIR=${SYSDIR} \
11724a5cfd2SDoug Barton	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
11824a5cfd2SDoug Barton	SRC_BASE=${SRC_BASE} \
11924a5cfd2SDoug Barton	OSVERSION=${OSRELDATE} \
12024a5cfd2SDoug Barton	WRKDIRPREFIX=${WRKDIRPREFIX}
1211e8e2a9dSDoug Barton
1221e8e2a9dSDoug Barton# The WRKDIR needs to be cleaned before building, and trying to change the target
1231e8e2a9dSDoug Barton# with a :C pattern below results in install -> instclean
1241e8e2a9dSDoug Bartonall:
1251e8e2a9dSDoug Barton.for __i in ${PORTS_MODULES}
1261e8e2a9dSDoug Barton	@${ECHO} "===> Ports module ${__i} (all)"
12712496806SYuri Pankov	cd ${PORTSDIR:U/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
1281e8e2a9dSDoug Barton.endfor
1291e8e2a9dSDoug Barton
1301e8e2a9dSDoug Barton.for __target in install reinstall clean
131f7479776SWarner Losh${__target}: ports-${__target}
132c8f0f808SWarner Loshports-${__target}:
1331fd4bb62SWarner Losh.for __i in ${PORTS_MODULES}
1341e8e2a9dSDoug Barton	@${ECHO} "===> Ports module ${__i} (${__target})"
13512496806SYuri Pankov	cd ${PORTSDIR:U/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
1361fd4bb62SWarner Losh.endfor
1371fd4bb62SWarner Losh.endfor
1381fd4bb62SWarner Losh.endif
1391fd4bb62SWarner Losh
1400b200141SAndrew Turner# Generate the .bin (booti images) kernel as an extra build output.
1410b200141SAndrew Turner# The targets and rules to generate these appear in Makefile.$MACHINE
1420b200141SAndrew Turner# if the platform supports it.
1430b200141SAndrew Turner.if ${MK_KERNEL_BIN} != "no"
1440b200141SAndrew TurnerKERNEL_EXTRA+= ${KERNEL_KO}.bin
1450b200141SAndrew TurnerKERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
1460b200141SAndrew TurnerCLEAN+=	${KERNEL_KO}.bin
1470b200141SAndrew Turner.endif
1480b200141SAndrew Turner
14973b893a7SRuslan Ermilov.ORDER: kernel-install modules-install
15073b893a7SRuslan Ermilov
151cc666701SBryan Drewerybeforebuild: .PHONY
152cc666701SBryan Drewerykernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
15373b893a7SRuslan Ermilov
154ee439c43SRuslan Ermilovkernel-cleandir: kernel-clean kernel-cleandepend
155ab404da2SRuslan Ermilov
156ab404da2SRuslan Ermilovkernel-clobber:
15704b603e5SRuslan Ermilov	find . -maxdepth 1 ! -type d ! -name version -delete
15804b603e5SRuslan Ermilov
15904b603e5SRuslan Ermilovkernel-obj:
16073b893a7SRuslan Ermilov
161cd785c1bSJohn Baldwin.if !defined(NO_MODULES)
16273b893a7SRuslan Ermilovmodules: modules-all
1630a036938SBryan Drewerymodules-depend: beforebuild
1640a036938SBryan Drewerymodules-all: beforebuild
16573b893a7SRuslan Ermilov
16673b893a7SRuslan Ermilov.if !defined(NO_MODULES_OBJ)
16773b893a7SRuslan Ermilovmodules-all modules-depend: modules-obj
16873b893a7SRuslan Ermilov.endif
16973b893a7SRuslan Ermilov.endif
17041c8eb30SWarner Losh
171697ca17bSWarner Losh.if !defined(DEBUG)
172697ca17bSWarner LoshFULLKERNEL=	${KERNEL_KO}
173697ca17bSWarner Losh.else
17405117b57SEd MasteFULLKERNEL=	${KERNEL_KO}.full
1755852ae2dSBryan Drewery${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
17605117b57SEd Maste	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
17798b45a8fSRuslan Ermilov	    ${FULLKERNEL} ${.TARGET}
1785852ae2dSBryan Drewery${KERNEL_KO}.debug: ${FULLKERNEL}
17998b45a8fSRuslan Ermilov	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
180d24864f7SRuslan Ermilovinstall.debug reinstall.debug: gdbinit
181d24864f7SRuslan Ermilov	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
182222aa33aSGreg Lehey
183c52c3a23SBruce Evans# Install gdbinit files for kernel debugging.
184222aa33aSGreg Leheygdbinit:
185c52c3a23SBruce Evans	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
186c52c3a23SBruce Evans	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
18708d0c00bSPoul-Henning Kamp	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
18825faff34SWarner Losh.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
18925faff34SWarner Losh	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
190222aa33aSGreg Lehey	    ${.CURDIR}/gdbinit.machine
19141c8eb30SWarner Losh.endif
192c7301f91SRuslan Ermilov.endif
19341c8eb30SWarner Losh
1945852ae2dSBryan Drewery${FULLKERNEL}: ${SYSTEM_DEP} vers.o
19541c8eb30SWarner Losh	@rm -f ${.TARGET}
19641c8eb30SWarner Losh	@echo linking ${.TARGET}
19741c8eb30SWarner Losh	${SYSTEM_LD}
198f4c1f0b9SAdrian Chadd.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
199f4c1f0b9SAdrian Chadd	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
200f4c1f0b9SAdrian Chadd.endif
20183cb5baeSMax Khon.if ${MK_CTF} != "no"
202a0a67cf0SWarner Losh	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
203a0a67cf0SWarner Losh	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
204cd49c2e8SMax Khon.endif
20541460aa5SRuslan Ermilov.if !defined(DEBUG)
20641460aa5SRuslan Ermilov	${OBJCOPY} --strip-debug ${.TARGET}
20741460aa5SRuslan Ermilov.endif
20841c8eb30SWarner Losh	${SYSTEM_LD_TAIL}
20941c8eb30SWarner Losh
21087c259d4SMatt MacyOBJS_DEPEND_GUESS+=	offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
211d7beff08SBryan Drewery			${MFILES:T:S/.m$/.h/}
2121d1f8d7cSMark Murray
21341c8eb30SWarner Losh.for mfile in ${MFILES}
21449b2349bSBruce Evans# XXX the low quality .m.o rules gnerated by config are normally used
21549b2349bSBruce Evans# instead of the .m.c rules here.
2165852ae2dSBryan Drewery${mfile:T:S/.m$/.c/}: ${mfile}
21749b2349bSBruce Evans	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
2185852ae2dSBryan Drewery${mfile:T:S/.m$/.h/}: ${mfile}
2199fa411aeSDavid E. O'Brien	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
22041c8eb30SWarner Losh.endfor
22141c8eb30SWarner Losh
22241c8eb30SWarner Loshkernel-clean:
2232b4da8aaSMarcel Moolenaar	rm -f *.o *.so *.pico *.ko *.s eddep errs \
22405117b57SEd Maste	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
2251cbb5888SWarner Losh	    tags vers.c \
2263365523eSRuslan Ermilov	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
22741c8eb30SWarner Losh	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
22874465532SRuslan Ermilov	    ${CLEAN}
22941c8eb30SWarner Losh
23041c8eb30SWarner Losh# This is a hack.  BFD "optimizes" away dynamic mode if there are no
23141c8eb30SWarner Losh# dynamic references.  We could probably do a '-Bforcedynamic' mode like
23241c8eb30SWarner Losh# in the a.out ld.  For now, this works.
233aea2a658SDavid E. O'Brienforce-dynamic-hack.c:
234aea2a658SDavid E. O'Brien	:> ${.TARGET}
235aea2a658SDavid E. O'Brien
236aea2a658SDavid E. O'Brienforce-dynamic-hack.pico: force-dynamic-hack.c Makefile
237aea2a658SDavid E. O'Brien	${CC} ${CCLDFLAGS} -shared ${CFLAGS} -nostdlib \
238aea2a658SDavid E. O'Brien	    force-dynamic-hack.c -o ${.TARGET}
23941c8eb30SWarner Losh
2406443773dSMatt Macyoffset.inc: $S/kern/genoffset.sh genoffset.o
2416443773dSMatt Macy	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
2426443773dSMatt Macy
2436443773dSMatt Macygenoffset.o: $S/kern/genoffset.c
244*12a6257aSAndrew Turner	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} \
2458e1a7e29SMark Johnston	    -fcommon $S/kern/genoffset.c
2466443773dSMatt Macy
247018617b8SMatt Macy# genoffset_test.o is not actually used for anything - the point of compiling it
248018617b8SMatt Macy# is to exercise the CTASSERT that checks that the offsets in the offset.inc
249018617b8SMatt Macy# _lite struct(s) match those in the original(s).
2504cee7eaeSBryan Drewerygenoffset_test.o: $S/kern/genoffset.c offset.inc
251*12a6257aSAndrew Turner	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} \
2528e1a7e29SMark Johnston	    -fcommon -DOFFSET_TEST $S/kern/genoffset.c -o ${.TARGET}
2536443773dSMatt Macy
2546443773dSMatt Macyassym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
2551558258bSRui Paulo	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
25641c8eb30SWarner Losh
2576443773dSMatt Macygenassym.o: $S/$M/$M/genassym.c  offset.inc
258*12a6257aSAndrew Turner	${CC} -c ${NOSAN_CFLAGS:N-flto*:N-fno-common} \
2598e1a7e29SMark Johnston	    -fcommon $S/$M/$M/genassym.c
26041c8eb30SWarner Losh
261817f7baaSBryan DreweryOBJS_DEPEND_GUESS+= opt_global.h
262817f7baaSBryan Drewerygenoffset.o genassym.o vers.o: opt_global.h
26341c8eb30SWarner Losh
264799483e4SBryan Drewery.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
265799483e4SBryan Drewery_meta_filemon=	1
266799483e4SBryan Drewery.endif
267e9d6b699SSimon J. Gerraty.if ${MK_DIRDEPS_BUILD} == "no"
268c30c436aSBryan Drewery# Skip reading .depend when not needed to speed up tree-walks and simple
269c30c436aSBryan Drewery# lookups.  For install, only do this if no other targets are specified.
270799483e4SBryan Drewery# Also skip generating or including .depend.* files if in meta+filemon mode
271210ecc00SBryan Drewery# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
272210ecc00SBryan Drewery# for _meta_filemon but not for _SKIP_DEPEND.
273906ce865SBryan Drewery.if !defined(NO_SKIP_DEPEND) && \
27471b12654SBryan Drewery    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
27571b12654SBryan Drewery    ${.TARGETS:M*obj} == ${.TARGETS} || \
276202d6f8cSBryan Drewery    ${.TARGETS:M*clean*} == ${.TARGETS} || \
277ecc39238SBryan Drewery    ${.TARGETS:M*install*} == ${.TARGETS})
278210ecc00SBryan Drewery_SKIP_DEPEND=	1
279210ecc00SBryan Drewery.endif
280210ecc00SBryan Drewery.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
2819fb02a70SBryan Drewery.MAKE.DEPENDFILE=	/dev/null
2829fb02a70SBryan Drewery.endif
283e9d6b699SSimon J. Gerraty.endif
2849fb02a70SBryan Drewery
285a13914f6SRuslan Ermilovkernel-depend: .depend
2866443773dSMatt MacySRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
2876324e649SWarner Losh	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
2886324e649SWarner Losh	${MFILES:T:S/.m$/.h/}
289a46134b6SBryan DreweryDEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
290cd1acc77SBryan DreweryDEPENDOBJS+=	${CLEAN:M*.o}
291b4585565SBryan DreweryDEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
292e32a2cbeSBryan Drewery.if ${MAKE_VERSION} < 20160220
293e32a2cbeSBryan DreweryDEPEND_MP?=	-MP
294e32a2cbeSBryan Drewery.endif
295210ecc00SBryan Drewery.if defined(_SKIP_DEPEND)
296210ecc00SBryan Drewery# Don't bother reading any .meta files
297210ecc00SBryan Drewery${DEPENDOBJS}:	.NOMETA
298210ecc00SBryan Drewery.depend:	.NOMETA
299210ecc00SBryan Drewery# Unset these to avoid looping/statting on them later.
300210ecc00SBryan Drewery.undef DEPENDOBJS
301b4585565SBryan Drewery.undef DEPENDFILES
302210ecc00SBryan Drewery.endif	# defined(_SKIP_DEPEND)
303e32a2cbeSBryan DreweryDEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
304cf1eeb33SBryan DreweryDEPEND_CFLAGS+=	-MT${.TARGET}
30570ca9ec4SBryan Drewery.if !defined(_meta_filemon)
306c2fd1d01SBryan Drewery.if !empty(DEPEND_CFLAGS)
307f84e07cbSBryan Drewery# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
308f84e07cbSBryan Drewery# as those are the only ones we will include.
309d7296a8fSBryan DreweryDEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
310f84e07cbSBryan DreweryCFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
311f84e07cbSBryan Drewery.endif
312b4585565SBryan Drewery.for __depend_obj in ${DEPENDFILES}
313e32a2cbeSBryan Drewery.if ${MAKE_VERSION} < 20160220
314e9508dc2SBryan Drewery.sinclude "${.OBJDIR}/${__depend_obj}"
315e32a2cbeSBryan Drewery.else
316e32a2cbeSBryan Drewery.dinclude "${.OBJDIR}/${__depend_obj}"
317e32a2cbeSBryan Drewery.endif
31870ca9ec4SBryan Drewery.endfor
31970ca9ec4SBryan Drewery.endif	# !defined(_meta_filemon)
320b9cd412eSBryan Drewery
321b9cd412eSBryan Drewery# Always run 'make depend' to generate dependencies early and to avoid the
322b9cd412eSBryan Drewery# need for manually running it.  For the kernel this is mostly a NOP since
323b9cd412eSBryan Drewery# all dependencies are correctly added or accounted for.  This is mostly to
324b9cd412eSBryan Drewery# ensure downstream uses of kernel-depend are handled.
325b9cd412eSBryan Drewerybeforebuild: kernel-depend
32670ca9ec4SBryan Drewery
32770ca9ec4SBryan Drewery# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
32870ca9ec4SBryan Drewery# For meta+filemon the .meta file is checked for since it is the dependency
32970ca9ec4SBryan Drewery# file used.
33070ca9ec4SBryan Drewery.for __obj in ${DEPENDOBJS:O:u}
331711019f7SBryan Drewery.if defined(_meta_filemon)
332711019f7SBryan Drewery_depfile=	${.OBJDIR}/${__obj}.meta
333711019f7SBryan Drewery.else
334711019f7SBryan Drewery_depfile=	${.OBJDIR}/.depend.${__obj}
335711019f7SBryan Drewery.endif
336711019f7SBryan Drewery.if !exists(${_depfile})
337d7beff08SBryan Drewery.if ${SYSTEM_OBJS:M${__obj}}
338d7beff08SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS}
3399f286e82SBryan Drewery.endif
340d7beff08SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
3413b828e44SBryan Drewery.elif defined(_meta_filemon)
3423b828e44SBryan Drewery# For meta mode we still need to know which file to depend on to avoid
3433b828e44SBryan Drewery# ambiguous suffix transformation rules from .PATH.  Meta mode does not
344c155deccSBryan Drewery# use .depend files.  We really only need source files, not headers since
345c155deccSBryan Drewery# they are typically in SRCS/beforebuild already.  For target-specific
346c155deccSBryan Drewery# guesses do include headers though since they may not be in SRCS.
3473b828e44SBryan Drewery.if ${SYSTEM_OBJS:M${__obj}}
3483b828e44SBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
3493b828e44SBryan Drewery.endif
350c155deccSBryan Drewery${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
351711019f7SBryan Drewery.endif	# !exists(${_depfile})
352d7beff08SBryan Drewery.endfor
353cf1eeb33SBryan Drewery
354b4585565SBryan Drewery.NOPATH: .depend ${DEPENDFILES}
355cf1eeb33SBryan Drewery
3565852ae2dSBryan Drewery.depend: .PRECIOUS ${SRCS}
35741c8eb30SWarner Losh
358a90232e1SRuslan Ermilov_ILINKS= machine
3598598392cSKonstantin Belousov.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
360915b4b09SNathan Whitehorn_ILINKS+= ${MACHINE_CPUARCH}
361a90232e1SRuslan Ermilov.endif
362544de89dSJohn Baldwin.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
363544de89dSJohn Baldwin_ILINKS+= x86
364544de89dSJohn Baldwin.endif
365d07600c5SBrooks Davis.if ${MACHINE_CPUARCH} == "amd64"
366d07600c5SBrooks Davis_ILINKS+= i386
367d07600c5SBrooks Davis.endif
368a90232e1SRuslan Ermilov
369a90232e1SRuslan Ermilov# Ensure that the link exists without depending on it when it exists.
3707114b176SMark Johnston# Ensure that debug info references the path in the source tree.
371a90232e1SRuslan Ermilov.for _link in ${_ILINKS}
372a90232e1SRuslan Ermilov.if !exists(${.OBJDIR}/${_link})
37329317e6aSBryan Drewery${SRCS} ${DEPENDOBJS}: ${_link}
374a90232e1SRuslan Ermilov.endif
3757114b176SMark Johnston.if ${_link} == "machine"
3767114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
3777114b176SMark Johnston.else
3787114b176SMark JohnstonCFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
3797114b176SMark Johnston.endif
380a90232e1SRuslan Ermilov.endfor
381a90232e1SRuslan Ermilov
382a90232e1SRuslan Ermilov${_ILINKS}:
383a90232e1SRuslan Ermilov	@case ${.TARGET} in \
384a90232e1SRuslan Ermilov	machine) \
385a90232e1SRuslan Ermilov		path=${S}/${MACHINE}/include ;; \
386544de89dSJohn Baldwin	*) \
387544de89dSJohn Baldwin		path=${S}/${.TARGET}/include ;; \
388a90232e1SRuslan Ermilov	esac ; \
389a90232e1SRuslan Ermilov	${ECHO} ${.TARGET} "->" $$path ; \
390454ce383SAlex Richardson	ln -fns $$path ${.TARGET}
391a90232e1SRuslan Ermilov
39274465532SRuslan Ermilov# .depend needs include links so we remove them only together.
393cf1eeb33SBryan Drewerykernel-cleandepend: .PHONY
394b4585565SBryan Drewery	rm -f .depend .depend.* ${_ILINKS}
39541c8eb30SWarner Losh
39641c8eb30SWarner Loshkernel-tags:
39710cd2843SYoshihiro Takahashi	@ls .depend.* > /dev/null 2>&1 || \
39882952c40SBryan Drewery	    { echo "you must make all first"; exit 1; }
39941c8eb30SWarner Losh	sh $S/conf/systags.sh
40041c8eb30SWarner Losh
40149773547SBryan Drewerykernel-install: .PHONY
40298b45a8fSRuslan Ermilov	@if [ ! -f ${KERNEL_KO} ] ; then \
40341c8eb30SWarner Losh		echo "You must build a kernel first." ; \
40441c8eb30SWarner Losh		exit 1 ; \
40541c8eb30SWarner Losh	fi
40641c8eb30SWarner Losh.if exists(${DESTDIR}${KODIR})
4075bb9250eSAlex Richardson	-thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
4081afc807bSRuslan Ermilov	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
40941c8eb30SWarner Losh		chflags -R noschg ${DESTDIR}${KODIR} ; \
41041c8eb30SWarner Losh		rm -rf ${DESTDIR}${KODIR} ; \
41105117b57SEd Maste		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
41241c8eb30SWarner Losh	else \
41341c8eb30SWarner Losh		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
41441c8eb30SWarner Losh			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
41541c8eb30SWarner Losh			rm -rf ${DESTDIR}${KODIR}.old ; \
41641c8eb30SWarner Losh		fi ; \
41741c8eb30SWarner Losh		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
41805117b57SEd Maste		if [ -n "${KERN_DEBUGDIR}" -a \
41905117b57SEd Maste		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
42005117b57SEd Maste			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
42105117b57SEd Maste			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
42205117b57SEd Maste		fi ; \
42398a32cb1SRuslan Ermilov		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
42441c8eb30SWarner Losh	fi
42541c8eb30SWarner Losh.endif
42641c8eb30SWarner Losh	mkdir -p ${DESTDIR}${KODIR}
427d3849ec3SKonstantin Belousov	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
428cd95660fSDavid E. O'Brien.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
42905117b57SEd Maste	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
430d3849ec3SKonstantin Belousov	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
431697ca17bSWarner Losh.endif
43292d0be02SWarner Losh.if defined(KERNEL_EXTRA_INSTALL)
433d3849ec3SKonstantin Belousov	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
43492d0be02SWarner Losh.endif
43592d0be02SWarner Losh
43692d0be02SWarner Losh
43741c8eb30SWarner Losh
4382cbc81ffSRuslan Ermilovkernel-reinstall:
43941c8eb30SWarner Losh	@-chflags -R noschg ${DESTDIR}${KODIR}
440d3849ec3SKonstantin Belousov	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
441cd95660fSDavid E. O'Brien.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
442d3849ec3SKonstantin Belousov	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
443697ca17bSWarner Losh.endif
44441c8eb30SWarner Losh
4455852ae2dSBryan Dreweryconfig.o env.o hints.o vers.o vnode_if.o:
44641c8eb30SWarner Losh	${NORMAL_C}
44783cb5baeSMax Khon	${NORMAL_CTFCONVERT}
44841c8eb30SWarner Losh
44989f361f7SSimon J. GerratyNEWVERS_ENV+= MAKE="${MAKE}"
450602f95d4SEd Maste.if ${MK_REPRODUCIBLE_BUILD} != "no"
45189f361f7SSimon J. GerratyNEWVERS_ARGS+= -R
452602f95d4SEd Maste.endif
453aea2a658SDavid E. O'Brienvers.c: .NOMETA_CMP $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP:Nvers.*}
45489f361f7SSimon J. Gerraty	${NEWVERS_ENV} sh $S/conf/newvers.sh ${NEWVERS_ARGS} ${KERN_IDENT}
45541c8eb30SWarner Losh
4565852ae2dSBryan Dreweryvnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
457b7bbb709SDavid E. O'Brien	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
45841c8eb30SWarner Losh
459a10892b0SRuslan Ermilovvnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
4605852ae2dSBryan Drewery    $S/kern/vnode_if.src
4615852ae2dSBryan Dreweryvnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
462b7bbb709SDavid E. O'Brien	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
4635852ae2dSBryan Dreweryvnode_if_newproto.h:
464aec0fb7bSPoul-Henning Kamp	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
4655852ae2dSBryan Dreweryvnode_if_typedef.h:
466aec0fb7bSPoul-Henning Kamp	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
46741c8eb30SWarner Losh
468cc787e3dSMarcel Moolenaar.if ${MFS_IMAGE:Uno} != "no"
469f4c1f0b9SAdrian Chadd.if empty(MD_ROOT_SIZE_CONFIGURED)
470f54c6ef1SJohn Baldwinembedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
4718a6ab0f7SJessica Clarke	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE=\""${MFS_IMAGE}"\" -c \
472f54c6ef1SJohn Baldwin	    $S/dev/md/embedfs.S -o ${.TARGET}
473cc787e3dSMarcel Moolenaar.endif
474f4c1f0b9SAdrian Chadd.endif
475cc787e3dSMarcel Moolenaar
476ddc474a8SRuslan Ermilov.include "kern.mk"
477