xref: /freebsd/sys/conf/kern.post.mk (revision 103d7cdfb7435591049413e1bc39482cb316efb7)
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.if defined(DTS) || defined(DTSO) || defined(FDT_DTS_FILE)
12.include "dtb.build.mk"
13
14KERNEL_EXTRA+=	${DTB} ${DTBO}
15CLEAN+=		${DTB} ${DTBO}
16
17kernel-install: _dtbinstall
18.ORDER: beforeinstall _dtbinstall
19.endif
20
21# In case the config had a makeoptions DESTDIR...
22.if defined(DESTDIR)
23MKMODULESENV+=	DESTDIR="${DESTDIR}"
24.endif
25SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
26MKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
27MKMODULESENV+=  MODULE_TIED=yes
28
29.if defined(CONF_CFLAGS)
30MKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
31.endif
32
33.if defined(WITH_CTF)
34MKMODULESENV+=	WITH_CTF="${WITH_CTF}"
35.endif
36
37.if defined(WITH_EXTRA_TCP_STACKS)
38MKMODULESENV+=	WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
39.endif
40
41.if !empty(KCSAN_ENABLED)
42MKMODULESENV+=	KCSAN_ENABLED="yes"
43.endif
44
45.if defined(SAN_CFLAGS)
46MKMODULESENV+=	SAN_CFLAGS="${SAN_CFLAGS}"
47.endif
48
49.if defined(GCOV_CFLAGS)
50MKMODULESENV+=	GCOV_CFLAGS="${GCOV_CFLAGS}"
51.endif
52
53.if !empty(COMPAT_FREEBSD32_ENABLED)
54MKMODULESENV+=	COMPAT_FREEBSD32_ENABLED="yes"
55.endif
56
57# Allow overriding the kernel debug directory, so kernel and user debug may be
58# installed in different directories. Setting it to "" restores the historical
59# behavior of installing debug files in the kernel directory.
60KERN_DEBUGDIR?=	${DEBUGDIR}
61
62.MAIN: all
63
64.if !defined(NO_MODULES)
65# Default prefix used for modules installed from ports
66LOCALBASE?=	/usr/local
67
68LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
69
70# Default to installing all modules installed by ports unless overridden
71# by the user.
72.if !defined(LOCAL_MODULES) && exists(${LOCAL_MODULES_DIR})
73LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
74.endif
75.endif
76
77.for target in all clean cleandepend cleandir clobber depend install \
78    ${_obj} reinstall tags
79${target}: kernel-${target}
80.if !defined(NO_MODULES)
81${target}: modules-${target}
82modules-${target}:
83.if !defined(MODULES_WITH_WORLD) && exists($S/modules)
84	cd $S/modules; ${MKMODULESENV} ${MAKE} \
85	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
86.endif
87.for module in ${LOCAL_MODULES}
88	@${ECHODIR} "===> ${module} (${target:S/^reinstall$/install/:S/^clobber$/cleandir/})"
89	@cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
90	    DIRPRFX="${module}/" \
91	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
92.endfor
93.endif
94.endfor
95
96# Handle ports (as defined by the user) that build kernel modules
97.if !defined(NO_MODULES) && defined(PORTS_MODULES)
98#
99# The ports tree needs some environment variables defined to match the new kernel
100#
101# SRC_BASE is how the ports tree refers to the location of the base source files
102.if !defined(SRC_BASE)
103SRC_BASE=	${SYSDIR:H:tA}
104.endif
105# OSVERSION is used by some ports to determine build options
106.if !defined(OSRELDATE)
107# Definition copied from src/Makefile.inc1
108OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
109		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
110.endif
111# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
112#
113# Ports search for some dependencies in PATH, so add the location of the
114# installed files
115WRKDIRPREFIX?=	${.OBJDIR}
116PORTSMODULESENV=\
117	env \
118	-u CC \
119	-u CXX \
120	-u CPP \
121	-u MAKESYSPATH \
122	-u MK_AUTO_OBJ \
123	-u MAKEOBJDIR \
124	MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
125	SYSDIR=${SYSDIR} \
126	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
127	SRC_BASE=${SRC_BASE} \
128	OSVERSION=${OSRELDATE} \
129	WRKDIRPREFIX=${WRKDIRPREFIX}
130
131# The WRKDIR needs to be cleaned before building, and trying to change the target
132# with a :C pattern below results in install -> instclean
133all:
134.for __i in ${PORTS_MODULES}
135	@${ECHO} "===> Ports module ${__i} (all)"
136	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
137.endfor
138
139.for __target in install reinstall clean
140${__target}: ports-${__target}
141ports-${__target}:
142.for __i in ${PORTS_MODULES}
143	@${ECHO} "===> Ports module ${__i} (${__target})"
144	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
145.endfor
146.endfor
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 ${CFLAGS:N-flto:N-fno-common} -fcommon $S/kern/genoffset.c
245
246# genoffset_test.o is not actually used for anything - the point of compiling it
247# is to exercise the CTASSERT that checks that the offsets in the offset.inc
248# _lite struct(s) match those in the original(s).
249genoffset_test.o: $S/kern/genoffset.c offset.inc
250	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon -DOFFSET_TEST \
251	    $S/kern/genoffset.c -o ${.TARGET}
252
253assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
254	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
255
256genassym.o: $S/$M/$M/genassym.c  offset.inc
257	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/$M/$M/genassym.c
258
259OBJS_DEPEND_GUESS+= opt_global.h
260genoffset.o genassym.o vers.o: opt_global.h
261
262.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
263_meta_filemon=	1
264.endif
265.if ${MK_DIRDEPS_BUILD} == "no"
266# Skip reading .depend when not needed to speed up tree-walks and simple
267# lookups.  For install, only do this if no other targets are specified.
268# Also skip generating or including .depend.* files if in meta+filemon mode
269# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
270# for _meta_filemon but not for _SKIP_DEPEND.
271.if !defined(NO_SKIP_DEPEND) && \
272    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
273    ${.TARGETS:M*obj} == ${.TARGETS} || \
274    ${.TARGETS:M*clean*} == ${.TARGETS} || \
275    ${.TARGETS:M*install*} == ${.TARGETS})
276_SKIP_DEPEND=	1
277.endif
278.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
279.MAKE.DEPENDFILE=	/dev/null
280.endif
281.endif
282
283kernel-depend: .depend
284SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
285	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
286	${MFILES:T:S/.m$/.h/}
287DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
288DEPENDOBJS+=	${CLEAN:M*.o}
289DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
290.if ${MAKE_VERSION} < 20160220
291DEPEND_MP?=	-MP
292.endif
293.if defined(_SKIP_DEPEND)
294# Don't bother reading any .meta files
295${DEPENDOBJS}:	.NOMETA
296.depend:	.NOMETA
297# Unset these to avoid looping/statting on them later.
298.undef DEPENDOBJS
299.undef DEPENDFILES
300.endif	# defined(_SKIP_DEPEND)
301DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
302DEPEND_CFLAGS+=	-MT${.TARGET}
303.if !defined(_meta_filemon)
304.if !empty(DEPEND_CFLAGS)
305# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
306# as those are the only ones we will include.
307DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
308CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
309.endif
310.for __depend_obj in ${DEPENDFILES}
311.if ${MAKE_VERSION} < 20160220
312.sinclude "${.OBJDIR}/${__depend_obj}"
313.else
314.dinclude "${.OBJDIR}/${__depend_obj}"
315.endif
316.endfor
317.endif	# !defined(_meta_filemon)
318
319# Always run 'make depend' to generate dependencies early and to avoid the
320# need for manually running it.  For the kernel this is mostly a NOP since
321# all dependencies are correctly added or accounted for.  This is mostly to
322# ensure downstream uses of kernel-depend are handled.
323beforebuild: kernel-depend
324
325# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
326# For meta+filemon the .meta file is checked for since it is the dependency
327# file used.
328.for __obj in ${DEPENDOBJS:O:u}
329.if defined(_meta_filemon)
330_depfile=	${.OBJDIR}/${__obj}.meta
331.else
332_depfile=	${.OBJDIR}/.depend.${__obj}
333.endif
334.if !exists(${_depfile})
335.if ${SYSTEM_OBJS:M${__obj}}
336${__obj}: ${OBJS_DEPEND_GUESS}
337.endif
338${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
339.elif defined(_meta_filemon)
340# For meta mode we still need to know which file to depend on to avoid
341# ambiguous suffix transformation rules from .PATH.  Meta mode does not
342# use .depend files.  We really only need source files, not headers since
343# they are typically in SRCS/beforebuild already.  For target-specific
344# guesses do include headers though since they may not be in SRCS.
345.if ${SYSTEM_OBJS:M${__obj}}
346${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
347.endif
348${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
349.endif	# !exists(${_depfile})
350.endfor
351
352.NOPATH: .depend ${DEPENDFILES}
353
354.depend: .PRECIOUS ${SRCS}
355
356_ILINKS= machine
357.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
358_ILINKS+= ${MACHINE_CPUARCH}
359.endif
360.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
361_ILINKS+= x86
362.endif
363.if ${MACHINE_CPUARCH} == "amd64"
364_ILINKS+= i386
365.endif
366
367# Ensure that the link exists without depending on it when it exists.
368# Ensure that debug info references the path in the source tree.
369.for _link in ${_ILINKS}
370.if !exists(${.OBJDIR}/${_link})
371${SRCS} ${DEPENDOBJS}: ${_link}
372.endif
373.if ${_link} == "machine"
374CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
375.else
376CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
377.endif
378.endfor
379
380${_ILINKS}:
381	@case ${.TARGET} in \
382	machine) \
383		path=${S}/${MACHINE}/include ;; \
384	*) \
385		path=${S}/${.TARGET}/include ;; \
386	esac ; \
387	${ECHO} ${.TARGET} "->" $$path ; \
388	ln -fns $$path ${.TARGET}
389
390# .depend needs include links so we remove them only together.
391kernel-cleandepend: .PHONY
392	rm -f .depend .depend.* ${_ILINKS}
393
394kernel-tags:
395	@ls .depend.* > /dev/null 2>&1 || \
396	    { echo "you must make all first"; exit 1; }
397	sh $S/conf/systags.sh
398
399kernel-install: .PHONY
400	@if [ ! -f ${KERNEL_KO} ] ; then \
401		echo "You must build a kernel first." ; \
402		exit 1 ; \
403	fi
404.if exists(${DESTDIR}${KODIR})
405	-thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
406	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
407		chflags -R noschg ${DESTDIR}${KODIR} ; \
408		rm -rf ${DESTDIR}${KODIR} ; \
409		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
410	else \
411		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
412			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
413			rm -rf ${DESTDIR}${KODIR}.old ; \
414		fi ; \
415		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
416		if [ -n "${KERN_DEBUGDIR}" -a \
417		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
418			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
419			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
420		fi ; \
421		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
422	fi
423.endif
424	mkdir -p ${DESTDIR}${KODIR}
425	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
426.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
427	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
428	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
429.endif
430.if defined(KERNEL_EXTRA_INSTALL)
431	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
432.endif
433
434
435
436kernel-reinstall:
437	@-chflags -R noschg ${DESTDIR}${KODIR}
438	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
439.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
440	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
441.endif
442
443config.o env.o hints.o vers.o vnode_if.o:
444	${NORMAL_C}
445	${NORMAL_CTFCONVERT}
446
447.if ${MK_REPRODUCIBLE_BUILD} != "no"
448REPRO_FLAG="-R"
449.endif
450vers.c: .NOMETA_CMP $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP:Nvers.*}
451	MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
452
453vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
454	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
455
456vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
457    $S/kern/vnode_if.src
458vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
459	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
460vnode_if_newproto.h:
461	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
462vnode_if_typedef.h:
463	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
464
465.if ${MFS_IMAGE:Uno} != "no"
466.if empty(MD_ROOT_SIZE_CONFIGURED)
467embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
468	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE="${MFS_IMAGE}" -c \
469	    $S/dev/md/embedfs.S -o ${.TARGET}
470.endif
471.endif
472
473.include "kern.mk"
474