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