Skip to content

Commit d9ba374

Browse files
authored
process_wrapper: replace C++ implementation with rust. (#1159)
1 parent e5fefdc commit d9ba374

16 files changed

+818
-987
lines changed

rust/private/rustc.bzl

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -922,20 +922,41 @@ 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+
# This uses startswith as on windows the basename will be process_wrapper_fake.exe.
926+
if not ctx.executable._process_wrapper.basename.startswith("process_wrapper_fake"):
927+
# Run as normal
928+
ctx.actions.run(
929+
executable = ctx.executable._process_wrapper,
930+
inputs = compile_inputs,
931+
outputs = action_outputs,
932+
env = env,
933+
arguments = args.all,
934+
mnemonic = "Rustc",
935+
progress_message = "Compiling Rust {} {}{} ({} files)".format(
936+
crate_info.type,
937+
ctx.label.name,
938+
formatted_version,
939+
len(crate_info.srcs.to_list()),
940+
),
941+
)
942+
else:
943+
# Run without process_wrapper
944+
if build_env_files or build_flags_files or stamp:
945+
fail("build_env_files, build_flags_files, stamp are not supported if use_process_wrapper is False")
946+
ctx.actions.run(
947+
executable = toolchain.rustc,
948+
inputs = compile_inputs,
949+
outputs = action_outputs,
950+
env = env,
951+
arguments = [args.rustc_flags],
952+
mnemonic = "Rustc",
953+
progress_message = "Compiling Rust (without process_wrapper) {} {}{} ({} files)".format(
954+
crate_info.type,
955+
ctx.label.name,
956+
formatted_version,
957+
len(crate_info.srcs.to_list()),
958+
),
959+
)
939960

940961
runfiles = ctx.runfiles(
941962
files = getattr(ctx.files, "data", []),

rust/private/transitions.bzl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,48 @@ with_import_macro_bootstrapping_mode = rule(
6464
),
6565
},
6666
)
67+
68+
def _without_process_wrapper_transition_impl(_settings, _attr):
69+
"""This transition allows rust_* rules to invoke rustc without process_wrapper."""
70+
return {
71+
"//rust/settings:use_process_wrapper": False,
72+
}
73+
74+
without_process_wrapper_transition = transition(
75+
implementation = _without_process_wrapper_transition_impl,
76+
inputs = [],
77+
outputs = ["//rust/settings:use_process_wrapper"],
78+
)
79+
80+
def _without_process_wrapper_impl(ctx):
81+
executable = ctx.executable.target
82+
link_name = ctx.label.name
83+
84+
# Append .exe if on windows
85+
if executable.extension:
86+
link_name = link_name + "." + executable.extension
87+
link = ctx.actions.declare_file(link_name)
88+
ctx.actions.symlink(
89+
output = link,
90+
target_file = executable,
91+
)
92+
return [
93+
DefaultInfo(
94+
executable = link,
95+
),
96+
]
97+
98+
without_process_wrapper = rule(
99+
implementation = _without_process_wrapper_impl,
100+
attrs = {
101+
"target": attr.label(
102+
cfg = without_process_wrapper_transition,
103+
allow_single_file = True,
104+
mandatory = True,
105+
executable = True,
106+
),
107+
"_allowlist_function_transition": attr.label(
108+
default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"),
109+
),
110+
},
111+
)

rust/settings/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ bool_flag(
2929
build_setting_default = False,
3030
)
3131

32+
# A flag that enables/disables the use of process_wrapper when invoking rustc.
33+
# This is useful for building process_wrapper itself without causing dependency cycles.
34+
bool_flag(
35+
name = "use_process_wrapper",
36+
build_setting_default = True,
37+
)
38+
3239
bzl_library(
3340
name = "bzl_lib",
3441
srcs = glob(["**/*.bzl"]),

util/process_wrapper/BUILD.bazel

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,45 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
2+
load("//rust:defs.bzl", "rust_binary", "rust_test")
23

3-
cc_binary(
4+
# buildifier: disable=bzl-visibility
5+
load("//rust/private:transitions.bzl", "without_process_wrapper")
6+
7+
alias(
48
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": [],
9+
actual = select({
10+
# This will never get used, it's only here to break the circular dependency to allow building process_wrapper
11+
":use_fake_process_wrapper": ":process_wrapper_fake",
12+
"//conditions:default": ":process_wrapper_impl",
2413
}),
2514
visibility = ["//visibility:public"],
2615
)
16+
17+
cc_binary(
18+
name = "process_wrapper_fake",
19+
srcs = ["fake.cc"],
20+
)
21+
22+
config_setting(
23+
name = "use_fake_process_wrapper",
24+
flag_values = {
25+
"//rust/settings:use_process_wrapper": "False",
26+
},
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+
)
41+
42+
rust_test(
43+
name = "process_wrapper_test",
44+
crate = ":process_wrapper_bin",
45+
)

util/process_wrapper/fake.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
// Noop on purpose.
3+
return 0;
4+
}

0 commit comments

Comments
 (0)