xref: /freebsd/sys/conf/kern.post.mk (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
134	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
135.endif
136.if ${MK_CTF} != "no"
137	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
138	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
139.endif
140.if !defined(DEBUG)
141	${OBJCOPY} --strip-debug ${.TARGET}
142.endif
143	${SYSTEM_LD_TAIL}
144
145.if !exists(${.OBJDIR}/.depend)
146${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
147.endif
148
149LNFILES=	${CFILES:T:S/.c$/.ln/}
150
151.for mfile in ${MFILES}
152# XXX the low quality .m.o rules gnerated by config are normally used
153# instead of the .m.c rules here.
154${mfile:T:S/.m$/.c/}: ${mfile}
155	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
156${mfile:T:S/.m$/.h/}: ${mfile}
157	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
158.endfor
159
160kernel-clean:
161	rm -f *.o *.so *.So *.ko *.s eddep errs \
162	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
163	    linterrs tags vers.c \
164	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
165	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
166	    ${CLEAN}
167
168lint: ${LNFILES}
169	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
170	    tee -a linterrs
171
172# This is a hack.  BFD "optimizes" away dynamic mode if there are no
173# dynamic references.  We could probably do a '-Bforcedynamic' mode like
174# in the a.out ld.  For now, this works.
175HACK_EXTRA_FLAGS?= -shared
176hack.So: Makefile
177	:> hack.c
178	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
179	rm -f hack.c
180
181# This rule stops ./assym.s in .depend from causing problems.
182./assym.s: assym.s
183
184assym.s: $S/kern/genassym.sh genassym.o
185	NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
186
187genassym.o: $S/$M/$M/genassym.c
188	${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
189
190${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
191
192# Normal files first
193CFILES_NORMAL=	${CFILES:N*/cddl/*:N*fs/nfsclient/nfs_clkdtrace*:N*/compat/linuxkpi/common/*:N*/ofed/*:N*/dev/mlx5/*}
194SFILES_NORMAL=	${SFILES:N*/cddl/*}
195
196# We have "special" -I include paths for zfs/dtrace files in 'depend'.
197CFILES_CDDL=	${CFILES:M*/cddl/*}
198SFILES_CDDL=	${SFILES:M*/cddl/*}
199
200# We have "special" -I include paths for LinuxKPI.
201CFILES_LINUXKPI=${CFILES:M*/compat/linuxkpi/common/*}
202
203# We have "special" -I include paths for OFED.
204CFILES_OFED=${CFILES:M*/ofed/*}
205
206# We have "special" -I include paths for MLX5.
207CFILES_MLX5=${CFILES:M*/dev/mlx5/*}
208
209kernel-depend: .depend
210# The argument list can be very long, so use make -V and xargs to
211# pass it to mkdep.
212_MKDEPCC:= ${CC:N${CCACHE_BIN}}
213SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
214	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
215	${MFILES:T:S/.m$/.h/}
216DEPENDFILES=	.depend
217.if ${MK_FAST_DEPEND} == "yes" && \
218    (${.MAKE.MODE:Unormal:Mmeta} == "" || ${.MAKE.MODE:Unormal:Mnofilemon} != "")
219DEPENDFILES+=	.depend.*
220DEPEND_CFLAGS+=	-MD -MP -MF.depend.${.TARGET}
221DEPEND_CFLAGS+=	-MT${.TARGET}
222CFLAGS+=	${DEPEND_CFLAGS}
223DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o
224DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:C/^/.depend./}
225.if ${.MAKEFLAGS:M-V} == ""
226.for __depend_obj in ${DEPENDFILES_OBJS}
227.sinclude "${__depend_obj}"
228.endfor
229.endif
230.endif	# ${MK_FAST_DEPEND} == "yes"
231
232.NOPATH: .depend ${DEPENDFILES_OBJS}
233
234.depend: .PRECIOUS ${SRCS}
235.if ${MK_FAST_DEPEND} == "no"
236	rm -f ${.TARGET}.tmp
237# C files
238	${MAKE} -V CFILES_NORMAL -V SYSTEM_CFILES -V GEN_CFILES | \
239	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${CFLAGS}
240	${MAKE} -V CFILES_CDDL | \
241	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_CFLAGS} \
242	    ${FBT_CFLAGS} ${DTRACE_CFLAGS}
243	${MAKE} -V CFILES_LINUXKPI | \
244	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
245		${CFLAGS} ${LINUXKPI_INCLUDES}
246	${MAKE} -V CFILES_OFED -V CFILES_MLX5 | \
247	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
248		${CFLAGS} ${OFEDINCLUDES}
249# Assembly files
250	${MAKE} -V SFILES_NORMAL | \
251	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ASM_CFLAGS}
252	${MAKE} -V SFILES_CDDL | \
253	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_ASM_CFLAGS}
254	mv ${.TARGET}.tmp ${.TARGET}
255.else
256	: > ${.TARGET}
257.endif
258
259_ILINKS= machine
260.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
261_ILINKS+= ${MACHINE_CPUARCH}
262.endif
263.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
264_ILINKS+= x86
265.endif
266
267# Ensure that the link exists without depending on it when it exists.
268.for _link in ${_ILINKS}
269.if !exists(${.OBJDIR}/${_link})
270${SRCS} ${CLEAN:M*.o}: ${_link}
271.endif
272.endfor
273
274${_ILINKS}:
275	@case ${.TARGET} in \
276	machine) \
277		path=${S}/${MACHINE}/include ;; \
278	*) \
279		path=${S}/${.TARGET}/include ;; \
280	esac ; \
281	${ECHO} ${.TARGET} "->" $$path ; \
282	ln -s $$path ${.TARGET}
283
284# .depend needs include links so we remove them only together.
285kernel-cleandepend: .PHONY
286	rm -f ${DEPENDFILES} ${_ILINKS}
287
288kernel-tags:
289	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
290	sh $S/conf/systags.sh
291
292kernel-install:
293	@if [ ! -f ${KERNEL_KO} ] ; then \
294		echo "You must build a kernel first." ; \
295		exit 1 ; \
296	fi
297.if exists(${DESTDIR}${KODIR})
298	-thiskernel=`sysctl -n kern.bootfile` ; \
299	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
300		chflags -R noschg ${DESTDIR}${KODIR} ; \
301		rm -rf ${DESTDIR}${KODIR} ; \
302		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
303	else \
304		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
305			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
306			rm -rf ${DESTDIR}${KODIR}.old ; \
307		fi ; \
308		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
309		if [ -n "${KERN_DEBUGDIR}" -a \
310		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
311			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
312			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
313		fi ; \
314		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
315	fi
316.endif
317	mkdir -p ${DESTDIR}${KODIR}
318	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
319.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
320	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
321	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
322.endif
323.if defined(KERNEL_EXTRA_INSTALL)
324	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
325.endif
326
327
328
329kernel-reinstall:
330	@-chflags -R noschg ${DESTDIR}${KODIR}
331	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
332.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
333	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
334.endif
335
336config.o env.o hints.o vers.o vnode_if.o:
337	${NORMAL_C}
338	${NORMAL_CTFCONVERT}
339
340config.ln env.ln hints.ln vers.ln vnode_if.ln:
341	${NORMAL_LINT}
342
343vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
344	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
345
346vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
347	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
348
349vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
350    $S/kern/vnode_if.src
351vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
352	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
353vnode_if_newproto.h:
354	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
355vnode_if_typedef.h:
356	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
357
358.if ${MFS_IMAGE:Uno} != "no"
359.if empty(MD_ROOT_SIZE_CONFIGURED)
360# Generate an object file from the file system image to embed in the kernel
361# via linking. Make sure the contents are in the mfs section and rename the
362# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
363# respectively.
364embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
365	${OBJCOPY} --input-target binary \
366	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
367	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
368	    ${MFS_IMAGE} ${.TARGET}
369	${OBJCOPY} \
370	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
371	    --redefine-sym \
372		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
373	    --redefine-sym \
374		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
375	    --redefine-sym \
376		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
377	    ${.TARGET}
378.endif
379.endif
380
381# XXX strictly, everything depends on Makefile because changes to ${PROF}
382# only appear there, but we don't handle that.
383
384.include "kern.mk"
385