Kbuild: allow subdirectories to override W= flags
Some subsystems have addressed all warnings of the W=1 level already,
so it makes sense to have a way to enable them per directory regardless
of the command line options.
Move the logic to identify the warning level to Makefile.lib, accumulating
the per-directory flags in $(KBUILD_EXTRA_WARN) so that each directory
can enable additional levels on top of the initial W= variable and its
parent directory settings.
This reverts part of 64a91907c896 ("kbuild: refactor
scripts/Makefile.extrawarn"), once more using separate variables
KBUILD_WARN_{W1,W2,W3} and KBUILD_WARN_{N1,N2,N3} to enable (W) or
disable (N) individual options in the correct order based on the current
settings. One downside is that this requires checking all the possible
options for the current compiler, which adds a bit of time to the
kernel build.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
diff --git a/Makefile b/Makefile
index f30ea66..076500b 100644
--- a/Makefile
+++ b/Makefile
@@ -515,6 +515,9 @@
export KBUILD_CPPFLAGS NOSTDINC_FLAGS LINUXINCLUDE OBJCOPYFLAGS KBUILD_LDFLAGS
export KBUILD_CFLAGS CFLAGS_KERNEL CFLAGS_MODULE
+export KBUILD_WARN_W1 KBUILD_WARN_N1 KBUILD_WARN_CPP_W1
+export KBUILD_WARN_W2 KBUILD_WARN_N2 KBUILD_WARN_CPP_W2
+export KBUILD_WARN_W3 KBUILD_WARN_N3 KBUILD_WARN_CPP_W3
export KBUILD_AFLAGS AFLAGS_KERNEL AFLAGS_MODULE
export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE
export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL
diff --git a/scripts/Makefile.extrawarn b/scripts/Makefile.extrawarn
index 3c35ca7..ba75e8e 100644
--- a/scripts/Makefile.extrawarn
+++ b/scripts/Makefile.extrawarn
@@ -23,90 +23,73 @@
#
# W=1 - warnings which may be relevant and do not occur too often
#
-ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
-KBUILD_CFLAGS += -Wunused
-KBUILD_CFLAGS += -Wmissing-declarations
-KBUILD_CFLAGS += -Wmissing-format-attribute
-KBUILD_CFLAGS += -Wmissing-prototypes
-KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
-
-else
+KBUILD_WARN_W1 += -Wunused
+KBUILD_WARN_W1 += -Wmissing-declarations
+KBUILD_WARN_W1 += -Wmissing-format-attribute
+KBUILD_WARN_W1 += -Wmissing-prototypes
+KBUILD_CPPFLAGS_W1 += -DKBUILD_EXTRA_WARN1
# Some diagnostics enabled by default are noisy.
# Suppress them by using -Wno... except for W=1.
ifdef CONFIG_CC_IS_CLANG
-KBUILD_CFLAGS += -Wno-format
-KBUILD_CFLAGS += -Wno-format-zero-length
-KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
-KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
+KBUILD_WARN_N1 += -Wno-format
+KBUILD_WARN_N1 += -Wno-format-zero-length
+KBUILD_WARN_N1 += $(call cc-disable-warning, pointer-to-enum-cast)
+KBUILD_WARN_N1 += -Wno-tautological-constant-out-of-range-compare
else
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
+KBUILD_WARN_N1 += $(call cc-disable-warning, unused-but-set-variable)
endif
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
-KBUILD_CFLAGS += -Wno-empty-body
-
-endif
-
+KBUILD_WARN_N1 += $(call cc-disable-warning, unused-const-variable)
+KBUILD_WARN_N1 += $(call cc-disable-warning, packed-not-aligned)
+KBUILD_WARN_N1 += -Wno-empty-body
#
# W=2 - warnings which occur quite often but may still be relevant
#
-ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
ifdef CONFIG_CC_IS_CLANG
-KBUILD_CFLAGS += -Wnested-externs
+KBUILD_WARN_W2 += -Wnested-externs
endif
-KBUILD_CFLAGS += -Wshadow
-KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
-KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
-KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
+KBUILD_WARN_W2 += -Wshadow
+KBUILD_WARN_W2 += $(call cc-option, -Wlogical-op)
+KBUILD_WARN_W2 += $(call cc-option, -Wmaybe-uninitialized)
+KBUILD_WARN_W2 += $(call cc-option, -Wunused-macros)
-KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
+KBUILD_CPPFLAGS_W2 += -DKBUILD_EXTRA_WARN2
-else
-
-KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
-KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
-KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
-KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
-KBUILD_CFLAGS += -Wno-format-security
-KBUILD_CFLAGS += -Wno-pointer-sign
-KBUILD_CFLAGS += $(call cc-disable-warning, maybe-uninitialized)
+KBUILD_WARN_N2 += $(call cc-disable-warning, restrict)
+KBUILD_WARN_N2 += $(call cc-disable-warning, format-truncation)
+KBUILD_WARN_N2 += $(call cc-disable-warning, format-overflow)
+KBUILD_WARN_N2 += $(call cc-disable-warning, address-of-packed-member)
+KBUILD_WARN_N2 += -Wno-format-security
+KBUILD_WARN_N2 += -Wno-pointer-sign
+KBUILD_WARN_N2 += $(call cc-disable-warning, maybe-uninitialized)
# The following turn off the warnings enabled by -Wextra
-KBUILD_CFLAGS += -Wno-missing-field-initializers
-KBUILD_CFLAGS += -Wno-type-limits
-
-endif
+KBUILD_WARN_N2 += -Wno-missing-field-initializers
+KBUILD_WARN_N2 += -Wno-type-limits
#
# W=3 - more obscure warnings, can most likely be ignored
#
-ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
+KBUILD_WARN_W3 += -Wbad-function-cast
+KBUILD_WARN_W3 += -Wcast-align
+KBUILD_WARN_W3 += -Wcast-qual
+KBUILD_WARN_W3 += -Wconversion
+KBUILD_WARN_W3 += -Wpacked
+KBUILD_WARN_W3 += -Wpadded
+KBUILD_WARN_W3 += -Wpointer-arith
+KBUILD_WARN_W3 += -Wredundant-decls
+KBUILD_WARN_W3 += -Wsign-compare
+KBUILD_WARN_W3 += -Wswitch-default
+KBUILD_WARN_W3 += $(call cc-option, -Wpacked-bitfield-compat)
-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_CFLAGS += $(call cc-option, -Wpacked-bitfield-compat)
+KBUILD_CPPFLAGS_W3 += -DKBUILD_EXTRA_WARN3
-KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN3
-
-else
-
-KBUILD_CFLAGS += -Wno-sign-compare
+KBUILD_WARN_N3 += -Wno-sign-compare
# This is enabled by -Wunused, but generally not helpful
-KBUILD_CFLAGS += -Wno-unused-parameter
-
-endif
+KBUILD_WARN_N3 += -Wno-unused-parameter
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 9413370..d50410f3 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -122,15 +122,44 @@
modname_flags = -DKBUILD_MODNAME=$(call name-fix,$(modname))
modfile_flags = -DKBUILD_MODFILE=$(call stringify,$(modfile))
+KBUILD_EXTRA_WARN := $(KBUILD_EXTRA_WARN)$(subdir-extra-warn-y)
+
+ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
+extra-warn1 = W1
+else
+extra-warn1 = N1
+endif
+
+ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
+extra-warn2 = W2
+else
+extra-warn2 = N2
+endif
+
+ifneq ($(findstring 3, $(KBUILD_EXTRA_WARN)),)
+extra-warn3 = W3
+else
+extra-warn3 = N3
+endif
+
+KBUILD_WARN_CPP = $(KBUILD_WARN_CPP_$(extra-warn1)) \
+ $(KBUILD_WARN_CPP_$(extra-warn2)) \
+ $(KBUILD_WARN_CPP_$(extra-warn3))
+
+KBUILD_WARN = $(KBUILD_WARN_$(extra-warn1)) \
+ $(KBUILD_WARN_$(extra-warn2)) \
+ $(KBUILD_WARN_$(extra-warn3))
+
_c_flags = $(filter-out $(CFLAGS_REMOVE_$(target-stem).o), \
$(filter-out $(ccflags-remove-y), \
- $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(ccflags-y)) \
+ $(KBUILD_CPPFLAGS) $(KBUILD_WARN_CPP) \
+ $(KBUILD_CFLAGS) $(KBUILD_WARN) $(ccflags-y)) \
$(CFLAGS_$(target-stem).o))
_a_flags = $(filter-out $(AFLAGS_REMOVE_$(target-stem).o), \
$(filter-out $(asflags-remove-y), \
$(KBUILD_CPPFLAGS) $(KBUILD_AFLAGS) $(asflags-y)) \
$(AFLAGS_$(target-stem).o))
-_cpp_flags = $(KBUILD_CPPFLAGS) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
+_cpp_flags = $(KBUILD_CPPFLAGS) $(KBUILD_WARN_CPP) $(cppflags-y) $(CPPFLAGS_$(target-stem).lds)
#
# Enable gcov profiling flags for a file, directory or for all files depending