Makefile | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
Update output Makefile in the corresponding tree only: for out-of-source
in-tree builds update $(objtree)/Makefile, for out-of-source out-of-tree
module builds update $(KBUILD_EXTMOD_OUTPUT)/Makefile instead.
In-source builds are not affected.
Since commit c9bb03ac2c66 ("kbuild: reduce output spam when building out
of tree"), building out-of-tree kernel modules out-of-source (make M=...
MO=...) causes a rewrite of $(objtree)/Makefile with KBUILD_EXTMOD and
KBUILD_EXTMOD_OUTPUT being set. That is problematic:
* $(objtree)/ must not be changed in any way when building out-of-tree
modules as it breaks other uses of $(objtree).
* Setting KBUILD_EXTMOD and KBUILD_EXTMOD_OUTPUT in $(objtree)/Makefile
kills the tree for incremental builds that start right there
('make -C $(objtree)'); builds starting in $(srctree) reset
$(objtree)/Makefile to its original content.
Further, $(KBUILD_EXTMOD_OUTPUT)/Makefile was not generated any more at
all.
This commit restores the previous kbuild behaviour prior to commit
c9bb03ac2c66 ("kbuild: reduce output spam when building out of tree")
but leaves in-place the use of filechk for output spam reduction.
Fixes: c9bb03ac2c66 ("kbuild: reduce output spam when building out of tree")
Reported-by: Anish Rashinkar <rashinkar.anish@gmail.com>
Closes: https://lore.kernel.org/r/CAOESE2Q2-0KUDaM0mUo+c_F-tMaUsBZ-gpnhdoe0rmYdgnnuJQ@mail.gmail.com
Cc: stable@vger.kernel.org
Tested-by: Philipp Hahn <p.hahn@avm.de>
Reviewed-by: Philipp Hahn <p.hahn@avm.de>
Signed-off-by: Nicolas Schier <n.schier@fritz.com>
---
Anish Rashinkar reported that building external kmods out-of-source
caused modifications of the build tree's Makefile (cp. Closes-link).
Anish, thanks for the well-detailed report!
---
A simple reproducer:
make O="${buildtree}" allnoconfig
scripts/config --file "${buildtree}"/.config --enable modules
make -C "${buildtree}" olddefconfig all
chmod u-w "${buildtree}"/Makefile "${buildtree}"
tempdir=$(mktemp -d -t kmod-oos-build.XXXX)
make O="${buildtree}" M=samples/kobject CONFIG_SAMPLE_KOBJECT=m MO="${tempdir}"
make -C "${tempdir}/" clean
make -C "${tempdir}/" CONFIG_SAMPLE_KOBJECT=m
---
Makefile | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/Makefile b/Makefile
index a8d8ed711f9b..b2ad52f6719d 100644
--- a/Makefile
+++ b/Makefile
@@ -681,13 +681,11 @@ filechk_makefile = { \
echo "include $(abs_srctree)/Makefile"; \
}
-$(objtree)/Makefile: FORCE
+PHONY += $(CURDIR)/Makefile
+$(CURDIR)/Makefile: FORCE
$(call filechk,makefile)
-# Prevent $(srcroot)/Makefile from inhibiting the rule to run.
-PHONY += $(objtree)/Makefile
-
-outputmakefile: $(objtree)/Makefile
+outputmakefile: $(CURDIR)/Makefile
ifeq ($(KBUILD_EXTMOD),)
@if [ -f $(srctree)/.config -o \
-d $(srctree)/include/config -o \
---
base-commit: 6946cd5d0aa4dd10a414ddcb7a10844fdb0ad345
change-id: 20260723-kbuild-do-not-modify-build-tree-makefile-with-mo-4f30095e12e3
Best regards,
--
Nicolas Schier
© 2016 - 2026 Red Hat, Inc.