xref: /freebsd/sys/conf/kern.post.mk (revision 656f49f8e2b0656824a5f10aeb760a00fdd3753f)
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.
198SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
199	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
200	${MFILES:T:S/.m$/.h/}
201.depend: .PRECIOUS ${SRCS}
202	rm -f .newdep
203	${MAKE} -V CFILES_NOCDDL -V SYSTEM_CFILES -V GEN_CFILES | \
204	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${CFLAGS}
205	${MAKE} -V CFILES_CDDL | \
206	    MKDEP_CPP="${CC} -E" CC="${CC}" xargs mkdep -a -f .newdep ${ZFS_CFLAGS} ${FBT_CFLAGS} ${DTRACE_CFLAGS}
207	${MAKE} -V SFILES_NOCDDL | \
208	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ASM_CFLAGS}
209	${MAKE} -V SFILES_CDDL | \
210	    MKDEP_CPP="${CC} -E" xargs mkdep -a -f .newdep ${ZFS_ASM_CFLAGS}
211	rm -f .depend
212	mv .newdep .depend
213
214_ILINKS= machine
215.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
216_ILINKS+= ${MACHINE_CPUARCH}
217.endif
218.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
219_ILINKS+= x86
220.endif
221
222# Ensure that the link exists without depending on it when it exists.
223.for _link in ${_ILINKS}
224.if !exists(${.OBJDIR}/${_link})
225${SRCS} ${CLEAN:M*.o}: ${_link}
226.endif
227.endfor
228
229${_ILINKS}:
230	@case ${.TARGET} in \
231	machine) \
232		path=${S}/${MACHINE}/include ;; \
233	*) \
234		path=${S}/${.TARGET}/include ;; \
235	esac ; \
236	${ECHO} ${.TARGET} "->" $$path ; \
237	ln -s $$path ${.TARGET}
238
239# .depend needs include links so we remove them only together.
240kernel-cleandepend:
241	rm -f .depend ${_ILINKS}
242
243kernel-tags:
244	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
245	sh $S/conf/systags.sh
246
247kernel-install:
248	@if [ ! -f ${KERNEL_KO} ] ; then \
249		echo "You must build a kernel first." ; \
250		exit 1 ; \
251	fi
252.if exists(${DESTDIR}${KODIR})
253	-thiskernel=`sysctl -n kern.bootfile` ; \
254	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
255		chflags -R noschg ${DESTDIR}${KODIR} ; \
256		rm -rf ${DESTDIR}${KODIR} ; \
257		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
258	else \
259		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
260			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
261			rm -rf ${DESTDIR}${KODIR}.old ; \
262		fi ; \
263		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
264		if [ -n "${KERN_DEBUGDIR}" -a \
265		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
266			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
267			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
268		fi ; \
269		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
270	fi
271.endif
272	mkdir -p ${DESTDIR}${KODIR}
273	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
274.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
275	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
276	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
277.endif
278.if defined(KERNEL_EXTRA_INSTALL)
279	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}
280.endif
281
282
283
284kernel-reinstall:
285	@-chflags -R noschg ${DESTDIR}${KODIR}
286	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}
287.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
288	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
289.endif
290
291config.o env.o hints.o vers.o vnode_if.o:
292	${NORMAL_C}
293	${NORMAL_CTFCONVERT}
294
295config.ln env.ln hints.ln vers.ln vnode_if.ln:
296	${NORMAL_LINT}
297
298vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
299	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
300
301vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
302	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
303
304vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
305    $S/kern/vnode_if.src
306vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
307	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
308vnode_if_newproto.h:
309	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
310vnode_if_typedef.h:
311	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
312
313.if ${MFS_IMAGE:Uno} != "no"
314# Generate an object file from the file system image to embed in the kernel
315# via linking. Make sure the contents are in the mfs section and rename the
316# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
317# respectively.
318embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
319	${OBJCOPY} --input-target binary \
320	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
321	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
322	    ${MFS_IMAGE} ${.TARGET}
323	${OBJCOPY} \
324	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
325	    --redefine-sym \
326		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
327	    --redefine-sym \
328		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
329	    --redefine-sym \
330		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
331	    ${.TARGET}
332.endif
333
334# XXX strictly, everything depends on Makefile because changes to ${PROF}
335# only appear there, but we don't handle that.
336
337.include "kern.mk"
338