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 .if defined(DTS) || defined(DTSO) || defined(FDT_DTS_FILE)
12 .include "dtb.build.mk"
14 KERNEL_EXTRA+= ${DTB} ${DTBO}
15 CLEAN+= ${DTB} ${DTBO}
17 kernel-install: _dtbinstall
18 .ORDER: beforeinstall _dtbinstall
21 # In case the config had a makeoptions DESTDIR...
23 MKMODULESENV+= DESTDIR="${DESTDIR}"
25 SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA}
26 MKMODULESENV+= KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}"
27 MKMODULESENV+= MODULE_TIED=yes
29 .if defined(CONF_CFLAGS)
30 MKMODULESENV+= CONF_CFLAGS="${CONF_CFLAGS}"
34 MKMODULESENV+= WITH_CTF="${WITH_CTF}"
37 .if defined(WITH_EXTRA_TCP_STACKS)
38 MKMODULESENV+= WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}"
41 .if defined(KCSAN_ENABLED)
42 MKMODULESENV+= KCSAN_ENABLED="yes"
45 .if defined(SAN_CFLAGS)
46 MKMODULESENV+= SAN_CFLAGS="${SAN_CFLAGS}"
49 .if defined(GCOV_CFLAGS)
50 MKMODULESENV+= GCOV_CFLAGS="${GCOV_CFLAGS}"
53 # Allow overriding the kernel debug directory, so kernel and user debug may be
54 # installed in different directories. Setting it to "" restores the historical
55 # behavior of installing debug files in the kernel directory.
56 KERN_DEBUGDIR?= ${DEBUGDIR}
60 .if !defined(NO_MODULES)
61 # Default prefix used for modules installed from ports
62 LOCALBASE?= /usr/local
64 LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
66 # Default to installing all modules installed by ports unless overridden
68 .if !defined(LOCAL_MODULES) && exists(${LOCAL_MODULES_DIR})
69 LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
73 .for target in all clean cleandepend cleandir clobber depend install \
74 ${_obj} reinstall tags
75 ${target}: kernel-${target}
76 .if !defined(NO_MODULES)
77 ${target}: modules-${target}
79 .if !defined(MODULES_WITH_WORLD) && exists($S/modules)
80 cd $S/modules; ${MKMODULESENV} ${MAKE} \
81 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
83 .for module in ${LOCAL_MODULES}
84 @${ECHODIR} "===> ${module} (${target:S/^reinstall$/install/:S/^clobber$/cleandir/})"
85 @cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
86 DIRPRFX="${module}/" \
87 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
92 # Handle ports (as defined by the user) that build kernel modules
93 .if !defined(NO_MODULES) && defined(PORTS_MODULES)
95 # The ports tree needs some environment variables defined to match the new kernel
97 # SRC_BASE is how the ports tree refers to the location of the base source files
98 .if !defined(SRC_BASE)
99 SRC_BASE= ${SYSDIR:H:tA}
101 # OSVERSION is used by some ports to determine build options
102 .if !defined(OSRELDATE)
103 # Definition copied from src/Makefile.inc1
104 OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
105 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
107 # Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
109 # Ports search for some dependencies in PATH, so add the location of the
111 WRKDIRPREFIX?= ${.OBJDIR}
120 MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
122 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
123 SRC_BASE=${SRC_BASE} \
124 OSVERSION=${OSRELDATE} \
125 WRKDIRPREFIX=${WRKDIRPREFIX}
127 # The WRKDIR needs to be cleaned before building, and trying to change the target
128 # with a :C pattern below results in install -> instclean
130 .for __i in ${PORTS_MODULES}
131 @${ECHO} "===> Ports module ${__i} (all)"
132 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
135 .for __target in install reinstall clean
136 ${__target}: ports-${__target}
138 .for __i in ${PORTS_MODULES}
139 @${ECHO} "===> Ports module ${__i} (${__target})"
140 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
145 .ORDER: kernel-install modules-install
148 kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
150 kernel-cleandir: kernel-clean kernel-cleandepend
153 find . -maxdepth 1 ! -type d ! -name version -delete
157 .if !defined(NO_MODULES)
159 modules-depend: beforebuild
160 modules-all: beforebuild
162 .if !defined(NO_MODULES_OBJ)
163 modules-all modules-depend: modules-obj
168 FULLKERNEL= ${KERNEL_KO}
170 FULLKERNEL= ${KERNEL_KO}.full
171 ${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
172 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
173 ${FULLKERNEL} ${.TARGET}
174 ${KERNEL_KO}.debug: ${FULLKERNEL}
175 ${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
176 install.debug reinstall.debug: gdbinit
177 cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
179 # Install gdbinit files for kernel debugging.
181 grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
182 sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
183 cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
184 .if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
185 cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
186 ${.CURDIR}/gdbinit.machine
190 ${FULLKERNEL}: ${SYSTEM_DEP} vers.o
192 @echo linking ${.TARGET}
194 .if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
195 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
197 .if ${MK_CTF} != "no"
198 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
199 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
202 ${OBJCOPY} --strip-debug ${.TARGET}
206 OBJS_DEPEND_GUESS+= offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
207 ${MFILES:T:S/.m$/.h/}
209 .for mfile in ${MFILES}
210 # XXX the low quality .m.o rules gnerated by config are normally used
211 # instead of the .m.c rules here.
212 ${mfile:T:S/.m$/.c/}: ${mfile}
213 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
214 ${mfile:T:S/.m$/.h/}: ${mfile}
215 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
219 rm -f *.o *.so *.pico *.ko *.s eddep errs \
220 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
222 vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
223 ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
226 # This is a hack. BFD "optimizes" away dynamic mode if there are no
227 # dynamic references. We could probably do a '-Bforcedynamic' mode like
228 # in the a.out ld. For now, this works.
231 ${CC} ${CCLDFLAGS} -shared ${CFLAGS} -nostdlib hack.c -o hack.pico
234 offset.inc: $S/kern/genoffset.sh genoffset.o
235 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
237 genoffset.o: $S/kern/genoffset.c
238 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/kern/genoffset.c
240 # genoffset_test.o is not actually used for anything - the point of compiling it
241 # is to exercise the CTASSERT that checks that the offsets in the offset.inc
242 # _lite struct(s) match those in the original(s).
243 genoffset_test.o: $S/kern/genoffset.c offset.inc
244 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon -DOFFSET_TEST \
245 $S/kern/genoffset.c -o ${.TARGET}
247 assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
248 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
250 genassym.o: $S/$M/$M/genassym.c offset.inc
251 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -fcommon $S/$M/$M/genassym.c
253 OBJS_DEPEND_GUESS+= opt_global.h
254 genoffset.o genassym.o vers.o: opt_global.h
256 .if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
259 # Skip reading .depend when not needed to speed up tree-walks and simple
260 # lookups. For install, only do this if no other targets are specified.
261 # Also skip generating or including .depend.* files if in meta+filemon mode
262 # since it will track dependencies itself. OBJS_DEPEND_GUESS is still used
263 # for _meta_filemon but not for _SKIP_DEPEND.
264 .if !defined(NO_SKIP_DEPEND) && \
265 ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
266 ${.TARGETS:M*obj} == ${.TARGETS} || \
267 ${.TARGETS:M*clean*} == ${.TARGETS} || \
268 ${.TARGETS:M*install*} == ${.TARGETS})
271 .if defined(_SKIP_DEPEND) || defined(_meta_filemon)
272 .MAKE.DEPENDFILE= /dev/null
275 kernel-depend: .depend
276 SRCS= assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
277 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
278 ${MFILES:T:S/.m$/.h/}
279 DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
280 DEPENDOBJS+= ${CLEAN:M*.o}
281 DEPENDFILES= ${DEPENDOBJS:O:u:C/^/.depend./}
282 .if ${MAKE_VERSION} < 20160220
285 .if defined(_SKIP_DEPEND)
286 # Don't bother reading any .meta files
287 ${DEPENDOBJS}: .NOMETA
289 # Unset these to avoid looping/statting on them later.
292 .endif # defined(_SKIP_DEPEND)
293 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET}
294 DEPEND_CFLAGS+= -MT${.TARGET}
295 .if !defined(_meta_filemon)
296 .if !empty(DEPEND_CFLAGS)
297 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
298 # as those are the only ones we will include.
299 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
300 CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
302 .for __depend_obj in ${DEPENDFILES}
303 .if ${MAKE_VERSION} < 20160220
304 .sinclude "${.OBJDIR}/${__depend_obj}"
306 .dinclude "${.OBJDIR}/${__depend_obj}"
309 .endif # !defined(_meta_filemon)
311 # Always run 'make depend' to generate dependencies early and to avoid the
312 # need for manually running it. For the kernel this is mostly a NOP since
313 # all dependencies are correctly added or accounted for. This is mostly to
314 # ensure downstream uses of kernel-depend are handled.
315 beforebuild: kernel-depend
317 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
318 # For meta+filemon the .meta file is checked for since it is the dependency
320 .for __obj in ${DEPENDOBJS:O:u}
321 .if defined(_meta_filemon)
322 _depfile= ${.OBJDIR}/${__obj}.meta
324 _depfile= ${.OBJDIR}/.depend.${__obj}
326 .if !exists(${_depfile})
327 .if ${SYSTEM_OBJS:M${__obj}}
328 ${__obj}: ${OBJS_DEPEND_GUESS}
330 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
331 .elif defined(_meta_filemon)
332 # For meta mode we still need to know which file to depend on to avoid
333 # ambiguous suffix transformation rules from .PATH. Meta mode does not
334 # use .depend files. We really only need source files, not headers since
335 # they are typically in SRCS/beforebuild already. For target-specific
336 # guesses do include headers though since they may not be in SRCS.
337 .if ${SYSTEM_OBJS:M${__obj}}
338 ${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
340 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
341 .endif # !exists(${_depfile})
344 .NOPATH: .depend ${DEPENDFILES}
346 .depend: .PRECIOUS ${SRCS}
348 _MAP_DEBUG_PREFIX= yes
351 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
352 _ILINKS+= ${MACHINE_CPUARCH}
354 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
358 # Ensure that the link exists without depending on it when it exists.
359 # Ensure that debug info references the path in the source tree.
360 .for _link in ${_ILINKS}
361 .if !exists(${.OBJDIR}/${_link})
362 ${SRCS} ${DEPENDOBJS}: ${_link}
364 .if defined(_MAP_DEBUG_PREFIX)
365 .if ${_link} == "machine"
366 CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
368 CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
374 @case ${.TARGET} in \
376 path=${S}/${MACHINE}/include ;; \
378 path=${S}/${.TARGET}/include ;; \
380 ${ECHO} ${.TARGET} "->" $$path ; \
381 ln -fns $$path ${.TARGET}
383 # .depend needs include links so we remove them only together.
384 kernel-cleandepend: .PHONY
385 rm -f .depend .depend.* ${_ILINKS}
388 @ls .depend.* > /dev/null 2>&1 || \
389 { echo "you must make all first"; exit 1; }
390 sh $S/conf/systags.sh
392 kernel-install: .PHONY
393 @if [ ! -f ${KERNEL_KO} ] ; then \
394 echo "You must build a kernel first." ; \
397 .if exists(${DESTDIR}${KODIR})
398 -thiskernel=`sysctl -n kern.bootfile || echo /boot/kernel/kernel` ; \
399 if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
400 chflags -R noschg ${DESTDIR}${KODIR} ; \
401 rm -rf ${DESTDIR}${KODIR} ; \
402 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
404 if [ -d ${DESTDIR}${KODIR}.old ] ; then \
405 chflags -R noschg ${DESTDIR}${KODIR}.old ; \
406 rm -rf ${DESTDIR}${KODIR}.old ; \
408 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
409 if [ -n "${KERN_DEBUGDIR}" -a \
410 -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
411 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
412 mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
414 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
417 mkdir -p ${DESTDIR}${KODIR}
418 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
419 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
420 mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
421 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
423 .if defined(KERNEL_EXTRA_INSTALL)
424 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
430 @-chflags -R noschg ${DESTDIR}${KODIR}
431 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
432 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
433 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
436 config.o env.o hints.o vers.o vnode_if.o:
440 .if ${MK_REPRODUCIBLE_BUILD} != "no"
443 vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
444 MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
446 vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
447 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
449 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
451 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
452 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
454 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
456 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
458 .if ${MFS_IMAGE:Uno} != "no"
459 .if empty(MD_ROOT_SIZE_CONFIGURED)
460 # Generate an object file from the file system image to embed in the kernel
461 # via linking. Make sure the contents are in the mfs section and rename the
462 # start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
464 embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
465 ${OBJCOPY} --input-target binary \
466 --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
467 --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
468 ${MFS_IMAGE} ${.TARGET}
470 --rename-section .data=mfs,contents,alloc,load,readonly,data \
472 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
474 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
476 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
481 # XXX strictly, everything depends on Makefile because changes to ${PROF}
482 # only appear there, but we don't handle that.