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