xref: /freebsd/sys/conf/kern.post.mk (revision 4f0a4502a1f33fef287ac558c98e5ef99a32216f)
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}/&;}
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# Allow overriding the kernel debug directory, so kernel and user debug may be
27# installed in different directories. Setting it to "" restores the historical
28# behavior of installing debug files in the kernel directory.
29KERN_DEBUGDIR?=	${DEBUGDIR}
30
31.MAIN: all
32
33.for target in all clean cleandepend cleandir clobber depend install \
34    obj reinstall tags
35${target}: kernel-${target}
36.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
37${target}: modules-${target}
38modules-${target}:
39	cd $S/modules; ${MKMODULESENV} ${MAKE} \
40	    ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
41.endif
42.endfor
43
44# Handle ports (as defined by the user) that build kernel modules
45.if !defined(NO_MODULES) && defined(PORTS_MODULES)
46#
47# The ports tree needs some environment variables defined to match the new kernel
48#
49# Ports search for some dependencies in PATH, so add the location of the installed files
50LOCALBASE?=	/usr/local
51# SRC_BASE is how the ports tree refers to the location of the base source files
52.if !defined(SRC_BASE)
53SRC_BASE!=	realpath "${SYSDIR:H}/"
54.endif
55# OSVERSION is used by some ports to determine build options
56.if !defined(OSRELDATE)
57# Definition copied from src/Makefile.inc1
58OSRELDATE!=	awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
59		    ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
60.endif
61# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
62WRKDIRPREFIX?=	${MAKEOBJDIRPREFIX}${SRC_BASE}/sys/${KERNCONF}
63PORTSMODULESENV=\
64	PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
65	SRC_BASE=${SRC_BASE} \
66	OSVERSION=${OSRELDATE} \
67	WRKDIRPREFIX=${WRKDIRPREFIX}
68
69# The WRKDIR needs to be cleaned before building, and trying to change the target
70# with a :C pattern below results in install -> instclean
71all:
72.for __i in ${PORTS_MODULES}
73	@${ECHO} "===> Ports module ${__i} (all)"
74	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean all
75.endfor
76
77.for __target in install reinstall clean
78${__target}: ports-${__target}
79ports-${__target}:
80.for __i in ${PORTS_MODULES}
81	@${ECHO} "===> Ports module ${__i} (${__target})"
82	cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/install/deinstall reinstall/:C/reinstall/deinstall reinstall/}
83.endfor
84.endfor
85.endif
86
87.ORDER: kernel-install modules-install
88
89kernel-all: ${KERNEL_KO} ${KERNEL_EXTRA}
90
91kernel-cleandir: kernel-clean kernel-cleandepend
92
93kernel-clobber:
94	find . -maxdepth 1 ! -type d ! -name version -delete
95
96kernel-obj:
97
98.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
99modules: modules-all
100
101.if !defined(NO_MODULES_OBJ)
102modules-all modules-depend: modules-obj
103.endif
104.endif
105
106.if !defined(DEBUG)
107FULLKERNEL=	${KERNEL_KO}
108.else
109FULLKERNEL=	${KERNEL_KO}.full
110${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
111	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
112	    ${FULLKERNEL} ${.TARGET}
113${KERNEL_KO}.debug: ${FULLKERNEL}
114	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
115install.debug reinstall.debug: gdbinit
116	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
117
118# Install gdbinit files for kernel debugging.
119gdbinit:
120	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
121	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
122	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
123.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
124	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
125	    ${.CURDIR}/gdbinit.machine
126.endif
127.endif
128
129${FULLKERNEL}: ${SYSTEM_DEP} vers.o
130	@rm -f ${.TARGET}
131	@echo linking ${.TARGET}
132	${SYSTEM_LD}
133.if ${MK_CTF} != "no"
134	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
135	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
136.endif
137.if !defined(DEBUG)
138	${OBJCOPY} --strip-debug ${.TARGET}
139.endif
140	${SYSTEM_LD_TAIL}
141
142.if !exists(${.OBJDIR}/.depend)
143${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
144.endif
145
146LNFILES=	${CFILES:T:S/.c$/.ln/}
147
148.for mfile in ${MFILES}
149# XXX the low quality .m.o rules gnerated by config are normally used
150# instead of the .m.c rules here.
151${mfile:T:S/.m$/.c/}: ${mfile}
152	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
153${mfile:T:S/.m$/.h/}: ${mfile}
154	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
155.endfor
156
157kernel-clean:
158	rm -f *.o *.so *.So *.ko *.s eddep errs \
159	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
160	    linterrs tags vers.c \
161	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
162	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
163	    ${CLEAN}
164
165lint: ${LNFILES}
166	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
167	    tee -a linterrs
168
169# This is a hack.  BFD "optimizes" away dynamic mode if there are no
170# dynamic references.  We could probably do a '-Bforcedynamic' mode like
171# in the a.out ld.  For now, this works.
172HACK_EXTRA_FLAGS?= -shared
173hack.So: Makefile
174	:> hack.c
175	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
176	rm -f hack.c
177
178# This rule stops ./assym.s in .depend from causing problems.
179./assym.s: assym.s
180
181assym.s: $S/kern/genassym.sh genassym.o
182	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
183
184genassym.o: $S/$M/$M/genassym.c
185	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
186
187${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
188
189# We have "special" -I include paths for zfs/dtrace files in 'depend'.
190CFILES_NOCDDL=	${CFILES:N*/cddl/*:N*fs/nfsclient/nfs_clkdtrace*}
191SFILES_NOCDDL=	${SFILES:N*/cddl/*}
192CFILES_CDDL=	${CFILES:M*/cddl/*}
193SFILES_CDDL=	${SFILES:M*/cddl/*}
194
195kernel-depend: .depend
196# The argument list can be very long, so use make -V and xargs to
197# pass it to mkdep.
198_MKDEPCC:= ${CC:N${CCACHE_BIN}}
199SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
200	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
201	${MFILES:T:S/.m$/.h/}
202DEPENDFILES=	.depend
203.if ${MK_FAST_DEPEND} == "yes" && ${.MAKE.MODE:Unormal:Mmeta*} == ""
204DEPENDFILES+=	.depend.*
205DEPEND_CFLAGS+=	-MD -MP -MF.depend.${.TARGET}
206DEPEND_CFLAGS+=	-MT${.TARGET}
207CFLAGS+=	${DEPEND_CFLAGS}
208DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o
209DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:C/^/.depend./}
210.if ${.MAKEFLAGS:M-V} == ""
211.for __depend_obj in ${DEPENDFILES_OBJS}
212.sinclude "${__depend_obj}"
213.endfor
214.endif
215.endif	# ${MK_FAST_DEPEND} == "yes"
216
217.NOPATH: .depend ${DEPENDFILES_OBJS}
218
219.depend: .PRECIOUS ${SRCS}
220.if ${MK_FAST_DEPEND} == "no"
221	rm -f ${.TARGET}.tmp
222	${MAKE} -V CFILES_NOCDDL -V SYSTEM_CFILES -V GEN_CFILES | \
223	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${CFLAGS}
224	${MAKE} -V CFILES_CDDL | \
225	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_CFLAGS} \
226	    ${FBT_CFLAGS} ${DTRACE_CFLAGS}
227	${MAKE} -V SFILES_NOCDDL | \
228	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ASM_CFLAGS}
229	${MAKE} -V SFILES_CDDL | \
230	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_ASM_CFLAGS}
231	mv ${.TARGET}.tmp ${.TARGET}
232.else
233	: > ${.TARGET}
234.endif
235
236_ILINKS= machine
237.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
238_ILINKS+= ${MACHINE_CPUARCH}
239.endif
240.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
241_ILINKS+= x86
242.endif
243
244# Ensure that the link exists without depending on it when it exists.
245.for _link in ${_ILINKS}
246.if !exists(${.OBJDIR}/${_link})
247${SRCS} ${CLEAN:M*.o}: ${_link}
248.endif
249.endfor
250
251${_ILINKS}:
252	@case ${.TARGET} in \
253	machine) \
254		path=${S}/${MACHINE}/include ;; \
255	*) \
256		path=${S}/${.TARGET}/include ;; \
257	esac ; \
258	${ECHO} ${.TARGET} "->" $$path ; \
259	ln -s $$path ${.TARGET}
260
261# .depend needs include links so we remove them only together.
262kernel-cleandepend: .PHONY
263	rm -f ${DEPENDFILES} ${_ILINKS}
264
265kernel-tags:
266	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
267	sh $S/conf/systags.sh
268
269kernel-install:
270	@if [ ! -f ${KERNEL_KO} ] ; then \
271		echo "You must build a kernel first." ; \
272		exit 1 ; \
273	fi
274.if exists(${DESTDIR}${KODIR})
275	-thiskernel=`sysctl -n kern.bootfile` ; \
276	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
277		chflags -R noschg ${DESTDIR}${KODIR} ; \
278		rm -rf ${DESTDIR}${KODIR} ; \
279		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
280	else \
281		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
282			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
283			rm -rf ${DESTDIR}${KODIR}.old ; \
284		fi ; \
285		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
286		if [ -n "${KERN_DEBUGDIR}" -a \
287		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
288			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
289			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
290		fi ; \
291		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
292	fi
293.endif
294	mkdir -p ${DESTDIR}${KODIR}
295	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
296.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
297	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
298	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
299.endif
300.if defined(KERNEL_EXTRA_INSTALL)
301	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
302.endif
303
304
305
306kernel-reinstall:
307	@-chflags -R noschg ${DESTDIR}${KODIR}
308	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
309.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
310	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
311.endif
312
313config.o env.o hints.o vers.o vnode_if.o:
314	${NORMAL_C}
315	${NORMAL_CTFCONVERT}
316
317config.ln env.ln hints.ln vers.ln vnode_if.ln:
318	${NORMAL_LINT}
319
320vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
321	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
322
323vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
324	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
325
326vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
327    $S/kern/vnode_if.src
328vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
329	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
330vnode_if_newproto.h:
331	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
332vnode_if_typedef.h:
333	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
334
335.if ${MFS_IMAGE:Uno} != "no"
336# Generate an object file from the file system image to embed in the kernel
337# via linking. Make sure the contents are in the mfs section and rename the
338# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
339# respectively.
340embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
341	${OBJCOPY} --input-target binary \
342	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
343	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
344	    ${MFS_IMAGE} ${.TARGET}
345	${OBJCOPY} \
346	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
347	    --redefine-sym \
348		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
349	    --redefine-sym \
350		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
351	    --redefine-sym \
352		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
353	    ${.TARGET}
354.endif
355
356# XXX strictly, everything depends on Makefile because changes to ${PROF}
357# only appear there, but we don't handle that.
358
359.include "kern.mk"
360