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# In case the config had a makeoptions DESTDIR... 12.if defined(DESTDIR) 13MKMODULESENV+= DESTDIR="${DESTDIR}" 14.endif 15SYSDIR?= ${S:C;^[^/];${.CURDIR}/&;:tA} 16MKMODULESENV+= KERNBUILDDIR="${.CURDIR}" SYSDIR="${SYSDIR}" 17 18.if defined(CONF_CFLAGS) 19MKMODULESENV+= CONF_CFLAGS="${CONF_CFLAGS}" 20.endif 21 22.if defined(WITH_CTF) 23MKMODULESENV+= WITH_CTF="${WITH_CTF}" 24.endif 25 26.if defined(WITH_EXTRA_TCP_STACKS) 27MKMODULESENV+= WITH_EXTRA_TCP_STACKS="${WITH_EXTRA_TCP_STACKS}" 28.endif 29 30# Allow overriding the kernel debug directory, so kernel and user debug may be 31# installed in different directories. Setting it to "" restores the historical 32# behavior of installing debug files in the kernel directory. 33KERN_DEBUGDIR?= ${DEBUGDIR} 34 35.MAIN: all 36 37.for target in all clean cleandepend cleandir clobber depend install \ 38 ${_obj} reinstall tags 39${target}: kernel-${target} 40.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules) 41${target}: modules-${target} 42modules-${target}: 43 cd $S/modules; ${MKMODULESENV} ${MAKE} \ 44 ${target:S/^reinstall$/install/:S/^clobber$/cleandir/} 45.endif 46.endfor 47 48# Handle ports (as defined by the user) that build kernel modules 49.if !defined(NO_MODULES) && defined(PORTS_MODULES) 50# 51# The ports tree needs some environment variables defined to match the new kernel 52# 53# Ports search for some dependencies in PATH, so add the location of the installed files 54LOCALBASE?= /usr/local 55# SRC_BASE is how the ports tree refers to the location of the base source files 56.if !defined(SRC_BASE) 57SRC_BASE= ${SYSDIR:H:tA} 58.endif 59# OSVERSION is used by some ports to determine build options 60.if !defined(OSRELDATE) 61# Definition copied from src/Makefile.inc1 62OSRELDATE!= awk '/^\#define[[:space:]]*__FreeBSD_version/ { print $$3 }' \ 63 ${MAKEOBJDIRPREFIX}${SRC_BASE}/include/osreldate.h 64.endif 65# Keep the related ports builds in the obj directory so that they are only rebuilt once per kernel build 66WRKDIRPREFIX?= ${.OBJDIR} 67PORTSMODULESENV=\ 68 env \ 69 -u CC \ 70 -u CXX \ 71 -u CPP \ 72 PATH=${PATH}:${LOCALBASE}/bin:${LOCALBASE}/sbin \ 73 SRC_BASE=${SRC_BASE} \ 74 OSVERSION=${OSRELDATE} \ 75 WRKDIRPREFIX=${WRKDIRPREFIX} 76 77# The WRKDIR needs to be cleaned before building, and trying to change the target 78# with a :C pattern below results in install -> instclean 79all: 80.for __i in ${PORTS_MODULES} 81 @${ECHO} "===> Ports module ${__i} (all)" 82 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B clean build 83.endfor 84 85.for __target in install reinstall clean 86${__target}: ports-${__target} 87ports-${__target}: 88.for __i in ${PORTS_MODULES} 89 @${ECHO} "===> Ports module ${__i} (${__target})" 90 cd $${PORTSDIR:-/usr/ports}/${__i}; ${PORTSMODULESENV} ${MAKE} -B ${__target:C/(re)?install/deinstall reinstall/} 91.endfor 92.endfor 93.endif 94 95.ORDER: kernel-install modules-install 96 97beforebuild: .PHONY 98kernel-all: beforebuild .WAIT ${KERNEL_KO} ${KERNEL_EXTRA} 99 100kernel-cleandir: kernel-clean kernel-cleandepend 101 102kernel-clobber: 103 find . -maxdepth 1 ! -type d ! -name version -delete 104 105kernel-obj: 106 107.if !defined(MODULES_WITH_WORLD) && !defined(NO_MODULES) && exists($S/modules) 108modules: modules-all 109 110.if !defined(NO_MODULES_OBJ) 111modules-all modules-depend: modules-obj 112.endif 113.endif 114 115.if !defined(DEBUG) 116FULLKERNEL= ${KERNEL_KO} 117.else 118FULLKERNEL= ${KERNEL_KO}.full 119${KERNEL_KO}: ${FULLKERNEL} ${KERNEL_KO}.debug 120 ${OBJCOPY} --strip-debug --add-gnu-debuglink=${KERNEL_KO}.debug \ 121 ${FULLKERNEL} ${.TARGET} 122${KERNEL_KO}.debug: ${FULLKERNEL} 123 ${OBJCOPY} --only-keep-debug ${FULLKERNEL} ${.TARGET} 124install.debug reinstall.debug: gdbinit 125 cd ${.CURDIR}; ${MAKE} ${.TARGET:R} 126 127# Install gdbinit files for kernel debugging. 128gdbinit: 129 grep -v '# XXX' ${S}/../tools/debugscripts/dot.gdbinit | \ 130 sed "s:MODPATH:${.OBJDIR}/modules:" > .gdbinit 131 cp ${S}/../tools/debugscripts/gdbinit.kernel ${.CURDIR} 132.if exists(${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH}) 133 cp ${S}/../tools/debugscripts/gdbinit.${MACHINE_CPUARCH} \ 134 ${.CURDIR}/gdbinit.machine 135.endif 136.endif 137 138${FULLKERNEL}: ${SYSTEM_DEP} vers.o 139 @rm -f ${.TARGET} 140 @echo linking ${.TARGET} 141 ${SYSTEM_LD} 142.if !empty(MD_ROOT_SIZE_CONFIGURED) && defined(MFS_IMAGE) 143 @sh ${S}/tools/embed_mfs.sh ${.TARGET} ${MFS_IMAGE} 144.endif 145.if ${MK_CTF} != "no" 146 @echo ${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ... 147 @${CTFMERGE} ${CTFFLAGS} -o ${.TARGET} ${SYSTEM_OBJS} vers.o 148.endif 149.if !defined(DEBUG) 150 ${OBJCOPY} --strip-debug ${.TARGET} 151.endif 152 ${SYSTEM_LD_TAIL} 153 154OBJS_DEPEND_GUESS+= assym.s vnode_if.h ${BEFORE_DEPEND:M*.h} \ 155 ${MFILES:T:S/.m$/.h/} 156 157.for mfile in ${MFILES} 158# XXX the low quality .m.o rules gnerated by config are normally used 159# instead of the .m.c rules here. 160${mfile:T:S/.m$/.c/}: ${mfile} 161 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -c 162${mfile:T:S/.m$/.h/}: ${mfile} 163 ${AWK} -f $S/tools/makeobjops.awk ${mfile} -h 164.endfor 165 166kernel-clean: 167 rm -f *.o *.so *.pico *.ko *.s eddep errs \ 168 ${FULLKERNEL} ${KERNEL_KO} ${KERNEL_KO}.debug \ 169 tags vers.c \ 170 vnode_if.c vnode_if.h vnode_if_newproto.h vnode_if_typedef.h \ 171 ${MFILES:T:S/.m$/.c/} ${MFILES:T:S/.m$/.h/} \ 172 ${CLEAN} 173 174# This is a hack. BFD "optimizes" away dynamic mode if there are no 175# dynamic references. We could probably do a '-Bforcedynamic' mode like 176# in the a.out ld. For now, this works. 177HACK_EXTRA_FLAGS?= -shared 178hack.pico: Makefile 179 :> hack.c 180 ${CC} ${HACK_EXTRA_FLAGS} -nostdlib hack.c -o hack.pico 181 rm -f hack.c 182 183assym.s: $S/kern/genassym.sh genassym.o 184 NM='${NM}' NMFLAGS='${NMFLAGS}' sh $S/kern/genassym.sh genassym.o > ${.TARGET} 185 186genassym.o: $S/$M/$M/genassym.c 187 ${CC} -c ${CFLAGS:N-flto:N-fno-common} $S/$M/$M/genassym.c 188 189${SYSTEM_OBJS} genassym.o vers.o: opt_global.h 190 191.if !empty(.MAKE.MODE:Unormal:Mmeta) && empty(.MAKE.MODE:Unormal:Mnofilemon) 192_meta_filemon= 1 193.endif 194# Skip reading .depend when not needed to speed up tree-walks and simple 195# lookups. For install, only do this if no other targets are specified. 196# Also skip generating or including .depend.* files if in meta+filemon mode 197# since it will track dependencies itself. OBJS_DEPEND_GUESS is still used 198# for _meta_filemon but not for _SKIP_DEPEND. 199.if !defined(NO_SKIP_DEPEND) && (make(*obj) || \ 200 ${.TARGETS:M*clean*} == ${.TARGETS} || \ 201 ${.TARGETS:M*install*} == ${.TARGETS}) 202_SKIP_DEPEND= 1 203.endif 204.if defined(_SKIP_DEPEND) || defined(_meta_filemon) 205.MAKE.DEPENDFILE= /dev/null 206.endif 207 208kernel-depend: .depend 209SRCS= assym.s vnode_if.h ${BEFORE_DEPEND} ${CFILES} \ 210 ${SYSTEM_CFILES} ${GEN_CFILES} ${SFILES} \ 211 ${MFILES:T:S/.m$/.h/} 212DEPENDFILES= .depend .depend.* 213DEPENDOBJS+= ${SYSTEM_OBJS} genassym.o 214DEPENDFILES_OBJS= ${DEPENDOBJS:O:u:C/^/.depend./} 215.if ${MAKE_VERSION} < 20160220 216DEPEND_MP?= -MP 217.endif 218.if defined(_SKIP_DEPEND) 219# Don't bother reading any .meta files 220${DEPENDOBJS}: .NOMETA 221.depend: .NOMETA 222# Unset these to avoid looping/statting on them later. 223.undef DEPENDSRCS 224.undef DEPENDOBJS 225.undef DEPENDFILES_OBJS 226.endif # defined(_SKIP_DEPEND) 227DEPEND_CFLAGS+= -MD ${DEPEND_MP} -MF.depend.${.TARGET} 228DEPEND_CFLAGS+= -MT${.TARGET} 229.if !defined(_meta_filemon) 230.if !empty(DEPEND_CFLAGS) 231# Only add in DEPEND_CFLAGS for CFLAGS on files we expect from DEPENDOBJS 232# as those are the only ones we will include. 233DEPEND_CFLAGS_CONDITION= "${DEPENDOBJS:M${.TARGET}}" != "" 234CFLAGS+= ${${DEPEND_CFLAGS_CONDITION}:?${DEPEND_CFLAGS}:} 235.endif 236.for __depend_obj in ${DEPENDFILES_OBJS} 237.if ${MAKE_VERSION} < 20160220 238.sinclude "${.OBJDIR}/${__depend_obj}" 239.else 240.dinclude "${.OBJDIR}/${__depend_obj}" 241.endif 242.endfor 243.endif # !defined(_meta_filemon) 244 245# Always run 'make depend' to generate dependencies early and to avoid the 246# need for manually running it. For the kernel this is mostly a NOP since 247# all dependencies are correctly added or accounted for. This is mostly to 248# ensure downstream uses of kernel-depend are handled. 249beforebuild: kernel-depend 250 251# Guess some dependencies for when no ${DEPENDFILE}.OBJ is generated yet. 252# For meta+filemon the .meta file is checked for since it is the dependency 253# file used. 254.for __obj in ${DEPENDOBJS:O:u} 255.if defined(_meta_filemon) 256_depfile= ${.OBJDIR}/${__obj}.meta 257.else 258_depfile= ${.OBJDIR}/.depend.${__obj} 259.endif 260.if !exists(${_depfile}) 261.if ${SYSTEM_OBJS:M${__obj}} 262${__obj}: ${OBJS_DEPEND_GUESS} 263.endif 264${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 265.elif defined(_meta_filemon) 266# For meta mode we still need to know which file to depend on to avoid 267# ambiguous suffix transformation rules from .PATH. Meta mode does not 268# use .depend files. We really only need source files, not headers since 269# they are typically in SRCS/beforebuild already. For target-specific 270# guesses do include headers though since they may not be in SRCS. 271.if ${SYSTEM_OBJS:M${__obj}} 272${__obj}: ${OBJS_DEPEND_GUESS:N*.h} 273.endif 274${__obj}: ${OBJS_DEPEND_GUESS.${__obj}} 275.endif # !exists(${_depfile}) 276.endfor 277 278.NOPATH: .depend ${DEPENDFILES_OBJS} 279 280.depend: .PRECIOUS ${SRCS} 281 282_ILINKS= machine 283.if ${MACHINE} != ${MACHINE_CPUARCH} && ${MACHINE} != "arm64" 284_ILINKS+= ${MACHINE_CPUARCH} 285.endif 286.if ${MACHINE_CPUARCH} == "i386" || ${MACHINE_CPUARCH} == "amd64" 287_ILINKS+= x86 288.endif 289 290# Ensure that the link exists without depending on it when it exists. 291.for _link in ${_ILINKS} 292.if !exists(${.OBJDIR}/${_link}) 293${SRCS} ${CLEAN:M*.o}: ${_link} 294.endif 295.endfor 296 297${_ILINKS}: 298 @case ${.TARGET} in \ 299 machine) \ 300 path=${S}/${MACHINE}/include ;; \ 301 *) \ 302 path=${S}/${.TARGET}/include ;; \ 303 esac ; \ 304 ${ECHO} ${.TARGET} "->" $$path ; \ 305 ln -fhs $$path ${.TARGET} 306 307# .depend needs include links so we remove them only together. 308kernel-cleandepend: .PHONY 309 rm -f ${DEPENDFILES} ${_ILINKS} 310 311kernel-tags: 312 @[ -f .depend ] || { echo "you must make depend first"; exit 1; } 313 sh $S/conf/systags.sh 314 315kernel-install: .PHONY 316 @if [ ! -f ${KERNEL_KO} ] ; then \ 317 echo "You must build a kernel first." ; \ 318 exit 1 ; \ 319 fi 320.if exists(${DESTDIR}${KODIR}) 321 -thiskernel=`sysctl -n kern.bootfile` ; \ 322 if [ ! "`dirname "$$thiskernel"`" -ef ${DESTDIR}${KODIR} ] ; then \ 323 chflags -R noschg ${DESTDIR}${KODIR} ; \ 324 rm -rf ${DESTDIR}${KODIR} ; \ 325 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ; \ 326 else \ 327 if [ -d ${DESTDIR}${KODIR}.old ] ; then \ 328 chflags -R noschg ${DESTDIR}${KODIR}.old ; \ 329 rm -rf ${DESTDIR}${KODIR}.old ; \ 330 fi ; \ 331 mv ${DESTDIR}${KODIR} ${DESTDIR}${KODIR}.old ; \ 332 if [ -n "${KERN_DEBUGDIR}" -a \ 333 -d ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ]; then \ 334 rm -rf ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \ 335 mv ${DESTDIR}${KERN_DEBUGDIR}${KODIR} ${DESTDIR}${KERN_DEBUGDIR}${KODIR}.old ; \ 336 fi ; \ 337 sysctl kern.bootfile=${DESTDIR}${KODIR}.old/"`basename "$$thiskernel"`" ; \ 338 fi 339.endif 340 mkdir -p ${DESTDIR}${KODIR} 341 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/ 342.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 343 mkdir -p ${DESTDIR}${KERN_DEBUGDIR}${KODIR} 344 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/ 345.endif 346.if defined(KERNEL_EXTRA_INSTALL) 347 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_EXTRA_INSTALL} ${DESTDIR}${KODIR}/ 348.endif 349 350 351 352kernel-reinstall: 353 @-chflags -R noschg ${DESTDIR}${KODIR} 354 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO} ${DESTDIR}${KODIR}/ 355.if defined(DEBUG) && !defined(INSTALL_NODEBUG) && ${MK_KERNEL_SYMBOLS} != "no" 356 ${INSTALL} -p -m 555 -o ${KMODOWN} -g ${KMODGRP} ${KERNEL_KO}.debug ${DESTDIR}${KERN_DEBUGDIR}${KODIR}/ 357.endif 358 359config.o env.o hints.o vers.o vnode_if.o: 360 ${NORMAL_C} 361 ${NORMAL_CTFCONVERT} 362 363.if ${MK_REPRODUCIBLE_BUILD} != "no" 364REPRO_FLAG="-r" 365.endif 366vers.c: $S/conf/newvers.sh $S/sys/param.h ${SYSTEM_DEP} 367 MAKE="${MAKE}" sh $S/conf/newvers.sh ${REPRO_FLAG} ${KERN_IDENT} 368 369vnode_if.c: $S/tools/vnode_if.awk $S/kern/vnode_if.src 370 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -c 371 372vnode_if.h vnode_if_newproto.h vnode_if_typedef.h: $S/tools/vnode_if.awk \ 373 $S/kern/vnode_if.src 374vnode_if.h: vnode_if_newproto.h vnode_if_typedef.h 375 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -h 376vnode_if_newproto.h: 377 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -p 378vnode_if_typedef.h: 379 ${AWK} -f $S/tools/vnode_if.awk $S/kern/vnode_if.src -q 380 381.if ${MFS_IMAGE:Uno} != "no" 382.if empty(MD_ROOT_SIZE_CONFIGURED) 383# Generate an object file from the file system image to embed in the kernel 384# via linking. Make sure the contents are in the mfs section and rename the 385# start/end/size variables to __start_mfs, __stop_mfs, and mfs_size, 386# respectively. 387embedfs_${MFS_IMAGE:T:R}.o: ${MFS_IMAGE} 388 ${OBJCOPY} --input-target binary \ 389 --output-target ${EMBEDFS_FORMAT.${MACHINE_ARCH}} \ 390 --binary-architecture ${EMBEDFS_ARCH.${MACHINE_ARCH}} \ 391 ${MFS_IMAGE} ${.TARGET} 392 ${OBJCOPY} \ 393 --rename-section .data=mfs,contents,alloc,load,readonly,data \ 394 --redefine-sym \ 395 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_size=__mfs_root_size \ 396 --redefine-sym \ 397 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_start=mfs_root \ 398 --redefine-sym \ 399 _binary_${MFS_IMAGE:C,[^[:alnum:]],_,g}_end=mfs_root_end \ 400 ${.TARGET} 401.endif 402.endif 403 404# XXX strictly, everything depends on Makefile because changes to ${PROF} 405# only appear there, but we don't handle that. 406 407.include "kern.mk" 408