build: reuse the output of slow compression steps

On a rebuild, the initrd and the initramfs kernel image usually have
the same content as before, but xz and lzma compress them again on
every build. For the OpenWrt One this takes about 6 s of the build.

Add scripts/cache-run.sh. It runs a command that creates one file from
another, and stores the output together with a hash of the input, the
command line and the tool. If they are the same in the next run, it
copies the stored output instead. The commands are deterministic, so
the result is identical.

On a hit the script compares the stored output with the file that is
already there and copies only when the two differ. The output then keeps
its timestamp, so a later step that compares timestamps does not treat it
as new work.

Use it for the initrd compression and for Build/lzma. bzip2 and lzop
stay as they are: bzip2 writes to stdout, and lzop stores the mtime of
its input. xz and gzip now keep the cpio, as the other tools do, so a
cache hit leaves the same files behind as a normal run.

The slot name follows the path of the output below $(KDIR), because two
targets in different directories can share a basename. cache-run.sh holds
a lock over the slot and swaps the stored output in with a rename, so two
image recipes on one slot never read a half written pair.

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 f7adeacb62
commit c6a1bf5966
4 files changed
+74 -3

No files matched your search

+6 -1
View File
@@ -466,6 +466,10 @@ define Build/fit
$(call Build/fit-image,$(1))
endef
# A slot name from the basename alone collides between two targets that share
# it in different directories below $(KDIR).
cache_slot = $(KDIR)/cache/$(subst /,_,$(patsubst $(KDIR)/%,%,$(1)))
define Build/libdeflate-gzip
$(STAGING_DIR_HOST)/bin/libdeflate-gzip -f -12 -c $@ $(1) > $@.new
@mv $@.new $@
@@ -593,7 +597,8 @@ define Build/lzma
endef
define Build/lzma-no-dict
$(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
$(CACHE_RUN) $(call cache_slot,$@).lzma $@ $@.new \
$(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
@mv $@.new $@
endef
+12 -2
View File
@@ -204,6 +204,11 @@ define Kernel/PrepareConfigPerRootfs
endef
ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
# $1: Per Device Rootfs ID
# $2: suffix of the compressed initrd
Kernel/CacheInitrd = $(CACHE_RUN) $(KERNEL_BUILD_DIR)/cache/initrd$(1).cpio.$(2) \
$(KERNEL_BUILD_DIR)/initrd$(1).cpio $(KERNEL_BUILD_DIR)/initrd$(1).cpio.$(2)
# $1: Custom TARGET_DIR. If omitted TARGET_DIR is used.
# $2: If defined Generate Per Rootfs Kernel Directory and use it
# For Separate Initramfs, the regular kernel is used as is, as its config
@@ -226,16 +231,21 @@ define Kernel/CompileImage/Initramfs
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2), \
$(STAGING_DIR_HOST)/bin/bzip2 -9 -c < $(KERNEL_BUILD_DIR)/initrd$(2).cpio > $(KERNEL_BUILD_DIR)/initrd$(2).cpio.bzip2;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP), \
$(STAGING_DIR_HOST)/bin/libdeflate-gzip -n -f -S .gzip -12 $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
$(call Kernel/CacheInitrd,$(2),gzip) \
$(STAGING_DIR_HOST)/bin/libdeflate-gzip -n -k -f -S .gzip -12 $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4), \
$(call Kernel/CacheInitrd,$(2),lz4) \
$(STAGING_DIR_HOST)/bin/lz4c -l -c1 -fz --favor-decSpeed $(KERNEL_BUILD_DIR)/initrd$(2).cpio $(KERNEL_BUILD_DIR)/initrd$(2).cpio.lz4;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA), \
$(call Kernel/CacheInitrd,$(2),lzma) \
$(STAGING_DIR_HOST)/bin/lzma e -lc1 -lp2 -pb2 $(KERNEL_BUILD_DIR)/initrd$(2).cpio $(KERNEL_BUILD_DIR)/initrd$(2).cpio.lzma;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO), \
$(STAGING_DIR_HOST)/bin/lzop -9 -f $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ), \
$(STAGING_DIR_HOST)/bin/xz -T$(if $(filter 1,$(NPROC)),2,0) -9 -fz --check=crc32 $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
$(call Kernel/CacheInitrd,$(2),xz) \
$(STAGING_DIR_HOST)/bin/xz -T$(if $(filter 1,$(NPROC)),2,0) -9 -fzk --check=crc32 $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD), \
$(call Kernel/CacheInitrd,$(2),zstd) \
$(STAGING_DIR_HOST)/bin/zstd -T0 -f -o $(KERNEL_BUILD_DIR)/initrd$(2).cpio.zstd $(KERNEL_BUILD_DIR)/initrd$(2).cpio;) \
}, gen-cpio$(2));,\
$(KERNEL_MAKE) $(if $(2),-C $(LINUX_DIR)$(2)) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all);) \