xref: /freebsd/sys/conf/kern.post.mk (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
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.
233hack.pico: Makefile
234	:> hack.c
235	${CC} ${CCLDFLAGS} -shared ${CFLAGS} -nostdlib hack.c -o hack.pico
236	rm -f hack.c
237
238offset.inc: $S/kern/genoffset.sh genoffset.o
239	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
240
241genoffset.o: $S/kern/genoffset.c
242	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/kern/genoffset.c
243
244# genoffset_test.o is not actually used for anything - the point of compiling it
245# is to exercise the CTASSERT that checks that the offsets in the offset.inc
246# _lite struct(s) match those in the original(s).
247genoffset_test.o: $S/kern/genoffset.c offset.inc
248	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon -DOFFSET_TEST \
249	    $S/kern/genoffset.c -o ${.TARGET}
250
251assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
252	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
253
254genassym.o: $S/$M/$M/genassym.c  offset.inc
255	${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/$M/$M/genassym.c
256
257OBJS_DEPEND_GUESS+= opt_global.h
258genoffset.o genassym.o vers.o: opt_global.h
259
260.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
261_meta_filemon=	1
262.endif
263# Skip reading .depend when not needed to speed up tree-walks and simple
264# lookups.  For install, only do this if no other targets are specified.
265# Also skip generating or including .depend.* files if in meta+filemon mode
266# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
267# for _meta_filemon but not for _SKIP_DEPEND.
268.if !defined(NO_SKIP_DEPEND) && \
269    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
270    ${.TARGETS:M*obj} == ${.TARGETS} || \
271    ${.TARGETS:M*clean*} == ${.TARGETS} || \
272    ${.TARGETS:M*install*} == ${.TARGETS})
273_SKIP_DEPEND=	1
274.endif
275.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
276.MAKE.DEPENDFILE=	/dev/null
277.endif
278
279kernel-depend: .depend
280SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
281	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
282	${MFILES:T:S/.m$/.h/}
283DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
284DEPENDOBJS+=	${CLEAN:M*.o}
285DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
286.if ${MAKE_VERSION} < 20160220
287DEPEND_MP?=	-MP
288.endif
289.if defined(_SKIP_DEPEND)
290# Don't bother reading any .meta files
291${DEPENDOBJS}:	.NOMETA
292.depend:	.NOMETA
293# Unset these to avoid looping/statting on them later.
294.undef DEPENDOBJS
295.undef DEPENDFILES
296.endif	# defined(_SKIP_DEPEND)
297DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
298DEPEND_CFLAGS+=	-MT${.TARGET}
299.if !defined(_meta_filemon)
300.if !empty(DEPEND_CFLAGS)
301# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
302# as those are the only ones we will include.
303DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
304CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
305.endif
306.for __depend_obj in ${DEPENDFILES}
307.if ${MAKE_VERSION} < 20160220
308.sinclude "${.OBJDIR}/${__depend_obj}"
309.else
310.dinclude "${.OBJDIR}/${__depend_obj}"
311.endif
312.endfor
313.endif	# !defined(_meta_filemon)
314
315# Always run 'make depend' to generate dependencies early and to avoid the
316# need for manually running it.  For the kernel this is mostly a NOP since
317# all dependencies are correctly added or accounted for.  This is mostly to
318# ensure downstream uses of kernel-depend are handled.
319beforebuild: kernel-depend
320
321# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
322# For meta+filemon the .meta file is checked for since it is the dependency
323# file used.
324.for __obj in ${DEPENDOBJS:O:u}
325.if defined(_meta_filemon)
326_depfile=	${.OBJDIR}/${__obj}.meta
327.else
328_depfile=	${.OBJDIR}/.depend.${__obj}
329.endif
330.if !exists(${_depfile})
331.if ${SYSTEM_OBJS:M${__obj}}
332${__obj}: ${OBJS_DEPEND_GUESS}
333.endif
334${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
335.elif defined(_meta_filemon)
336# For meta mode we still need to know which file to depend on to avoid
337# ambiguous suffix transformation rules from .PATH.  Meta mode does not
338# use .depend files.  We really only need source files, not headers since
339# they are typically in SRCS/beforebuild already.  For target-specific
340# guesses do include headers though since they may not be in SRCS.
341.if ${SYSTEM_OBJS:M${__obj}}
342${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
343.endif
344${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
345.endif	# !exists(${_depfile})
346.endfor
347
348.NOPATH: .depend ${DEPENDFILES}
349
350.depend: .PRECIOUS ${SRCS}
351
352_MAP_DEBUG_PREFIX= yes
353
354_ILINKS= machine
355.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
356_ILINKS+= ${MACHINE_CPUARCH}
357.endif
358.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
359_ILINKS+= x86
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 defined(_MAP_DEBUG_PREFIX)
369.if ${_link} == "machine"
370CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
371.else
372CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
373.endif
374.endif
375.endfor
376
377${_ILINKS}:
378	@case ${.TARGET} in \
379	machine) \
380		path=${S}/${MACHINE}/include ;; \
381	*) \
382		path=${S}/${.TARGET}/include ;; \
383	esac ; \
384	${ECHO} ${.TARGET} "->" $$path ; \
385	ln -fns $$path ${.TARGET}
386
387# .depend needs include links so we remove them only together.
388kernel-cleandepend: .PHONY
389	rm -f .depend .depend.* ${_ILINKS}
390
391kernel-tags:
392	@ls .depend.* > /dev/null 2>&1 || \
393	    { echo "you must make all first"; exit 1; }
394	sh $S/conf/systags.sh
395
396kernel-install: .PHONY
397	@if [ ! -f ${KERNEL_KO} ] ; then \
398		echo "You must build a kernel first." ; \
399		exit 1 ; \
400	fi
401.if exists(${DESTDIR}${KODIR})
402	-thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
403	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
404		chflags -R noschg ${DESTDIR}${KODIR} ; \
405		rm -rf ${DESTDIR}${KODIR} ; \
406		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
407	else \
408		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
409			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
410			rm -rf ${DESTDIR}${KODIR}.old ; \
411		fi ; \
412		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
413		if [ -n "${KERN_DEBUGDIR}" -a \
414		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
415			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
416			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
417		fi ; \
418		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
419	fi
420.endif
421	mkdir -p ${DESTDIR}${KODIR}
422	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
423.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
424	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
425	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
426.endif
427.if defined(KERNEL_EXTRA_INSTALL)
428	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
429.endif
430
431
432
433kernel-reinstall:
434	@-chflags -R noschg ${DESTDIR}${KODIR}
435	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
436.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
437	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
438.endif
439
440config.o env.o hints.o vers.o vnode_if.o:
441	${NORMAL_C}
442	${NORMAL_CTFCONVERT}
443
444.if ${MK_REPRODUCIBLE_BUILD} != "no"
445REPRO_FLAG="-R"
446.endif
447vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
448	MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
449
450vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
451	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
452
453vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
454    $S/kern/vnode_if.src
455vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
456	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
457vnode_if_newproto.h:
458	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
459vnode_if_typedef.h:
460	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
461
462.if ${MFS_IMAGE:Uno} != "no"
463.if empty(MD_ROOT_SIZE_CONFIGURED)
464embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
465	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE="${MFS_IMAGE}" -c \
466	    $S/dev/md/embedfs.S -o ${.TARGET}
467.endif
468.endif
469
470.include "kern.mk"
471