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(SAN_CFLAGS)
42 MKMODULESENV+= SAN_CFLAGS="${SAN_CFLAGS}"
45 .if defined(GCOV_CFLAGS)
46 MKMODULESENV+= GCOV_CFLAGS="${GCOV_CFLAGS}"
49 # Allow overriding the kernel debug directory, so kernel and user debug may be
50 # installed in different directories. Setting it to "" restores the historical
51 # behavior of installing debug files in the kernel directory.
52 KERN_DEBUGDIR?= ${DEBUGDIR}
56 .if !defined(NO_MODULES)
57 # Default prefix used for modules installed from ports
58 LOCALBASE?= /usr/local
60 LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules
62 # Default to installing all modules installed by ports unless overridden
64 .if !defined(LOCAL_MODULES) && exists($LOCAL_MODULES_DIR)
65 LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR}
69 .for target in all clean cleandepend cleandir clobber depend install \
70 ${_obj} reinstall tags
71 ${target}: kernel-${target}
72 .if !defined(NO_MODULES)
73 ${target}: modules-${target}
75 .if !defined(MODULES_WITH_WORLD) && exists($S/modules)
76 cd $S/modules; ${MKMODULESENV} ${MAKE} \
77 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
79 .for module in ${LOCAL_MODULES}
80 cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \
81 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/}
86 # Handle ports (as defined by the user) that build kernel modules
87 .if !defined(NO_MODULES) && defined(PORTS_MODULES)
89 # The ports tree needs some environment variables defined to match the new kernel
91 # SRC_BASE is how the ports tree refers to the location of the base source files
92 .if !defined(SRC_BASE)
93 SRC_BASE= ${SYSDIR:H:tA}
95 # OSVERSION is used by some ports to determine build options
96 .if !defined(OSRELDATE)
97 # Definition copied from src/Makefile.inc1
98 OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \
99 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h
101 # Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build
103 # Ports search for some dependencies in PATH, so add the location of the
105 WRKDIRPREFIX?= ${.OBJDIR}
114 MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \
116 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \
117 SRC_BASE=${SRC_BASE} \
118 OSVERSION=${OSRELDATE} \
119 WRKDIRPREFIX=${WRKDIRPREFIX}
121 # The WRKDIR needs to be cleaned before building, and trying to change the target
122 # with a :C pattern below results in install -> instclean
124 .for __i in ${PORTS_MODULES}
125 @${ECHO} "===> Ports module ${__i} (all)"
126 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build
129 .for __target in install reinstall clean
130 ${__target}: ports-${__target}
132 .for __i in ${PORTS_MODULES}
133 @${ECHO} "===> Ports module ${__i} (${__target})"
134 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/}
139 .ORDER: kernel-install modules-install
142 kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA}
144 kernel-cleandir: kernel-clean kernel-cleandepend
147 find . -maxdepth 1 ! -type d ! -name version -delete
151 .if !defined(NO_MODULES)
153 modules-depend: beforebuild
154 modules-all: beforebuild
156 .if !defined(NO_MODULES_OBJ)
157 modules-all modules-depend: modules-obj
162 FULLKERNEL= ${KERNEL_KO}
164 FULLKERNEL= ${KERNEL_KO}.full
165 ${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug
166 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \
167 ${FULLKERNEL} ${.TARGET}
168 ${KERNEL_KO}.debug: ${FULLKERNEL}
169 ${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET}
170 install.debug reinstall.debug: gdbinit
171 cd ${.CURDIR}; ${MAKE} ${.TARGET:R}
173 # Install gdbinit files for kernel debugging.
175 grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \
176 sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit
177 cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR}
178 .if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH})
179 cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \
180 ${.CURDIR}/gdbinit.machine
184 ${FULLKERNEL}: ${SYSTEM_DEP} vers.o
186 @echo linking ${.TARGET}
188 .if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE)
189 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE}
191 .if ${MK_CTF} != "no"
192 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ...
193 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
196 ${OBJCOPY} --strip-debug ${.TARGET}
200 OBJS_DEPEND_GUESS+= offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \
201 ${MFILES:T:S/.m$/.h/}
203 .for mfile in ${MFILES}
204 # XXX the low quality .m.o rules gnerated by config are normally used
205 # instead of the .m.c rules here.
206 ${mfile:T:S/.m$/.c/}: ${mfile}
207 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -c
208 ${mfile:T:S/.m$/.h/}: ${mfile}
209 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -h
213 rm -f *.o *.so *.pico *.ko *.s eddep errs \
214 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \
216 vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \
217 ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \
220 # This is a hack. BFD "optimizes" away dynamic mode if there are no
221 # dynamic references. We could probably do a '-Bforcedynamic' mode like
222 # in the a.out ld. For now, this works.
223 HACK_EXTRA_FLAGS?= -shared
226 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico
229 offset.inc: $S/kern/genoffset.sh genoffset.o
230 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET}
232 genoffset.o: $S/kern/genoffset.c
233 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c
235 # genoffset_test.o is not actually used for anything - the point of compiling it
236 # is to exercise the CTASSERT that checks that the offsets in the offset.inc
237 # _lite struct(s) match those in the original(s).
238 genoffset_test.o: $S/kern/genoffset.c offset.inc
239 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -DOFFSET_TEST \
240 $S/kern/genoffset.c -o ${.TARGET}
242 assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o
243 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET}
245 genassym.o: $S/$M/$M/genassym.c offset.inc
246 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c
248 OBJS_DEPEND_GUESS+= opt_global.h
249 genoffset.o genassym.o vers.o: opt_global.h
251 .if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon)
254 # Skip reading .depend when not needed to speed up tree-walks and simple
255 # lookups. For install, only do this if no other targets are specified.
256 # Also skip generating or including .depend.* files if in meta+filemon mode
257 # since it will track dependencies itself. OBJS_DEPEND_GUESS is still used
258 # for _meta_filemon but not for _SKIP_DEPEND.
259 .if !defined(NO_SKIP_DEPEND) && \
260 ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \
261 ${.TARGETS:M*obj} == ${.TARGETS} || \
262 ${.TARGETS:M*clean*} == ${.TARGETS} || \
263 ${.TARGETS:M*install*} == ${.TARGETS})
266 .if defined(_SKIP_DEPEND) || defined(_meta_filemon)
267 .MAKE.DEPENDFILE= /dev/null
270 kernel-depend: .depend
271 SRCS= assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \
272 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \
273 ${MFILES:T:S/.m$/.h/}
274 DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o
275 DEPENDOBJS+= ${CLEAN:M*.o}
276 DEPENDFILES= ${DEPENDOBJS:O:u:C/^/.depend./}
277 .if ${MAKE_VERSION} < 20160220
280 .if defined(_SKIP_DEPEND)
281 # Don't bother reading any .meta files
282 ${DEPENDOBJS}: .NOMETA
284 # Unset these to avoid looping/statting on them later.
287 .endif # defined(_SKIP_DEPEND)
288 DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET}
289 DEPEND_CFLAGS+= -MT${.TARGET}
290 .if !defined(_meta_filemon)
291 .if !empty(DEPEND_CFLAGS)
292 # Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS
293 # as those are the only ones we will include.
294 DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != ""
295 CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:}
297 .for __depend_obj in ${DEPENDFILES}
298 .if ${MAKE_VERSION} < 20160220
299 .sinclude "${.OBJDIR}/${__depend_obj}"
301 .dinclude "${.OBJDIR}/${__depend_obj}"
304 .endif # !defined(_meta_filemon)
306 # Always run 'make depend' to generate dependencies early and to avoid the
307 # need for manually running it. For the kernel this is mostly a NOP since
308 # all dependencies are correctly added or accounted for. This is mostly to
309 # ensure downstream uses of kernel-depend are handled.
310 beforebuild: kernel-depend
312 # Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet.
313 # For meta+filemon the .meta file is checked for since it is the dependency
315 .for __obj in ${DEPENDOBJS:O:u}
316 .if defined(_meta_filemon)
317 _depfile= ${.OBJDIR}/${__obj}.meta
319 _depfile= ${.OBJDIR}/.depend.${__obj}
321 .if !exists(${_depfile})
322 .if ${SYSTEM_OBJS:M${__obj}}
323 ${__obj}: ${OBJS_DEPEND_GUESS}
325 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
326 .elif defined(_meta_filemon)
327 # For meta mode we still need to know which file to depend on to avoid
328 # ambiguous suffix transformation rules from .PATH. Meta mode does not
329 # use .depend files. We really only need source files, not headers since
330 # they are typically in SRCS/beforebuild already. For target-specific
331 # guesses do include headers though since they may not be in SRCS.
332 .if ${SYSTEM_OBJS:M${__obj}}
333 ${__obj}: ${OBJS_DEPEND_GUESS:N*.h}
335 ${__obj}: ${OBJS_DEPEND_GUESS.${__obj}}
336 .endif # !exists(${_depfile})
339 .NOPATH: .depend ${DEPENDFILES}
341 .depend: .PRECIOUS ${SRCS}
343 .if ${COMPILER_TYPE} == "clang" || \
344 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000)
345 _MAP_DEBUG_PREFIX= yes
349 .if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64"
350 _ILINKS+= ${MACHINE_CPUARCH}
352 .if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64"
356 # Ensure that the link exists without depending on it when it exists.
357 # Ensure that debug info references the path in the source tree.
358 .for _link in ${_ILINKS}
359 .if !exists(${.OBJDIR}/${_link})
360 ${SRCS} ${DEPENDOBJS}: ${_link}
362 .if defined(_MAP_DEBUG_PREFIX)
363 .if ${_link} == "machine"
364 CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include
366 CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include
372 @case ${.TARGET} in \
374 path=${S}/${MACHINE}/include ;; \
376 path=${S}/${.TARGET}/include ;; \
378 ${ECHO} ${.TARGET} "->" $$path ; \
379 ln -fns $$path ${.TARGET}
381 # .depend needs include links so we remove them only together.
382 kernel-cleandepend: .PHONY
383 rm -f .depend .depend.* ${_ILINKS}
386 @[ -f .depend ] || { echo "you must make depend first"; exit 1; }
387 sh $S/conf/systags.sh
389 kernel-install: .PHONY
390 @if [ ! -f ${KERNEL_KO} ] ; then \
391 echo "You must build a kernel first." ; \
394 .if exists(${DESTDIR}${KODIR})
395 -thiskernel=`sysctl -n kern.bootfile` ; \
396 if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \
397 chflags -R noschg ${DESTDIR}${KODIR} ; \
398 rm -rf ${DESTDIR}${KODIR} ; \
399 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \
401 if [ -d ${DESTDIR}${KODIR}.old ] ; then \
402 chflags -R noschg ${DESTDIR}${KODIR}.old ; \
403 rm -rf ${DESTDIR}${KODIR}.old ; \
405 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \
406 if [ -n "${KERN_DEBUGDIR}" -a \
407 -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \
408 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
409 mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \
411 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \
414 mkdir -p ${DESTDIR}${KODIR}
415 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
416 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
417 mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR}
418 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
420 .if defined(KERNEL_EXTRA_INSTALL)
421 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/
427 @-chflags -R noschg ${DESTDIR}${KODIR}
428 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/
429 .if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no"
430 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/
433 config.o env.o hints.o vers.o vnode_if.o:
437 .if ${MK_REPRODUCIBLE_BUILD} != "no"
440 vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP}
441 MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT}
443 vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src
444 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c
446 vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \
448 vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h
449 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h
451 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p
453 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q
455 .if ${MFS_IMAGE:Uno} != "no"
456 .if empty(MD_ROOT_SIZE_CONFIGURED)
457 # Generate an object file from the file system image to embed in the kernel
458 # via linking. Make sure the contents are in the mfs section and rename the
459 # start/end/size variables to __start_mfs, __stop_mfs, and mfs_size,
461 embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE}
462 ${OBJCOPY} --input-target binary \
463 --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \
464 --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \
465 ${MFS_IMAGE} ${.TARGET}
467 --rename-section .data=mfs,contents,alloc,load,readonly,data \
469 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \
471 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \
473 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \
478 # XXX strictly, everything depends on Makefile because changes to ${PROF}
479 # only appear there, but we don't handle that.