xref: /freebsd/sys/conf/kern.post.mk (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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.ORDER: kernel-install modules-install
145
146beforebuild: .PHONY
147kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
148
149kernel-cleandir: kernel-clean kernel-cleandepend
150
151kernel-clobber:
152	find . -maxdepth 1 ! -type d ! -name version -delete
153
154kernel-obj:
155
156.if !defined(NO_MODULES)
157modules: modules-all
158modules-depend: beforebuild
159modules-all: beforebuild
160
161.if !defined(NO_MODULES_OBJ)
162modules-all modules-depend: modules-obj
163.endif
164.endif
165
166.if !defined(DEBUG)
167FULLKERNEL=	${KERNEL_KO}
168.else
169FULLKERNEL=	${KERNEL_KO}.full
170${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
171	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
172	    ${FULLKERNEL} ${.TARGET}
173${KERNEL_KO}.debug: ${FULLKERNEL}
174	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
175install.debug reinstall.debug: gdbinit
176	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
177
178# Install gdbinit files for kernel debugging.
179gdbinit:
180	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
181	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
182	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
183.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
184	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
185	    ${.CURDIR}/gdbinit.machine
186.endif
187.endif
188
189${FULLKERNEL}: ${SYSTEM_DEP} vers.o
190	@rm -f ${.TARGET}
191	@echo linking ${.TARGET}
192	${SYSTEM_LD}
193.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
194	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
195.endif
196.if ${MK_CTF} != "no"
197	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
198	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
199.endif
200.if !defined(DEBUG)
201	${OBJCOPY} --strip-debug ${.TARGET}
202.endif
203	${SYSTEM_LD_TAIL}
204
205OBJS_DEPEND_GUESS+=	offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
206			${MFILES:T:S/.m$/.h/}
207
208.for mfile in ${MFILES}
209# XXX the low quality .m.o rules gnerated by config are normally used
210# instead of the .m.c rules here.
211${mfile:T:S/.m$/.c/}: ${mfile}
212	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
213${mfile:T:S/.m$/.h/}: ${mfile}
214	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
215.endfor
216
217kernel-clean:
218	rm -f *.o *.so *.pico *.ko *.s eddep errs \
219	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
220	    tags vers.c \
221	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
222	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
223	    ${CLEAN}
224
225# This is a hack.  BFD "optimizes" away dynamic mode if there are no
226# dynamic references.  We could probably do a '-Bforcedynamic' mode like
227# in the a.out ld.  For now, this works.
228force-dynamic-hack.c:
229	:> ${.TARGET}
230
231force-dynamic-hack.pico: force-dynamic-hack.c Makefile
232	${CC} ${CCLDFLAGS} -shared ${CFLAGS} -nostdlib \
233	    force-dynamic-hack.c -o ${.TARGET}
234
235offset.inc: $S/kern/genoffset.sh genoffset.o
236	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
237
238genoffset.o: $S/kern/genoffset.c
239	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/kern/genoffset.c
240
241# genoffset_test.o is not actually used for anything - the point of compiling it
242# is to exercise the CTASSERT that checks that the offsets in the offset.inc
243# _lite struct(s) match those in the original(s).
244genoffset_test.o: $S/kern/genoffset.c offset.inc
245	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon -DOFFSET_TEST \
246	    $S/kern/genoffset.c -o ${.TARGET}
247
248assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
249	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
250
251genassym.o: $S/$M/$M/genassym.c  offset.inc
252	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/$M/$M/genassym.c
253
254OBJS_DEPEND_GUESS+= opt_global.h
255genoffset.o genassym.o vers.o: opt_global.h
256
257.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
258_meta_filemon=	1
259.endif
260.if ${MK_DIRDEPS_BUILD} == "no"
261# Skip reading .depend when not needed to speed up tree-walks and simple
262# lookups.  For install, only do this if no other targets are specified.
263# Also skip generating or including .depend.* files if in meta+filemon mode
264# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
265# for _meta_filemon but not for _SKIP_DEPEND.
266.if !defined(NO_SKIP_DEPEND) && \
267    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
268    ${.TARGETS:M*obj} == ${.TARGETS} || \
269    ${.TARGETS:M*clean*} == ${.TARGETS} || \
270    ${.TARGETS:M*install*} == ${.TARGETS})
271_SKIP_DEPEND=	1
272.endif
273.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
274.MAKE.DEPENDFILE=	/dev/null
275.endif
276.endif
277
278kernel-depend: .depend
279SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
280	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
281	${MFILES:T:S/.m$/.h/}
282DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
283DEPENDOBJS+=	${CLEAN:M*.o}
284DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
285.if ${MAKE_VERSION} < 20160220
286DEPEND_MP?=	-MP
287.endif
288.if defined(_SKIP_DEPEND)
289# Don't bother reading any .meta files
290${DEPENDOBJS}:	.NOMETA
291.depend:	.NOMETA
292# Unset these to avoid looping/statting on them later.
293.undef DEPENDOBJS
294.undef DEPENDFILES
295.endif	# defined(_SKIP_DEPEND)
296DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
297DEPEND_CFLAGS+=	-MT${.TARGET}
298.if !defined(_meta_filemon)
299.if !empty(DEPEND_CFLAGS)
300# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
301# as those are the only ones we will include.
302DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
303CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
304.endif
305.for __depend_obj in ${DEPENDFILES}
306.if ${MAKE_VERSION} < 20160220
307.sinclude "${.OBJDIR}/${__depend_obj}"
308.else
309.dinclude "${.OBJDIR}/${__depend_obj}"
310.endif
311.endfor
312.endif	# !defined(_meta_filemon)
313
314# Always run 'make depend' to generate dependencies early and to avoid the
315# need for manually running it.  For the kernel this is mostly a NOP since
316# all dependencies are correctly added or accounted for.  This is mostly to
317# ensure downstream uses of kernel-depend are handled.
318beforebuild: kernel-depend
319
320# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
321# For meta+filemon the .meta file is checked for since it is the dependency
322# file used.
323.for __obj in ${DEPENDOBJS:O:u}
324.if defined(_meta_filemon)
325_depfile=	${.OBJDIR}/${__obj}.meta
326.else
327_depfile=	${.OBJDIR}/.depend.${__obj}
328.endif
329.if !exists(${_depfile})
330.if ${SYSTEM_OBJS:M${__obj}}
331${__obj}: ${OBJS_DEPEND_GUESS}
332.endif
333${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
334.elif defined(_meta_filemon)
335# For meta mode we still need to know which file to depend on to avoid
336# ambiguous suffix transformation rules from .PATH.  Meta mode does not
337# use .depend files.  We really only need source files, not headers since
338# they are typically in SRCS/beforebuild already.  For target-specific
339# guesses do include headers though since they may not be in SRCS.
340.if ${SYSTEM_OBJS:M${__obj}}
341${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
342.endif
343${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
344.endif	# !exists(${_depfile})
345.endfor
346
347.NOPATH: .depend ${DEPENDFILES}
348
349.depend: .PRECIOUS ${SRCS}
350
351_ILINKS= machine
352.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
353_ILINKS+= ${MACHINE_CPUARCH}
354.endif
355.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
356_ILINKS+= x86
357.endif
358.if ${MACHINE_CPUARCH} == "amd64"
359_ILINKS+= i386
360.endif
361
362# Ensure that the link exists without depending on it when it exists.
363# Ensure that debug info references the path in the source tree.
364.for _link in ${_ILINKS}
365.if !exists(${.OBJDIR}/${_link})
366${SRCS} ${DEPENDOBJS}: ${_link}
367.endif
368.if ${_link} == "machine"
369CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
370.else
371CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
372.endif
373.endfor
374
375${_ILINKS}:
376	@case ${.TARGET} in \
377	machine) \
378		path=${S}/${MACHINE}/include ;; \
379	*) \
380		path=${S}/${.TARGET}/include ;; \
381	esac ; \
382	${ECHO} ${.TARGET} "->" $$path ; \
383	ln -fns $$path ${.TARGET}
384
385# .depend needs include links so we remove them only together.
386kernel-cleandepend: .PHONY
387	rm -f .depend .depend.* ${_ILINKS}
388
389kernel-tags:
390	@ls .depend.* > /dev/null 2>&1 || \
391	    { echo "you must make all first"; exit 1; }
392	sh $S/conf/systags.sh
393
394kernel-install: .PHONY
395	@if [ ! -f ${KERNEL_KO} ] ; then \
396		echo "You must build a kernel first." ; \
397		exit 1 ; \
398	fi
399.if exists(${DESTDIR}${KODIR})
400	-thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
401	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
402		chflags -R noschg ${DESTDIR}${KODIR} ; \
403		rm -rf ${DESTDIR}${KODIR} ; \
404		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
405	else \
406		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
407			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
408			rm -rf ${DESTDIR}${KODIR}.old ; \
409		fi ; \
410		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
411		if [ -n "${KERN_DEBUGDIR}" -a \
412		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
413			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
414			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
415		fi ; \
416		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
417	fi
418.endif
419	mkdir -p ${DESTDIR}${KODIR}
420	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
421.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
422	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
423	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
424.endif
425.if defined(KERNEL_EXTRA_INSTALL)
426	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
427.endif
428
429
430
431kernel-reinstall:
432	@-chflags -R noschg ${DESTDIR}${KODIR}
433	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
434.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
435	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
436.endif
437
438config.o env.o hints.o vers.o vnode_if.o:
439	${NORMAL_C}
440	${NORMAL_CTFCONVERT}
441
442NEWVERS_ENV+= MAKE="${MAKE}"
443.if ${MK_REPRODUCIBLE_BUILD} != "no"
444NEWVERS_ARGS+= -R
445.endif
446vers.c: .NOMETA_CMP $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP:Nvers.*}
447	${NEWVERS_ENV} sh $S/conf/newvers.sh ${NEWVERS_ARGS} ${KERN_IDENT}
448
449vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
450	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
451
452vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
453    $S/kern/vnode_if.src
454vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
455	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
456vnode_if_newproto.h:
457	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
458vnode_if_typedef.h:
459	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
460
461.if ${MFS_IMAGE:Uno} != "no"
462.if empty(MD_ROOT_SIZE_CONFIGURED)
463embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
464	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE=\""${MFS_IMAGE}"\" -c \
465	    $S/dev/md/embedfs.S -o ${.TARGET}
466.endif
467.endif
468
469# Generate the .bin (booti images) kernel as an extra build output.
470# The targets and rules to generate these appear in Makefile.$MACHINE
471# if the platform supports it.
472.if ${MK_KERNEL_BIN} != "no"
473KERNEL_EXTRA+= ${KERNEL_KO}.bin
474KERNEL_EXTRA_INSTALL+= ${KERNEL_KO}.bin
475CLEAN+=	${KERNEL_KO}.bin
476.endif
477
478.include "kern.mk"
479