Skip to content

Commit 901ed83

Browse files
committed
Add system_sysroot to eliminate toolchain transfers per action
When system_sysroot is set on rust_toolchain, the platform-installed Rust toolchain is used directly instead of shipping toolchain files as action inputs. This significantly speeds up both local and remote builds by avoiding ~670MB of toolchain file transfers per action. Changes: - toolchain.bzl: Add system_sysroot attr, override sysroot/make_variables, exclude toolchain files from all_files when set - rustc.bzl: Use system_rustc_path for rustc invocations - cargo_build_script.bzl: Use system paths for CARGO, RUSTC, RUSTDOC env vars
1 parent 3f4f431 commit 901ed83

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

cargo/private/cargo_build_script.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,10 @@ def _cargo_build_script_impl(ctx):
416416
env.update(ctx.configuration.default_shell_env)
417417

418418
if toolchain.cargo:
419-
env["CARGO"] = "${pwd}/%s" % toolchain.cargo.path
419+
if toolchain.system_sysroot:
420+
env["CARGO"] = toolchain.system_sysroot + "/bin/cargo"
421+
else:
422+
env["CARGO"] = "${pwd}/%s" % toolchain.cargo.path
420423

421424
env.update({
422425
"CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
@@ -425,8 +428,8 @@ def _cargo_build_script_impl(ctx):
425428
"HOST": toolchain.exec_triple.str,
426429
"NUM_JOBS": "1",
427430
"OPT_LEVEL": compilation_mode_opt_level,
428-
"RUSTC": toolchain.rustc.path,
429-
"RUSTDOC": toolchain.rust_doc.path,
431+
"RUSTC": toolchain.system_rustc_path if toolchain.system_rustc_path else toolchain.rustc.path,
432+
"RUSTDOC": (toolchain.system_sysroot + "/bin/rustdoc") if toolchain.system_sysroot else toolchain.rust_doc.path,
430433
"TARGET": toolchain.target_flag_value,
431434
# OUT_DIR is set by the runner itself, rather than on the action.
432435
})

rust/private/rustc.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,7 @@ def rustc_compile_action(
14051405
attr = attr,
14061406
file = ctx.file,
14071407
toolchain = toolchain,
1408-
tool_path = toolchain.rustc.path,
1408+
tool_path = toolchain.system_rustc_path if toolchain.system_rustc_path else toolchain.rustc.path,
14091409
cc_toolchain = cc_toolchain,
14101410
emit = emit,
14111411
feature_configuration = feature_configuration,
@@ -1432,7 +1432,7 @@ def rustc_compile_action(
14321432
attr = attr,
14331433
file = ctx.file,
14341434
toolchain = toolchain,
1435-
tool_path = toolchain.rustc.path,
1435+
tool_path = toolchain.system_rustc_path if toolchain.system_rustc_path else toolchain.rustc.path,
14361436
cc_toolchain = cc_toolchain,
14371437
emit = emit,
14381438
feature_configuration = feature_configuration,

rust/toolchain.bzl

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,16 @@ def _rust_toolchain_impl(ctx):
429429
sysroot_path = sysroot.sysroot_anchor.dirname
430430
sysroot_short_path, _, _ = sysroot.sysroot_anchor.short_path.rpartition("/")
431431

432+
# When system_sysroot is set, override paths to use the platform-installed toolchain.
433+
# This must happen before make_variables is constructed so all consumers see system paths.
434+
if ctx.attr.system_sysroot:
435+
sysroot_path = ctx.attr.system_sysroot
436+
sysroot_short_path = ctx.attr.system_sysroot
437+
432438
# Variables for make variable expansion
433439
make_variables = {
434-
"RUSTC": sysroot.rustc.path,
435-
"RUSTDOC": sysroot.rustdoc.path,
440+
"RUSTC": (ctx.attr.system_sysroot + "/bin/rustc") if ctx.attr.system_sysroot else sysroot.rustc.path,
441+
"RUSTDOC": (ctx.attr.system_sysroot + "/bin/rustdoc") if ctx.attr.system_sysroot else sysroot.rustdoc.path,
436442
"RUST_DEFAULT_EDITION": ctx.attr.default_edition or "",
437443
"RUST_SYSROOT": sysroot_path,
438444
"RUST_SYSROOT_SHORT": sysroot_short_path,
@@ -563,9 +569,21 @@ def _rust_toolchain_impl(ctx):
563569
)
564570

565571
# Include C++ toolchain files to ensure tools like 'ar' are available for cross-compilation
566-
all_files_depsets = [sysroot.all_files]
567-
if cc_toolchain and cc_toolchain.all_files:
568-
all_files_depsets.append(cc_toolchain.all_files)
572+
if ctx.attr.system_sysroot:
573+
# When system_sysroot is set, exclude Rust toolchain files from action inputs.
574+
# The execution environment (local or remote) has the toolchain installed at this path.
575+
# This eliminates ~670MB of toolchain file transfers per remote action.
576+
all_files_depsets = []
577+
if cc_toolchain and cc_toolchain.all_files:
578+
all_files_depsets.append(cc_toolchain.all_files)
579+
580+
# Keep linker as input (it's custom and tiny)
581+
if sysroot.linker:
582+
all_files_depsets.append(depset([sysroot.linker]))
583+
else:
584+
all_files_depsets = [sysroot.all_files]
585+
if cc_toolchain and cc_toolchain.all_files:
586+
all_files_depsets.append(cc_toolchain.all_files)
569587

570588
toolchain = platform_common.ToolchainInfo(
571589
all_files = depset(transitive = all_files_depsets),
@@ -605,6 +623,8 @@ def _rust_toolchain_impl(ctx):
605623
per_crate_rustc_flags = ctx.attr.per_crate_rustc_flags,
606624
sysroot = sysroot_path,
607625
sysroot_short_path = sysroot_short_path,
626+
system_rustc_path = ctx.attr.system_sysroot + "/bin/rustc" if ctx.attr.system_sysroot else "",
627+
system_sysroot = ctx.attr.system_sysroot,
608628
target_arch = target_arch,
609629
target_flag_value = target_json.path if target_json else target_triple.str,
610630
target_json = target_json,
@@ -831,6 +851,17 @@ rust_toolchain = rule(
831851
"opt": "debuginfo",
832852
},
833853
),
854+
"system_sysroot": attr.string(
855+
doc = (
856+
"When set, use the platform-provided Rust toolchain at this absolute path " +
857+
"instead of shipping toolchain files as action inputs. This eliminates large " +
858+
"toolchain file transfers per action when using remote execution with " +
859+
"workers that have the Rust toolchain pre-installed. The path should point " +
860+
"to a rustup toolchain directory (e.g. " +
861+
"/home/user/.rustup/toolchains/nightly-2026-01-29-x86_64-unknown-linux-gnu)."
862+
),
863+
default = "",
864+
),
834865
"target_json": attr.string(
835866
doc = ("Override the target_triple with a custom target specification. " +
836867
"For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"),

0 commit comments

Comments
 (0)