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(SAN_CFLAGS) 42MKMODULESENV+= SAN_CFLAGS="${SAN_CFLAGS}" 43.endif 44 45.if defined(GCOV_CFLAGS) 46MKMODULESENV+= GCOV_CFLAGS="${GCOV_CFLAGS}" 47.endif 48 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. 52KERN_DEBUGDIR?= ${DEBUGDIR} 53 54.MAIN: all 55 56.if !defined(NO_MODULES) 57# Default prefix used for modules installed from ports 58LOCALBASE?= /usr/local 59 60LOCAL_MODULES_DIR?= ${LOCALBASE}/sys/modules 61 62# Default to installing all modules installed by ports unless overridden 63# by the user. 64.if !defined(LOCAL_MODULES) && exists($LOCAL_MODULES_DIR) 65LOCAL_MODULES!= ls ${LOCAL_MODULES_DIR} 66.endif 67.endif 68 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} 74modules-${target}: 75.if !defined(MODULES_WITH_WORLD) && exists($S/modules) 76 cd $S/modules; ${MKMODULESENV} ${MAKE} \ 77 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/} 78.endif 79.for module in ${LOCAL_MODULES} 80 cd ${LOCAL_MODULES_DIR}/${module}; ${MKMODULESENV} ${MAKE} \ 81 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/} 82.endfor 83.endif 84.endfor 85 86# Handle ports (as defined by the user) that build kernel modules 87.if !defined(NO_MODULES) && defined(PORTS_MODULES) 88# 89# The ports tree needs some environment variables defined to match the new kernel 90# 91# SRC_BASE is how the ports tree refers to the location of the base source files 92.if !defined(SRC_BASE) 93SRC_BASE= ${SYSDIR:H:tA} 94.endif 95# OSVERSION is used by some ports to determine build options 96.if !defined(OSRELDATE) 97# Definition copied from src/Makefile.inc1 98OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \ 99 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h 100.endif 101# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build 102# 103# Ports search for some dependencies in PATH, so add the location of the 104# installed files 105WRKDIRPREFIX?= ${.OBJDIR} 106PORTSMODULESENV=\ 107 env \ 108 -u CC \ 109 -u CXX \ 110 -u CPP \ 111 -u MAKESYSPATH \ 112 -u MK_AUTO_OBJ \ 113 -u MAKEOBJDIR \ 114 MAKEFLAGS="${MAKEFLAGS:M*:tW:S/^-m /-m_/g:S/ -m / -m_/g:tw:N-m_*:NMK_AUTO_OBJ=*}" \ 115 SYSDIR=${SYSDIR} \ 116 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \ 117 SRC_BASE=${SRC_BASE} \ 118 OSVERSION=${OSRELDATE} \ 119 WRKDIRPREFIX=${WRKDIRPREFIX} 120 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 123all: 124.for __i in ${PORTS_MODULES} 125 @${ECHO} "===> Ports module ${__i} (all)" 126 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build 127.endfor 128 129.for __target in install reinstall clean 130${__target}: ports-${__target} 131ports-${__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/} 135.endfor 136.endfor 137.endif 138 139.ORDER: kernel-install modules-install 140 141beforebuild: .PHONY 142kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA} 143 144kernel-cleandir: kernel-clean kernel-cleandepend 145 146kernel-clobber: 147 find . -maxdepth 1 ! -type d ! -name version -delete 148 149kernel-obj: 150 151.if !defined(NO_MODULES) 152modules: modules-all 153modules-depend: beforebuild 154modules-all: beforebuild 155 156.if !defined(NO_MODULES_OBJ) 157modules-all modules-depend: modules-obj 158.endif 159.endif 160 161.if !defined(DEBUG) 162FULLKERNEL= ${KERNEL_KO} 163.else 164FULLKERNEL= ${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} 170install.debug reinstall.debug: gdbinit 171 cd ${.CURDIR}; ${MAKE} ${.TARGET:R} 172 173# Install gdbinit files for kernel debugging. 174gdbinit: 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 181.endif 182.endif 183 184${FULLKERNEL}: ${SYSTEM_DEP} vers.o 185 @rm -f ${.TARGET} 186 @echo linking ${.TARGET} 187 ${SYSTEM_LD} 188.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE) 189 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE} 190.endif 191.if ${MK_CTF} != "no" 192 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ... 193 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o 194.endif 195.if !defined(DEBUG) 196 ${OBJCOPY} --strip-debug ${.TARGET} 197.endif 198 ${SYSTEM_LD_TAIL} 199 200OBJS_DEPEND_GUESS+= offset.inc assym.inc vnode_if.h ${BEFORE_DEPEND:M*.h} \ 201 ${MFILES:T:S/.m$/.h/} 202 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 210.endfor 211 212kernel-clean: 213 rm -f *.o *.so *.pico *.ko *.s eddep errs \ 214 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \ 215 tags vers.c \ 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/} \ 218 ${CLEAN} 219 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. 223HACK_EXTRA_FLAGS?= -shared 224hack.pico: Makefile 225 :> hack.c 226 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico 227 rm -f hack.c 228 229offset.inc: $S/kern/genoffset.sh genoffset.o 230 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genoffset.sh genoffset.o > ${.TARGET} 231 232genoffset.o: $S/kern/genoffset.c 233 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/kern/genoffset.c 234 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). 238genoffset_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} 241 242assym.inc: $S/kern/genassym.sh genassym.o genoffset_test.o 243 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET} 244 245genassym.o: $S/$M/$M/genassym.c offset.inc 246 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c 247 248OBJS_DEPEND_GUESS+= opt_global.h 249genoffset.o genassym.o vers.o: opt_global.h 250 251.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon) 252_meta_filemon= 1 253.endif 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}) 264_SKIP_DEPEND= 1 265.endif 266.if defined(_SKIP_DEPEND) || defined(_meta_filemon) 267.MAKE.DEPENDFILE= /dev/null 268.endif 269 270kernel-depend: .depend 271SRCS= assym.inc offset.inc vnode_if.h ${BEFORE_DEPEND} ${CFILES} \ 272 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \ 273 ${MFILES:T:S/.m$/.h/} 274DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o genoffset.o genoffset_test.o 275DEPENDOBJS+= ${CLEAN:M*.o} 276DEPENDFILES= ${DEPENDOBJS:O:u:C/^/.depend./} 277.if ${MAKE_VERSION} < 20160220 278DEPEND_MP?= -MP 279.endif 280.if defined(_SKIP_DEPEND) 281# Don't bother reading any .meta files 282${DEPENDOBJS}: .NOMETA 283.depend: .NOMETA 284# Unset these to avoid looping/statting on them later. 285.undef DEPENDOBJS 286.undef DEPENDFILES 287.endif # defined(_SKIP_DEPEND) 288DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET} 289DEPEND_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. 294DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != "" 295CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:} 296.endif 297.for __depend_obj in ${DEPENDFILES} 298.if ${MAKE_VERSION} < 20160220 299.sinclude "${.OBJDIR}/${__depend_obj}" 300.else 301.dinclude "${.OBJDIR}/${__depend_obj}" 302.endif 303.endfor 304.endif # !defined(_meta_filemon) 305 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. 310beforebuild: kernel-depend 311 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 314# file used. 315.for __obj in ${DEPENDOBJS:O:u} 316.if defined(_meta_filemon) 317_depfile= ${.OBJDIR}/${__obj}.meta 318.else 319_depfile= ${.OBJDIR}/.depend.${__obj} 320.endif 321.if !exists(${_depfile}) 322.if ${SYSTEM_OBJS:M${__obj}} 323${__obj}: ${OBJS_DEPEND_GUESS} 324.endif 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} 334.endif 335${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 336.endif # !exists(${_depfile}) 337.endfor 338 339.NOPATH: .depend ${DEPENDFILES} 340 341.depend: .PRECIOUS ${SRCS} 342 343.if ${COMPILER_TYPE} == "clang" || \ 344 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 60000) 345_MAP_DEBUG_PREFIX= yes 346.endif 347 348_ILINKS= machine 349.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64" 350_ILINKS+= ${MACHINE_CPUARCH} 351.endif 352.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 353_ILINKS+= x86 354.endif 355 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} 361.endif 362.if defined(_MAP_DEBUG_PREFIX) 363.if ${_link} == "machine" 364CFLAGS+= -fdebug-prefix-map=./machine=${SYSDIR}/${MACHINE}/include 365.else 366CFLAGS+= -fdebug-prefix-map=./${_link}=${SYSDIR}/${_link}/include 367.endif 368.endif 369.endfor 370 371${_ILINKS}: 372 @case ${.TARGET} in \ 373 machine) \ 374 path=${S}/${MACHINE}/include ;; \ 375 *) \ 376 path=${S}/${.TARGET}/include ;; \ 377 esac ; \ 378 ${ECHO} ${.TARGET} "->" $$path ; \ 379 ln -fns $$path ${.TARGET} 380 381# .depend needs include links so we remove them only together. 382kernel-cleandepend: .PHONY 383 rm -f .depend .depend.* ${_ILINKS} 384 385kernel-tags: 386 @[ -f .depend ] || { echo "you must make depend first"; exit 1; } 387 sh $S/conf/systags.sh 388 389kernel-install: .PHONY 390 @if [ ! -f ${KERNEL_KO} ] ; then \ 391 echo "You must build a kernel first." ; \ 392 exit 1 ; \ 393 fi 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} ; \ 400 else \ 401 if [ -d ${DESTDIR}${KODIR}.old ] ; then \ 402 chflags -R noschg ${DESTDIR}${KODIR}.old ; \ 403 rm -rf ${DESTDIR}${KODIR}.old ; \ 404 fi ; \ 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 ; \ 410 fi ; \ 411 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \ 412 fi 413.endif 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}/ 419.endif 420.if defined(KERNEL_EXTRA_INSTALL) 421 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/ 422.endif 423 424 425 426kernel-reinstall: 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}/ 431.endif 432 433config.o env.o hints.o vers.o vnode_if.o: 434 ${NORMAL_C} 435 ${NORMAL_CTFCONVERT} 436 437.if ${MK_REPRODUCIBLE_BUILD} != "no" 438REPRO_FLAG="-R" 439.endif 440vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP} 441 MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT} 442 443vnode_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 445 446vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \ 447 $S/kern/vnode_if.src 448vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 449 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h 450vnode_if_newproto.h: 451 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p 452vnode_if_typedef.h: 453 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q 454 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, 460# respectively. 461embedfs_${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} 466 ${OBJCOPY} \ 467 --rename-section .data=mfs,contents,alloc,load,readonly,data \ 468 --redefine-sym \ 469 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \ 470 --redefine-sym \ 471 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \ 472 --redefine-sym \ 473 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \ 474 ${.TARGET} 475.endif 476.endif 477 478# XXX strictly, everything depends on Makefile because changes to ${PROF} 479# only appear there, but we don't handle that. 480 481.include "kern.mk" 482