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