kernel: skip kbuild if the kernel tree did not change

kbuild runs twice per build, for the modules and for the image, and
takes about half a second each time even if there is nothing to do.

Store a hash of the kbuild command line after a successful run, and
skip the next run if the hash is the same and no file in the kernel
tree or the toolchain is newer than the stamp. The OpenWrt files at the
top of the kernel tree are ignored, apart from .config. Directories are
ignored, as kbuild changes them with temporary files, so a deleted file
alone does not trigger a run.

With CONFIG_STRIP_KERNEL_EXPORTS the image link reads symtab.h, which
sits above the kernel tree and follows the modules of the packages. The
scan of the image run names that file as well.

The workaround that deletes vmlinux now only runs together with kbuild.

Signed-off-by: John Crispin <john@phrozen.org>
This commit is contained in:
John Crispin committed 2026-09-22 18:18:58 +02:00
1 parent 714886f3e4
commit 9d84fbff36
1 file changed
+29 -3
+29 -3
View File
@@ -133,9 +133,35 @@ define Kernel/Configure/Initramfs
$(call Kernel/SetInitramfs,$(1),$(2))
endef
# The image link reads symtab.h through EXTRA_LDSFLAGS. It sits above
# $(LINUX_DIR), so the scan below has to name it. A package built module
# changes it without a change in the kernel tree.
KERNEL_MAKE_DEPENDS_image := $(if $(CONFIG_STRIP_KERNEL_EXPORTS),$(KERNEL_BUILD_DIR)/symtab.h)
# $(1): stamp name, $(2): make targets and options, $(3): commands to run before kbuild
# kbuild takes about half a second even if nothing changed, and runs twice
# per build. Skip it if the command line did not change since its last
# successful run, and no file in the kernel tree or the toolchain is newer.
# The build system files at the top of the kernel tree are not inputs of
# kbuild, apart from .config.
define Kernel/Make
{ \
stamp=$(LINUX_DIR)/.kbuild_$(1); \
key="$$$$(printf '%s' '$(subst ','\'',$(KERNEL_MAKE) $(2))' | $(MKHASH) md5)"; \
[ "$$$$(cat $$$$stamp 2>/dev/null)" = "$$$$key" ] && [ -z "$$$$(find $(LINUX_DIR) $(TOOLCHAIN_DIR)/bin \
$(KERNEL_MAKE_DEPENDS_$(1)) \
\( -path '$(LINUX_DIR)/.*' ! -path '$(LINUX_DIR)/.config' \) -prune -o \
! -type d -newer $$$$stamp -print -quit)" ] || { \
rm -f $$$$stamp; \
$(3) \
$(KERNEL_MAKE) $(2) && echo "$$$$key" > $$$$stamp; \
}; \
}
endef
define Kernel/CompileModules/Default
rm -f $(LINUX_DIR)/vmlinux $(LINUX_DIR)/System.map
+$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
+$(call Kernel/Make,modules,$(if $(KERNELNAME),$(KERNELNAME),all) modules, \
rm -f $(LINUX_DIR)/vmlinux $(LINUX_DIR)/System.map;)
# If .config did not change, use the previous timestamp to avoid package rebuilds
cmp -s $(LINUX_DIR)/.config $(LINUX_DIR)/.config.modules.save && \
mv $(LINUX_DIR)/.config.modules.save $(LINUX_DIR)/.config; \
@@ -165,7 +191,7 @@ endef
define Kernel/CompileImage/Default
rm -f $(TARGET_DIR)/init
+$(KERNEL_MAKE) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all)
+$(call Kernel/Make,image,$(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all))
$(call Kernel/CopyImage)
endef