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