3 # Part of a unified Makefile for building kernels. This part contains all
4 # of the definitions that need to be before %BEFORE_DEPEND.
6 # Allow user to configure things that only effect src tree builds.
7 # Note: This is duplicated from src.sys.mk to ensure that we include
8 # /etc/src.conf when building the kernel. Kernels can be built without
9 # the rest of /usr/src, but they still always process SRCCONF even though
10 # the normal mechanisms to prevent that (compiling out of tree) won't
11 # work. To ensure they do work, we have to duplicate thee few lines here.
12 SRCCONF?= /etc/src.conf
13 .if (exists(${SRCCONF}) || ${SRCCONF} != "/etc/src.conf") && !target(_srcconf_included_)
19 .include <bsd.compiler.mk>
20 .include "kern.opts.mk"
22 # The kernel build always occurs in the object directory which is .CURDIR.
23 .if ${.MAKE.MODE:Unormal:Mmeta}
24 .MAKE.MODE+= curdirOk=yes
27 # The kernel build always expects .OBJDIR=.CURDIR.
30 .if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
34 .if !defined(NO_OBJWALK)
38 # Can be overridden by makeoptions or /etc/make.conf
41 KODIR?= /boot/${KERNEL}
42 LDSCRIPT_NAME?= ldscript.$M
43 LDSCRIPT?= $S/conf/${LDSCRIPT_NAME}
56 .if ${MACHINE_CPUARCH} == "amd64" && ${COMPILER_TYPE} != "clang"
57 _COPTFLAGS_EXTRA=-frename-registers
61 COPTFLAGS?=-O2 -pipe ${_COPTFLAGS_EXTRA}
62 .if !empty(COPTFLAGS:M-O[23s]) && empty(COPTFLAGS:M-fno-strict-aliasing)
63 COPTFLAGS+= -fno-strict-aliasing
65 .if !defined(NO_CPU_COPTFLAGS)
66 COPTFLAGS+= ${_CPUCFLAGS}
70 INCLUDES= ${NOSTDINC} ${INCLMAGIC} -I. -I$S -I$S/contrib/ck/include
72 CFLAGS= ${COPTFLAGS} ${DEBUG}
73 CFLAGS+= ${INCLUDES} -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h
74 CFLAGS_PARAM_INLINE_UNIT_GROWTH?=100
75 CFLAGS_PARAM_LARGE_FUNCTION_GROWTH?=1000
76 .if ${MACHINE_CPUARCH} == "mips"
77 CFLAGS_ARCH_PARAMS?=--param max-inline-insns-single=1000 -DMACHINE_ARCH='"${MACHINE_ARCH}"'
79 CFLAGS.gcc+= -fms-extensions -finline-limit=${INLINE_LIMIT}
80 CFLAGS.gcc+= --param inline-unit-growth=${CFLAGS_PARAM_INLINE_UNIT_GROWTH}
81 CFLAGS.gcc+= --param large-function-growth=${CFLAGS_PARAM_LARGE_FUNCTION_GROWTH}
82 CFLAGS.gcc+= -fms-extensions
83 .if defined(CFLAGS_ARCH_PARAMS)
84 CFLAGS.gcc+=${CFLAGS_ARCH_PARAMS}
87 # The following should be removed no earlier than LLVM11 being imported into the
88 # tree, to ensure we don't regress the build. LLVM11 and GCC10 will switch the
89 # default over to -fno-common, making this redundant.
92 # XXX LOCORE means "don't declare C stuff" not "for locore.s".
93 ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${CFLAGS} ${ASM_CFLAGS.${.IMPSRC:T}}
95 .if defined(PROFLEVEL) && ${PROFLEVEL} >= 1
97 CFLAGS.gcc+= -falign-functions=16
99 CFLAGS+= -DGPROF4 -DGUPROF
101 .if ${COMPILER_TYPE} == "gcc"
102 PROF+= -mprofiler-epilogue
108 DEFINED_PROF= ${PROF}
110 KCSAN_ENABLED!= grep KCSAN opt_global.h || true ; echo
111 .if !empty(KCSAN_ENABLED)
112 SAN_CFLAGS+= -fsanitize=thread
115 KUBSAN_ENABLED!= grep KUBSAN opt_global.h || true ; echo
116 .if !empty(KUBSAN_ENABLED)
117 SAN_CFLAGS+= -fsanitize=undefined
120 COVERAGE_ENABLED!= grep COVERAGE opt_global.h || true ; echo
121 .if !empty(COVERAGE_ENABLED)
122 .if ${COMPILER_TYPE} == "clang" || \
123 (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
124 SAN_CFLAGS+= -fsanitize-coverage=trace-pc,trace-cmp
126 SAN_CFLAGS+= -fsanitize-coverage=trace-pc
130 CFLAGS+= ${SAN_CFLAGS}
132 GCOV_ENABLED!= grep GCOV opt_global.h || true ; echo
133 .if !empty(GCOV_ENABLED)
134 .if ${COMPILER_TYPE} == "gcc"
135 GCOV_CFLAGS+= -fprofile-arcs -ftest-coverage
139 CFLAGS+= ${GCOV_CFLAGS}
141 # Put configuration-specific C flags last (except for ${PROF}) so that they
142 # can override the others.
143 CFLAGS+= ${CONF_CFLAGS}
145 .if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
146 LDFLAGS+= --build-id=sha1
149 .if (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
150 ${MACHINE_CPUARCH} == "i386" || ${MACHINE} == "powerpc") && \
151 defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == ""
152 .error amd64/arm64/i386/ppc* kernel requires linker ifunc support
154 .if ${MACHINE_CPUARCH} == "amd64"
155 LDFLAGS+= -z max-page-size=2097152
156 .if ${LINKER_TYPE} != "lld"
157 LDFLAGS+= -z common-page-size=4096
159 .if defined(LINKER_FEATURES) && !${LINKER_FEATURES:Mifunc-noplt}
160 .warning "Linker ${LD} does not support -z ifunc-noplt -> ifunc calls are unoptimized."
162 LDFLAGS+= -z notext -z ifunc-noplt
165 .endif # ${MACHINE_CPUARCH} == "amd64"
167 .if ${MACHINE_CPUARCH} == "riscv"
168 # Hack: Work around undefined weak symbols being out of range when linking with
169 # LLD (address is a PC-relative calculation, and BFD works around this by
170 # rewriting the instructions to generate an absolute address of 0); -fPIE
171 # avoids this since it uses the GOT for all extern symbols, which is overly
172 # inefficient for us. Drop once undefined weak symbols work with medany.
173 .if ${LINKER_TYPE} == "lld"
178 NORMAL_C= ${CC} -c ${CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
179 NORMAL_S= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} ${.IMPSRC}
180 PROFILE_C= ${CC} -c ${CFLAGS} ${WERROR} ${.IMPSRC}
181 NORMAL_C_NOWERROR= ${CC} -c ${CFLAGS} ${PROF} ${.IMPSRC}
183 NORMAL_M= ${AWK} -f $S/tools/makeobjops.awk ${.IMPSRC} -c ; \
184 ${CC} -c ${CFLAGS} ${WERROR} ${PROF} ${.PREFIX}.c
186 NORMAL_FW= uudecode -o ${.TARGET} ${.ALLSRC}
187 NORMAL_FWO= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} -o ${.TARGET} \
188 $S/kern/firmw.S -DFIRMW_FILE="${.ALLSRC:M*.fw}" \
189 -DFIRMW_SYMBOL="${.ALLSRC:M*.fw:C/[-.\/]/_/g}"
191 # for ZSTD in the kernel (include zstd/lib/freebsd before other CFLAGS)
192 ZSTD_C= ${CC} -c -DZSTD_HEAPMODE=1 -I$S/contrib/zstd/lib/freebsd ${CFLAGS} -I$S/contrib/zstd/lib -I$S/contrib/zstd/lib/common ${WERROR} -Wno-inline -Wno-missing-prototypes ${PROF} -U__BMI__ ${.IMPSRC}
193 # https://github.com/facebook/zstd/commit/812e8f2a [zstd 1.4.1]
194 # "Note that [GCC] autovectorization still does not do a good job on the
195 # optimized version, so it's turned off via attribute and flag. I found
196 # that neither attribute nor command-line flag were entirely successful in
197 # turning off vectorization, which is why there were both."
198 .if ${COMPILER_TYPE} == "gcc"
199 ZSTD_DECOMPRESS_BLOCK_FLAGS= -fno-tree-vectorize
202 ZINCDIR=$S/contrib/openzfs/include
203 # Common for dtrace / zfs
205 -DFREEBSD_NAMECACHE \
210 -include $S/modules/zfs/static_ccompile.h \
213 -I${ZINCDIR}/os/freebsd \
214 -I${ZINCDIR}/os/freebsd/spl \
215 -I${ZINCDIR}/os/freebsd/zfs \
217 -I$S/contrib/openzfs/module/zstd/include \
218 -I$S/contrib/openzfs/module/zstd/lib/freebsd/ \
220 -Wno-unknown-pragmas \
221 -Wno-missing-prototypes \
223 -Wno-strict-prototypes \
226 -Wno-redundant-decls \
227 -Wno-missing-braces \
233 -Wno-unknown-pragmas \
234 -Wno-duplicate-decl-specifier \
235 -include ${ZINCDIR}/os/freebsd/spl/sys/ccompile.h \
236 -I$S/cddl/contrib/opensolaris/uts/common \
237 -I$S -I$S/cddl/compat/opensolaris
238 CDDL_C= ${CC} -c ${CDDL_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
240 # Special flags for managing the compat compiles for ZFS
241 ZFS_CFLAGS+= ${CDDL_CFLAGS} -DBUILDING_ZFS -DHAVE_UIO_ZEROCOPY \
242 -DWITH_NETDUMP -D__KERNEL__ -D_SYS_CONDVAR_H_ -DSMP \
243 -DIN_FREEBSD_BASE -DHAVE_KSID
245 .if ${MACHINE_ARCH} == "amd64"
246 ZFS_CFLAGS+= -DHAVE_AVX2 -DHAVE_AVX -D__x86_64 -DHAVE_SSE2 -DHAVE_AVX512F \
247 -DHAVE_SSSE3 -DHAVE_AVX512BW
250 .if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "powerpc" || \
251 ${MACHINE_ARCH} == "powerpcspe" || ${MACHINE_ARCH} == "arm"
252 ZFS_CFLAGS+= -DBITS_PER_LONG=32
254 ZFS_CFLAGS+= -DBITS_PER_LONG=64
258 ZFS_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${ZFS_CFLAGS}
259 ZFS_C= ${CC} -c ${ZFS_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
260 ZFS_RPC_C= ${CC} -c ${ZFS_CFLAGS} -DHAVE_RPC_TYPES ${WERROR} ${PROF} ${.IMPSRC}
261 ZFS_S= ${CC} -c ${ZFS_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
265 # Special flags for managing the compat compiles for DTrace
266 DTRACE_CFLAGS= -DBUILDING_DTRACE ${CDDL_CFLAGS} -I$S/cddl/dev/dtrace -I$S/cddl/dev/dtrace/${MACHINE_CPUARCH}
267 .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
268 DTRACE_CFLAGS+= -I$S/cddl/contrib/opensolaris/uts/intel -I$S/cddl/dev/dtrace/x86
270 DTRACE_CFLAGS+= -I$S/cddl/contrib/opensolaris/common/util -I$S -DDIS_MEM -DSMP -I$S/cddl/compat/opensolaris
271 DTRACE_CFLAGS+= -I$S/cddl/contrib/opensolaris/uts/common
272 DTRACE_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${DTRACE_CFLAGS}
273 DTRACE_C= ${CC} -c ${DTRACE_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
274 DTRACE_S= ${CC} -c ${DTRACE_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
276 # Special flags for managing the compat compiles for DTrace/FBT
277 FBT_CFLAGS= -DBUILDING_DTRACE -nostdinc -I$S/cddl/dev/fbt/${MACHINE_CPUARCH} -I$S/cddl/dev/fbt ${CDDL_CFLAGS} -I$S/cddl/compat/opensolaris -I$S/cddl/contrib/opensolaris/uts/common
278 .if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
279 FBT_CFLAGS+= -I$S/cddl/dev/fbt/x86
281 FBT_C= ${CC} -c ${FBT_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
283 .if ${MK_CTF} != "no"
284 NORMAL_CTFCONVERT= ${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
285 .elif ${MAKE_VERSION} >= 5201111300
288 NORMAL_CTFCONVERT= @:
291 # Linux Kernel Programming Interface C-flags
292 LINUXKPI_INCLUDES= -I$S/compat/linuxkpi/common/include
293 LINUXKPI_C= ${NORMAL_C} ${LINUXKPI_INCLUDES}
295 # Infiniband C flags. Correct include paths and omit errors that linux
297 OFEDINCLUDES= -I$S/ofed/include -I$S/ofed/include/uapi ${LINUXKPI_INCLUDES}
298 OFEDNOERR= -Wno-cast-qual -Wno-pointer-arith
299 OFEDCFLAGS= ${CFLAGS:N-I*} -DCONFIG_INFINIBAND_USER_MEM \
300 ${OFEDINCLUDES} ${CFLAGS:M-I*} ${OFEDNOERR}
301 OFED_C_NOIMP= ${CC} -c -o ${.TARGET} ${OFEDCFLAGS} ${WERROR} ${PROF}
302 OFED_C= ${OFED_C_NOIMP} ${.IMPSRC}
305 MLXFW_C= ${OFED_C_NOIMP} \
306 -I${SRCTOP}/sys/contrib/xz-embedded/freebsd \
307 -I${SRCTOP}/sys/contrib/xz-embedded/linux/lib/xz \
310 GEN_CFILES= $S/$M/$M/genassym.c ${MFILES:T:S/.m$/.c/}
311 SYSTEM_CFILES= config.c env.c hints.c vnode_if.c
312 SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
313 SYSTEM_OBJS= locore.o ${MDOBJS} ${OBJS}
314 SYSTEM_OBJS+= ${SYSTEM_CFILES:.c=.o}
315 SYSTEM_OBJS+= hack.pico
317 KEYMAP=kbdcontrol -P ${SRCTOP}/share/vt/keymaps -P ${SRCTOP}/share/syscons/keymaps
318 KEYMAP_FIX=sed -e 's/^static keymap_t.* = /static keymap_t key_map = /' -e 's/^static accentmap_t.* = /static accentmap_t accent_map = /'
320 MD_ROOT_SIZE_CONFIGURED!= grep MD_ROOT_SIZE opt_md.h || true ; echo
321 .if ${MFS_IMAGE:Uno} != "no"
322 .if empty(MD_ROOT_SIZE_CONFIGURED)
323 SYSTEM_OBJS+= embedfs_${MFS_IMAGE:T:R}.o
327 ${LD} -m ${LD_EMULATION} -Bdynamic -T ${LDSCRIPT} ${_LDFLAGS} \
328 --no-warn-mismatch --warn-common --export-dynamic \
329 --dynamic-linker /red/herring -X
330 SYSTEM_LD= @${SYSTEM_LD_BASECMD} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
331 SYSTEM_LD_TAIL= @${OBJCOPY} --strip-symbol gcc2_compiled. ${.TARGET} ; \
332 ${SIZE} ${.TARGET} ; chmod 755 ${.TARGET}
333 SYSTEM_DEP+= ${LDSCRIPT}
335 # Calculate path for .m files early, if needed.
336 .if !defined(NO_MODULES) && !defined(__MPATH) && !make(install) && \
337 (empty(.MAKEFLAGS:M-V) || defined(NO_SKIP_MPATH))
338 __MPATH!=find ${S:tA}/ -name \*_if.m
341 # MKMODULESENV is set here so that port makefiles can augment
344 MKMODULESENV+= MAKEOBJDIRPREFIX=${.OBJDIR}/modules KMODDIR=${KODIR}
345 MKMODULESENV+= MACHINE_CPUARCH=${MACHINE_CPUARCH}
346 MKMODULESENV+= MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH}
347 MKMODULESENV+= MODULES_EXTRA="${MODULES_EXTRA}" WITHOUT_MODULES="${WITHOUT_MODULES}"
348 MKMODULESENV+= ARCH_FLAGS="${ARCH_FLAGS}"
349 .if (${KERN_IDENT} == LINT)
350 MKMODULESENV+= ALL_MODULES=LINT
352 .if defined(MODULES_OVERRIDE)
353 MKMODULESENV+= MODULES_OVERRIDE="${MODULES_OVERRIDE}"
356 MKMODULESENV+= DEBUG_FLAGS="${DEBUG}"
358 .if !defined(NO_MODULES)
359 MKMODULESENV+= __MPATH="${__MPATH}"
362 # Detect kernel config options that force stack frames to be turned on.
363 DDB_ENABLED!= grep DDB opt_ddb.h || true ; echo
364 DTR_ENABLED!= grep KDTRACE_FRAME opt_kdtrace.h || true ; echo
365 HWPMC_ENABLED!= grep HWPMC opt_hwpmc_hooks.h || true ; echo