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.if defined(DTS) || defined(DTSO) || defined(FDT_DTS_FILE) 12.include "dtb.build.mk" 13 14KERNEL_EXTRA+= ${DTB} ${DTBO} 15CLEAN+= ${DTB} ${DTBO} 16 17kernel-install: _dtbinstall 18.ORDER: beforeinstall _dtbinstall 19.endif 20 21# In case the config had a makeoptions DESTDIR... 22.if defined(DESTDIR) 23MKMODULESENV+= DESTDIR="${DESTDIR}" 24.endif 25SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA} 26MKMODULESENV+= KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}" 27MKMODULESENV+= MODULE_TIED=yes 28 29.if defined(CONF_CFLAGS) 30MKMODULESENV+= CONF_CFLAGS="${CONF_CFLAGS}" 31.endif 32 33.if defined(WITH_CTF) 34MKMODULESENV+= WITH_CTF="${WITH_CTF}" 35.endif 36 37.if defined(WITH_EXTRA_TCP_STACKS) 38MKMODULESENV+= WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}" 39.endif 40 41.if defined(KCSAN_ENABLED) 42MKMODULESENV+= KCSAN_ENABLED="yes" 43.endif 44 45.if defined(SAN_CFLAGS) 46MKMODULESENV+= SAN_CFLAGS="${SAN_CFLAGS}" 47.endif 48 49.if defined(GCOV_CFLAGS) 50MKMODULESENV+= GCOV_CFLAGS="${GCOV_CFLAGS}" 51.endif 52 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. 56KERN_DEBUGDIR?= ${DEBUGDIR} 57 58.MAIN: all 59 60.if !defined(NO_MODULES) 61# Default prefix used for modules installed from ports 62LOCALBASE?= /usr/local 63 64LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules 65 66# Default to installing all modules installed by ports unless overridden 67# by the user. 68.if !defined(LOCAL_MODULES) && exists(${LOCAL_MODULES_DIR}) 69LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR} 70.endif 71.endif 72 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} 78modules-${target}: 79.if !defined(MODULES_WITH_WORLD) && exists($S/modules) 80 cd $S/modules; ${MKMODULESENV} ${MAKE} \ 81 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/} 82.endif 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/} 88.endfor 89.endif 90.endfor 91 92# Handle ports (as defined by the user) that build kernel modules 93.if !defined(NO_MODULES) && defined(PORTS_MODULES) 94# 95# The ports tree needs some environment variables defined to match the new kernel 96# 97# SRC_BASE is how the ports tree refers to the location of the base source files 98.if !defined(SRC_BASE) 99SRC_BASE= ${SYSDIR:H:tA} 100.endif 101# OSVERSION is used by some ports to determine build options 102.if !defined(OSRELDATE) 103# Definition copied from src/Makefile.inc1 104OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \ 105 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h 106.endif 107# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build 108# 109# Ports search for some dependencies in PATH, so add the location of the 110# installed files 111WRKDIRPREFIX?= ${.OBJDIR} 112PORTSMODULESENV=\ 113 env \ 114 -u CC \ 115 -u CXX \ 116 -u CPP \ 117 -u MAKESYSPATH \ 118 -u MK_AUTO_OBJ \ 119 -u MAKEOBJDIR \ 120 MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \ 121 SYSDIR=${SYSDIR} \ 122 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \ 123 SRC_BASE=${SRC_BASE} \ 124 OSVERSION=${OSRELDATE} \ 125 WRKDIRPREFIX=${WRKDIRPREFIX} 126 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 129all: 130.for __i in ${PORTS_MODULES} 131 @${ECHO} "===> Ports module ${__i} (all)" 132 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build 133.endfor 134 135.for __target in install reinstall clean 136${__target}: ports-${__target} 137ports-${__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/} 141.endfor 142.endfor 143.endif 144 145.ORDER: kernel-install modules-install 146 147beforebuild: .PHONY 148kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA} 149 150kernel-cleandir: kernel-clean kernel-cleandepend 151 152kernel-clobber: 153 find . -maxdepth 1 ! -type d ! -name version -delete 154 155kernel-obj: 156 157.if !defined(NO_MODULES) 158modules: modules-all 159modules-depend: beforebuild 160modules-all: beforebuild 161 162.if !defined(NO_MODULES_OBJ) 163modules-all modules-depend: modules-obj 164.endif 165.endif 166 167.if !defined(DEBUG) 168FULLKERNEL= ${KERNEL_KO} 169.else 170FULLKERNEL= ${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} 176install.debug reinstall.debug: gdbinit 177 cd ${.CURDIR}; ${MAKE} ${.TARGET:R} 178 179# Install gdbinit files for kernel debugging. 180gdbinit: 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 187.endif 188.endif 189 190${FULLKERNEL}: ${SYSTEM_DEP} vers.o 191 @rm -f ${.TARGET} 192 @echo linking ${.TARGET} 193 ${SYSTEM_LD} 194.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE) 195 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE} 196.endif 197.if ${MK_CTF} != "no" 198 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ... 199 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o 200.endif 201.if !defined(DEBUG) 202 ${OBJCOPY} --strip-debug ${.TARGET} 203.endif 204 ${SYSTEM_LD_TAIL} 205 206OBJS_DEPEND_GUESS+= offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \ 207 ${MFILES:T:S/.m$/.h/} 208 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 216.endfor 217 218kernel-clean: 219 rm -f *.o *.so *.pico *.ko *.s eddep errs \ 220 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \ 221 tags vers.c \ 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/} \ 224 ${CLEAN} 225 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. 229HACK_EXTRA_FLAGS?= -shared 230hack.pico: Makefile 231 :> hack.c 232 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico 233 rm -f hack.c 234 235offset.inc: $S/kern/genoffset.sh genoffset.o 236 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET} 237 238genoffset.o: $S/kern/genoffset.c 239 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c 240 241# genoffset_test.o is not actually used for anything - the point of compiling it 242# is to exercise the CTASSERT that checks that the offsets in the offset.inc 243# _lite struct(s) match those in the original(s). 244genoffset_test.o: $S/kern/genoffset.c offset.inc 245 ${CC} -c ${CFLAGS:N-flto:N-fno-common} -DOFFSET_TEST \ 246 $S/kern/genoffset.c -o ${.TARGET} 247 248assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o 249 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET} 250 251genassym.o: $S/$M/$M/genassym.c offset.inc 252 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c 253 254OBJS_DEPEND_GUESS+= opt_global.h 255genoffset.o genassym.o vers.o: opt_global.h 256 257.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon) 258_meta_filemon= 1 259.endif 260# Skip reading .depend when not needed to speed up tree-walks and simple 261# lookups. For install, only do this if no other targets are specified. 262# Also skip generating or including .depend.* files if in meta+filemon mode 263# since it will track dependencies itself. OBJS_DEPEND_GUESS is still used 264# for _meta_filemon but not for _SKIP_DEPEND. 265.if !defined(NO_SKIP_DEPEND) && \ 266 ((!empty(.MAKEFLAGS:M-V) && empty(.MAKEFLAGS:M*DEP*)) || \ 267 ${.TARGETS:M*obj} == ${.TARGETS} || \ 268 ${.TARGETS:M*clean*} == ${.TARGETS} || \ 269 ${.TARGETS:M*install*} == ${.TARGETS}) 270_SKIP_DEPEND= 1 271.endif 272.if defined(_SKIP_DEPEND) || defined(_meta_filemon) 273.MAKE.DEPENDFILE= /dev/null 274.endif 275 276kernel-depend: .depend 277SRCS= assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \ 278 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \ 279 ${MFILES:T:S/.m$/.h/} 280DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o 281DEPENDOBJS+= ${CLEAN:M*.o} 282DEPENDFILES= ${DEPENDOBJS:O:u:C/^/.depend./} 283.if ${MAKE_VERSION} < 20160220 284DEPEND_MP?= -MP 285.endif 286.if defined(_SKIP_DEPEND) 287# Don't bother reading any .meta files 288${DEPENDOBJS}: .NOMETA 289.depend: .NOMETA 290# Unset these to avoid looping/statting on them later. 291.undef DEPENDOBJS 292.undef DEPENDFILES 293.endif # defined(_SKIP_DEPEND) 294DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET} 295DEPEND_CFLAGS+= -MT${.TARGET} 296.if !defined(_meta_filemon) 297.if !empty(DEPEND_CFLAGS) 298# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS 299# as those are the only ones we will include. 300DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != "" 301CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:} 302.endif 303.for __depend_obj in ${DEPENDFILES} 304.if ${MAKE_VERSION} < 20160220 305.sinclude "${.OBJDIR}/${__depend_obj}" 306.else 307.dinclude "${.OBJDIR}/${__depend_obj}" 308.endif 309.endfor 310.endif # !defined(_meta_filemon) 311 312# Always run 'make depend' to generate dependencies early and to avoid the 313# need for manually running it. For the kernel this is mostly a NOP since 314# all dependencies are correctly added or accounted for. This is mostly to 315# ensure downstream uses of kernel-depend are handled. 316beforebuild: kernel-depend 317 318# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet. 319# For meta+filemon the .meta file is checked for since it is the dependency 320# file used. 321.for __obj in ${DEPENDOBJS:O:u} 322.if defined(_meta_filemon) 323_depfile= ${.OBJDIR}/${__obj}.meta 324.else 325_depfile= ${.OBJDIR}/.depend.${__obj} 326.endif 327.if !exists(${_depfile}) 328.if ${SYSTEM_OBJS:M${__obj}} 329${__obj}: ${OBJS_DEPEND_GUESS} 330.endif 331${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 332.elif defined(_meta_filemon) 333# For meta mode we still need to know which file to depend on to avoid 334# ambiguous suffix transformation rules from .PATH. Meta mode does not 335# use .depend files. We really only need source files, not headers since 336# they are typically in SRCS/beforebuild already. For target-specific 337# guesses do include headers though since they may not be in SRCS. 338.if ${SYSTEM_OBJS:M${__obj}} 339${__obj}: ${OBJS_DEPEND_GUESS:N*.h} 340.endif 341${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 342.endif # !exists(${_depfile}) 343.endfor 344 345.NOPATH: .depend ${DEPENDFILES} 346 347.depend: .PRECIOUS ${SRCS} 348 349.if ${COMPILER_TYPE} == "clang" || \ 350 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000) 351_MAP_DEBUG_PREFIX= yes 352.endif 353 354_ILINKS= machine 355.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64" 356_ILINKS+= ${MACHINE_CPUARCH} 357.endif 358.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 359_ILINKS+= x86 360.endif 361 362# Ensure that the link exists without depending on it when it exists. 363# Ensure that debug info references the path in the source tree. 364.for _link in ${_ILINKS} 365.if !exists(${.OBJDIR}/${_link}) 366${SRCS} ${DEPENDOBJS}: ${_link} 367.endif 368.if defined(_MAP_DEBUG_PREFIX) 369.if ${_link} == "machine" 370CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include 371.else 372CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include 373.endif 374.endif 375.endfor 376 377${_ILINKS}: 378 @case ${.TARGET} in \ 379 machine) \ 380 path=${S}/${MACHINE}/include ;; \ 381 *) \ 382 path=${S}/${.TARGET}/include ;; \ 383 esac ; \ 384 ${ECHO} ${.TARGET} "->" $$path ; \ 385 ln -fns $$path ${.TARGET} 386 387# .depend needs include links so we remove them only together. 388kernel-cleandepend: .PHONY 389 rm -f .depend .depend.* ${_ILINKS} 390 391kernel-tags: 392 @[ -f .depend ] || { echo "you must make depend first"; exit 1; } 393 sh $S/conf/systags.sh 394 395kernel-install: .PHONY 396 @if [ ! -f ${KERNEL_KO} ] ; then \ 397 echo "You must build a kernel first." ; \ 398 exit 1 ; \ 399 fi 400.if exists(${DESTDIR}${KODIR}) 401 -thiskernel=`sysctl -n kern.bootfile` ; \ 402 if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \ 403 chflags -R noschg ${DESTDIR}${KODIR} ; \ 404 rm -rf ${DESTDIR}${KODIR} ; \ 405 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \ 406 else \ 407 if [ -d ${DESTDIR}${KODIR}.old ] ; then \ 408 chflags -R noschg ${DESTDIR}${KODIR}.old ; \ 409 rm -rf ${DESTDIR}${KODIR}.old ; \ 410 fi ; \ 411 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \ 412 if [ -n "${KERN_DEBUGDIR}" -a \ 413 -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \ 414 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \ 415 mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \ 416 fi ; \ 417 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \ 418 fi 419.endif 420 mkdir -p ${DESTDIR}${KODIR} 421 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/ 422.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 423 mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR} 424 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/ 425.endif 426.if defined(KERNEL_EXTRA_INSTALL) 427 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/ 428.endif 429 430 431 432kernel-reinstall: 433 @-chflags -R noschg ${DESTDIR}${KODIR} 434 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/ 435.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 436 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/ 437.endif 438 439config.o env.o hints.o vers.o vnode_if.o: 440 ${NORMAL_C} 441 ${NORMAL_CTFCONVERT} 442 443.if ${MK_REPRODUCIBLE_BUILD} != "no" 444REPRO_FLAG="-R" 445.endif 446vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP} 447 MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT} 448 449vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src 450 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c 451 452vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \ 453 $S/kern/vnode_if.src 454vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 455 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h 456vnode_if_newproto.h: 457 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p 458vnode_if_typedef.h: 459 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q 460 461.if ${MFS_IMAGE:Uno} != "no" 462.if empty(MD_ROOT_SIZE_CONFIGURED) 463# Generate an object file from the file system image to embed in the kernel 464# via linking. Make sure the contents are in the mfs section and rename the 465# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size, 466# respectively. 467embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} 468 ${OBJCOPY} --input-target binary \ 469 --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \ 470 --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \ 471 ${MFS_IMAGE} ${.TARGET} 472 ${OBJCOPY} \ 473 --rename-section .data=mfs,contents,alloc,load,readonly,data \ 474 --redefine-sym \ 475 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \ 476 --redefine-sym \ 477 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \ 478 --redefine-sym \ 479 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \ 480 ${.TARGET} 481.endif 482.endif 483 484# XXX strictly, everything depends on Makefile because changes to ${PROF} 485# only appear there, but we don't handle that. 486 487.include "kern.mk" 488