xref: /freebsd/sys/conf/kern.post.mk (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1
2# Part of a unified Makefile for building kernels.  This part includes all
3# the definitions that need to be after all the % directives except %RULES
4# and ones that act like they are part of %RULES.
5#
6# Most make variables should not be defined in this file.  Instead, they
7# should be defined in the kern.pre.mk so that port makefiles can
8# override or augment them.
9
10.if defined(DTS) || defined(DTSO) || defined(FDT_DTS_FILE)
11.include "dtb.build.mk"
12
13KERNEL_EXTRA+=	${DTB} ${DTBO}
14CLEAN+=		${DTB} ${DTBO}
15
16kernel-install: _dtbinstall
17.ORDER: beforeinstall _dtbinstall
18.endif
19
20# In case the config had a makeoptions DESTDIR...
21.if defined(DESTDIR)
22MKMODULESENV+=	DESTDIR="${DESTDIR}"
23.endif
24SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
25MKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
26MKMODULESENV+=  MODULE_TIED=yes
27
28.if defined(CONF_CFLAGS)
29MKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
30.endif
31
32.if defined(WITH_CTF)
33MKMODULESENV+=	WITH_CTF="${WITH_CTF}"
34.endif
35
36.if !empty(KCSAN_ENABLED)
37MKMODULESENV+=	KCSAN_ENABLED="yes"
38.endif
39
40.if defined(SAN_CFLAGS)
41MKMODULESENV+=	SAN_CFLAGS="${SAN_CFLAGS}"
42.endif
43
44.if defined(GCOV_CFLAGS)
45MKMODULESENV+=	GCOV_CFLAGS="${GCOV_CFLAGS}"
46.endif
47
48.if !empty(COMPAT_FREEBSD32_ENABLED)
49MKMODULESENV+=	COMPAT_FREEBSD32_ENABLED="yes"
50.endif
51
52# Allow overriding the kernel debug directory, so kernel and user debug may be
53# installed in different directories. Setting it to "" restores the historical
54# behavior of installing debug files in the kernel directory.
55KERN_DEBUGDIR?=	${DEBUGDIR}
56
57.MAIN: all
58
59.if !defined(NO_MODULES)
60# Default prefix used for modules installed from ports
61LOCALBASE?=	/usr/local
62
63LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
64
65# Default to installing all modules installed by ports unless overridden
66# by the user.
67.if !defined(LOCAL_MODULES) && exists(${LOCAL_MODULES_DIR})
68LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
69.endif
70.endif
71
72.for target in all clean cleandepend cleandir clobber depend install \
73    ${_obj} reinstall tags
74${target}: kernel-${target}
75.if !defined(NO_MODULES)
76${target}: modules-${target}
77modules-${target}:
78.if !defined(MODULES_WITH_WORLD) && exists($S/modules)
79	cd $S/modules; ${MKMODULESENV} ${MAKE} \
80	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
81.endif
82.for module in ${LOCAL_MODULES}
83	@${ECHODIR} "===> ${module} (${target:S/^reinstall$/install/:S/^clobber$/cleandir/})"
84	@cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
85	    DIRPRFX="${module}/" \
86	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
87.endfor
88.endif
89.endfor
90
91# Handle ports (as defined by the user) that build kernel modules
92.if !defined(NO_MODULES) && defined(PORTS_MODULES)
93#
94# The ports tree needs some environment variables defined to match the new kernel
95#
96# SRC_BASE is how the ports tree refers to the location of the base source files
97.if !defined(SRC_BASE)
98SRC_BASE=	${SYSDIR:H:tA}
99.endif
100# OSVERSION is used by some ports to determine build options
101.if !defined(OSRELDATE)
102# Definition copied from src/Makefile.inc1
103OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
104		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
105.endif
106# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
107#
108# Ports search for some dependencies in PATH, so add the location of the
109# installed files
110WRKDIRPREFIX?=	${.OBJDIR}
111PORTSMODULESENV=\
112	env \
113	-u CC \
114	-u CXX \
115	-u CPP \
116	-u MAKESYSPATH \
117	-u MK_AUTO_OBJ \
118	-u MAKEOBJDIR \
119	MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
120	SYSDIR=${SYSDIR} \
121	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
122	SRC_BASE=${SRC_BASE} \
123	OSVERSION=${OSRELDATE} \
124	WRKDIRPREFIX=${WRKDIRPREFIX}
125
126# The WRKDIR needs to be cleaned before building, and trying to change the target
127# with a :C pattern below results in install -> instclean
128all:
129.for __i in ${PORTS_MODULES}
130	@${ECHO} "===> Ports module ${__i} (all)"
131	cd ${PORTSDIR:U/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
132.endfor
133
134.for __target in install reinstall clean
135${__target}: ports-${__target}
136ports-${__target}:
137.for __i in ${PORTS_MODULES}
138	@${ECHO} "===> Ports module ${__i} (${__target})"
139	cd ${PORTSDIR:U/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
140.endfor
141.endfor
142.endif
143
144# Generate the .bin (booti images) kernel as an extra build output.
145# The targets and rules to generate these appear in Makefile.$MACHINE
146# if the platform supports it.
147.if ${MK_KERNEL_BIN} != "no"
148KERNEL_EXTRA+= ${KERNEL_KO}.bin
149KERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
150CLEAN+=	${KERNEL_KO}.bin
151.endif
152
153.ORDER: kernel-install modules-install
154
155beforebuild: .PHONY
156kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
157
158kernel-cleandir: kernel-clean kernel-cleandepend
159
160kernel-clobber:
161	find . -maxdepth 1 ! -type d ! -name version -delete
162
163kernel-obj:
164
165.if !defined(NO_MODULES)
166modules: modules-all
167modules-depend: beforebuild
168modules-all: beforebuild
169
170.if !defined(NO_MODULES_OBJ)
171modules-all modules-depend: modules-obj
172.endif
173.endif
174
175.if !defined(DEBUG)
176FULLKERNEL=	${KERNEL_KO}
177.else
178FULLKERNEL=	${KERNEL_KO}.full
179${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
180	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
181	    ${FULLKERNEL} ${.TARGET}
182${KERNEL_KO}.debug: ${FULLKERNEL}
183	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
184install.debug reinstall.debug: gdbinit
185	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
186
187# Install gdbinit files for kernel debugging.
188gdbinit:
189	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
190	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
191	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
192.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
193	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
194	    ${.CURDIR}/gdbinit.machine
195.endif
196.endif
197
198${FULLKERNEL}: ${SYSTEM_DEP} vers.o
199	@rm -f ${.TARGET}
200	@echo linking ${.TARGET}
201	${SYSTEM_LD}
202.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
203	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
204.endif
205.if ${MK_CTF} != "no"
206	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
207	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
208.endif
209.if !defined(DEBUG)
210	${OBJCOPY} --strip-debug ${.TARGET}
211.endif
212	${SYSTEM_LD_TAIL}
213
214OBJS_DEPEND_GUESS+=	offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
215			${MFILES:T:S/.m$/.h/}
216
217.for mfile in ${MFILES}
218# XXX the low quality .m.o rules gnerated by config are normally used
219# instead of the .m.c rules here.
220${mfile:T:S/.m$/.c/}: ${mfile}
221	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
222${mfile:T:S/.m$/.h/}: ${mfile}
223	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
224.endfor
225
226kernel-clean:
227	rm -f *.o *.so *.pico *.ko *.s eddep errs \
228	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
229	    tags vers.c \
230	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
231	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
232	    ${CLEAN}
233
234# This is a hack.  BFD "optimizes" away dynamic mode if there are no
235# dynamic references.  We could probably do a '-Bforcedynamic' mode like
236# in the a.out ld.  For now, this works.
237force-dynamic-hack.c:
238	:> ${.TARGET}
239
240force-dynamic-hack.pico: force-dynamic-hack.c Makefile
241	${CC} ${CCLDFLAGS} -shared ${CFLAGS} -nostdlib \
242	    force-dynamic-hack.c -o ${.TARGET}
243
244offset.inc: $S/kern/genoffset.sh genoffset.o
245	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
246
247genoffset.o: $S/kern/genoffset.c
248	${CC} -c ${CFLAGS:N-flto*:N-fno-common:N-fsanitize*:N-fno-sanitize*} \
249	    -fcommon $S/kern/genoffset.c
250
251# genoffset_test.o is not actually used for anything - the point of compiling it
252# is to exercise the CTASSERT that checks that the offsets in the offset.inc
253# _lite struct(s) match those in the original(s).
254genoffset_test.o: $S/kern/genoffset.c offset.inc
255	${CC} -c ${CFLAGS:N-flto*:N-fno-common:N-fsanitize*:N-fno-sanitize*} \
256	    -fcommon -DOFFSET_TEST $S/kern/genoffset.c -o ${.TARGET}
257
258assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
259	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
260
261genassym.o: $S/$M/$M/genassym.c  offset.inc
262	${CC} -c ${CFLAGS:N-flto*:N-fno-common:N-fsanitize*:N-fno-sanitize*} \
263	    -fcommon $S/$M/$M/genassym.c
264
265OBJS_DEPEND_GUESS+= opt_global.h
266genoffset.o genassym.o vers.o: opt_global.h
267
268.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
269_meta_filemon=	1
270.endif
271.if ${MK_DIRDEPS_BUILD} == "no"
272# Skip reading .depend when not needed to speed up tree-walks and simple
273# lookups.  For install, only do this if no other targets are specified.
274# Also skip generating or including .depend.* files if in meta+filemon mode
275# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
276# for _meta_filemon but not for _SKIP_DEPEND.
277.if !defined(NO_SKIP_DEPEND) && \
278    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
279    ${.TARGETS:M*obj} == ${.TARGETS} || \
280    ${.TARGETS:M*clean*} == ${.TARGETS} || \
281    ${.TARGETS:M*install*} == ${.TARGETS})
282_SKIP_DEPEND=	1
283.endif
284.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
285.MAKE.DEPENDFILE=	/dev/null
286.endif
287.endif
288
289kernel-depend: .depend
290SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
291	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
292	${MFILES:T:S/.m$/.h/}
293DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
294DEPENDOBJS+=	${CLEAN:M*.o}
295DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
296.if ${MAKE_VERSION} < 20160220
297DEPEND_MP?=	-MP
298.endif
299.if defined(_SKIP_DEPEND)
300# Don't bother reading any .meta files
301${DEPENDOBJS}:	.NOMETA
302.depend:	.NOMETA
303# Unset these to avoid looping/statting on them later.
304.undef DEPENDOBJS
305.undef DEPENDFILES
306.endif	# defined(_SKIP_DEPEND)
307DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
308DEPEND_CFLAGS+=	-MT${.TARGET}
309.if !defined(_meta_filemon)
310.if !empty(DEPEND_CFLAGS)
311# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
312# as those are the only ones we will include.
313DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
314CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
315.endif
316.for __depend_obj in ${DEPENDFILES}
317.if ${MAKE_VERSION} < 20160220
318.sinclude "${.OBJDIR}/${__depend_obj}"
319.else
320.dinclude "${.OBJDIR}/${__depend_obj}"
321.endif
322.endfor
323.endif	# !defined(_meta_filemon)
324
325# Always run 'make depend' to generate dependencies early and to avoid the
326# need for manually running it.  For the kernel this is mostly a NOP since
327# all dependencies are correctly added or accounted for.  This is mostly to
328# ensure downstream uses of kernel-depend are handled.
329beforebuild: kernel-depend
330
331# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
332# For meta+filemon the .meta file is checked for since it is the dependency
333# file used.
334.for __obj in ${DEPENDOBJS:O:u}
335.if defined(_meta_filemon)
336_depfile=	${.OBJDIR}/${__obj}.meta
337.else
338_depfile=	${.OBJDIR}/.depend.${__obj}
339.endif
340.if !exists(${_depfile})
341.if ${SYSTEM_OBJS:M${__obj}}
342${__obj}: ${OBJS_DEPEND_GUESS}
343.endif
344${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
345.elif defined(_meta_filemon)
346# For meta mode we still need to know which file to depend on to avoid
347# ambiguous suffix transformation rules from .PATH.  Meta mode does not
348# use .depend files.  We really only need source files, not headers since
349# they are typically in SRCS/beforebuild already.  For target-specific
350# guesses do include headers though since they may not be in SRCS.
351.if ${SYSTEM_OBJS:M${__obj}}
352${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
353.endif
354${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
355.endif	# !exists(${_depfile})
356.endfor
357
358.NOPATH: .depend ${DEPENDFILES}
359
360.depend: .PRECIOUS ${SRCS}
361
362_ILINKS= machine
363.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
364_ILINKS+= ${MACHINE_CPUARCH}
365.endif
366.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
367_ILINKS+= x86
368.endif
369.if ${MACHINE_CPUARCH} == "amd64"
370_ILINKS+= i386
371.endif
372
373# Ensure that the link exists without depending on it when it exists.
374# Ensure that debug info references the path in the source tree.
375.for _link in ${_ILINKS}
376.if !exists(${.OBJDIR}/${_link})
377${SRCS} ${DEPENDOBJS}: ${_link}
378.endif
379.if ${_link} == "machine"
380CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
381.else
382CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
383.endif
384.endfor
385
386${_ILINKS}:
387	@case ${.TARGET} in \
388	machine) \
389		path=${S}/${MACHINE}/include ;; \
390	*) \
391		path=${S}/${.TARGET}/include ;; \
392	esac ; \
393	${ECHO} ${.TARGET} "->" $$path ; \
394	ln -fns $$path ${.TARGET}
395
396# .depend needs include links so we remove them only together.
397kernel-cleandepend: .PHONY
398	rm -f .depend .depend.* ${_ILINKS}
399
400kernel-tags:
401	@ls .depend.* > /dev/null 2>&1 || \
402	    { echo "you must make all first"; exit 1; }
403	sh $S/conf/systags.sh
404
405kernel-install: .PHONY
406	@if [ ! -f ${KERNEL_KO} ] ; then \
407		echo "You must build a kernel first." ; \
408		exit 1 ; \
409	fi
410.if exists(${DESTDIR}${KODIR})
411	-thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
412	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
413		chflags -R noschg ${DESTDIR}${KODIR} ; \
414		rm -rf ${DESTDIR}${KODIR} ; \
415		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
416	else \
417		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
418			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
419			rm -rf ${DESTDIR}${KODIR}.old ; \
420		fi ; \
421		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
422		if [ -n "${KERN_DEBUGDIR}" -a \
423		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
424			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
425			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
426		fi ; \
427		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
428	fi
429.endif
430	mkdir -p ${DESTDIR}${KODIR}
431	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
432.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
433	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
434	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
435.endif
436.if defined(KERNEL_EXTRA_INSTALL)
437	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
438.endif
439
440
441
442kernel-reinstall:
443	@-chflags -R noschg ${DESTDIR}${KODIR}
444	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
445.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
446	${INSTALL} -p -m ${KMODMODE} -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
447.endif
448
449config.o env.o hints.o vers.o vnode_if.o:
450	${NORMAL_C}
451	${NORMAL_CTFCONVERT}
452
453NEWVERS_ENV+= MAKE="${MAKE}"
454.if ${MK_REPRODUCIBLE_BUILD} != "no"
455NEWVERS_ARGS+= -R
456.endif
457vers.c: .NOMETA_CMP $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP:Nvers.*}
458	${NEWVERS_ENV} sh $S/conf/newvers.sh ${NEWVERS_ARGS} ${KERN_IDENT}
459
460vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
461	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
462
463vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
464    $S/kern/vnode_if.src
465vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
466	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
467vnode_if_newproto.h:
468	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
469vnode_if_typedef.h:
470	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
471
472.if ${MFS_IMAGE:Uno} != "no"
473.if empty(MD_ROOT_SIZE_CONFIGURED)
474embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
475	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE=\""${MFS_IMAGE}"\" -c \
476	    $S/dev/md/embedfs.S -o ${.TARGET}
477.endif
478.endif
479
480.include "kern.mk"
481