Skip to content

Commit caf1b82

Browse files
committed
add the worker as a toolchain
1 parent 5942456 commit caf1b82

File tree

6 files changed

+86
-14
lines changed

6 files changed

+86
-14
lines changed

rust/private/rust.bzl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,6 @@ _rust_common_attrs = {
370370
allow_single_file = True,
371371
cfg = "exec",
372372
),
373-
"_process_wrapper_worker": attr.label(
374-
default = "@rustc_worker//file",
375-
# Actually we may want the persistent worker to be built by the rust rules themselves, but right now we are just going to use a binary.
376-
executable = True,
377-
allow_single_file = True,
378-
cfg = "exec",
379-
),
380373
}
381374

382375
_rust_library_attrs = {
@@ -412,6 +405,7 @@ rust_library = rule(
412405
toolchains = [
413406
"@io_bazel_rules_rust//rust:toolchain",
414407
"@bazel_tools//tools/cpp:toolchain_type",
408+
"@io_bazel_rules_rust//worker:toolchain_type",
415409
],
416410
doc = """
417411
Builds a Rust library crate.
@@ -502,6 +496,7 @@ rust_binary = rule(
502496
toolchains = [
503497
"@io_bazel_rules_rust//rust:toolchain",
504498
"@bazel_tools//tools/cpp:toolchain_type",
499+
"@io_bazel_rules_rust//worker:toolchain_type",
505500
],
506501
doc = """
507502
Builds a Rust binary crate.
@@ -600,6 +595,7 @@ rust_test = rule(
600595
toolchains = [
601596
"@io_bazel_rules_rust//rust:toolchain",
602597
"@bazel_tools//tools/cpp:toolchain_type",
598+
"@io_bazel_rules_rust//worker:toolchain_type",
603599
],
604600
doc = """
605601
Builds a Rust test crate.
@@ -748,6 +744,7 @@ rust_test_binary = rule(
748744
toolchains = [
749745
"@io_bazel_rules_rust//rust:toolchain",
750746
"@bazel_tools//tools/cpp:toolchain_type",
747+
"@io_bazel_rules_rust//worker:toolchain_type",
751748
],
752749
doc = """
753750
Builds a Rust test binary, without marking this rule as a Bazel test.
@@ -771,6 +768,7 @@ rust_benchmark = rule(
771768
toolchains = [
772769
"@io_bazel_rules_rust//rust:toolchain",
773770
"@bazel_tools//tools/cpp:toolchain_type",
771+
"@io_bazel_rules_rust//worker:toolchain_type",
774772
],
775773
doc = """
776774
Builds a Rust benchmark test.

rust/private/rustc.bzl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ def construct_arguments(
306306
out_dir,
307307
build_env_file,
308308
build_flags_files,
309-
maker_path):
309+
maker_path,
310+
use_worker=False):
310311
output_dir = getattr(crate_info.output, "dirname") if hasattr(crate_info.output, "dirname") else None
311312

312313
linker_script = getattr(file, "linker_script") if hasattr(file, "linker_script") else None
@@ -315,8 +316,10 @@ def construct_arguments(
315316

316317
# Wrapper args first
317318
args = ctx.actions.args()
318-
args.set_param_file_format("multiline")
319-
args.use_param_file("@%s", use_always=True)
319+
if use_worker:
320+
# Write the args to a param file that will be used by Bazel to send messages to the worker.
321+
args.set_param_file_format("multiline")
322+
args.use_param_file("@%s", use_always=True)
320323

321324
if build_env_file != None:
322325
args.add("--env-file", build_env_file)
@@ -443,6 +446,8 @@ def rustc_compile_action(
443446
- DepInfo: The transitive dependencies of this crate.
444447
- DefaultInfo: The output file for this crate, and its runfiles.
445448
"""
449+
worker_binary = ctx.toolchains["@io_bazel_rules_rust//worker:toolchain_type"].worker_binary
450+
446451
dep_info, build_info = collect_deps(
447452
ctx.label,
448453
crate_info.deps,
@@ -478,22 +483,36 @@ def rustc_compile_action(
478483
build_env_file,
479484
build_flags_files,
480485
maker_path = None,
486+
use_worker = worker_binary != None,
481487
)
482488

483489
if hasattr(ctx.attr, "version") and ctx.attr.version != "0.0.0":
484490
formatted_version = " v{}".format(ctx.attr.version)
485491
else:
486492
formatted_version = ""
487493

494+
if worker_binary != None:
495+
executable = worker_binary
496+
tools = [ctx.executable._process_wrapper]
497+
arguments = [ctx.executable._process_wrapper.path, toolchain.rustc.path, ctx.var["COMPILATION_MODE"], args]
498+
execution_requirements = { "supports-workers": "1" }
499+
else:
500+
# Not all execution platforms support a worker.
501+
executable = ctx.executable._process_wrapper
502+
tools = []
503+
arguments = [args]
504+
execution_requirements = {}
505+
# TODO: Select if worker is to be enabled.
506+
488507
ctx.actions.run(
489-
executable = ctx.executable._process_wrapper_worker,
508+
executable = executable,
490509
inputs = compile_inputs,
491510
outputs = [crate_info.output],
492-
tools = [ctx.executable._process_wrapper],
511+
tools = tools,
493512
env = env,
494-
arguments = [ctx.executable._process_wrapper.path, toolchain.rustc.path, ctx.var["COMPILATION_MODE"], args],
513+
arguments = arguments,
495514
mnemonic = "Rustc",
496-
execution_requirements = { "supports-workers": "1" },
515+
execution_requirements = execution_requirements,
497516
progress_message = "Compiling Rust {} {}{} ({} files)".format(
498517
crate_info.type,
499518
ctx.label.name,

rust/repositories.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ load(":known_shas.bzl", "FILE_KEY_TO_SHA")
22
load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext", "system_to_dylib_ext", "system_to_staticlib_ext", "system_to_stdlib_linkflags", "triple_to_constraint_set", "triple_to_system")
33
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
44
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
5+
load("//worker:repositories.bzl", "rust_worker_repositories")
56

67
DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"
78

@@ -93,6 +94,8 @@ def rust_repositories(
9394
sha256s = sha256s,
9495
)
9596

97+
rust_worker_repositories()
98+
9699
def _check_version_valid(version, iso_date, param_prefix = ""):
97100
"""Verifies that the provided rust version and iso_date make sense."""
98101

worker/BUILD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("//worker:toolchain.bzl", "worker_toolchain")
2+
3+
toolchain_type(name = "toolchain_type")
4+
5+
worker_toolchain(
6+
name = "worker_linux_x86_64",
7+
worker_binary = "@rust_worker_linux_x86_64//file",
8+
)
9+
10+
worker_toolchain(name = "worker_dummy")
11+
12+
toolchain(
13+
name = "linux_x86_64",
14+
exec_compatible_with = [
15+
"@platforms//os:linux",
16+
"@platforms//cpu:x86_64",
17+
],
18+
toolchain = ":worker_linux_x86_64",
19+
toolchain_type = ":toolchain_type",
20+
)
21+
22+
toolchain(
23+
name = "dummy",
24+
toolchain = ":worker_dummy",
25+
toolchain_type = ":toolchain_type",
26+
)

worker/repositories.bzl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
2+
3+
def rust_worker_repositories():
4+
http_file(
5+
name = "rust_worker_linux_x86_64",
6+
executable = True,
7+
sha256 = "e3383d062752bcbeeb8a97d36bba0dea244db77b45a083bee718978a0deb5d37",
8+
urls = ["https://github.com/nikhilm/rustc-worker/releases/download/v0.2.0/rustc-worker-linux_x86_64"],
9+
)
10+
11+
native.register_toolchains(
12+
"@io_bazel_rules_rust//worker:linux_x86_64",
13+
"@io_bazel_rules_rust//worker:dummy",
14+
)

worker/toolchain.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def _worker_toolchain_impl(ctx):
2+
toolchain_info = platform_common.ToolchainInfo(
3+
worker_binary = ctx.executable.worker_binary,
4+
)
5+
return [toolchain_info]
6+
7+
worker_toolchain = rule(
8+
implementation = _worker_toolchain_impl,
9+
attrs = {
10+
"worker_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec"),
11+
},
12+
)

0 commit comments

Comments
 (0)