mirror of
https://github.com/torvalds/linux.git
synced 2026-10-22 12:52:02 +02:00
Each kernel make invocation begins with ~30 compiler and linker runs each
of which performs duplicate probe for a number of compiler and linker
options.
This is useless work - the compiler and its version is known, so use these
to determine which options are available, once.
Using the newly introduced cc-option-str, track the result of each probe in
CC_OPT_*, LD_OPT_* variables and have Makefile's simply append the symbol.
Some care has to be taken around negative options, i.e. -Wno-xxx, as gcc
accepts an unknown -Wno- without complaint and only complains later, so
instead probe the positive form for these cases.
Use CC_HAS_* For those probes that gate a Makefile conditional rather than
a simple option, as per the established pattern for this.
This change avoids a subtle issue with ppc32 (big endian) - clang doesn't
accept -fno-stack-clash-protection on this sub-target, so keep this option
in the Makefile.
With gcc and clang, defconfig and allmodconfig, the recorded command
lines are unchanged, a build with nothing to do rebuilds nothing and W=1
continues to function correctly.
Doing this improves all builds, but has a particularly positive impact on
no-op builds (builds where nothing has changed).
Whole build, 128-thread Threadripper 9980X, best of N runs:
before after delta
-------------------------------
x86 defconfig, no-op make, gcc 0.78s 0.60s -0.18s (-23%)
x86 defconfig, no-op make, clang 0.94s 0.66s -0.28s (-30%)
x86 defconfig, touch mm/vma.c, gcc 8.6s 8.5s -0.16s (-2%)
x86 defconfig, touch mm/vma.c, clang 8.0s 7.7s -0.24s (-3%)
x86 defconfig, clean, clang 29.8s 29.4s -0.37s (-1%)
x86 allmodconfig, no-op make, gcc 12.9s 12.7s -0.23s (-2%)
x86 allmodconfig, no-op make, clang 14.2s 13.9s -0.29s (-2%)
x86 allmodconfig, touch mm/vma.c, clang 32.2s 31.9s -0.30s (-1%)
Assisted-by: LLM
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Link: https://patch.msgid.link/20260923-build-speedup-v4-12-73128809a4a4@kernel.org
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
220 lines
7.7 KiB
Makefile
220 lines
7.7 KiB
Makefile
# SPDX-License-Identifier: GPL-2.0
|
|
# ==========================================================================
|
|
# make W=... settings
|
|
#
|
|
# There are four warning groups enabled by W=1, W=2, W=3, and W=e
|
|
# They are independent, and can be combined like W=12 or W=123e.
|
|
# ==========================================================================
|
|
|
|
# Default set of warnings, always enabled
|
|
KBUILD_CFLAGS += -Wall
|
|
KBUILD_CFLAGS += -Wextra
|
|
KBUILD_CFLAGS += -Wundef
|
|
KBUILD_CFLAGS += -Werror=implicit-function-declaration
|
|
KBUILD_CFLAGS += -Werror=implicit-int
|
|
KBUILD_CFLAGS += -Werror=return-type
|
|
KBUILD_CFLAGS += -Werror=strict-prototypes
|
|
KBUILD_CFLAGS += -Wno-format-security
|
|
KBUILD_CFLAGS += -Wno-trigraphs
|
|
KBUILD_CFLAGS += -Wno-frame-address
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_ADDRESS_OF_PACKED_MEMBER)
|
|
KBUILD_CFLAGS += -Wmissing-declarations
|
|
KBUILD_CFLAGS += -Wmissing-prototypes
|
|
|
|
ifneq ($(CONFIG_FRAME_WARN),0)
|
|
KBUILD_CFLAGS += -Wframe-larger-than=$(CONFIG_FRAME_WARN)
|
|
endif
|
|
|
|
KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
# Clang checks for overflow/truncation with '%p', while GCC does not:
|
|
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_OVERFLOW_NON_KPRINTF)
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_TRUNCATION_NON_KPRINTF)
|
|
|
|
# Clang may emit a warning when a const variable, such as the dummy variables
|
|
# in typecheck(), or const member of an aggregate type are not initialized,
|
|
# which can result in unexpected behavior. However, in many audited cases of
|
|
# the "field" variant of the warning, this is intentional because the field is
|
|
# never used within a particular call path, the field is within a union with
|
|
# other non-const members, or the containing object is not const so the field
|
|
# can be modified via memcpy() / memset(). While the variable warning also gets
|
|
# disabled with this same switch, there should not be too much coverage lost
|
|
# because -Wuninitialized will still flag when an uninitialized const variable
|
|
# is used.
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_DEFAULT_CONST_INIT_UNSAFE)
|
|
else
|
|
|
|
# gcc inanely warns about local variables called 'main'
|
|
KBUILD_CFLAGS += -Wno-main
|
|
endif
|
|
|
|
# Too noisy on range checks and in macros handling both signed and unsigned.
|
|
KBUILD_CFLAGS += -Wno-type-limits
|
|
|
|
# These result in bogus false positives
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_DANGLING_POINTER)
|
|
|
|
# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
|
|
# Function array parameters should, however, be usable, but -Wvla will
|
|
# warn for those. Clang has no way yet to distinguish between the VLA
|
|
# types, so depend on GCC for now to keep stack VLAs out of the tree.
|
|
# https://github.com/llvm/llvm-project/issues/57098
|
|
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WVLA_LARGER_THAN)
|
|
|
|
# disable pointer signed / unsigned warnings in gcc 4.0
|
|
KBUILD_CFLAGS += -Wno-pointer-sign
|
|
|
|
# In order to make sure new function cast mismatches are not introduced
|
|
# in the kernel (to avoid tripping CFI checking), the kernel should be
|
|
# globally built with -Wcast-function-type.
|
|
KBUILD_CFLAGS += -Wcast-function-type
|
|
|
|
# Currently, disable -Wstringop-overflow for GCC 11, globally.
|
|
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(if $(CONFIG_CC_HAS_WSTRINGOP_OVERFLOW),-Wno-stringop-overflow)
|
|
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(if $(CONFIG_CC_HAS_WSTRINGOP_OVERFLOW),-Wstringop-overflow)
|
|
|
|
# Currently, disable -Wunterminated-string-initialization as broken
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_UNTERMINATED_STRING_INITIALIZATION)
|
|
|
|
# The allocators already balk at large sizes, so silence the compiler
|
|
# warnings for bounds checks involving those possible values. While
|
|
# -Wno-alloc-size-larger-than would normally be used here, earlier versions
|
|
# of gcc (<9.1) weirdly don't handle the option correctly when _other_
|
|
# warnings are produced (?!). Using -Walloc-size-larger-than=SIZE_MAX
|
|
# doesn't work (as it is documented to), silently resolving to "0" prior to
|
|
# version 9.1 (and producing an error more recently). Numeric values larger
|
|
# than PTRDIFF_MAX also don't work prior to version 9.1, which are silently
|
|
# ignored, continuing to default to PTRDIFF_MAX. So, left with no other
|
|
# choice, we must perform a versioned check to disable this warning.
|
|
# https://lore.kernel.org/lkml/20210824115859.187f272f@canb.auug.org.au
|
|
KBUILD_CFLAGS-$(call gcc-min-version, 90100) += -Wno-alloc-size-larger-than
|
|
KBUILD_CFLAGS += $(KBUILD_CFLAGS-y) $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
|
|
|
|
# Prohibit date/time macros, which would make the build non-deterministic
|
|
KBUILD_CFLAGS += -Werror=date-time
|
|
|
|
# enforce correct pointer usage
|
|
KBUILD_CFLAGS += -Werror=incompatible-pointer-types
|
|
|
|
# Require designated initializers for all marked structures
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WERROR_DESIGNATED_INIT)
|
|
|
|
# Warn if there is an enum types mismatch
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WENUM_CONVERSION)
|
|
|
|
KBUILD_CFLAGS += -Wunused
|
|
|
|
#
|
|
# W=1 - warnings which may be relevant and do not occur too often
|
|
#
|
|
ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wmissing-format-attribute
|
|
KBUILD_CFLAGS += -Wmissing-include-dirs
|
|
KBUILD_CFLAGS += -Wunused-const-variable
|
|
|
|
KBUILD_CPPFLAGS += -Wundef
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
|
|
|
|
else
|
|
|
|
# Some diagnostics enabled by default are noisy.
|
|
# Suppress them by using -Wno... except for W=1.
|
|
KBUILD_CFLAGS += -Wno-unused-but-set-variable
|
|
KBUILD_CFLAGS += -Wno-unused-const-variable
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_PACKED_NOT_ALIGNED)
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_OVERFLOW)
|
|
ifdef CONFIG_CC_IS_GCC
|
|
KBUILD_CFLAGS += -Wno-format-truncation
|
|
endif
|
|
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_STRINGOP_TRUNCATION)
|
|
|
|
KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
KBUILD_CFLAGS += -Wno-pointer-to-enum-cast
|
|
KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
|
|
KBUILD_CFLAGS += -Wno-unaligned-access
|
|
KBUILD_CFLAGS += -Wno-enum-compare-conditional
|
|
endif
|
|
|
|
endif
|
|
|
|
#
|
|
# W=2 - warnings which occur quite often but may still be relevant
|
|
#
|
|
ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wdisabled-optimization
|
|
KBUILD_CFLAGS += -Wshadow
|
|
KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
|
|
KBUILD_CFLAGS += -Wunused-macros
|
|
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
|
|
|
|
else
|
|
|
|
# The following turn off the warnings enabled by -Wextra
|
|
KBUILD_CFLAGS += -Wno-missing-field-initializers
|
|
KBUILD_CFLAGS += -Wno-shift-negative-value
|
|
|
|
ifdef CONFIG_CC_IS_CLANG
|
|
KBUILD_CFLAGS += -Wno-enum-enum-conversion
|
|
endif
|
|
|
|
ifdef CONFIG_CC_IS_GCC
|
|
KBUILD_CFLAGS += -Wno-maybe-uninitialized
|
|
endif
|
|
|
|
endif
|
|
|
|
#
|
|
# W=3 - more obscure warnings, can most likely be ignored
|
|
#
|
|
ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
|
|
|
|
KBUILD_CFLAGS += -Wbad-function-cast
|
|
KBUILD_CFLAGS += -Wcast-align
|
|
KBUILD_CFLAGS += -Wcast-qual
|
|
KBUILD_CFLAGS += -Wconversion
|
|
KBUILD_CFLAGS += -Wpacked
|
|
KBUILD_CFLAGS += -Wpadded
|
|
KBUILD_CFLAGS += -Wpointer-arith
|
|
KBUILD_CFLAGS += -Wredundant-decls
|
|
KBUILD_CFLAGS += -Wsign-compare
|
|
KBUILD_CFLAGS += -Wswitch-default
|
|
|
|
KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
|
|
|
|
else
|
|
|
|
# The following turn off the warnings enabled by -Wextra
|
|
KBUILD_CFLAGS += -Wno-sign-compare
|
|
KBUILD_CFLAGS += -Wno-unused-parameter
|
|
|
|
endif
|
|
|
|
#
|
|
# W=e and CONFIG_WERROR - error out on warnings
|
|
#
|
|
ifneq ($(findstring e, $(KBUILD_EXTRA_WARN))$(CONFIG_WERROR),)
|
|
|
|
KBUILD_CPPFLAGS += -Werror
|
|
KBUILD_AFLAGS += -Wa,--fatal-warnings
|
|
KBUILD_LDFLAGS += --fatal-warnings
|
|
KBUILD_USERCFLAGS += -Werror
|
|
KBUILD_USERLDFLAGS += -Wl,--fatal-warnings
|
|
KBUILD_RUSTFLAGS += -Dwarnings
|
|
|
|
# While hostprog flags are used during build bootstrapping (thus should not
|
|
# depend on CONFIG_ symbols), -Werror is disruptive and should be opted into.
|
|
# Only apply -Werror to hostprogs built after the initial Kconfig stage.
|
|
KBUILD_HOSTCFLAGS += -Werror
|
|
KBUILD_HOSTLDFLAGS += -Wl,--fatal-warnings
|
|
KBUILD_HOSTRUSTFLAGS += -Dwarnings
|
|
|
|
endif
|