xref: /freebsd/sys/conf/kern.post.mk (revision 1323ec57)
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# 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
281kernel-depend: .depend
282SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
283	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
284	${MFILES:T:S/.m$/.h/}
285DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
286DEPENDOBJS+=	${CLEAN:M*.o}
287DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
288.if ${MAKE_VERSION} < 20160220
289DEPEND_MP?=	-MP
290.endif
291.if defined(_SKIP_DEPEND)
292# Don't bother reading any .meta files
293${DEPENDOBJS}:	.NOMETA
294.depend:	.NOMETA
295# Unset these to avoid looping/statting on them later.
296.undef DEPENDOBJS
297.undef DEPENDFILES
298.endif	# defined(_SKIP_DEPEND)
299DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
300DEPEND_CFLAGS+=	-MT${.TARGET}
301.if !defined(_meta_filemon)
302.if !empty(DEPEND_CFLAGS)
303# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
304# as those are the only ones we will include.
305DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
306CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
307.endif
308.for __depend_obj in ${DEPENDFILES}
309.if ${MAKE_VERSION} < 20160220
310.sinclude "${.OBJDIR}/${__depend_obj}"
311.else
312.dinclude "${.OBJDIR}/${__depend_obj}"
313.endif
314.endfor
315.endif	# !defined(_meta_filemon)
316
317# Always run 'make depend' to generate dependencies early and to avoid the
318# need for manually running it.  For the kernel this is mostly a NOP since
319# all dependencies are correctly added or accounted for.  This is mostly to
320# ensure downstream uses of kernel-depend are handled.
321beforebuild: kernel-depend
322
323# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
324# For meta+filemon the .meta file is checked for since it is the dependency
325# file used.
326.for __obj in ${DEPENDOBJS:O:u}
327.if defined(_meta_filemon)
328_depfile=	${.OBJDIR}/${__obj}.meta
329.else
330_depfile=	${.OBJDIR}/.depend.${__obj}
331.endif
332.if !exists(${_depfile})
333.if ${SYSTEM_OBJS:M${__obj}}
334${__obj}: ${OBJS_DEPEND_GUESS}
335.endif
336${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
337.elif defined(_meta_filemon)
338# For meta mode we still need to know which file to depend on to avoid
339# ambiguous suffix transformation rules from .PATH.  Meta mode does not
340# use .depend files.  We really only need source files, not headers since
341# they are typically in SRCS/beforebuild already.  For target-specific
342# guesses do include headers though since they may not be in SRCS.
343.if ${SYSTEM_OBJS:M${__obj}}
344${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
345.endif
346${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
347.endif	# !exists(${_depfile})
348.endfor
349
350.NOPATH: .depend ${DEPENDFILES}
351
352.depend: .PRECIOUS ${SRCS}
353
354_MAP_DEBUG_PREFIX= yes
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
364# Ensure that the link exists without depending on it when it exists.
365# Ensure that debug info references the path in the source tree.
366.for _link in ${_ILINKS}
367.if !exists(${.OBJDIR}/${_link})
368${SRCS} ${DEPENDOBJS}: ${_link}
369.endif
370.if defined(_MAP_DEBUG_PREFIX)
371.if ${_link} == "machine"
372CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
373.else
374CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
375.endif
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
446.if ${MK_REPRODUCIBLE_BUILD} != "no"
447REPRO_FLAG="-R"
448.endif
449vers.c: .NOMETA_CMP $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP:Nvers.*}
450	MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
451
452vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
453	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
454
455vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
456    $S/kern/vnode_if.src
457vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
458	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
459vnode_if_newproto.h:
460	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
461vnode_if_typedef.h:
462	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
463
464.if ${MFS_IMAGE:Uno} != "no"
465.if empty(MD_ROOT_SIZE_CONFIGURED)
466embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} $S/dev/md/embedfs.S
467	${CC} ${CFLAGS} ${ACFLAGS} -DMFS_IMAGE="${MFS_IMAGE}" -c \
468	    $S/dev/md/embedfs.S -o ${.TARGET}
469.endif
470.endif
471
472.include "kern.mk"
473