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