xref: /freebsd/sys/conf/kern.pre.mk (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1
2# Part of a unified Makefile for building kernels.  This part contains all
3# of the definitions that need to be before %BEFORE_DEPEND.
4
5# Allow user to configure things that only effect src tree builds.
6# Note: This is duplicated from src.sys.mk to ensure that we include
7# /etc/src.conf when building the kernel. Kernels can be built without
8# the rest of /usr/src, but they still always process SRCCONF even though
9# the normal mechanisms to prevent that (compiling out of tree) won't
10# work. To ensure they do work, we have to duplicate thee few lines here.
11SRCCONF?=	/etc/src.conf
12.if (exists(${SRCCONF}) || ${SRCCONF} != "/etc/src.conf") && !target(_srcconf_included_)
13.include "${SRCCONF}"
14_srcconf_included_:
15.endif
16
17.include <bsd.own.mk>
18.include <bsd.compiler.mk>
19.include "kern.opts.mk"
20
21# The kernel build always occurs in the object directory which is .CURDIR.
22.if ${.MAKE.MODE:Unormal:Mmeta}
23.MAKE.MODE+=	curdirOk=yes
24.endif
25
26# The kernel build always expects .OBJDIR=.CURDIR.
27.OBJDIR: ${.CURDIR}
28
29.if defined(NO_OBJWALK) || ${MK_AUTO_OBJ} == "yes"
30NO_OBJWALK=		t
31NO_MODULES_OBJ=	t
32.endif
33.if !defined(NO_OBJWALK)
34_obj=		obj
35.endif
36
37# Can be overridden by makeoptions or /etc/make.conf
38KERNEL_KO?=	kernel
39KERNEL?=	kernel
40KODIR?=		/boot/${KERNEL}
41LDSCRIPT_NAME?=	ldscript.$M
42LDSCRIPT?=	$S/conf/${LDSCRIPT_NAME}
43
44M=		${MACHINE}
45
46AWK?=		awk
47CP?=		cp
48ELFDUMP?=	elfdump
49NM?=		nm
50OBJCOPY?=	objcopy
51SIZE?=		size
52
53.if defined(DEBUG)
54CTFFLAGS+=	-g
55.endif
56.if ${MACHINE_CPUARCH} == "amd64" && ${COMPILER_TYPE} != "clang"
57_COPTFLAGS_EXTRA=-frename-registers
58.else
59_COPTFLAGS_EXTRA=
60.endif
61COPTFLAGS?=-O2 -pipe ${_COPTFLAGS_EXTRA}
62.if !empty(COPTFLAGS:M-O[23s]) && empty(COPTFLAGS:M-fno-strict-aliasing)
63COPTFLAGS+= -fno-strict-aliasing
64.endif
65.if !defined(NO_CPU_COPTFLAGS)
66COPTFLAGS+= ${_CPUCFLAGS}
67.endif
68NOSTDINC= -nostdinc
69
70INCLUDES= ${NOSTDINC} ${INCLMAGIC} -I. -I$S -I$S/contrib/ck/include
71
72CFLAGS=	${COPTFLAGS} ${DEBUG}
73CFLAGS+= ${INCLUDES} -D_KERNEL -DHAVE_KERNEL_OPTION_HEADERS -include opt_global.h
74CFLAGS_PARAM_INLINE_UNIT_GROWTH?=100
75CFLAGS_PARAM_LARGE_FUNCTION_GROWTH?=1000
76CFLAGS.gcc+= -fms-extensions -finline-limit=${INLINE_LIMIT}
77CFLAGS.gcc+= --param inline-unit-growth=${CFLAGS_PARAM_INLINE_UNIT_GROWTH}
78CFLAGS.gcc+= --param large-function-growth=${CFLAGS_PARAM_LARGE_FUNCTION_GROWTH}
79CFLAGS.gcc+= -fms-extensions
80.if defined(CFLAGS_ARCH_PARAMS)
81CFLAGS.gcc+=${CFLAGS_ARCH_PARAMS}
82.endif
83WERROR?=	-Werror
84# The following should be removed no earlier than LLVM11 being imported into the
85# tree, to ensure we don't regress the build.  LLVM11 and GCC10 will switch the
86# default over to -fno-common, making this redundant.
87CFLAGS+=	-fno-common
88
89# XXX LOCORE means "don't declare C stuff" not "for locore.s".
90ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${CFLAGS} ${ASM_CFLAGS.${.IMPSRC:T}}
91
92COMPAT_FREEBSD32_ENABLED!= grep COMPAT_FREEBSD32 opt_global.h || true ; echo
93
94KASAN_ENABLED!=	grep KASAN opt_global.h || true ; echo
95.if !empty(KASAN_ENABLED)
96SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kasan \
97		-fsanitize=kernel-address \
98		-mllvm -asan-stack=true \
99		-mllvm -asan-instrument-dynamic-allocas=true \
100		-mllvm -asan-globals=true \
101		-mllvm -asan-use-after-scope=true \
102		-mllvm -asan-instrumentation-with-call-threshold=0 \
103		-mllvm -asan-instrument-byval=false
104
105.if ${MACHINE_CPUARCH} == "aarch64"
106# KASAN/ARM64 TODO: -asan-mapping-offset is calculated from:
107#	   (VM_KERNEL_MIN_ADDRESS >> KASAN_SHADOW_SCALE_SHIFT) + $offset = KASAN_MIN_ADDRESS
108#
109#	This is different than amd64, where we have a different
110#	KASAN_MIN_ADDRESS, and this offset value should eventually be
111#	upstreamed similar to: https://reviews.llvm.org/D98285
112#
113SAN_CFLAGS+=	-mllvm -asan-mapping-offset=0xdfff208000000000
114.endif
115.endif
116
117KCSAN_ENABLED!=	grep KCSAN opt_global.h || true ; echo
118.if !empty(KCSAN_ENABLED)
119SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kcsan \
120		-fsanitize=thread
121.endif
122
123KMSAN_ENABLED!= grep KMSAN opt_global.h || true ; echo
124.if !empty(KMSAN_ENABLED)
125# Disable -fno-sanitize-memory-param-retval until interceptors have been
126# updated to work properly with it.
127SAN_CFLAGS+=	-DSAN_NEEDS_INTERCEPTORS -DSAN_INTERCEPTOR_PREFIX=kmsan \
128		-fsanitize=kernel-memory
129.if ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 160000
130SAN_CFLAGS+=	-fno-sanitize-memory-param-retval
131.endif
132.endif
133
134KUBSAN_ENABLED!=	grep KUBSAN opt_global.h || true ; echo
135.if !empty(KUBSAN_ENABLED)
136SAN_CFLAGS+=	-fsanitize=undefined
137.endif
138
139COVERAGE_ENABLED!=	grep COVERAGE opt_global.h || true ; echo
140.if !empty(COVERAGE_ENABLED)
141.if ${COMPILER_TYPE} == "clang" || \
142    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
143SAN_CFLAGS+=	-fsanitize-coverage=trace-pc,trace-cmp
144.else
145SAN_CFLAGS+=	-fsanitize-coverage=trace-pc
146.endif
147.endif
148
149CFLAGS+=	${SAN_CFLAGS}
150
151GCOV_ENABLED!=	grep GCOV opt_global.h || true ; echo
152.if !empty(GCOV_ENABLED)
153.if ${COMPILER_TYPE} == "gcc"
154GCOV_CFLAGS+=	 -fprofile-arcs -ftest-coverage
155.endif
156.endif
157
158CFLAGS+=	${GCOV_CFLAGS}
159
160# Put configuration-specific C flags last so that they can override
161# the others.
162CFLAGS+=	${CONF_CFLAGS}
163
164.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
165LDFLAGS+=	--build-id=sha1
166.endif
167
168.if (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
169    ${MACHINE_CPUARCH} == "i386" || ${MACHINE} == "powerpc") && \
170    defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == "" && \
171    !make(install)
172.error amd64/arm64/i386/ppc* kernel requires linker ifunc support
173.endif
174.if ${MACHINE_CPUARCH} == "amd64"
175LDFLAGS+=	-z max-page-size=2097152
176.if ${LINKER_TYPE} != "lld"
177LDFLAGS+=	-z common-page-size=4096
178.else
179.if defined(LINKER_FEATURES) && !${LINKER_FEATURES:Mifunc-noplt}
180.warning "Linker ${LD} does not support -z ifunc-noplt -> ifunc calls are unoptimized."
181.else
182LDFLAGS+=	-z notext -z ifunc-noplt
183.endif
184.endif
185.endif  # ${MACHINE_CPUARCH} == "amd64"
186
187.if ${MACHINE_CPUARCH} == "riscv"
188# Hack: Work around undefined weak symbols being out of range when linking with
189# LLD (address is a PC-relative calculation, and BFD works around this by
190# rewriting the instructions to generate an absolute address of 0); -fPIE
191# avoids this since it uses the GOT for all extern symbols, which is overly
192# inefficient for us. Drop once undefined weak symbols work with medany.
193.if ${LINKER_TYPE} == "lld"
194CFLAGS+=	-fPIE
195.endif
196.endif
197
198NORMAL_C= ${CC} -c ${CFLAGS} ${WERROR} ${.IMPSRC}
199NORMAL_S= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} ${.IMPSRC}
200NORMAL_C_NOWERROR= ${CC} -c ${CFLAGS} ${.IMPSRC}
201
202NORMAL_M= ${AWK} -f $S/tools/makeobjops.awk ${.IMPSRC} -c ; \
203	  ${CC} -c ${CFLAGS} ${WERROR} ${.PREFIX}.c
204
205NORMAL_FW= uudecode -o ${.TARGET} ${.ALLSRC}
206NORMAL_FWO= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} -o ${.TARGET} \
207	$S/kern/firmw.S -DFIRMW_FILE=\""${.ALLSRC:M*.fw}"\" \
208	-DFIRMW_SYMBOL="${.ALLSRC:M*.fw:C/[-.\/]/_/g}"
209
210# for ZSTD in the kernel (include zstd/lib/freebsd before other CFLAGS)
211ZSTD_C= ${CC} -c -DZSTD_HEAPMODE=1 -I$S/contrib/zstd/lib/freebsd ${CFLAGS} \
212	-I$S/contrib/zstd/lib -I$S/contrib/zstd/lib/common ${WERROR} \
213	-Wno-missing-prototypes -U__BMI__ -DZSTD_NO_INTRINSICS ${.IMPSRC}
214# https://github.com/facebook/zstd/commit/812e8f2a [zstd 1.4.1]
215# "Note that [GCC] autovectorization still does not do a good job on the
216# optimized version, so it's turned off via attribute and flag.  I found
217# that neither attribute nor command-line flag were entirely successful in
218# turning off vectorization, which is why there were both."
219.if ${COMPILER_TYPE} == "gcc"
220ZSTD_DECOMPRESS_BLOCK_FLAGS= -fno-tree-vectorize
221.endif
222
223ZINCDIR=$S/contrib/openzfs/include
224# Common for dtrace / zfs
225CDDL_CFLAGS=	\
226	-DFREEBSD_NAMECACHE \
227	-D_SYS_VMEM_H_ \
228	-D__KERNEL \
229	-D__KERNEL__ \
230	-nostdinc \
231	-include $S/modules/zfs/static_ccompile.h \
232	-I${ZINCDIR} \
233	-I${ZINCDIR}/os/freebsd \
234	-I${ZINCDIR}/os/freebsd/spl \
235	-I${ZINCDIR}/os/freebsd/zfs  \
236	-I$S/modules/zfs \
237	-I$S/contrib/openzfs/module/zstd/include \
238	${CFLAGS} \
239	-Wno-cast-qual \
240	-Wno-duplicate-decl-specifier \
241	-Wno-missing-braces \
242	-Wno-missing-prototypes \
243	-Wno-parentheses \
244	-Wno-pointer-arith \
245	-Wno-strict-prototypes \
246	-Wno-switch \
247	-Wno-undef \
248	-Wno-uninitialized \
249	-Wno-unknown-pragmas \
250	-Wno-unused \
251	-include ${ZINCDIR}/os/freebsd/spl/sys/ccompile.h \
252	-I$S/cddl/contrib/opensolaris/uts/common \
253	-I$S -I$S/cddl/compat/opensolaris
254CDDL_C=		${CC} -c ${CDDL_CFLAGS} ${WERROR} ${.IMPSRC}
255
256# Special flags for managing the compat compiles for ZFS
257ZFS_CFLAGS+=	-I$S/contrib/openzfs/module/icp/include \
258	${CDDL_CFLAGS} -DBUILDING_ZFS -DHAVE_UIO_ZEROCOPY \
259	-DWITH_NETDUMP -D__KERNEL__ -D_SYS_CONDVAR_H_ -DSMP
260
261.if ${MACHINE_ARCH} == "amd64"
262ZFS_CFLAGS+= -D__x86_64 -DHAVE_SSE2 -DHAVE_SSSE3 -DHAVE_SSE4_1 -DHAVE_SSE4_2 \
263	-DHAVE_AVX -DHAVE_AVX2 -DHAVE_AVX512F -DHAVE_AVX512VL -DHAVE_AVX512BW
264.endif
265
266.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "powerpc" || \
267	${MACHINE_ARCH} == "powerpcspe" || ${MACHINE_ARCH} == "arm"
268ZFS_CFLAGS+= -DBITS_PER_LONG=32
269.else
270ZFS_CFLAGS+= -DBITS_PER_LONG=64
271.endif
272
273
274ZFS_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${ZFS_CFLAGS}
275ZFS_C=		${CC} -c ${ZFS_CFLAGS} ${WERROR} ${.IMPSRC}
276ZFS_RPC_C=	${CC} -c ${ZFS_CFLAGS} -DHAVE_RPC_TYPES ${WERROR} ${.IMPSRC}
277ZFS_S=		${CC} -c ${ZFS_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
278
279# ATH driver
280ATH_CFLAGS=	-I${SRCTOP}/sys/dev/ath ${NO_WUNUSED_BUT_SET_VARIABLE}
281ATH_C=		${CC} -c ${CFLAGS} ${WERROR} ${ATH_CFLAGS} ${.IMPSRC}
282
283# Special flags for managing the compat compiles for DTrace
284DTRACE_CFLAGS=	-DBUILDING_DTRACE ${CDDL_CFLAGS} -I$S/cddl/dev/dtrace -I$S/cddl/dev/dtrace/${MACHINE_CPUARCH}
285.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
286DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/intel -I$S/cddl/dev/dtrace/x86
287.endif
288DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/util -I$S -DDIS_MEM -DSMP -I$S/cddl/compat/opensolaris
289DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/common
290DTRACE_ASM_CFLAGS=	-x assembler-with-cpp -DLOCORE ${DTRACE_CFLAGS}
291DTRACE_C=	${CC} -c ${DTRACE_CFLAGS}	${WERROR} ${.IMPSRC}
292DTRACE_S=	${CC} -c ${DTRACE_ASM_CFLAGS}	${WERROR} ${.IMPSRC}
293
294# zlib code supports systems that are quite old, but will fix this issue once C2x gets radified.
295# see https://github.com/madler/zlib/issues/633 for details
296ZLIB_CFLAGS=	-Wno-cast-qual ${NO_WDEPRECATED_NON_PROTOTYPE} ${NO_WSTRICT_PROTOTYPES}
297ZLIB_C=		${CC} -c ${CFLAGS} ${WERROR} ${ZLIB_CFLAGS} ${.IMPSRC}
298
299# Special flags for managing the compat compiles for DTrace/FBT
300FBT_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
301.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
302FBT_CFLAGS+=	-I$S/cddl/dev/fbt/x86
303.endif
304FBT_C=		${CC} -c ${FBT_CFLAGS}		${WERROR} ${.IMPSRC}
305
306.if ${MK_CTF} != "no"
307NORMAL_CTFCONVERT=	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
308.elif ${MAKE_VERSION} >= 5201111300
309NORMAL_CTFCONVERT=
310.else
311NORMAL_CTFCONVERT=	@:
312.endif
313
314# Linux Kernel Programming Interface C-flags
315LINUXKPI_INCLUDES=	-I$S/compat/linuxkpi/common/include \
316			-I$S/compat/linuxkpi/dummy/include
317LINUXKPI_C=		${NORMAL_C} ${LINUXKPI_INCLUDES}
318
319# Infiniband C flags.  Correct include paths and omit errors that linux
320# does not honor.
321OFEDINCLUDES=	-I$S/ofed/include -I$S/ofed/include/uapi ${LINUXKPI_INCLUDES}
322OFEDNOERR=	-Wno-cast-qual -Wno-pointer-arith
323OFEDCFLAGS=	${CFLAGS:N-I*} -DCONFIG_INFINIBAND_USER_MEM \
324		${OFEDINCLUDES} ${CFLAGS:M-I*} ${OFEDNOERR}
325OFED_C_NOIMP=	${CC} -c -o ${.TARGET} ${OFEDCFLAGS} ${WERROR}
326OFED_C=		${OFED_C_NOIMP} ${.IMPSRC}
327
328# mlxfw C flags.
329MLXFW_C=	${OFED_C_NOIMP} \
330		-I${SRCTOP}/sys/contrib/xz-embedded/freebsd \
331		-I${SRCTOP}/sys/contrib/xz-embedded/linux/lib/xz \
332		${.IMPSRC}
333
334GEN_CFILES= $S/$M/$M/genassym.c ${MFILES:T:S/.m$/.c/}
335SYSTEM_CFILES= config.c env.c hints.c vnode_if.c
336SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
337SYSTEM_OBJS= locore.o ${MDOBJS} ${OBJS}
338SYSTEM_OBJS+= ${SYSTEM_CFILES:.c=.o}
339SYSTEM_OBJS+= force-dynamic-hack.pico
340
341KEYMAP=kbdcontrol -P ${SRCTOP}/share/vt/keymaps -P ${SRCTOP}/share/syscons/keymaps
342KEYMAP_FIX=sed -e 's/^static keymap_t.* = /static keymap_t key_map = /' -e 's/^static accentmap_t.* = /static accentmap_t accent_map = /'
343
344MD_ROOT_SIZE_CONFIGURED!=	grep MD_ROOT_SIZE opt_md.h || true ; echo
345.if ${MFS_IMAGE:Uno} != "no"
346.if empty(MD_ROOT_SIZE_CONFIGURED)
347SYSTEM_OBJS+= embedfs_${MFS_IMAGE:T:R}.o
348.endif
349.endif
350SYSTEM_LD_BASECMD= \
351	${LD} -m ${LD_EMULATION} -Bdynamic -T ${LDSCRIPT} ${_LDFLAGS} \
352	--no-warn-mismatch --warn-common --export-dynamic \
353	--dynamic-linker /red/herring -X
354SYSTEM_LD= @${SYSTEM_LD_BASECMD} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
355SYSTEM_LD_TAIL= @${SIZE} ${.TARGET} ; chmod 755 ${.TARGET}
356SYSTEM_DEP+= ${LDSCRIPT}
357
358# Calculate path for .m files early, if needed.
359.if !defined(NO_MODULES) && !defined(__MPATH) && !make(install) && \
360    (empty(.MAKEFLAGS:M-V) || defined(NO_SKIP_MPATH))
361__MPATH!=find ${S:tA}/ -name \*_if.m
362.endif
363
364# MKMODULESENV is set here so that port makefiles can augment
365# them.
366
367MKMODULESENV+=	MAKEOBJDIRPREFIX=${.OBJDIR}/modules KMODDIR=${KODIR}
368MKMODULESENV+=	MACHINE_CPUARCH=${MACHINE_CPUARCH}
369MKMODULESENV+=	MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH}
370MKMODULESENV+=	MODULES_EXTRA="${MODULES_EXTRA}" WITHOUT_MODULES="${WITHOUT_MODULES}"
371MKMODULESENV+=	ARCH_FLAGS="${ARCH_FLAGS}"
372.if (${KERN_IDENT} == LINT)
373MKMODULESENV+=	ALL_MODULES=LINT
374.endif
375.if defined(MODULES_OVERRIDE)
376MKMODULESENV+=	MODULES_OVERRIDE="${MODULES_OVERRIDE}"
377.endif
378.if defined(DEBUG)
379MKMODULESENV+=	DEBUG_FLAGS="${DEBUG}"
380.endif
381.if !defined(NO_MODULES)
382MKMODULESENV+=	__MPATH="${__MPATH}"
383.endif
384
385# Detect kernel config options that force stack frames to be turned on.
386DDB_ENABLED!=	grep DDB opt_ddb.h || true ; echo
387DTRACE_ENABLED!=grep KDTRACE_FRAME opt_kdtrace.h || true ; echo
388HWPMC_ENABLED!=	grep HWPMC opt_hwpmc_hooks.h || true ; echo
389