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.
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.
11 # In case the config had a makeoptions DESTDIR...
13 MKMODULESENV+= DESTDIR="${DESTDIR}"
15 SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;}
16 MKMODULESENV+= KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
18 .if defined(CONF_CFLAGS)
19 MKMODULESENV+= CONF_CFLAGS="${CONF_CFLAGS}"
23 MKMODULESENV+= WITH_CTF="${WITH_CTF}"
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.
29 KERN_DEBUGDIR?= ${DEBUGDIR}
33 .for target in all clean cleandepend cleandir clobber depend install \
35 ${target}: kernel-${target}
36 .if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
37 ${target}: modules-${target}
39 cd $S/modules; ${MKMODULESENV} ${MAKE} \
40 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
44 # Handle ports (as defined by the user) that build kernel modules
45 .if !defined(NO_MODULES) && defined(PORTS_MODULES)
47 # The ports tree needs some environment variables defined to match the new kernel
49 # Ports search for some dependencies in PATH, so add the location of the installed files
50 LOCALBASE?= /usr/local
51 # SRC_BASE is how the ports tree refers to the location of the base source files
52 .if !defined(SRC_BASE)
53 SRC_BASE!= realpath "${SYSDIR:H}/"
55 # OSVERSION is used by some ports to determine build options
56 .if !defined(OSRELDATE)
57 # Definition copied from src/Makefile.inc1
58 OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
59 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
61 # Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
62 WRKDIRPREFIX?= ${MAKEOBJDIRPREFIX}${SRC_BASE}/sys/${KERNCONF}
64 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
65 SRC_BASE=${SRC_BASE} \
66 OSVERSION=${OSRELDATE} \
67 WRKDIRPREFIX=${WRKDIRPREFIX}
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
72 .for __i in ${PORTS_MODULES}
73 @${ECHO} "===> Ports module ${__i} (all)"
74 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean all
77 .for __target in install reinstall clean
78 ${__target}: ports-${__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/}
87 .ORDER: kernel-install modules-install
89 kernel-all: ${KERNEL_KO} ${KERNEL_EXTRA}
91 kernel-cleandir: kernel-clean kernel-cleandepend
94 find . -maxdepth 1 ! -type d ! -name version -delete
98 .if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules)
101 .if !defined(NO_MODULES_OBJ)
102 modules-all modules-depend: modules-obj
107 FULLKERNEL= ${KERNEL_KO}
109 FULLKERNEL= ${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}
115 install.debug reinstall.debug: gdbinit
116 cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
118 # Install gdbinit files for kernel debugging.
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
129 ${FULLKERNEL}: ${SYSTEM_DEP} vers.o
131 @echo linking ${.TARGET}
133 .if ${MK_CTF} != "no"
134 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
135 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
138 ${OBJCOPY} --strip-debug ${.TARGET}
142 .if !exists(${.OBJDIR}/.depend)
143 ${SYSTEM_OBJS}: assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} ${MFILES:T:S/.m$/.h/}
146 LNFILES= ${CFILES:T:S/.c$/.ln/}
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
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/} \
166 ${LINT} ${LINTKERNFLAGS} ${CFLAGS:M-[DILU]*} ${.ALLSRC} 2>&1 | \
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.
172 HACK_EXTRA_FLAGS?= -shared
175 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.So
178 # This rule stops ./assym.s in .depend from causing problems.
181 assym.s: $S/kern/genassym.sh genassym.o
182 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
184 genassym.o: $S/$M/$M/genassym.c
185 ${CC} -c ${CFLAGS:N-fno-common} $S/$M/$M/genassym.c
187 ${SYSTEM_OBJS} genassym.o vers.o: opt_global.h
189 # We have "special" -I include paths for zfs/dtrace files in 'depend'.
190 CFILES_NOCDDL= ${CFILES:N*/cddl/*:N*fs/nfsclient/nfs_clkdtrace*}
191 SFILES_NOCDDL= ${SFILES:N*/cddl/*}
192 CFILES_CDDL= ${CFILES:M*/cddl/*}
193 SFILES_CDDL= ${SFILES:M*/cddl/*}
195 kernel-depend: .depend
196 # The argument list can be very long, so use make -V and xargs to
198 _MKDEPCC:= ${CC:N${CCACHE_BIN}}
199 SRCS= assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
200 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
201 ${MFILES:T:S/.m$/.h/}
203 .if ${MK_FAST_DEPEND} == "yes" && ${.MAKE.MODE:Unormal:Mmeta*} == ""
204 DEPENDFILES+= .depend.*
205 DEPEND_CFLAGS+= -MD -MP -MF.depend.${.TARGET}
206 DEPEND_CFLAGS+= -MT${.TARGET}
207 CFLAGS+= ${DEPEND_CFLAGS}
208 DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o
209 DEPENDFILES_OBJS= ${DEPENDOBJS:O:u:C/^/.depend./}
210 .if ${.MAKEFLAGS:M-V} == ""
211 .for __depend_obj in ${DEPENDFILES_OBJS}
212 .sinclude "${__depend_obj}"
215 .endif # ${MK_FAST_DEPEND} == "yes"
217 .NOPATH: .depend ${DEPENDFILES_OBJS}
219 .depend: .PRECIOUS ${SRCS}
220 .if ${MK_FAST_DEPEND} == "no"
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}
237 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
238 _ILINKS+= ${MACHINE_CPUARCH}
240 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
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}
252 @case ${.TARGET} in \
254 path=${S}/${MACHINE}/include ;; \
256 path=${S}/${.TARGET}/include ;; \
258 ${ECHO} ${.TARGET} "->" $$path ; \
259 ln -s $$path ${.TARGET}
261 # .depend needs include links so we remove them only together.
262 kernel-cleandepend: .PHONY
263 rm -f ${DEPENDFILES} ${_ILINKS}
266 @[ -f .depend ] || { echo "you must make depend first"; exit 1; }
267 sh $S/conf/systags.sh
270 @if [ ! -f ${KERNEL_KO} ] ; then \
271 echo "You must build a kernel first." ; \
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} ; \
281 if [ -d ${DESTDIR}${KODIR}.old ] ; then \
282 chflags -R noschg ${DESTDIR}${KODIR}.old ; \
283 rm -rf ${DESTDIR}${KODIR}.old ; \
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 ; \
291 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
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}/
300 .if defined(KERNEL_EXTRA_INSTALL)
301 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
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}/
313 config.o env.o hints.o vers.o vnode_if.o:
317 config.ln env.ln hints.ln vers.ln vnode_if.ln:
320 vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
321 MAKE=${MAKE} sh $S/conf/newvers.sh ${KERN_IDENT}
323 vnode_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
326 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
328 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
329 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
331 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
333 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
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,
340 embedfs_${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}
346 --rename-section .data=mfs,contents,alloc,load,readonly,data \
348 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
350 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
352 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
356 # XXX strictly, everything depends on Makefile because changes to ${PROF}
357 # only appear there, but we don't handle that.