xref: /freebsd/sys/conf/kern.post.mk (revision 731d06abf2105cc0873fa84e972178f9f37ca760)
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 !empty(FDT_DTS_FILE)
12DTS+=			${FDT_DTS_FILE}
13.endif
14.if defined(DTS) || defined(DTSO)
15.include "dtb.build.mk"
16
17KERNEL_EXTRA+=	${DTB} ${DTBO}
18CLEAN+=		${DTB} ${DTBO}
19
20kernel-install: _dtbinstall
21.ORDER: beforeinstall _dtbinstall
22.endif
23
24# In case the config had a makeoptions DESTDIR...
25.if defined(DESTDIR)
26MKMODULESENV+=	DESTDIR="${DESTDIR}"
27.endif
28SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
29MKMODULESENV+=	KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
30MKMODULESENV+=  MODULE_TIED=yes
31
32.if defined(CONF_CFLAGS)
33MKMODULESENV+=	CONF_CFLAGS="${CONF_CFLAGS}"
34.endif
35
36.if defined(WITH_CTF)
37MKMODULESENV+=	WITH_CTF="${WITH_CTF}"
38.endif
39
40.if defined(WITH_EXTRA_TCP_STACKS)
41MKMODULESENV+=	WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
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# 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	cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
84	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
85.endfor
86.endif
87.endfor
88
89# Handle ports (as defined by the user) that build kernel modules
90.if !defined(NO_MODULES) && defined(PORTS_MODULES)
91#
92# The ports tree needs some environment variables defined to match the new kernel
93#
94# SRC_BASE is how the ports tree refers to the location of the base source files
95.if !defined(SRC_BASE)
96SRC_BASE=	${SYSDIR:H:tA}
97.endif
98# OSVERSION is used by some ports to determine build options
99.if !defined(OSRELDATE)
100# Definition copied from src/Makefile.inc1
101OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
102		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
103.endif
104# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
105#
106# Ports search for some dependencies in PATH, so add the location of the
107# installed files
108WRKDIRPREFIX?=	${.OBJDIR}
109PORTSMODULESENV=\
110	env \
111	-u CC \
112	-u CXX \
113	-u CPP \
114	-u MAKESYSPATH \
115	-u MK_AUTO_OBJ \
116	-u MAKEOBJDIR \
117	MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
118	SYSDIR=${SYSDIR} \
119	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
120	SRC_BASE=${SRC_BASE} \
121	OSVERSION=${OSRELDATE} \
122	WRKDIRPREFIX=${WRKDIRPREFIX}
123
124# The WRKDIR needs to be cleaned before building, and trying to change the target
125# with a :C pattern below results in install -> instclean
126all:
127.for __i in ${PORTS_MODULES}
128	@${ECHO} "===> Ports module ${__i} (all)"
129	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
130.endfor
131
132.for __target in install reinstall clean
133${__target}: ports-${__target}
134ports-${__target}:
135.for __i in ${PORTS_MODULES}
136	@${ECHO} "===> Ports module ${__i} (${__target})"
137	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
138.endfor
139.endfor
140.endif
141
142.ORDER: kernel-install modules-install
143
144beforebuild: .PHONY
145kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
146
147kernel-cleandir: kernel-clean kernel-cleandepend
148
149kernel-clobber:
150	find . -maxdepth 1 ! -type d ! -name version -delete
151
152kernel-obj:
153
154.if !defined(NO_MODULES)
155modules: modules-all
156modules-depend: beforebuild
157modules-all: beforebuild
158
159.if !defined(NO_MODULES_OBJ)
160modules-all modules-depend: modules-obj
161.endif
162.endif
163
164.if !defined(DEBUG)
165FULLKERNEL=	${KERNEL_KO}
166.else
167FULLKERNEL=	${KERNEL_KO}.full
168${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
169	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
170	    ${FULLKERNEL} ${.TARGET}
171${KERNEL_KO}.debug: ${FULLKERNEL}
172	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
173install.debug reinstall.debug: gdbinit
174	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
175
176# Install gdbinit files for kernel debugging.
177gdbinit:
178	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
179	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
180	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
181.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
182	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
183	    ${.CURDIR}/gdbinit.machine
184.endif
185.endif
186
187${FULLKERNEL}: ${SYSTEM_DEP} vers.o
188	@rm -f ${.TARGET}
189	@echo linking ${.TARGET}
190	${SYSTEM_LD}
191.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
192	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
193.endif
194.if ${MK_CTF} != "no"
195	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
196	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
197.endif
198.if !defined(DEBUG)
199	${OBJCOPY} --strip-debug ${.TARGET}
200.endif
201	${SYSTEM_LD_TAIL}
202
203OBJS_DEPEND_GUESS+=	offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
204			${MFILES:T:S/.m$/.h/}
205
206.for mfile in ${MFILES}
207# XXX the low quality .m.o rules gnerated by config are normally used
208# instead of the .m.c rules here.
209${mfile:T:S/.m$/.c/}: ${mfile}
210	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
211${mfile:T:S/.m$/.h/}: ${mfile}
212	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
213.endfor
214
215kernel-clean:
216	rm -f *.o *.so *.pico *.ko *.s eddep errs \
217	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
218	    tags vers.c \
219	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
220	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
221	    ${CLEAN}
222
223# This is a hack.  BFD "optimizes" away dynamic mode if there are no
224# dynamic references.  We could probably do a '-Bforcedynamic' mode like
225# in the a.out ld.  For now, this works.
226HACK_EXTRA_FLAGS?= -shared
227hack.pico: Makefile
228	:> hack.c
229	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico
230	rm -f hack.c
231
232offset.inc: $S/kern/genoffset.sh genoffset.o
233	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
234
235genoffset.o: $S/kern/genoffset.c
236	${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c
237
238# genoffset_test.o is not actually used for anything - the point of compiling it
239# is to exercise the CTASSERT that checks that the offsets in the offset.inc
240# _lite struct(s) match those in the original(s).
241genoffset_test.o: $S/kern/genoffset.c offset.inc
242	${CC} -c ${CFLAGS:N-flto:N-fno-common} -DOFFSET_TEST \
243	    $S/kern/genoffset.c -o ${.TARGET}
244
245assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
246	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
247
248genassym.o: $S/$M/$M/genassym.c  offset.inc
249	${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c
250
251OBJS_DEPEND_GUESS+= opt_global.h
252genoffset.o genassym.o vers.o: opt_global.h
253
254.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
255_meta_filemon=	1
256.endif
257# Skip reading .depend when not needed to speed up tree-walks and simple
258# lookups.  For install, only do this if no other targets are specified.
259# Also skip generating or including .depend.* files if in meta+filemon mode
260# since it will track dependencies itself.  OBJS_DEPEND_GUESS is still used
261# for _meta_filemon but not for _SKIP_DEPEND.
262.if !defined(NO_SKIP_DEPEND) && \
263    ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
264    ${.TARGETS:M*obj} == ${.TARGETS} || \
265    ${.TARGETS:M*clean*} == ${.TARGETS} || \
266    ${.TARGETS:M*install*} == ${.TARGETS})
267_SKIP_DEPEND=	1
268.endif
269.if defined(_SKIP_DEPEND) || defined(_meta_filemon)
270.MAKE.DEPENDFILE=	/dev/null
271.endif
272
273kernel-depend: .depend
274SRCS=	assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
275	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
276	${MFILES:T:S/.m$/.h/}
277DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
278DEPENDOBJS+=	${CLEAN:M*.o}
279DEPENDFILES=	${DEPENDOBJS:O:u:C/^/.depend./}
280.if ${MAKE_VERSION} < 20160220
281DEPEND_MP?=	-MP
282.endif
283.if defined(_SKIP_DEPEND)
284# Don't bother reading any .meta files
285${DEPENDOBJS}:	.NOMETA
286.depend:	.NOMETA
287# Unset these to avoid looping/statting on them later.
288.undef DEPENDOBJS
289.undef DEPENDFILES
290.endif	# defined(_SKIP_DEPEND)
291DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
292DEPEND_CFLAGS+=	-MT${.TARGET}
293.if !defined(_meta_filemon)
294.if !empty(DEPEND_CFLAGS)
295# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
296# as those are the only ones we will include.
297DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
298CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
299.endif
300.for __depend_obj in ${DEPENDFILES}
301.if ${MAKE_VERSION} < 20160220
302.sinclude "${.OBJDIR}/${__depend_obj}"
303.else
304.dinclude "${.OBJDIR}/${__depend_obj}"
305.endif
306.endfor
307.endif	# !defined(_meta_filemon)
308
309# Always run 'make depend' to generate dependencies early and to avoid the
310# need for manually running it.  For the kernel this is mostly a NOP since
311# all dependencies are correctly added or accounted for.  This is mostly to
312# ensure downstream uses of kernel-depend are handled.
313beforebuild: kernel-depend
314
315# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
316# For meta+filemon the .meta file is checked for since it is the dependency
317# file used.
318.for __obj in ${DEPENDOBJS:O:u}
319.if defined(_meta_filemon)
320_depfile=	${.OBJDIR}/${__obj}.meta
321.else
322_depfile=	${.OBJDIR}/.depend.${__obj}
323.endif
324.if !exists(${_depfile})
325.if ${SYSTEM_OBJS:M${__obj}}
326${__obj}: ${OBJS_DEPEND_GUESS}
327.endif
328${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
329.elif defined(_meta_filemon)
330# For meta mode we still need to know which file to depend on to avoid
331# ambiguous suffix transformation rules from .PATH.  Meta mode does not
332# use .depend files.  We really only need source files, not headers since
333# they are typically in SRCS/beforebuild already.  For target-specific
334# guesses do include headers though since they may not be in SRCS.
335.if ${SYSTEM_OBJS:M${__obj}}
336${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
337.endif
338${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
339.endif	# !exists(${_depfile})
340.endfor
341
342.NOPATH: .depend ${DEPENDFILES}
343
344.depend: .PRECIOUS ${SRCS}
345
346.if ${COMPILER_TYPE} == "clang" || \
347    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000)
348_MAP_DEBUG_PREFIX= yes
349.endif
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
359# Ensure that the link exists without depending on it when it exists.
360# Ensure that debug info references the path in the source tree.
361.for _link in ${_ILINKS}
362.if !exists(${.OBJDIR}/${_link})
363${SRCS} ${CLEAN:M*.o}: ${_link}
364.endif
365.if defined(_MAP_DEBUG_PREFIX)
366.if ${_link} == "machine"
367CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
368.else
369CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
370.endif
371.endif
372.endfor
373
374${_ILINKS}:
375	@case ${.TARGET} in \
376	machine) \
377		path=${S}/${MACHINE}/include ;; \
378	*) \
379		path=${S}/${.TARGET}/include ;; \
380	esac ; \
381	${ECHO} ${.TARGET} "->" $$path ; \
382	ln -fns $$path ${.TARGET}
383
384# .depend needs include links so we remove them only together.
385kernel-cleandepend: .PHONY
386	rm -f .depend .depend.* ${_ILINKS}
387
388kernel-tags:
389	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
390	sh $S/conf/systags.sh
391
392kernel-install: .PHONY
393	@if [ ! -f ${KERNEL_KO} ] ; then \
394		echo "You must build a kernel first." ; \
395		exit 1 ; \
396	fi
397.if exists(${DESTDIR}${KODIR})
398	-thiskernel=`sysctl -n kern.bootfile` ; \
399	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
400		chflags -R noschg ${DESTDIR}${KODIR} ; \
401		rm -rf ${DESTDIR}${KODIR} ; \
402		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
403	else \
404		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
405			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
406			rm -rf ${DESTDIR}${KODIR}.old ; \
407		fi ; \
408		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
409		if [ -n "${KERN_DEBUGDIR}" -a \
410		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
411			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
412			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
413		fi ; \
414		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
415	fi
416.endif
417	mkdir -p ${DESTDIR}${KODIR}
418	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
419.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
420	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
421	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
422.endif
423.if defined(KERNEL_EXTRA_INSTALL)
424	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
425.endif
426
427
428
429kernel-reinstall:
430	@-chflags -R noschg ${DESTDIR}${KODIR}
431	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
432.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
433	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
434.endif
435
436config.o env.o hints.o vers.o vnode_if.o:
437	${NORMAL_C}
438	${NORMAL_CTFCONVERT}
439
440.if ${MK_REPRODUCIBLE_BUILD} != "no"
441REPRO_FLAG="-R"
442.endif
443vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
444	MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
445
446vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
447	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
448
449vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
450    $S/kern/vnode_if.src
451vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
452	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
453vnode_if_newproto.h:
454	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
455vnode_if_typedef.h:
456	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
457
458.if ${MFS_IMAGE:Uno} != "no"
459.if empty(MD_ROOT_SIZE_CONFIGURED)
460# Generate an object file from the file system image to embed in the kernel
461# via linking. Make sure the contents are in the mfs section and rename the
462# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
463# respectively.
464embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
465	${OBJCOPY} --input-target binary \
466	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
467	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
468	    ${MFS_IMAGE} ${.TARGET}
469	${OBJCOPY} \
470	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
471	    --redefine-sym \
472		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
473	    --redefine-sym \
474		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
475	    --redefine-sym \
476		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
477	    ${.TARGET}
478.endif
479.endif
480
481# XXX strictly, everything depends on Makefile because changes to ${PROF}
482# only appear there, but we don't handle that.
483
484.include "kern.mk"
485