extensions: add kernel-rust for Rust support in kernel builds#9386
extensions: add kernel-rust for Rust support in kernel builds#9386igorpecovnik merged 1 commit intoarmbian:mainfrom
Conversation
|
Important Review skippedAuto reviews are limited based on label configuration. 🏷️ Required labels (at least one) (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughAdds a new extension script that integrates Rust toolchains into the kernel build flow: apt-based dependency selection (rustup flow commented), multi-step tool discovery, LLVM/libclang version detection and toolchain override, host readiness checks, kernel config/make hooks, and propagation of explicit Rust tool paths and options. (≤50 words) Changes
Sequence Diagram(s)sequenceDiagram
participant Build as Build System
participant Ext as Rust Extension
participant Apt as APT/Package Manager
participant Finder as Tool Discoverer
participant Kernel as Kernel Build
Build->>Ext: Request Rust capability
Ext->>Apt: Select/install Rust/LLVM/bindgen packages (apt flow)
Apt-->>Ext: Packages installed / available
Ext->>Finder: _find_rust_tool (versioned PATH, dpkg lists, unversioned PATH)
Finder-->>Ext: rustc, rustfmt, bindgen paths
Ext->>Ext: detect libclang/LLVM version -> override toolchain version
Ext->>Kernel: Inject RUST config flags and make params (RUSTC, RUSTFMT, BINDGEN, RUST_LIB_SRC, LLVM/CC)
Kernel-->>Build: Kernel config and make params updated
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
tested against #9381 just for fun. Seems to work: https://paste.armbian.com/javugatico No clue if this is an issue. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@extensions/kernel-rust.sh`:
- Around line 314-319: The element added to common_make_envs sets RUST_LIB_SRC
with embedded single quotes which will be preserved when passed to env -i;
update the construction so quoting is consistent by either removing the embedded
single quotes in the value added to common_make_envs (i.e., push
RUST_LIB_SRC=${rust_lib_src} instead) or ensure the caller that expands
common_make_envs uses the bash array expansion with the `@Q` operator
(common_make_envs[@]@Q) like common_make_params_quoted does; modify the code
that builds/expands common_make_envs or the element insertion for RUST_LIB_SRC
so the final env invocation receives a properly quoted value rather than literal
single quotes.
🧹 Nitpick comments (5)
extensions/kernel-rust.sh (5)
124-131: Loose grep pattern may match unintended binaries from dpkg file list.
grep "/bin/${base}"is a substring match. Forbase="rustc", it would also match paths like/usr/lib/rust-1.85/bin/rustc-wrapperor/usr/lib/rust-1.85/bin/rustc-demangler. Combined withhead -1, the result depends ondpkg -Loutput order, which could non-deterministically pick the wrong binary.Anchor the grep to match the exact binary name:
Proposed fix
- tool_path="$(dpkg -L "${versioned}" 2> /dev/null | grep "/bin/${base}" | head -1 || true)" + tool_path="$(dpkg -L "${versioned}" 2> /dev/null | grep -E "/bin/${base}\$" | head -1 || true)"
265-279: CC glob pattern could be tightened slightly.
CC=*clang)matches any value ending inclang, including hypotheticalCC=somethingclang. A more precise pattern likeCC=*[[:space:]]clang|CC=clangwould be safer. However, in practice the only values set upstream areCC=ccache clangorCC=clang, so this is a minor robustness nit.
296-304: Consider a fallback for RUST_LIB_SRC discovery.The path
/usr/src/rustc-${rustc_full_version}/libraryis the only APT convention checked. If a future Debian/Ubuntu ships rust-src with a different layout (e.g.,rust-srcinstead ofrust-X.YY-src), this silently falls through to the warning on line 318. A secondary glob ordpkg -Llookup on the rust-src package could improve robustness.
156-196: Large commented-out block adds noise.~40 lines of commented-out rustup code. While it's useful as documentation of the alternative method, consider moving it to a separate file or a doc comment/README section to keep the active code clean.
75-89: LLVM packages don't use_apt_pickfallback, unlike Rust/bindgen packages.Lines 82–85 use
_apt_pickfor graceful fallback, but line 88 hardcodes versionedclang-${LLVM_APT_VERSION}/lld-${LLVM_APT_VERSION}/llvm-${LLVM_APT_VERSION}without fallback to unversioned. If auto-detection returns a version that isn't packaged, apt will fail with a less helpful error. This is likely intentional since the version is auto-detected, but worth noting for cross-distro robustness.
This is strange. Ubuntu 24.04 noble bindgen-0.71 is in the noble-updates repository. |
|
@coderabbitai Regarding the "Actionable" comment about This is not a bug — it's the established pattern used throughout the framework. All elements in common_make_envs=(
"CCACHE_BASEDIR='$(pwd)'" # line 21
"CCACHE_TEMPDIR='${CCACHE_TEMPDIR:?}'" # line 22
"CCACHE_DIR='${CCACHE_DIR}'" # line 34
"RUST_LIB_SRC='${rust_lib_src}'" # our addition - same format
)Why this works correctly:
Why changing it would be wrong:
The logged command output ( |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 1289
I've now verified Your implementation of I appreciate you providing this context—it helps me better understand the framework's conventions going forward. ✏️ Learnings added
🧠 Learnings used |
|
Noble-updates is present and used. packages were mostly up to date before trying. Nothing rust-related after updating. |
|
We all knew this was coming, but I wonder if it's too soon for Rust stuff. We've enough on our plates without it. Any practical use-case already? (Otherwise it's a lot of work, which will invariably change, for little benefit). I don't oppose, just wonder. |
|
No, there was no immediate, specific goal; it was an attempt to "prepare for the future." Well, and I wanted to see what it would be like there, in the future. You're probably right. Nothing bad will happen if we wait another 2-3 months, but there will almost certainly be more certainty and clarity. |
|
Yeah, maybe the "exciting" Rust stuff is still out of tree (eg Ref the complexity, maybe it is more related to |
|
tyr there, I build it already. But have no way to run it. |
|
During the implementation of this extension, almost all the complexity was related to coordinating rustc versions, packages, and libraries, taking into account what could be installed from the repositories available on the build machine. And even then, in the end, everything fell into place in the correct chain for me, but for some reason for you it didn’t. |
Yeah that would be a bit more intricate indeed. We have somewhat similar with Python (which used to be from |
|
The main thing is to figure out how to integrate the hook into the container image creation process. The rest is easy. |
|
Migrate the kernel-rust extension from APT packages to rustup. Instead of distro-specific packages (rustc-1.85, bindgen-0.71, clang-20 and ~150 lines of version selection logic), the toolchain is installed via rustup into Removed LLVM management from the extension — cargo-installed bindgen uses the system libclang-dev via dlopen, so matching clang/lld/llvm versions is no longer necessary. Added Works identically on Ubuntu 24.04/25.10/26.04, Debian 13, both natively and in Docker. |
|
I use dkms bcachefs from https://apt.bcachefs.org / with an edge kernel on its Helios64 and ODroidM1. |
Add extension that enables CONFIG_RUST in kernel menuconfig and configures the build environment (rustc, rustfmt, bindgen, rust-src) using versioned APT packages from noble-security/noble-updates. Handles env -i in run_kernel_make_internal by passing tool paths via RUSTC, RUSTFMT, BINDGEN make params and RUST_LIB_SRC env var. Includes optional RUST_KERNEL_SAMPLES=yes for building sample Rust modules (rust_minimal, rust_print, rust_driver_faux) as a toolchain smoke test. Tested: kernel 6.19 build for rockchip64 on aarch64, both with and without Docker. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
rpardini
left a comment
There was a problem hiding this comment.
Single, self-contained, optional, extension. Let's merge so easier to test. Any issues we can fix later...?
|
✅ This PR has been reviewed and approved — all set for merge! |
|
At the moment, I myself use this extension to build kernels in my home lab, I am not aware of any errors or workarounds. |
|
Is this documented already? |
|
Only in the form of comments at the top of the extension file. |
Summary
Extension that enables
CONFIG_RUSTin kernel builds. Installs Rust toolchain via rustup intocache/tools/rustup/, cached by a content-addressable hash of versions and architecture.How it works:
${SRC}/cache/tools/rustup/libclang-dev(for bindgen's dlopen)RUSTC,RUSTFMT,BINDGEN) and env vars (RUST_LIB_SRC) — uses direct toolchain binaries (not rustup proxies) to work withenv -iinrun_kernel_make_internal()_Rkey to artifact version hash so toolchain version changes trigger kernel rebuildRUST_KERNEL_SAMPLES=yesbuilds sample Rust modules for smoke testingExtensibility:
RUST_EXTRA_COMPONENTSarray — other extensions can request additional rustup components (e.g.clippy,llvm-tools)RUST_EXTRA_CARGO_CRATESarray — other extensions can request cargo crates with optional version pinning (e.g."mdbook","cargo-deb@2.11.0")Distro support: works identically on Ubuntu 24.04/25.10/26.04, Debian 13, both natively and in Docker. No distro-specific logic needed — rustup is distro-agnostic, libclang-dev is universally available.
Usage
Test plan
kernel-config— CONFIG_RUST appears in menuconfigmodprobe rust_minimal🤖 Generated with Claude Code
Summary by CodeRabbit
New Features