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