Skip to content

Commit 928c1c1

Browse files
committed
RFC: rust_toolchains submodule
1 parent af24d27 commit 928c1c1

File tree

16 files changed

+217
-34
lines changed

16 files changed

+217
-34
lines changed

MODULE.bazel

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ module(
1212
bazel_dep(name = "bazel_features", version = "1.32.0")
1313
bazel_dep(name = "bazel_skylib", version = "1.8.2")
1414
bazel_dep(name = "platforms", version = "1.0.0")
15+
bazel_dep(name = "rust_toolchains", version = "0.68.1")
1516
bazel_dep(name = "rules_cc", version = "0.2.4")
1617
bazel_dep(name = "rules_license", version = "1.0.0")
1718
bazel_dep(name = "rules_shell", version = "0.6.1")
1819
bazel_dep(name = "apple_support", version = "1.24.1", repo_name = "build_bazel_apple_support")
1920

21+
bazel_dep(name = "rust_toolchains", version = "0.0.1")
22+
local_path_override(
23+
module_name = "rust_toolchains",
24+
path = "rust_toolchains",
25+
)
26+
2027
internal_deps = use_extension("//rust/private:internal_extensions.bzl", "i")
2128
use_repo(
2229
internal_deps,
@@ -45,10 +52,10 @@ use_repo(
4552

4653
rust = use_extension("//rust:extensions.bzl", "rust")
4754
rust.toolchain(edition = "2021")
48-
use_repo(rust, "rust_toolchains")
55+
use_repo(rust, rules_rust_toolchains = "rust_toolchains")
4956

5057
register_toolchains(
51-
"@rust_toolchains//:all",
58+
"@rules_rust_toolchains//:all",
5259
)
5360

5461
rust_host_tools = use_extension("//rust:extensions.bzl", "rust_host_tools")

cargo/private/cargo_build_script.bzl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ load(
2121
"deduplicate",
2222
"expand_dict_value_locations",
2323
"find_cc_toolchain",
24+
"find_optional_toolchain",
2425
"find_toolchain",
2526
_name_to_crate_name = "name_to_crate_name",
2627
)
@@ -315,14 +316,15 @@ def _cargo_build_script_impl(ctx):
315316
"""
316317
script = ctx.executable.script
317318
script_info = ctx.attr.script[CargoBuildScriptRunfilesInfo]
318-
toolchain = find_toolchain(ctx)
319+
rustc_toolchain = find_toolchain(ctx)
320+
cargo_toolchain = find_optional_toolchain(ctx, "@rust_toolchains//cargo:toolchain_type") or rustc_toolchain
319321
out_dir = ctx.actions.declare_directory(ctx.label.name + ".out_dir")
320322
env_out = ctx.actions.declare_file(ctx.label.name + ".env")
321323
dep_env_out = ctx.actions.declare_file(ctx.label.name + ".depenv")
322324
flags_out = ctx.actions.declare_file(ctx.label.name + ".flags")
323325
link_flags = ctx.actions.declare_file(ctx.label.name + ".linkflags")
324326
link_search_paths = ctx.actions.declare_file(ctx.label.name + ".linksearchpaths") # rustc-link-search, propagated from transitive dependencies
325-
compilation_mode_opt_level = get_compilation_mode_opts(ctx, toolchain).opt_level
327+
compilation_mode_opt_level = get_compilation_mode_opts(ctx, rustc_toolchain).opt_level
326328

327329
script_tools = []
328330
script_data = []
@@ -364,7 +366,7 @@ def _cargo_build_script_impl(ctx):
364366
if pkg_name == "":
365367
pkg_name = name_to_pkg_name(ctx.label.name)
366368

367-
toolchain_tools = [toolchain.all_files]
369+
toolchain_tools = [rustc_toolchain.all_files]
368370

369371
env = {}
370372

@@ -380,19 +382,19 @@ def _cargo_build_script_impl(ctx):
380382
if use_default_shell_env:
381383
env.update(ctx.configuration.default_shell_env)
382384

383-
if toolchain.cargo:
384-
env["CARGO"] = "${pwd}/%s" % toolchain.cargo.path
385+
if cargo_toolchain.cargo:
386+
env["CARGO"] = "${pwd}/%s" % cargo_toolchain.cargo.path
385387

386388
env.update({
387389
"CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
388390
"CARGO_MANIFEST_DIR": manifest_dir,
389391
"CARGO_PKG_NAME": pkg_name,
390-
"HOST": toolchain.exec_triple.str,
392+
"HOST": rustc_toolchain.exec_triple.str,
391393
"NUM_JOBS": "1",
392394
"OPT_LEVEL": compilation_mode_opt_level,
393-
"RUSTC": toolchain.rustc.path,
394-
"RUSTDOC": toolchain.rust_doc.path,
395-
"TARGET": toolchain.target_flag_value,
395+
"RUSTC": rustc_toolchain.rustc.path,
396+
"RUSTDOC": rustc_toolchain.rust_doc.path,
397+
"TARGET": rustc_toolchain.target_flag_value,
396398
# OUT_DIR is set by the runner itself, rather than on the action.
397399
})
398400

@@ -414,7 +416,7 @@ def _cargo_build_script_impl(ctx):
414416
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
415417
# We hope that the linker env is sufficient for the whole cc_toolchain.
416418
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
417-
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", toolchain, cc_toolchain, feature_configuration, None)
419+
linker, _, link_args, linker_env = get_linker_and_args(ctx, "bin", rustc_toolchain, cc_toolchain, feature_configuration, None)
418420
env.update(**linker_env)
419421
env["LD"] = linker
420422
env["LDFLAGS"] = " ".join(_pwd_flags(link_args))
@@ -477,7 +479,7 @@ def _cargo_build_script_impl(ctx):
477479
# https://github.com/rust-lang/cargo/issues/9600
478480
env["CARGO_ENCODED_RUSTFLAGS"] = "\\x1f".join([
479481
# Allow build scripts to locate the generated sysroot
480-
"--sysroot=${{pwd}}/{}".format(toolchain.sysroot),
482+
"--sysroot=${{pwd}}/{}".format(rustc_toolchain.sysroot),
481483
] + ctx.attr.rustc_flags)
482484

483485
for f in ctx.attr.crate_features:
@@ -488,7 +490,7 @@ def _cargo_build_script_impl(ctx):
488490
env["CARGO_MANIFEST_LINKS"] = links
489491

490492
# Add environment variables from the Rust toolchain.
491-
env.update(toolchain.env)
493+
env.update(rustc_toolchain.env)
492494

493495
known_variables = {}
494496

@@ -526,7 +528,7 @@ def _cargo_build_script_impl(ctx):
526528
direct = [
527529
script,
528530
ctx.executable._cargo_build_script_runner,
529-
] + fallback_tools + ([toolchain.target_json] if toolchain.target_json else []),
531+
] + fallback_tools + ([rustc_toolchain.target_json] if rustc_toolchain.target_json else []),
530532
transitive = script_data + script_tools + toolchain_tools,
531533
)
532534

@@ -772,7 +774,8 @@ cargo_build_script = rule(
772774
},
773775
fragments = ["cpp"],
774776
toolchains = [
775-
str(Label("//rust:toolchain_type")),
777+
config_common.toolchain_type("@rust_toolchains//cargo:toolchain_type", mandatory = False),
778+
str(Label("@rust_toolchains//rustc:toolchain_type")),
776779
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
777780
],
778781
)

rust/BUILD.bazel

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ exports_files([
99
"toolchain.bzl",
1010
])
1111

12-
toolchain_type(
12+
alias(
1313
name = "toolchain_type",
14+
actual = "@rust_toolchains//rustc:toolchain_type",
15+
deprecation = "instead use `@rust_toolchains//rustc:toolchain_type`",
16+
tags = ["manual"],
1417
)
1518

1619
alias(
1720
name = "toolchain",
1821
actual = "toolchain_type",
19-
deprecation = "instead use `@rules_rust//rust:toolchain_type`",
22+
deprecation = "instead use `@rust_toolchains//rustc:toolchain_type`",
2023
tags = ["manual"],
2124
)
2225

rust/private/clippy.bzl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ load(
3434
"//rust/private:utils.bzl",
3535
"determine_output_hash",
3636
"find_cc_toolchain",
37+
"find_optional_toolchain",
3738
"find_toolchain",
3839
)
3940
load("//rust/settings:incompatible.bzl", "IncompatibleFlagInfo")
@@ -120,7 +121,16 @@ def rust_clippy_action(ctx, clippy_executable, process_wrapper, crate_info, conf
120121
Returns:
121122
None
122123
"""
123-
toolchain = find_toolchain(ctx)
124+
rustc_toolchain = find_toolchain(ctx)
125+
rustc_toolchain_type = "@rust_toolchains//rustc:toolchain_type"
126+
127+
clippy_toolchain = find_optional_toolchain(ctx, "@rust_toolchains//clippy:toolchain_type")
128+
if clippy_toolchain:
129+
clippy_toolchain_type = "@rust_toolchains//clippy:toolchain_type"
130+
else:
131+
clippy_toolchain_type = rustc_toolchain_type
132+
clippy_toolchain = rustc_toolchain
133+
124134
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
125135

126136
dep_info, build_info, _ = collect_deps(
@@ -146,7 +156,7 @@ def rust_clippy_action(ctx, clippy_executable, process_wrapper, crate_info, conf
146156
ctx.rule.files,
147157
# Clippy doesn't need to invoke transitive linking, therefore doesn't need linkstamps.
148158
depset([]),
149-
toolchain,
159+
rustc_toolchain,
150160
cc_toolchain,
151161
feature_configuration,
152162
crate_info,
@@ -164,7 +174,7 @@ def rust_clippy_action(ctx, clippy_executable, process_wrapper, crate_info, conf
164174
ctx = ctx,
165175
attr = ctx.rule.attr,
166176
file = ctx.file,
167-
toolchain = toolchain,
177+
toolchain = rustc_toolchain,
168178
tool_path = clippy_executable.path,
169179
cc_toolchain = cc_toolchain,
170180
feature_configuration = feature_configuration,
@@ -232,7 +242,7 @@ def rust_clippy_action(ctx, clippy_executable, process_wrapper, crate_info, conf
232242
arguments = args.all,
233243
mnemonic = "Clippy",
234244
progress_message = "Clippy %{label}",
235-
toolchain = "@rules_rust//rust:toolchain_type",
245+
toolchain = clippy_toolchain_type,
236246
)
237247

238248
def _clippy_aspect_impl(target, ctx):
@@ -246,7 +256,8 @@ def _clippy_aspect_impl(target, ctx):
246256
if not crate_info:
247257
return [ClippyInfo(output = depset([]))]
248258

249-
toolchain = find_toolchain(ctx)
259+
clippy_toolchain = find_optional_toolchain(ctx, "@rust_toolchains//clippy:toolchain_type") or find_toolchain(ctx)
260+
toolchain = clippy_toolchain or find_toolchain(ctx)
250261

251262
# For remote execution purposes, the clippy_out file must be a sibling of crate_info.output
252263
# or rustc may fail to create intermediate output files because the directory does not exist.
@@ -359,7 +370,8 @@ rust_clippy_aspect = aspect(
359370
[rust_common.test_crate_info],
360371
],
361372
toolchains = [
362-
str(Label("//rust:toolchain_type")),
373+
config_common.toolchain_type("@rust_toolchains//clippy:toolchain_type", mandatory = False),
374+
str(Label("@rust_toolchains//rustc:toolchain_type")),
363375
config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False),
364376
],
365377
implementation = _clippy_aspect_impl,

rust/private/toolchain_utils.bzl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
"""A module defining toolchain utilities"""
1+
"""A module defining toolchain utilities."""
2+
3+
def _find_rustc_toolchain(ctx):
4+
return ctx.toolchains[str(Label("@rust_toolchains//rustc:toolchain_type"))]
25

36
def _toolchain_files_impl(ctx):
4-
toolchain = ctx.toolchains[str(Label("//rust:toolchain_type"))]
7+
rustc_toolchain = _find_rustc_toolchain(ctx)
8+
clippy_toolchain_label = str(Label("@rust_toolchains//clippy:toolchain_type"))
9+
clippy_toolchain = ctx.toolchains[clippy_toolchain_label] if clippy_toolchain_label in ctx.toolchains else None
10+
cargo_toolchain_label = str(Label("@rust_toolchains//cargo:toolchain_type"))
11+
cargo_toolchain = ctx.toolchains[cargo_toolchain_label] if cargo_toolchain_label in ctx.toolchains else None
12+
13+
if ctx.attr.tool == "clippy" or ctx.attr.tool == "cargo-clippy":
14+
toolchain = clippy_toolchain or rustc_toolchain
15+
elif ctx.attr.tool == "cargo":
16+
toolchain = cargo_toolchain or rustc_toolchain
17+
else:
18+
toolchain = rustc_toolchain
519

620
runfiles = None
721
if ctx.attr.tool == "cargo":
@@ -84,12 +98,14 @@ toolchain_files = rule(
8498
),
8599
},
86100
toolchains = [
87-
str(Label("//rust:toolchain_type")),
101+
str(Label("@rust_toolchains//rustc:toolchain_type")),
102+
config_common.toolchain_type("@rust_toolchains//clippy:toolchain_type", mandatory = False),
103+
config_common.toolchain_type("@rust_toolchains//cargo:toolchain_type", mandatory = False),
88104
],
89105
)
90106

91107
def _current_rust_toolchain_impl(ctx):
92-
toolchain = ctx.toolchains[str(Label("@rules_rust//rust:toolchain_type"))]
108+
toolchain = _find_rustc_toolchain(ctx)
93109

94110
return [
95111
toolchain,
@@ -103,7 +119,7 @@ current_rust_toolchain = rule(
103119
doc = "A rule for exposing the current registered `rust_toolchain`.",
104120
implementation = _current_rust_toolchain_impl,
105121
toolchains = [
106-
str(Label("@rules_rust//rust:toolchain_type")),
122+
str(Label("@rust_toolchains//rustc:toolchain_type")),
107123
],
108124
)
109125

rust/private/utils.bzl

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,42 @@ def parse_env_strings(entries):
6060
return env_vars
6161

6262
def find_toolchain(ctx):
63-
"""Finds the first rust toolchain that is configured.
63+
"""Finds the first rustc toolchain that is configured.
6464
6565
Args:
6666
ctx (ctx): The ctx object for the current target.
6767
6868
Returns:
69-
rust_toolchain: A Rust toolchain context.
69+
rust_toolchain: A Rustc toolchain context.
7070
"""
71-
return ctx.toolchains[Label("//rust:toolchain_type")]
71+
rustc_toolchain_type = str(Label("@rust_toolchains//rustc:toolchain_type"))
72+
if rustc_toolchain_type in ctx.toolchains:
73+
return ctx.toolchains[rustc_toolchain_type]
74+
75+
# Keep compatibility for rules that still request the legacy type label.
76+
legacy_toolchain_type = str(Label("//rust:toolchain_type"))
77+
if legacy_toolchain_type in ctx.toolchains:
78+
return ctx.toolchains[legacy_toolchain_type]
79+
80+
fail(
81+
"No Rust toolchain configured. Expected @rust_toolchains//rustc:toolchain_type " +
82+
"or //rust:toolchain_type.",
83+
)
84+
85+
def find_optional_toolchain(ctx, toolchain_type):
86+
"""Finds the configured toolchain for a given type if it exists.
87+
88+
Args:
89+
ctx (ctx): The ctx object for the current target.
90+
toolchain_type (str): The toolchain type label.
91+
92+
Returns:
93+
rust_toolchain or None: The toolchain context or None if unavailable.
94+
"""
95+
toolchain_label = str(Label(toolchain_type))
96+
if toolchain_label in ctx.toolchains:
97+
return ctx.toolchains[toolchain_label]
98+
return None
7299

73100
# A global kill switch to test without a cc toolchain present.
74101
_FORCE_DISABLE_CC_TOOLCHAIN = False

rust/rust_analyzer/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package(default_visibility = ["//visibility:public"])
22

3-
toolchain_type(
3+
alias(
44
name = "toolchain_type",
5+
actual = "@rust_toolchains//rust_analyzer:toolchain_type",
6+
deprecation = "instead use `@rust_toolchains//rust_analyzer:toolchain_type`",
7+
tags = ["manual"],
58
)

rust/rustfmt/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package(default_visibility = ["//visibility:public"])
22

3-
toolchain_type(
3+
alias(
44
name = "toolchain_type",
5+
actual = "@rust_toolchains//rustfmt:toolchain_type",
6+
deprecation = "instead use `@rust_toolchains//rustfmt:toolchain_type`",
7+
tags = ["manual"],
58
)

rust_toolchains/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package(default_visibility = ["//visibility:public"])

rust_toolchains/MODULE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module(
2+
name = "rust_toolchains",
3+
version = "0.0.1",
4+
)

0 commit comments

Comments
 (0)