xref: /freebsd/sys/conf/kern.pre.mk (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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
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
76.if ${MACHINE_CPUARCH} == "mips"
77CFLAGS_ARCH_PARAMS?=--param max-inline-insns-single=1000 -DMACHINE_ARCH='"${MACHINE_ARCH}"'
78.endif
79CFLAGS.gcc+= -fms-extensions -finline-limit=${INLINE_LIMIT}
80CFLAGS.gcc+= --param inline-unit-growth=${CFLAGS_PARAM_INLINE_UNIT_GROWTH}
81CFLAGS.gcc+= --param large-function-growth=${CFLAGS_PARAM_LARGE_FUNCTION_GROWTH}
82CFLAGS.gcc+= -fms-extensions
83.if defined(CFLAGS_ARCH_PARAMS)
84CFLAGS.gcc+=${CFLAGS_ARCH_PARAMS}
85.endif
86WERROR?=	-Werror
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.
90CFLAGS+=	-fno-common
91
92# XXX LOCORE means "don't declare C stuff" not "for locore.s".
93ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${CFLAGS} ${ASM_CFLAGS.${.IMPSRC:T}}
94
95KASAN_ENABLED!=	grep KASAN opt_global.h || true ; echo
96.if !empty(KASAN_ENABLED)
97SAN_CFLAGS+=	-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.endif
104
105KCSAN_ENABLED!=	grep KCSAN opt_global.h || true ; echo
106.if !empty(KCSAN_ENABLED)
107SAN_CFLAGS+=	-fsanitize=thread
108.endif
109
110KUBSAN_ENABLED!=	grep KUBSAN opt_global.h || true ; echo
111.if !empty(KUBSAN_ENABLED)
112SAN_CFLAGS+=	-fsanitize=undefined
113.endif
114
115COVERAGE_ENABLED!=	grep COVERAGE opt_global.h || true ; echo
116.if !empty(COVERAGE_ENABLED)
117.if ${COMPILER_TYPE} == "clang" || \
118    (${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} >= 80100)
119SAN_CFLAGS+=	-fsanitize-coverage=trace-pc,trace-cmp
120.else
121SAN_CFLAGS+=	-fsanitize-coverage=trace-pc
122.endif
123.endif
124
125CFLAGS+=	${SAN_CFLAGS}
126
127GCOV_ENABLED!=	grep GCOV opt_global.h || true ; echo
128.if !empty(GCOV_ENABLED)
129.if ${COMPILER_TYPE} == "gcc"
130GCOV_CFLAGS+=	 -fprofile-arcs -ftest-coverage
131.endif
132.endif
133
134CFLAGS+=	${GCOV_CFLAGS}
135
136# Put configuration-specific C flags last so that they can override
137# the others.
138CFLAGS+=	${CONF_CFLAGS}
139
140.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
141LDFLAGS+=	--build-id=sha1
142.endif
143
144.if (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
145    ${MACHINE_CPUARCH} == "i386" || ${MACHINE} == "powerpc") && \
146    defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == "" && \
147    !make(install)
148.error amd64/arm64/i386/ppc* kernel requires linker ifunc support
149.endif
150.if ${MACHINE_CPUARCH} == "amd64"
151LDFLAGS+=	-z max-page-size=2097152
152.if ${LINKER_TYPE} != "lld"
153LDFLAGS+=	-z common-page-size=4096
154.else
155.if defined(LINKER_FEATURES) && !${LINKER_FEATURES:Mifunc-noplt}
156.warning "Linker ${LD} does not support -z ifunc-noplt -> ifunc calls are unoptimized."
157.else
158LDFLAGS+=	-z notext -z ifunc-noplt
159.endif
160.endif
161.endif  # ${MACHINE_CPUARCH} == "amd64"
162
163.if ${MACHINE_CPUARCH} == "riscv"
164# Hack: Work around undefined weak symbols being out of range when linking with
165# LLD (address is a PC-relative calculation, and BFD works around this by
166# rewriting the instructions to generate an absolute address of 0); -fPIE
167# avoids this since it uses the GOT for all extern symbols, which is overly
168# inefficient for us. Drop once undefined weak symbols work with medany.
169.if ${LINKER_TYPE} == "lld"
170CFLAGS+=	-fPIE
171.endif
172.endif
173
174NORMAL_C= ${CC} -c ${CFLAGS} ${WERROR} ${.IMPSRC}
175NORMAL_S= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} ${.IMPSRC}
176NORMAL_C_NOWERROR= ${CC} -c ${CFLAGS} ${.IMPSRC}
177
178NORMAL_M= ${AWK} -f $S/tools/makeobjops.awk ${.IMPSRC} -c ; \
179	  ${CC} -c ${CFLAGS} ${WERROR} ${.PREFIX}.c
180
181NORMAL_FW= uudecode -o ${.TARGET} ${.ALLSRC}
182NORMAL_FWO= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} -o ${.TARGET} \
183	$S/kern/firmw.S -DFIRMW_FILE="${.ALLSRC:M*.fw}" \
184	-DFIRMW_SYMBOL="${.ALLSRC:M*.fw:C/[-.\/]/_/g}"
185
186# for ZSTD in the kernel (include zstd/lib/freebsd before other CFLAGS)
187ZSTD_C= ${CC} -c -DZSTD_HEAPMODE=1 -I$S/contrib/zstd/lib/freebsd ${CFLAGS} \
188	-I$S/contrib/zstd/lib -I$S/contrib/zstd/lib/common ${WERROR} \
189	-Wno-missing-prototypes -U__BMI__ -DZSTD_NO_INTRINSICS ${.IMPSRC}
190# https://github.com/facebook/zstd/commit/812e8f2a [zstd 1.4.1]
191# "Note that [GCC] autovectorization still does not do a good job on the
192# optimized version, so it's turned off via attribute and flag.  I found
193# that neither attribute nor command-line flag were entirely successful in
194# turning off vectorization, which is why there were both."
195.if ${COMPILER_TYPE} == "gcc"
196ZSTD_DECOMPRESS_BLOCK_FLAGS= -fno-tree-vectorize
197.endif
198
199ZINCDIR=$S/contrib/openzfs/include
200# Common for dtrace / zfs
201CDDL_CFLAGS=	\
202	-DFREEBSD_NAMECACHE \
203	-D_SYS_VMEM_H_ \
204	-D__KERNEL \
205	-D__KERNEL__ \
206	-nostdinc \
207	-include $S/modules/zfs/static_ccompile.h \
208	-I${ZINCDIR} \
209	-I${ZINCDIR}/os/freebsd \
210	-I${ZINCDIR}/os/freebsd/spl \
211	-I${ZINCDIR}/os/freebsd/zfs  \
212	-I$S/modules/zfs \
213	-I$S/contrib/openzfs/module/zstd/include \
214	${CFLAGS} \
215	-Wno-cast-qual \
216	-Wno-duplicate-decl-specifier \
217	-Wno-missing-braces \
218	-Wno-missing-prototypes \
219	-Wno-nested-externs \
220	-Wno-parentheses \
221	-Wno-pointer-arith \
222	-Wno-redundant-decls \
223	-Wno-strict-prototypes \
224	-Wno-switch \
225	-Wno-undef \
226	-Wno-uninitialized \
227	-Wno-unknown-pragmas \
228	-Wno-unused \
229	-include ${ZINCDIR}/os/freebsd/spl/sys/ccompile.h \
230	-I$S/cddl/contrib/opensolaris/uts/common \
231	-I$S -I$S/cddl/compat/opensolaris
232CDDL_C=		${CC} -c ${CDDL_CFLAGS} ${WERROR} ${.IMPSRC}
233
234# Special flags for managing the compat compiles for ZFS
235ZFS_CFLAGS+=	${CDDL_CFLAGS} -DBUILDING_ZFS -DHAVE_UIO_ZEROCOPY \
236	-DWITH_NETDUMP -D__KERNEL__ -D_SYS_CONDVAR_H_ -DSMP \
237	-DIN_FREEBSD_BASE -DHAVE_KSID
238
239.if ${MACHINE_ARCH} == "amd64"
240ZFS_CFLAGS+= -DHAVE_AVX2 -DHAVE_AVX -D__x86_64 -DHAVE_SSE2 -DHAVE_AVX512F \
241	-DHAVE_SSSE3 -DHAVE_AVX512BW
242.endif
243
244.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "powerpc" || \
245	${MACHINE_ARCH} == "powerpcspe" || ${MACHINE_ARCH} == "arm"
246ZFS_CFLAGS+= -DBITS_PER_LONG=32
247.else
248ZFS_CFLAGS+= -DBITS_PER_LONG=64
249.endif
250
251
252ZFS_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${ZFS_CFLAGS}
253ZFS_C=		${CC} -c ${ZFS_CFLAGS} ${WERROR} ${.IMPSRC}
254ZFS_RPC_C=	${CC} -c ${ZFS_CFLAGS} -DHAVE_RPC_TYPES ${WERROR} ${.IMPSRC}
255ZFS_S=		${CC} -c ${ZFS_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
256
257
258
259# Special flags for managing the compat compiles for DTrace
260DTRACE_CFLAGS=	-DBUILDING_DTRACE ${CDDL_CFLAGS} -I$S/cddl/dev/dtrace -I$S/cddl/dev/dtrace/${MACHINE_CPUARCH}
261.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
262DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/intel -I$S/cddl/dev/dtrace/x86
263.endif
264DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/util -I$S -DDIS_MEM -DSMP -I$S/cddl/compat/opensolaris
265DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/common
266DTRACE_ASM_CFLAGS=	-x assembler-with-cpp -DLOCORE ${DTRACE_CFLAGS}
267DTRACE_C=	${CC} -c ${DTRACE_CFLAGS}	${WERROR} ${.IMPSRC}
268DTRACE_S=	${CC} -c ${DTRACE_ASM_CFLAGS}	${WERROR} ${.IMPSRC}
269
270# Special flags for managing the compat compiles for DTrace/FBT
271FBT_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
272.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
273FBT_CFLAGS+=	-I$S/cddl/dev/fbt/x86
274.endif
275FBT_C=		${CC} -c ${FBT_CFLAGS}		${WERROR} ${.IMPSRC}
276
277.if ${MK_CTF} != "no"
278NORMAL_CTFCONVERT=	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
279.elif ${MAKE_VERSION} >= 5201111300
280NORMAL_CTFCONVERT=
281.else
282NORMAL_CTFCONVERT=	@:
283.endif
284
285# Linux Kernel Programming Interface C-flags
286LINUXKPI_INCLUDES=	-I$S/compat/linuxkpi/common/include
287LINUXKPI_C=		${NORMAL_C} ${LINUXKPI_INCLUDES}
288
289# Infiniband C flags.  Correct include paths and omit errors that linux
290# does not honor.
291OFEDINCLUDES=	-I$S/ofed/include -I$S/ofed/include/uapi ${LINUXKPI_INCLUDES}
292OFEDNOERR=	-Wno-cast-qual -Wno-pointer-arith
293OFEDCFLAGS=	${CFLAGS:N-I*} -DCONFIG_INFINIBAND_USER_MEM \
294		${OFEDINCLUDES} ${CFLAGS:M-I*} ${OFEDNOERR}
295OFED_C_NOIMP=	${CC} -c -o ${.TARGET} ${OFEDCFLAGS} ${WERROR}
296OFED_C=		${OFED_C_NOIMP} ${.IMPSRC}
297
298# mlxfw C flags.
299MLXFW_C=	${OFED_C_NOIMP} \
300		-I${SRCTOP}/sys/contrib/xz-embedded/freebsd \
301		-I${SRCTOP}/sys/contrib/xz-embedded/linux/lib/xz \
302		${.IMPSRC}
303
304GEN_CFILES= $S/$M/$M/genassym.c ${MFILES:T:S/.m$/.c/}
305SYSTEM_CFILES= config.c env.c hints.c vnode_if.c
306SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
307SYSTEM_OBJS= locore.o ${MDOBJS} ${OBJS}
308SYSTEM_OBJS+= ${SYSTEM_CFILES:.c=.o}
309SYSTEM_OBJS+= hack.pico
310
311KEYMAP=kbdcontrol -P ${SRCTOP}/share/vt/keymaps -P ${SRCTOP}/share/syscons/keymaps
312KEYMAP_FIX=sed -e 's/^static keymap_t.* = /static keymap_t key_map = /' -e 's/^static accentmap_t.* = /static accentmap_t accent_map = /'
313
314MD_ROOT_SIZE_CONFIGURED!=	grep MD_ROOT_SIZE opt_md.h || true ; echo
315.if ${MFS_IMAGE:Uno} != "no"
316.if empty(MD_ROOT_SIZE_CONFIGURED)
317SYSTEM_OBJS+= embedfs_${MFS_IMAGE:T:R}.o
318.endif
319.endif
320SYSTEM_LD_BASECMD= \
321	${LD} -m ${LD_EMULATION} -Bdynamic -T ${LDSCRIPT} ${_LDFLAGS} \
322	--no-warn-mismatch --warn-common --export-dynamic \
323	--dynamic-linker /red/herring -X
324SYSTEM_LD= @${SYSTEM_LD_BASECMD} -o ${.TARGET} ${SYSTEM_OBJS} vers.o
325SYSTEM_LD_TAIL= @${OBJCOPY} --strip-symbol gcc2_compiled. ${.TARGET} ; \
326	${SIZE} ${.TARGET} ; chmod 755 ${.TARGET}
327SYSTEM_DEP+= ${LDSCRIPT}
328
329# Calculate path for .m files early, if needed.
330.if !defined(NO_MODULES) && !defined(__MPATH) && !make(install) && \
331    (empty(.MAKEFLAGS:M-V) || defined(NO_SKIP_MPATH))
332__MPATH!=find ${S:tA}/ -name \*_if.m
333.endif
334
335# MKMODULESENV is set here so that port makefiles can augment
336# them.
337
338MKMODULESENV+=	MAKEOBJDIRPREFIX=${.OBJDIR}/modules KMODDIR=${KODIR}
339MKMODULESENV+=	MACHINE_CPUARCH=${MACHINE_CPUARCH}
340MKMODULESENV+=	MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH}
341MKMODULESENV+=	MODULES_EXTRA="${MODULES_EXTRA}" WITHOUT_MODULES="${WITHOUT_MODULES}"
342MKMODULESENV+=	ARCH_FLAGS="${ARCH_FLAGS}"
343.if (${KERN_IDENT} == LINT)
344MKMODULESENV+=	ALL_MODULES=LINT
345.endif
346.if defined(MODULES_OVERRIDE)
347MKMODULESENV+=	MODULES_OVERRIDE="${MODULES_OVERRIDE}"
348.endif
349.if defined(DEBUG)
350MKMODULESENV+=	DEBUG_FLAGS="${DEBUG}"
351.endif
352.if !defined(NO_MODULES)
353MKMODULESENV+=	__MPATH="${__MPATH}"
354.endif
355
356# Detect kernel config options that force stack frames to be turned on.
357DDB_ENABLED!=	grep DDB opt_ddb.h || true ; echo
358DTR_ENABLED!=	grep KDTRACE_FRAME opt_kdtrace.h || true ; echo
359HWPMC_ENABLED!=	grep HWPMC opt_hwpmc_hooks.h || true ; echo
360