[PATCH 22/23] kbuild: build rust crates in parallel with the rest of the build
From: Lorenzo Stoakes (ARM)
Date: Tue Sep 08 2026 - 17:17:09 EST
When CONFIG_RUST is specified make prepare builds all of the rust
components before descending into the tree.
This means nothing can be done until all of these are compiled, resulting
in a stall at the start of every clean CONFIG_RUST build.
This also implicitly slows down every rust-capable LLVM allmodconfig build
as this enables the CONFIG_RUST option.
Fix this by allowing rust crates to be built alongside the C code.
Remove the rust build from the make prepare step, then establish a
dependency between rust code located elsewhere in the tree upon rust
components contained in rust/.
Do this by establishing a top-level list of directories containing rust
code, KBUILD_RUST_DIRS, upon which the dependency is expressed.
Finally, ensure that no rust code is located elsewhere and fail the build
if any is found to ensure that nothing is missed in future.
Care is taken to ensure the rust/ dependency is also established for any
module being built to account for external rust modules which rely upon it.
Every Rust and C object was confirmed to be byte-for-byte identical after
this change.
Whole build, 128-thread Threadripper 9980X, best of N runs:
before after delta
-------------------------------
x86 defconfig+RUST, clean 36.5s 32.6s -3.9s (-11%)
x86 allmodconfig, clean 297.0s 278.3s -18.7s (-6%)
Assisted-by: LLM
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
---
Kbuild | 5 +++++
Makefile | 11 ++++++++++-
scripts/Makefile.build | 5 ++++-
3 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/Kbuild b/Kbuild
index a6a0192dea08..9670556059dd 100644
--- a/Kbuild
+++ b/Kbuild
@@ -115,3 +115,8 @@ obj-$(CONFIG_NET) += net/
obj-y += virt/
obj-y += $(ARCH_DRIVERS)
obj-$(CONFIG_DRM_HEADER_TEST) += include/
+
+# Rust code elsewhere in the tree depends upon rust/.
+ifdef CONFIG_RUST
+$(KBUILD_RUST_DIRS): | rust
+endif
diff --git a/Makefile b/Makefile
index 7d23d7b64be3..3805f72428cb 100644
--- a/Makefile
+++ b/Makefile
@@ -1429,11 +1429,14 @@ prepare0: archprepare
$(Q)$(MAKE) $(build)=. prepare
$(Q)$(MAKE) $(build)=scripts/mod
+ifdef CONFIG_RUST
+export KBUILD_RUST_DIRS := drivers lib mm samples
+endif
+
# All the preparing..
prepare: prepare0
ifdef CONFIG_RUST
+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
- $(Q)$(MAKE) $(build)=rust
endif
PHONY += remove-stale-files
@@ -1760,6 +1763,12 @@ modules: modules_prepare
# Target to prepare building external modules
modules_prepare: prepare
$(Q)$(MAKE) $(build)=scripts scripts/module.lds
+ifdef CONFIG_RUST
+# Ensure rust/ is build before any external rust module which will rely upon it.
+ifeq ($(MAKECMDGOALS),modules_prepare)
+ $(Q)$(MAKE) $(build)=rust
+endif
+endif
endif # CONFIG_MODULES
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index b9093b39cc2f..77351d6006fa 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -360,8 +360,11 @@ quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
$(cmd_ld_single)) \
$(cmd_objtool)
+rust-dir-ok = $(or $(KBUILD_EXTMOD),$(filter rust $(KBUILD_RUST_DIRS),$(firstword $(subst /, ,$@))))
+
+# Ensure that any rust code located elsewhere from rust/ is listed in KBUILD_RUST_DIRS.
define rule_rustc_o_rs
- $(call cmd_and_fixdep,rustc_o_rs)
+ $(if $(rust-dir-ok),,$(error $@: Rust code in a directory not listed in KBUILD_RUST_DIRS, see the top Makefile))$(call cmd_and_fixdep,rustc_o_rs)
$(call cmd,gen_objtooldep)
endef
--
2.55.0