Skip to content

Commit a010585

Browse files
committed
process_wrapper: replace C++ implementation with rust.
The main advantages of this work are that it eliminates a bunch of platform-specific C++ code in favor of the Rust standard library (mainly std::process::Command and std::fs::File) and it simplifies future improvements in the area of pipelining and incremental compilation. Building a Rust process_wrapper requires a way to use rust_binary without support from process_wrapper itself. This is achieved through a transition in //util/process_wrapper/transitions.bzl.
1 parent 5d9a74d commit a010585

File tree

12 files changed

+731
-988
lines changed

12 files changed

+731
-988
lines changed

rust/private/rustc.bzl

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -922,20 +922,40 @@ def rustc_compile_action(
922922
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM")
923923
action_outputs.append(dsym_folder)
924924

925-
ctx.actions.run(
926-
executable = ctx.executable._process_wrapper,
927-
inputs = compile_inputs,
928-
outputs = action_outputs,
929-
env = env,
930-
arguments = args.all,
931-
mnemonic = "Rustc",
932-
progress_message = "Compiling Rust {} {}{} ({} files)".format(
933-
crate_info.type,
934-
ctx.label.name,
935-
formatted_version,
936-
len(crate_info.srcs.to_list()),
937-
),
938-
)
925+
if ctx.executable._process_wrapper.basename != "process_wrapper_fake":
926+
# Run as normal
927+
ctx.actions.run(
928+
executable = ctx.executable._process_wrapper,
929+
inputs = compile_inputs,
930+
outputs = action_outputs,
931+
env = env,
932+
arguments = args.all,
933+
mnemonic = "Rustc",
934+
progress_message = "Compiling Rust {} {}{} ({} files)".format(
935+
crate_info.type,
936+
ctx.label.name,
937+
formatted_version,
938+
len(crate_info.srcs.to_list()),
939+
),
940+
)
941+
else:
942+
# Run without process_wrapper
943+
if build_env_files or build_flags_files or stamp:
944+
fail("build_env_files, build_flags_files, stamp are not supported if use_process_wrapper is False")
945+
ctx.actions.run(
946+
executable = toolchain.rustc,
947+
inputs = compile_inputs,
948+
outputs = action_outputs,
949+
env = env,
950+
arguments = [args.rustc_flags],
951+
mnemonic = "Rustc",
952+
progress_message = "Compiling Rust (without process_wrapper) {} {}{} ({} files)".format(
953+
crate_info.type,
954+
ctx.label.name,
955+
formatted_version,
956+
len(crate_info.srcs.to_list()),
957+
),
958+
)
939959

940960
runfiles = ctx.runfiles(
941961
files = getattr(ctx.files, "data", []),

util/process_wrapper/BUILD.bazel

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
load("@rules_cc//cc:defs.bzl", "cc_binary")
1+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
2+
load("//rust:defs.bzl", "rust_binary", "rust_test")
3+
load("//util/process_wrapper:transitions.bzl", "without_process_wrapper")
24

3-
cc_binary(
5+
alias(
46
name = "process_wrapper",
5-
srcs = [
6-
"process_wrapper.cc",
7-
"system.h",
8-
"utils.h",
9-
"utils.cc",
10-
] + select({
11-
"@bazel_tools//src/conditions:host_windows": [
12-
"system_windows.cc",
13-
],
14-
"//conditions:default": [
15-
"system_posix.cc",
16-
],
17-
}),
18-
defines = [] + select({
19-
"@bazel_tools//src/conditions:host_windows": [
20-
"UNICODE",
21-
"_UNICODE",
22-
],
23-
"//conditions:default": [],
7+
actual = select({
8+
# This will never get used, it's only here to break the circular dependency to allow building process_wrapper
9+
":no_process_wrapper": ":process_wrapper_fake",
10+
"//conditions:default": ":process_wrapper_impl",
2411
}),
2512
visibility = ["//visibility:public"],
2613
)
14+
15+
config_setting(
16+
name = "no_process_wrapper",
17+
flag_values = {
18+
":use_process_wrapper": "False",
19+
},
20+
)
21+
22+
# A flag that enables/disables the use of process_wrapper when invoking rustc.
23+
# This is useful for building process_wrapper itself without causing dependency cycles.
24+
bool_flag(
25+
name = "use_process_wrapper",
26+
build_setting_default = True,
27+
)
28+
29+
# Changing the name of this rule requires a corresponding
30+
# change in //rust/private/rustc.bzl:925
31+
without_process_wrapper(
32+
name = "process_wrapper_impl",
33+
target = ":process_wrapper_bin",
34+
visibility = ["//visibility:public"],
35+
)
36+
37+
rust_binary(
38+
name = "process_wrapper_bin",
39+
srcs = glob(["*.rs"]),
40+
stamp = 0, # Required for tests targeting this not to fail.
41+
)
42+
43+
rust_test(
44+
name = "process_wrapper_test",
45+
srcs = glob(["*.rs"]),
46+
crate = "process_wrapper",
47+
deps = [":process_wrapper_impl"],
48+
)

0 commit comments

Comments
 (0)