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