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