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