xref: /freebsd/sys/conf/kern.post.mk (revision 1e248b8346cbf585990a4da0199c6cc3b9cc2340)
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./}
234.if ${MAKE_VERSION} < 20160220
235DEPEND_MP?=	-MP
236.endif
237DEPEND_CFLAGS+=	-MD ${DEPEND_MP} -MF.depend.${.TARGET}
238DEPEND_CFLAGS+=	-MT${.TARGET}
239.if !defined(_meta_filemon)
240.if defined(.PARSEDIR)
241# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
242# as those are the only ones we will include.
243DEPEND_CFLAGS_CONDITION= !empty(DEPENDOBJS:M${.TARGET})
244CFLAGS+=	${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
245.else
246CFLAGS+=	${DEPEND_CFLAGS}
247.endif
248.if !defined(_SKIP_READ_DEPEND)
249.for __depend_obj in ${DEPENDFILES_OBJS}
250.if ${MAKE_VERSION} < 20160220
251.sinclude "${.OBJDIR}/${__depend_obj}"
252.else
253.dinclude "${.OBJDIR}/${__depend_obj}"
254.endif
255.endfor
256.endif	# !defined(_SKIP_READ_DEPEND)
257.endif	# !defined(_meta_filemon)
258
259# Always run 'make depend' to generate dependencies early and to avoid the
260# need for manually running it.  For the kernel this is mostly a NOP since
261# all dependencies are correctly added or accounted for.  This is mostly to
262# ensure downstream uses of kernel-depend are handled.
263beforebuild: kernel-depend
264.endif	# ${MK_FAST_DEPEND} == "yes"
265
266# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
267# For meta+filemon the .meta file is checked for since it is the dependency
268# file used.
269.if ${MK_FAST_DEPEND} == "yes"
270.for __obj in ${DEPENDOBJS:O:u}
271.if (defined(_meta_filemon) && !exists(${.OBJDIR}/${__obj}.meta)) || \
272    (!defined(_meta_filemon) && !exists(${.OBJDIR}/.depend.${__obj}))
273.if ${SYSTEM_OBJS:M${__obj}}
274${__obj}: ${OBJS_DEPEND_GUESS}
275.endif
276${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
277.endif
278.endfor
279.endif
280
281.NOPATH: .depend ${DEPENDFILES_OBJS}
282
283.depend: .PRECIOUS ${SRCS}
284.if ${MK_FAST_DEPEND} == "no"
285	rm -f ${.TARGET}.tmp
286# C files
287	${MAKE} -V CFILES_NORMAL -V SYSTEM_CFILES -V GEN_CFILES | \
288	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${CFLAGS}
289	${MAKE} -V CFILES_CDDL | \
290	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_CFLAGS} \
291	    ${FBT_CFLAGS} ${DTRACE_CFLAGS}
292	${MAKE} -V CFILES_LINUXKPI | \
293	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
294		${CFLAGS} ${LINUXKPI_INCLUDES}
295	${MAKE} -V CFILES_OFED -V CFILES_MLX5 | \
296	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp \
297		${CFLAGS} ${OFEDINCLUDES}
298# Assembly files
299	${MAKE} -V SFILES_NORMAL | \
300	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ASM_CFLAGS}
301	${MAKE} -V SFILES_CDDL | \
302	    CC="${_MKDEPCC}" xargs mkdep -a -f ${.TARGET}.tmp ${ZFS_ASM_CFLAGS}
303	mv ${.TARGET}.tmp ${.TARGET}
304.endif
305
306_ILINKS= machine
307.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
308_ILINKS+= ${MACHINE_CPUARCH}
309.endif
310.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
311_ILINKS+= x86
312.endif
313
314# Ensure that the link exists without depending on it when it exists.
315.for _link in ${_ILINKS}
316.if !exists(${.OBJDIR}/${_link})
317${SRCS} ${CLEAN:M*.o}: ${_link}
318.endif
319.endfor
320
321${_ILINKS}:
322	@case ${.TARGET} in \
323	machine) \
324		path=${S}/${MACHINE}/include ;; \
325	*) \
326		path=${S}/${.TARGET}/include ;; \
327	esac ; \
328	${ECHO} ${.TARGET} "->" $$path ; \
329	ln -s $$path ${.TARGET}
330
331# .depend needs include links so we remove them only together.
332kernel-cleandepend: .PHONY
333	rm -f ${DEPENDFILES} ${_ILINKS}
334
335kernel-tags:
336	@[ -f .depend ] || { echo "you must make depend first"; exit 1; }
337	sh $S/conf/systags.sh
338
339kernel-install:
340	@if [ ! -f ${KERNEL_KO} ] ; then \
341		echo "You must build a kernel first." ; \
342		exit 1 ; \
343	fi
344.if exists(${DESTDIR}${KODIR})
345	-thiskernel=`sysctl -n kern.bootfile` ; \
346	if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
347		chflags -R noschg ${DESTDIR}${KODIR} ; \
348		rm -rf ${DESTDIR}${KODIR} ; \
349		rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
350	else \
351		if [ -d ${DESTDIR}${KODIR}.old ] ; then \
352			chflags -R noschg ${DESTDIR}${KODIR}.old ; \
353			rm -rf ${DESTDIR}${KODIR}.old ; \
354		fi ; \
355		mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
356		if [ -n "${KERN_DEBUGDIR}" -a \
357		     -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
358			rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
359			mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
360		fi ; \
361		sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
362	fi
363.endif
364	mkdir -p ${DESTDIR}${KODIR}
365	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
366.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
367	mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
368	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
369.endif
370.if defined(KERNEL_EXTRA_INSTALL)
371	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
372.endif
373
374
375
376kernel-reinstall:
377	@-chflags -R noschg ${DESTDIR}${KODIR}
378	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
379.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
380	${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
381.endif
382
383config.o env.o hints.o vers.o vnode_if.o:
384	${NORMAL_C}
385	${NORMAL_CTFCONVERT}
386
387config.ln env.ln hints.ln vers.ln vnode_if.ln:
388	${NORMAL_LINT}
389
390vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
391	MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
392
393vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
394	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
395
396vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
397    $S/kern/vnode_if.src
398vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
399	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
400vnode_if_newproto.h:
401	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
402vnode_if_typedef.h:
403	${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
404
405.if ${MFS_IMAGE:Uno} != "no"
406.if empty(MD_ROOT_SIZE_CONFIGURED)
407# Generate an object file from the file system image to embed in the kernel
408# via linking. Make sure the contents are in the mfs section and rename the
409# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
410# respectively.
411embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
412	${OBJCOPY} --input-target binary \
413	    --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
414	    --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
415	    ${MFS_IMAGE} ${.TARGET}
416	${OBJCOPY} \
417	    --rename-section .data=mfs,contents,alloc,load,readonly,data \
418	    --redefine-sym \
419		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
420	    --redefine-sym \
421		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
422	    --redefine-sym \
423		_binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
424	    ${.TARGET}
425.endif
426.endif
427
428# XXX strictly, everything depends on Makefile because changes to ${PROF}
429# only appear there, but we don't handle that.
430
431.include "kern.mk"
432