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