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