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