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