xref: /freebsd/sys/conf/kern.post.mk (revision 7d536dc855c85c15bf45f033d108a61b1f3cecc3)
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=	${SYSDIR:H:tA}
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
89beforebuild: .PHONY
90kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
91
92kernel-cleandir: kernel-clean kernel-cleandepend
93
94kernel-clobber:
95	find . -maxdepth 1 ! -type d ! -name version -delete
96
97kernel-obj:
98
99.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
100modules: modules-all
101
102.if !defined(NO_MODULES_OBJ)
103modules-all modules-depend: modules-obj
104.endif
105.endif
106
107.if !defined(DEBUG)
108FULLKERNEL=	${KERNEL_KO}
109.else
110FULLKERNEL=	${KERNEL_KO}.full
111${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
112	${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
113	    ${FULLKERNEL} ${.TARGET}
114${KERNEL_KO}.debug: ${FULLKERNEL}
115	${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
116install.debug reinstall.debug: gdbinit
117	cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
118
119# Install gdbinit files for kernel debugging.
120gdbinit:
121	grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
122	    sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
123	cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
124.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
125	cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
126	    ${.CURDIR}/gdbinit.machine
127.endif
128.endif
129
130${FULLKERNEL}: ${SYSTEM_DEP} vers.o
131	@rm -f ${.TARGET}
132	@echo linking ${.TARGET}
133	${SYSTEM_LD}
134.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
135	@sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
136.endif
137.if ${MK_CTF} != "no"
138	@echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
139	@${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
140.endif
141.if !defined(DEBUG)
142	${OBJCOPY} --strip-debug ${.TARGET}
143.endif
144	${SYSTEM_LD_TAIL}
145
146OBJS_DEPEND_GUESS+=	assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} \
147			${MFILES:T:S/.m$/.h/}
148.if ${MK_FAST_DEPEND} == "no" && !exists(${.OBJDIR}/.depend)
149${SYSTEM_OBJS}: ${OBJS_DEPEND_GUESS}
150.endif
151
152LNFILES=	${CFILES:T:S/.c$/.ln/}
153
154.for mfile in ${MFILES}
155# XXX the low quality .m.o rules gnerated by config are normally used
156# instead of the .m.c rules here.
157${mfile:T:S/.m$/.c/}: ${mfile}
158	${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
159${mfile:T:S/.m$/.h/}: ${mfile}
160	${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
161.endfor
162
163kernel-clean:
164	rm -f *.o *.so *.So *.ko *.s eddep errs \
165	    ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
166	    linterrs tags vers.c \
167	    vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
168	    ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
169	    ${CLEAN}
170
171lint: ${LNFILES}
172	${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
173	    tee -a linterrs
174
175# This is a hack.  BFD "optimizes" away dynamic mode if there are no
176# dynamic references.  We could probably do a '-Bforcedynamic' mode like
177# in the a.out ld.  For now, this works.
178HACK_EXTRA_FLAGS?= -shared
179hack.So: Makefile
180	:> hack.c
181	${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
182	rm -f hack.c
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
209# Skip reading .depend when not needed to speed up tree-walks
210# and simple lookups.
211.if !empty(.MAKEFLAGS:M-V${_V_READ_DEPEND}) || make(obj) || make(clean*) || \
212    make(install*) || make(kernel-obj) || make(kernel-clean*) || \
213    make(kernel-install*)
214_SKIP_READ_DEPEND=	1
215.MAKE.DEPENDFILE=	/dev/null
216.endif
217
218kernel-depend: .depend
219# The argument list can be very long, so use make -V and xargs to
220# pass it to mkdep.
221_MKDEPCC:= ${CC:N${CCACHE_BIN}}
222SRCS=	assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
223	${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
224	${MFILES:T:S/.m$/.h/}
225DEPENDFILES=	.depend .depend.*
226# Skip generating or including .depend.* files if in meta+filemon mode since
227# it will track dependencies itself.  OBJS_DEPEND_GUESS is still used though.
228.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
229_meta_filemon=	1
230.endif
231.if ${MK_FAST_DEPEND} == "yes"
232DEPENDOBJS+=	${SYSTEM_OBJS} genassym.o
233DEPENDFILES_OBJS=	${DEPENDOBJS:O:u:C/^/.depend./}
234DEPEND_CFLAGS+=	-MD -MP -MF.depend.${.TARGET}
235DEPEND_CFLAGS+=	-MT${.TARGET}
236.if !defined(_meta_filemon)
237.if defined(.PARSEDIR)
238# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
239# as those are the only ones we will include.
240DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET})
241CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
242.else
243CFLAGS+=	${DEPEND_CFLAGS}
244.endif
245.if !defined(_SKIP_READ_DEPEND)
246.for __depend_obj in ${DEPENDFILES_OBJS}
247.sinclude "${.OBJDIR}/${__depend_obj}"
248.endfor
249.endif	# !defined(_SKIP_READ_DEPEND)
250.endif	# !defined(_meta_filemon)
251
252# Always run 'make depend' to generate dependencies early and to avoid the
253# need for manually running it.  For the kernel this is mostly a NOP since
254# all dependencies are correctly added or accounted for.  This is mostly to
255# ensure downstream uses of kernel-depend are handled.
256beforebuild: kernel-depend
257.endif	# ${MK_FAST_DEPEND} == "yes"
258
259# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
260# For meta+filemon the .meta file is checked for since it is the dependency
261# file used.
262.if ${MK_FAST_DEPEND} == "yes"
263.for __obj in ${DEPENDOBJS:O:u}
264.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
265    (!defined(_meta_filemon) && !exists(${.OBJDIR}/.depend.${__obj}))
266.if ${SYSTEM_OBJS:M${__obj}}
267${__obj}: ${OBJS_DEPEND_GUESS}
268.endif
269${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
270.endif
271.endfor
272.endif
273
274.NOPATH: .depend ${DEPENDFILES_OBJS}
275
276.depend: .PRECIOUS ${SRCS}
277.if ${MK_FAST_DEPEND} == "no"
278	rm -f ${.TARGET}.tmp
279# C files
280	${MAKE} -V CFILES_NORMAL -V SYSTEM_CFILES -V GEN_CFILES | \
281	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${CFLAGS}
282	${MAKE} -V CFILES_CDDL | \
283	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_CFLAGS} \
284	    ${FBT_CFLAGS} ${DTRACE_CFLAGS}
285	${MAKE} -V CFILES_LINUXKPI | \
286	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
287		${CFLAGS} ${LINUXKPI_INCLUDES}
288	${MAKE} -V CFILES_OFED -V CFILES_MLX5 | \
289	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
290		${CFLAGS} ${OFEDINCLUDES}
291# Assembly files
292	${MAKE} -V SFILES_NORMAL | \
293	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ASM_CFLAGS}
294	${MAKE} -V SFILES_CDDL | \
295	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_ASM_CFLAGS}
296	mv ${.TARGET}.tmp ${.TARGET}
297.endif
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 -s $$path ${.TARGET}
323
324# .depend needs include links so we remove them only together.
325kernel-cleandepend: .PHONY
326	rm -f ${DEPENDFILES} ${_ILINKS}
327
328kernel-tags:
329	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
330	sh $S/conf/systags.sh
331
332kernel-install:
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
380config.ln env.ln hints.ln vers.ln vnode_if.ln:
381	${NORMAL_LINT}
382
383vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
384	MAKE=${MAKE} sh $S/conf/newvers.sh ${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