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
96.if ${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} < 50000
97WERROR?=	-Wno-error
98.else
99WERROR?=	-Werror
100.endif
101# The following should be removed no earlier than LLVM11 being imported into the
102# tree, to ensure we don't regress the build.  LLVM11 and GCC10 will switch the
103# default over to -fno-common, making this redundant.
104CFLAGS+=	-fno-common
105
106# XXX LOCORE means "don't declare C stuff" not "for locore.s".
107ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${CFLAGS} ${ASM_CFLAGS.${.IMPSRC:T}} 
108
109.if defined(PROFLEVEL) && ${PROFLEVEL} >= 1
110CFLAGS+=	-DGPROF
111CFLAGS.gcc+=	-falign-functions=16
112.if ${PROFLEVEL} >= 2
113CFLAGS+=	-DGPROF4 -DGUPROF
114PROF=		-pg
115.if ${COMPILER_TYPE} == "gcc"
116PROF+=		-mprofiler-epilogue
117.endif
118.else
119PROF=		-pg
120.endif
121.endif
122DEFINED_PROF=	${PROF}
123
124# Put configuration-specific C flags last (except for ${PROF}) so that they
125# can override the others.
126CFLAGS+=	${CONF_CFLAGS}
127
128.if defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mbuild-id}
129LDFLAGS+=	--build-id=sha1
130.endif
131
132.if (${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
133    ${MACHINE_CPUARCH} == "i386") && \
134    defined(LINKER_FEATURES) && ${LINKER_FEATURES:Mifunc} == "" && \
135    !make(install)
136.error amd64/arm64/i386 kernel requires linker ifunc support
137.endif
138.if ${MACHINE_CPUARCH} == "amd64"
139LDFLAGS+=	-z max-page-size=2097152
140.if ${LINKER_TYPE} != "lld"
141LDFLAGS+=	-z common-page-size=4096
142.else
143LDFLAGS+=	-z notext -z ifunc-noplt
144.endif
145.endif
146
147.if ${MACHINE_CPUARCH} == "riscv"
148# Hack: Work around undefined weak symbols being out of range when linking with
149# LLD (address is a PC-relative calculation, and BFD works around this by
150# rewriting the instructions to generate an absolute address of 0); -fPIE
151# avoids this since it uses the GOT for all extern symbols, which is overly
152# inefficient for us. Drop once undefined weak symbols work with medany.
153.if ${LINKER_TYPE} == "lld"
154CFLAGS+=	-fPIE
155.endif
156.endif
157
158NORMAL_C= ${CC} -c ${CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
159NORMAL_S= ${CC:N${CCACHE_BIN}} -c ${ASM_CFLAGS} ${WERROR} ${.IMPSRC}
160PROFILE_C= ${CC} -c ${CFLAGS} ${WERROR} ${.IMPSRC}
161NORMAL_C_NOWERROR= ${CC} -c ${CFLAGS} ${PROF} ${.IMPSRC}
162
163NORMAL_M= ${AWK} -f $S/tools/makeobjops.awk ${.IMPSRC} -c ; \
164	  ${CC} -c ${CFLAGS} ${WERROR} ${PROF} ${.PREFIX}.c
165
166NORMAL_FW= uudecode -o ${.TARGET} ${.ALLSRC}
167NORMAL_FWO= ${LD} -b binary --no-warn-mismatch -d -warn-common -r \
168	-m ${LD_EMULATION} -o ${.TARGET} ${.ALLSRC:M*.fw}
169
170# for ZSTD in the kernel (include zstd/lib/freebsd before other CFLAGS)
171ZSTD_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}
172
173# Common for dtrace / zfs
174CDDL_CFLAGS=	-DFREEBSD_NAMECACHE -nostdinc -I$S/cddl/compat/opensolaris -I$S/cddl/contrib/opensolaris/uts/common -I$S -I$S/cddl/contrib/opensolaris/common ${CFLAGS} -Wno-unknown-pragmas -Wno-missing-prototypes -Wno-undef -Wno-strict-prototypes -Wno-cast-qual -Wno-parentheses -Wno-redundant-decls -Wno-missing-braces -Wno-uninitialized -Wno-unused -Wno-inline -Wno-switch -Wno-pointer-arith -Wno-unknown-pragmas
175CDDL_CFLAGS+=	-include $S/cddl/compat/opensolaris/sys/debug_compat.h
176CDDL_C=		${CC} -c ${CDDL_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
177
178# Special flags for managing the compat compiles for ZFS
179ZFS_CFLAGS=	-DBUILDING_ZFS -I$S/cddl/contrib/opensolaris/uts/common/fs/zfs
180ZFS_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/common/fs/zfs/lua
181ZFS_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/common/zmod
182ZFS_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/lz4
183ZFS_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/zfs
184ZFS_CFLAGS+=	${CDDL_CFLAGS}
185ZFS_ASM_CFLAGS= -x assembler-with-cpp -DLOCORE ${ZFS_CFLAGS}
186ZFS_C=		${CC} -c ${ZFS_CFLAGS} ${WERROR} ${PROF} ${.IMPSRC}
187ZFS_S=		${CC} -c ${ZFS_ASM_CFLAGS} ${WERROR} ${.IMPSRC}
188
189# Special flags for managing the compat compiles for DTrace
190DTRACE_CFLAGS=	-DBUILDING_DTRACE ${CDDL_CFLAGS} -I$S/cddl/dev/dtrace -I$S/cddl/dev/dtrace/${MACHINE_CPUARCH}
191.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
192DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/uts/intel -I$S/cddl/dev/dtrace/x86
193.endif
194DTRACE_CFLAGS+=	-I$S/cddl/contrib/opensolaris/common/util -I$S -DDIS_MEM -DSMP
195DTRACE_ASM_CFLAGS=	-x assembler-with-cpp -DLOCORE ${DTRACE_CFLAGS}
196DTRACE_C=	${CC} -c ${DTRACE_CFLAGS}	${WERROR} ${PROF} ${.IMPSRC}
197DTRACE_S=	${CC} -c ${DTRACE_ASM_CFLAGS}	${WERROR} ${.IMPSRC}
198
199# Special flags for managing the compat compiles for DTrace/FBT
200FBT_CFLAGS=	-DBUILDING_DTRACE -nostdinc -I$S/cddl/dev/fbt/${MACHINE_CPUARCH} -I$S/cddl/dev/fbt -I$S/cddl/compat/opensolaris -I$S/cddl/contrib/opensolaris/uts/common -I$S ${CDDL_CFLAGS}
201.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
202FBT_CFLAGS+=	-I$S/cddl/dev/fbt/x86
203.endif
204FBT_C=		${CC} -c ${FBT_CFLAGS}		${WERROR} ${PROF} ${.IMPSRC}
205
206.if ${MK_CTF} != "no"
207NORMAL_CTFCONVERT=	${CTFCONVERT} ${CTFFLAGS} ${.TARGET}
208.elif ${MAKE_VERSION} >= 5201111300
209NORMAL_CTFCONVERT=
210.else
211NORMAL_CTFCONVERT=	@:
212.endif
213
214# Linux Kernel Programming Interface C-flags
215LINUXKPI_INCLUDES=	-I$S/compat/linuxkpi/common/include
216LINUXKPI_C=		${NORMAL_C} ${LINUXKPI_INCLUDES}
217
218# Infiniband C flags.  Correct include paths and omit errors that linux
219# does not honor.
220OFEDINCLUDES=	-I$S/ofed/include -I$S/ofed/include/uapi ${LINUXKPI_INCLUDES}
221OFEDNOERR=	-Wno-cast-qual -Wno-pointer-arith
222OFEDCFLAGS=	${CFLAGS:N-I*} -DCONFIG_INFINIBAND_USER_MEM \
223		${OFEDINCLUDES} ${CFLAGS:M-I*} ${OFEDNOERR}
224OFED_C_NOIMP=	${CC} -c -o ${.TARGET} ${OFEDCFLAGS} ${WERROR} ${PROF}
225OFED_C=		${OFED_C_NOIMP} ${.IMPSRC}
226
227# mlxfw C flags.
228MLXFW_C=	${OFED_C_NOIMP} \
229		-I${SRCTOP}/sys/contrib/xz-embedded/freebsd \
230		-I${SRCTOP}/sys/contrib/xz-embedded/linux/lib/xz \
231		${.IMPSRC}
232
233GEN_CFILES= $S/$M/$M/genassym.c ${MFILES:T:S/.m$/.c/}
234SYSTEM_CFILES= config.c env.c hints.c vnode_if.c
235SYSTEM_DEP= Makefile ${SYSTEM_OBJS}
236SYSTEM_OBJS= locore.o ${MDOBJS} ${OBJS}
237SYSTEM_OBJS+= ${SYSTEM_CFILES:.c=.o}
238SYSTEM_OBJS+= hack.pico
239
240MD_ROOT_SIZE_CONFIGURED!=	grep MD_ROOT_SIZE opt_md.h || true ; echo
241.if ${MFS_IMAGE:Uno} != "no"
242.if empty(MD_ROOT_SIZE_CONFIGURED)
243SYSTEM_OBJS+= embedfs_${MFS_IMAGE:T:R}.o
244.endif
245.endif
246SYSTEM_LD= @${LD} -m ${LD_EMULATION} -Bdynamic -T ${LDSCRIPT} ${_LDFLAGS} \
247	--no-warn-mismatch --warn-common --export-dynamic \
248	--dynamic-linker /red/herring \
249	-o ${.TARGET} -X ${SYSTEM_OBJS} vers.o
250SYSTEM_LD_TAIL= @${OBJCOPY} --strip-symbol gcc2_compiled. ${.TARGET} ; \
251	${SIZE} ${.TARGET} ; chmod 755 ${.TARGET}
252SYSTEM_DEP+= ${LDSCRIPT}
253
254# Calculate path for .m files early, if needed.
255.if !defined(NO_MODULES) && !defined(__MPATH) && !make(install) && \
256    (empty(.MAKEFLAGS:M-V) || defined(NO_SKIP_MPATH))
257__MPATH!=find ${S:tA}/ -name \*_if.m
258.endif
259
260# MKMODULESENV is set here so that port makefiles can augment
261# them.
262
263MKMODULESENV+=	MAKEOBJDIRPREFIX=${.OBJDIR}/modules KMODDIR=${KODIR}
264MKMODULESENV+=	MACHINE_CPUARCH=${MACHINE_CPUARCH}
265MKMODULESENV+=	MACHINE=${MACHINE} MACHINE_ARCH=${MACHINE_ARCH}
266MKMODULESENV+=	MODULES_EXTRA="${MODULES_EXTRA}" WITHOUT_MODULES="${WITHOUT_MODULES}"
267MKMODULESENV+=	ARCH_FLAGS="${ARCH_FLAGS}"
268.if (${KERN_IDENT} == LINT)
269MKMODULESENV+=	ALL_MODULES=LINT
270.endif
271.if defined(MODULES_OVERRIDE)
272MKMODULESENV+=	MODULES_OVERRIDE="${MODULES_OVERRIDE}"
273.endif
274.if defined(DEBUG)
275MKMODULESENV+=	DEBUG_FLAGS="${DEBUG}"
276.endif
277.if !defined(NO_MODULES)
278MKMODULESENV+=	__MPATH="${__MPATH}"
279.endif
280
281# Detect kernel config options that force stack frames to be turned on.
282DDB_ENABLED!=	grep DDB opt_ddb.h || true ; echo
283DTR_ENABLED!=	grep KDTRACE_FRAME opt_kdtrace.h || true ; echo
284HWPMC_ENABLED!=	grep HWPMC opt_hwpmc_hooks.h || true ; echo
285