Add system_sysroot to rust_toolchain for pre-installed toolchains#3928
Add system_sysroot to rust_toolchain for pre-installed toolchains#3928rejuvenile wants to merge 1 commit intobazelbuild:mainfrom
Conversation
Introduce a repository rule that probes the system for an installed Rust toolchain and generates BUILD targets with sysroot_path set. This follows the cc_configure pattern from rules_cc: the repository rule runs at fetch time, discovers the toolchain, and generates appropriate targets. Changes: - New rust/private/system_rust_configure.bzl with: - rust_system_toolchain_repository repository rule - system_rust_ext module extension for bzlmod - System probing (rustc --print sysroot, --version, --print target-libdir) - BUILD file generation with rust_toolchain + toolchain targets - rust/toolchain.bzl: Add sysroot_path attribute to rust_toolchain. When set, sysroot files are excluded from action inputs and --sysroot is passed to rustc pointing at the system installation. - rust/private/rustc.bzl: Use system rustc path when sysroot_path is set - rust/private/clippy.bzl: Use system clippy-driver when sysroot_path is set - rust/private/rustdoc.bzl: Use system rustdoc when sysroot_path is set - rust/private/unpretty.bzl: Use system rustc when sysroot_path is set - cargo/private/cargo_build_script.bzl: Use system tool paths for CARGO, RUSTC, RUSTDOC env vars when sysroot_path is set This replaces the previous system_sysroot boolean approach with a cleaner separation: the repository rule handles system probing, and the toolchain rule simply accepts a sysroot_path string for path redirection.
901ed83 to
2f03f47
Compare
| "opt": "debuginfo", | ||
| }, | ||
| ), | ||
| "sysroot_path": attr.string( |
There was a problem hiding this comment.
Is this needed if you're building the toolchain in a repository rule? Why not symlink the toolchain directory into the repository and wire things up with nominal filegroups?
There was a problem hiding this comment.
Is this needed if you're building the toolchain in a repository rule? Why not symlink the toolchain directory into the repository and wire things up with nominal filegroups?
I had a small bias towards not transferring the symlinks during remote execution, but more importantly, I don't how how symlinks would work with sandboxed execution, since they would point outside of the sandbox. With the current approach, Bazel symlinks the toolchain into the sandbox itself.
There was a problem hiding this comment.
Then would it be sensible to have wrapper scripts that simply exec out to a hard-coded path on disk? I've seen this pattern in a number of places and haven't noticed a major difference
Summary
Adds
rust_system_toolchain_repository, a repository rule that discovers a pre-installed Rust toolchain on the system, following thecc_configurepattern from rules_cc.Problem
When using remote execution with workers that have the Rust toolchain pre-installed, the entire sysroot (~670MB) is transferred as action inputs for every compilation action. This creates significant overhead when the toolchain is already available on the worker.
Solution
A repository rule (
rust_system_toolchain_repository) that:rustc --print sysroot,rustc --version --verbose,rustc --print target-libdirrust_toolchain()targets where sysroot files are excluded from action inputsrustc,cargo,rustdoc,clippy-driver)The
rust_toolchainrule gains asysroot_pathstring attribute. When set,--sysroot=<path>is passed to rustc and sysroot files are excluded from action inputs.New files
rust/private/system_rust_configure.bzl— repository rule + module extensionModified files
rust/toolchain.bzl— addssysroot_pathattributerust/private/rustc.bzl— uses system paths whensysroot_pathis setrust/private/clippy.bzl,rustdoc.bzl,unpretty.bzl— samecargo/private/cargo_build_script.bzl— uses system CARGO/RUSTC pathsUsage
This follows the reviewer feedback on #3920 suggesting a repository rule approach similar to
@rules_cc//cc/private/toolchain/cc_configure.bzl.Test plan