Skip to content

Commit 2f03f47

Browse files
committed
Add rust_system_toolchain_repository for pre-installed toolchains
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.
1 parent 3f4f431 commit 2f03f47

7 files changed

Lines changed: 457 additions & 27 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.sysroot_path:
420+
env["CARGO"] = toolchain.sysroot_path + "/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.sysroot_path + "/bin/rustc") if toolchain.sysroot_path else toolchain.rustc.path,
432+
"RUSTDOC": (toolchain.sysroot_path + "/bin/rustdoc") if toolchain.sysroot_path 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/clippy.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ def rust_clippy_action(ctx, clippy_executable, process_wrapper, crate_info, conf
160160
crate_info_dict["rustc_output"] = clippy_diagnostics_file
161161
crate_info = rust_common.create_crate_info(**crate_info_dict)
162162

163+
clippy_tool_path = (toolchain.sysroot_path + "/bin/clippy-driver") if toolchain.sysroot_path else clippy_executable.path
164+
163165
args, env = construct_arguments(
164166
ctx = ctx,
165167
attr = ctx.rule.attr,
166168
file = ctx.file,
167169
toolchain = toolchain,
168-
tool_path = clippy_executable.path,
170+
tool_path = clippy_tool_path,
169171
cc_toolchain = cc_toolchain,
170172
feature_configuration = feature_configuration,
171173
crate_info = crate_info,

rust/private/rustc.bzl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,10 @@ def construct_arguments(
11931193
{},
11941194
))
11951195

1196-
# Ensure the sysroot is set for the target platform
1197-
if toolchain._toolchain_generated_sysroot:
1196+
# Ensure the sysroot is set for the target platform.
1197+
# When sysroot_path is set (system toolchain), always pass --sysroot so rustc
1198+
# finds the standard library at the pre-installed location.
1199+
if toolchain.sysroot_path or toolchain._toolchain_generated_sysroot:
11981200
rustc_flags.add(toolchain.sysroot, format = "--sysroot=%s")
11991201

12001202
if toolchain._rename_first_party_crates:
@@ -1400,12 +1402,16 @@ def rustc_compile_action(
14001402
elif ctx.attr.require_explicit_unstable_features == -1:
14011403
require_explicit_unstable_features = toolchain.require_explicit_unstable_features
14021404

1405+
# When using a system sysroot, use the system rustc path instead of the
1406+
# sysroot-symlinked binary.
1407+
rustc_tool_path = (toolchain.sysroot_path + "/bin/rustc") if toolchain.sysroot_path else toolchain.rustc.path
1408+
14031409
args, env_from_args = construct_arguments(
14041410
ctx = ctx,
14051411
attr = attr,
14061412
file = ctx.file,
14071413
toolchain = toolchain,
1408-
tool_path = toolchain.rustc.path,
1414+
tool_path = rustc_tool_path,
14091415
cc_toolchain = cc_toolchain,
14101416
emit = emit,
14111417
feature_configuration = feature_configuration,
@@ -1432,7 +1438,7 @@ def rustc_compile_action(
14321438
attr = attr,
14331439
file = ctx.file,
14341440
toolchain = toolchain,
1435-
tool_path = toolchain.rustc.path,
1441+
tool_path = rustc_tool_path,
14361442
cc_toolchain = cc_toolchain,
14371443
emit = emit,
14381444
feature_configuration = feature_configuration,

rust/private/rustdoc.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,19 @@ def rustdoc_compile_action(
126126
# arguments expecting to do so.
127127
rustdoc_crate_info = _strip_crate_info_output(crate_info)
128128

129+
if toolchain.sysroot_path:
130+
rustdoc_tool_path = toolchain.sysroot_path + "/bin/rustdoc"
131+
elif is_test:
132+
rustdoc_tool_path = toolchain.rust_doc.short_path
133+
else:
134+
rustdoc_tool_path = toolchain.rust_doc.path
135+
129136
args, env = construct_arguments(
130137
ctx = ctx,
131138
attr = ctx.attr,
132139
file = ctx.file,
133140
toolchain = toolchain,
134-
tool_path = toolchain.rust_doc.short_path if is_test else toolchain.rust_doc.path,
141+
tool_path = rustdoc_tool_path,
135142
cc_toolchain = cc_toolchain,
136143
feature_configuration = feature_configuration,
137144
crate_info = rustdoc_crate_info,

0 commit comments

Comments
 (0)