]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/conf/kern.mk
MFC r350739-r350740 (by cem)
[FreeBSD/FreeBSD.git] / sys / conf / kern.mk
1 # $FreeBSD$
2
3 #
4 # Warning flags for compiling the kernel and components of the kernel:
5 #
6 CWARNFLAGS?=    -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes \
7                 -Wmissing-prototypes -Wpointer-arith -Wcast-qual \
8                 -Wundef -Wno-pointer-sign ${FORMAT_EXTENSIONS} \
9                 -Wmissing-include-dirs -fdiagnostics-show-option \
10                 -Wno-unknown-pragmas \
11                 ${CWARNEXTRA}
12 #
13 # The following flags are next up for working on:
14 #       -Wextra
15
16 # Disable a few warnings for clang, since there are several places in the
17 # kernel where fixing them is more trouble than it is worth, or where there is
18 # a false positive.
19 .if ${COMPILER_TYPE} == "clang"
20 NO_WCONSTANT_CONVERSION=        -Wno-error-constant-conversion
21 NO_WSHIFT_COUNT_NEGATIVE=       -Wno-shift-count-negative
22 NO_WSHIFT_COUNT_OVERFLOW=       -Wno-shift-count-overflow
23 NO_WSELF_ASSIGN=                -Wno-self-assign
24 NO_WUNNEEDED_INTERNAL_DECL=     -Wno-error-unneeded-internal-declaration
25 NO_WSOMETIMES_UNINITIALIZED=    -Wno-error-sometimes-uninitialized
26 NO_WCAST_QUAL=                  -Wno-error-cast-qual
27 NO_WTAUTOLOGICAL_POINTER_COMPARE= -Wno-tautological-pointer-compare
28 NO_WARRAY_BOUNDS=               -Wno-error-array-bounds
29 # Several other warnings which might be useful in some cases, but not severe
30 # enough to error out the whole kernel build.  Display them anyway, so there is
31 # some incentive to fix them eventually.
32 CWARNEXTRA?=    -Wno-error-tautological-compare -Wno-error-empty-body \
33                 -Wno-error-parentheses-equality -Wno-error-unused-function \
34                 -Wno-error-pointer-sign
35 .if ${COMPILER_VERSION} >= 30700
36 CWARNEXTRA+=    -Wno-error-shift-negative-value
37 .endif
38 .if ${COMPILER_VERSION} >= 40000
39 CWARNEXTRA+=    -Wno-address-of-packed-member
40 .endif
41 .if ${COMPILER_VERSION} >= 100000
42 NO_WMISLEADING_INDENTATION=     -Wno-misleading-indentation
43 .endif
44
45 CLANG_NO_IAS= -no-integrated-as
46 .if ${COMPILER_VERSION} < 30500
47 # XXX: clang < 3.5 integrated-as doesn't grok .codeNN directives
48 CLANG_NO_IAS34= -no-integrated-as
49 .endif
50 .endif
51
52 .if ${COMPILER_TYPE} == "gcc"
53 .if ${COMPILER_VERSION} >= 40800
54 # Catch-all for all the things that are in our tree, but for which we're
55 # not yet ready for this compiler.
56 NO_WUNUSED_BUT_SET_VARIABLE = -Wno-unused-but-set-variable
57 CWARNEXTRA?=    -Wno-error=address                              \
58                 -Wno-error=aggressive-loop-optimizations        \
59                 -Wno-error=array-bounds                         \
60                 -Wno-error=attributes                           \
61                 -Wno-error=cast-qual                            \
62                 -Wno-error=enum-compare                         \
63                 -Wno-error=inline                               \
64                 -Wno-error=maybe-uninitialized                  \
65                 -Wno-error=overflow                             \
66                 -Wno-error=sequence-point                       \
67                 -Wno-error=unused-but-set-variable
68 .if ${COMPILER_VERSION} >= 60100
69 CWARNEXTRA+=    -Wno-error=misleading-indentation               \
70                 -Wno-error=nonnull-compare                      \
71                 -Wno-error=shift-overflow                       \
72                 -Wno-error=tautological-compare
73 .endif
74 .if ${COMPILER_VERSION} >= 70200
75 CWARNEXTRA+=    -Wno-error=memset-elt-size
76 .endif
77 .if ${COMPILER_VERSION} >= 80000
78 CWARNEXTRA+=    -Wno-error=packed-not-aligned
79 .endif
80 .else
81 # For gcc 4.2, eliminate the too-often-wrong warnings about uninitialized vars.
82 CWARNEXTRA?=    -Wno-uninitialized
83 # GCC 4.2 doesn't have -Wno-error=cast-qual, so just disable the warning for
84 # the few files that are already known to generate cast-qual warnings.
85 NO_WCAST_QUAL= -Wno-cast-qual
86 .endif
87 .endif
88
89 # This warning is utter nonsense
90 CWARNFLAGS+=    -Wno-format-zero-length
91
92 # External compilers may not support our format extensions.  Allow them
93 # to be disabled.  WARNING: format checking is disabled in this case.
94 .if ${MK_FORMAT_EXTENSIONS} == "no"
95 FORMAT_EXTENSIONS=      -Wno-format
96 .elif ${COMPILER_TYPE} == "clang" && ${COMPILER_VERSION} >= 30600
97 FORMAT_EXTENSIONS=      -D__printf__=__freebsd_kprintf__
98 .else
99 FORMAT_EXTENSIONS=      -fformat-extensions
100 .endif
101
102 #
103 # On i386, do not align the stack to 16-byte boundaries.  Otherwise GCC 2.95
104 # and above adds code to the entry and exit point of every function to align the
105 # stack to 16-byte boundaries -- thus wasting approximately 12 bytes of stack
106 # per function call.  While the 16-byte alignment may benefit micro benchmarks,
107 # it is probably an overall loss as it makes the code bigger (less efficient
108 # use of code cache tag lines) and uses more stack (less efficient use of data
109 # cache tag lines).  Explicitly prohibit the use of FPU, SSE and other SIMD
110 # operations inside the kernel itself.  These operations are exclusively
111 # reserved for user applications.
112 #
113 # gcc:
114 # Setting -mno-mmx implies -mno-3dnow
115 # Setting -mno-sse implies -mno-sse2, -mno-sse3 and -mno-ssse3
116 #
117 # clang:
118 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
119 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
120 #
121 .if ${MACHINE_CPUARCH} == "i386"
122 CFLAGS.gcc+=    -mno-align-long-strings -mpreferred-stack-boundary=2
123 CFLAGS.clang+=  -mno-aes -mno-avx
124 CFLAGS+=        -mno-mmx -mno-sse -msoft-float
125 INLINE_LIMIT?=  8000
126 .endif
127
128 .if ${MACHINE_CPUARCH} == "arm"
129 INLINE_LIMIT?=  8000
130 .endif
131
132 .if ${MACHINE_CPUARCH} == "aarch64"
133 # We generally don't want fpu instructions in the kernel.
134 CFLAGS += -mgeneral-regs-only
135 # Reserve x18 for pcpu data
136 CFLAGS += -ffixed-x18
137 INLINE_LIMIT?=  8000
138 .endif
139
140 #
141 # For RISC-V we specify the soft-float ABI (lp64) to avoid the use of floating
142 # point registers within the kernel. We also specify the "medium" code model,
143 # which generates code suitable for a 2GiB addressing range located at any
144 # offset, allowing modules to be located anywhere in the 64-bit address space.
145 # Note that clang and GCC refer to this code model as "medium" and "medany"
146 # respectively.
147 #
148 .if ${MACHINE_CPUARCH} == "riscv"
149 CFLAGS+=        -march=rv64imafdc -mabi=lp64
150 CFLAGS.clang+=  -mcmodel=medium
151 CFLAGS.gcc+=    -mcmodel=medany
152 INLINE_LIMIT?=  8000
153
154 .if ${LINKER_FEATURES:Mriscv-relaxations} == ""
155 CFLAGS+=        -mno-relax
156 .endif
157 .endif
158
159 #
160 # For sparc64 we want the medany code model so modules may be located
161 # anywhere in the 64-bit address space.  We also tell GCC to use floating
162 # point emulation.  This avoids using floating point registers for integer
163 # operations which it has a tendency to do.
164 #
165 .if ${MACHINE_CPUARCH} == "sparc64"
166 CFLAGS.clang+=  -mcmodel=large -fno-dwarf2-cfi-asm
167 CFLAGS.gcc+=    -mcmodel=medany -msoft-float
168 INLINE_LIMIT?=  15000
169 .endif
170
171 #
172 # For AMD64, we explicitly prohibit the use of FPU, SSE and other SIMD
173 # operations inside the kernel itself.  These operations are exclusively
174 # reserved for user applications.
175 #
176 # gcc:
177 # Setting -mno-mmx implies -mno-3dnow
178 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3 and -mfpmath=387
179 #
180 # clang:
181 # Setting -mno-mmx implies -mno-3dnow and -mno-3dnowa
182 # Setting -mno-sse implies -mno-sse2, -mno-sse3, -mno-ssse3, -mno-sse41 and -mno-sse42
183 # (-mfpmath= is not supported)
184 #
185 .if ${MACHINE_CPUARCH} == "amd64"
186 CFLAGS.clang+=  -mno-aes -mno-avx
187 CFLAGS+=        -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float \
188                 -fno-asynchronous-unwind-tables
189 INLINE_LIMIT?=  8000
190 .endif
191
192 #
193 # For PowerPC we tell gcc to use floating point emulation.  This avoids using
194 # floating point registers for integer operations which it has a tendency to do.
195 # Also explicitly disable Altivec instructions inside the kernel.
196 #
197 .if ${MACHINE_CPUARCH} == "powerpc"
198 CFLAGS+=        -mno-altivec -msoft-float
199 INLINE_LIMIT?=  15000
200 .endif
201
202 .if ${MACHINE_ARCH} == "powerpcspe"
203 CFLAGS.gcc+=    -mno-spe
204 .endif
205
206 #
207 # Use dot symbols (or, better, the V2 ELF ABI) on powerpc64 to make
208 # DDB happy. ELFv2, if available, has some other efficiency benefits.
209 #
210 .if ${MACHINE_ARCH} == "powerpc64"
211 .if ${COMPILER_VERSION} >= 40900
212 CFLAGS.gcc+=    -mabi=elfv2
213 .else
214 CFLAGS.gcc+=    -mcall-aixdesc
215 .endif
216 CFLAGS.clang+=  -mabi=elfv2
217 .endif
218
219 #
220 # For MIPS we also tell gcc to use floating point emulation
221 #
222 .if ${MACHINE_CPUARCH} == "mips"
223 CFLAGS+=        -msoft-float
224 INLINE_LIMIT?=  8000
225 .endif
226
227 #
228 # GCC 3.0 and above like to do certain optimizations based on the
229 # assumption that the program is linked against libc.  Stop this.
230 #
231 CFLAGS+=        -ffreestanding
232
233 #
234 # The C standard leaves signed integer overflow behavior undefined.
235 # gcc and clang opimizers take advantage of this.  The kernel makes
236 # use of signed integer wraparound mechanics so we need the compiler
237 # to treat it as a wraparound and not take shortcuts.
238 #
239 CFLAGS+=        -fwrapv
240
241 #
242 # GCC SSP support
243 #
244 .if ${MK_SSP} != "no" && \
245     ${MACHINE_CPUARCH} != "arm" && ${MACHINE_CPUARCH} != "mips"
246 CFLAGS+=        -fstack-protector
247 .endif
248
249 #
250 # Retpoline speculative execution vulnerability mitigation (CVE-2017-5715)
251 #
252 .if defined(COMPILER_FEATURES) && ${COMPILER_FEATURES:Mretpoline} != "" && \
253     ${MK_KERNEL_RETPOLINE} != "no"
254 CFLAGS+=        -mretpoline
255 .endif
256
257 #
258 # Add -gdwarf-2 when compiling -g. The default starting in clang v3.4
259 # and gcc 4.8 is to generate DWARF version 4. However, our tools don't
260 # cope well with DWARF 4, so force it to genereate DWARF2, which they
261 # understand. Do this unconditionally as it is harmless when not needed,
262 # but critical for these newer versions.
263 #
264 .if ${CFLAGS:M-g} != "" && ${CFLAGS:M-gdwarf*} == ""
265 CFLAGS+=        -gdwarf-2
266 .endif
267
268 CFLAGS+= ${CWARNFLAGS:M*} ${CWARNFLAGS.${.IMPSRC:T}}
269 CFLAGS+= ${CFLAGS.${COMPILER_TYPE}} ${CFLAGS.${.IMPSRC:T}}
270
271 # Tell bmake not to mistake standard targets for things to be searched for
272 # or expect to ever be up-to-date.
273 PHONY_NOTMAIN = afterdepend afterinstall all beforedepend beforeinstall \
274                 beforelinking build build-tools buildfiles buildincludes \
275                 checkdpadd clean cleandepend cleandir cleanobj configure \
276                 depend distclean distribute exe \
277                 html includes install installfiles installincludes \
278                 obj objlink objs objwarn \
279                 realinstall regress \
280                 tags whereobj
281
282 .PHONY: ${PHONY_NOTMAIN}
283 .NOTMAIN: ${PHONY_NOTMAIN}
284
285 CSTD=           c99
286
287 .if ${CSTD} == "k&r"
288 CFLAGS+=        -traditional
289 .elif ${CSTD} == "c89" || ${CSTD} == "c90"
290 CFLAGS+=        -std=iso9899:1990
291 .elif ${CSTD} == "c94" || ${CSTD} == "c95"
292 CFLAGS+=        -std=iso9899:199409
293 .elif ${CSTD} == "c99"
294 CFLAGS+=        -std=iso9899:1999
295 .else # CSTD
296 CFLAGS+=        -std=${CSTD}
297 .endif # CSTD
298
299 # Set target-specific linker emulation name.
300 LD_EMULATION_aarch64=aarch64elf
301 LD_EMULATION_amd64=elf_x86_64_fbsd
302 LD_EMULATION_arm=armelf_fbsd
303 LD_EMULATION_armv6=armelf_fbsd
304 LD_EMULATION_armv7=armelf_fbsd
305 LD_EMULATION_i386=elf_i386_fbsd
306 LD_EMULATION_mips= elf32btsmip_fbsd
307 LD_EMULATION_mipshf= elf32btsmip_fbsd
308 LD_EMULATION_mips64= elf64btsmip_fbsd
309 LD_EMULATION_mips64hf= elf64btsmip_fbsd
310 LD_EMULATION_mipsel= elf32ltsmip_fbsd
311 LD_EMULATION_mipselhf= elf32ltsmip_fbsd
312 LD_EMULATION_mips64el= elf64ltsmip_fbsd
313 LD_EMULATION_mips64elhf= elf64ltsmip_fbsd
314 LD_EMULATION_mipsn32= elf32btsmipn32_fbsd
315 LD_EMULATION_mipsn32el= elf32btsmipn32_fbsd   # I don't think this is a thing that works
316 LD_EMULATION_powerpc= elf32ppc_fbsd
317 LD_EMULATION_powerpcspe= elf32ppc_fbsd
318 LD_EMULATION_powerpc64= elf64ppc_fbsd
319 LD_EMULATION_riscv64= elf64lriscv
320 LD_EMULATION_sparc64= elf64_sparc_fbsd
321 LD_EMULATION=${LD_EMULATION_${MACHINE_ARCH}}