Skip to content

Commit

Permalink
process_wrapper: replace C++ implementation with rust.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gigaroby committed Mar 1, 2022
1 parent 5d9a74d commit 8065ee5
Show file tree
Hide file tree
Showing 12 changed files with 746 additions and 988 deletions.
48 changes: 34 additions & 14 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -922,20 +922,40 @@ def rustc_compile_action(
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM")
action_outputs.append(dsym_folder)

ctx.actions.run(
executable = ctx.executable._process_wrapper,
inputs = compile_inputs,
outputs = action_outputs,
env = env,
arguments = args.all,
mnemonic = "Rustc",
progress_message = "Compiling Rust {} {}{} ({} files)".format(
crate_info.type,
ctx.label.name,
formatted_version,
len(crate_info.srcs.to_list()),
),
)
if ctx.executable._process_wrapper.basename != "process_wrapper_fake":
# Run as normal
ctx.actions.run(
executable = ctx.executable._process_wrapper,
inputs = compile_inputs,
outputs = action_outputs,
env = env,
arguments = args.all,
mnemonic = "Rustc",
progress_message = "Compiling Rust {} {}{} ({} files)".format(
crate_info.type,
ctx.label.name,
formatted_version,
len(crate_info.srcs.to_list()),
),
)
else:
# Run without process_wrapper
if build_env_files or build_flags_files or stamp:
fail("build_env_files, build_flags_files, stamp are not supported if use_process_wrapper is False")
ctx.actions.run(
executable = toolchain.rustc,
inputs = compile_inputs,
outputs = action_outputs,
env = env,
arguments = [args.rustc_flags],
mnemonic = "Rustc",
progress_message = "Compiling Rust (without process_wrapper) {} {}{} ({} files)".format(
crate_info.type,
ctx.label.name,
formatted_version,
len(crate_info.srcs.to_list()),
),
)

runfiles = ctx.runfiles(
files = getattr(ctx.files, "data", []),
Expand Down
62 changes: 41 additions & 21 deletions util/process_wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//rust:defs.bzl", "rust_binary", "rust_test")
load("//util/process_wrapper:transitions.bzl", "without_process_wrapper")

cc_binary(
alias(
name = "process_wrapper",
srcs = [
"process_wrapper.cc",
"system.h",
"utils.h",
"utils.cc",
] + select({
"@bazel_tools//src/conditions:host_windows": [
"system_windows.cc",
],
"//conditions:default": [
"system_posix.cc",
],
}),
defines = [] + select({
"@bazel_tools//src/conditions:host_windows": [
"UNICODE",
"_UNICODE",
],
"//conditions:default": [],
actual = select({
# This will never get used, it's only here to break the circular dependency to allow building process_wrapper
":no_process_wrapper": ":process_wrapper_fake",
"//conditions:default": ":process_wrapper_impl",
}),
visibility = ["//visibility:public"],
)

config_setting(
name = "no_process_wrapper",
flag_values = {
":use_process_wrapper": "False",
},
)

# A flag that enables/disables the use of process_wrapper when invoking rustc.
# This is useful for building process_wrapper itself without causing dependency cycles.
bool_flag(
name = "use_process_wrapper",
build_setting_default = True,
)

# Changing the name of this rule requires a corresponding
# change in //rust/private/rustc.bzl:925
without_process_wrapper(
name = "process_wrapper_impl",
target = ":process_wrapper_bin",
visibility = ["//visibility:public"],
)

rust_binary(
name = "process_wrapper_bin",
srcs = glob(["*.rs"]),
)

rust_test(
name = "process_wrapper_test",
srcs = glob(["*.rs"]),
crate = "process_wrapper_bin",
)
Loading

0 comments on commit 8065ee5

Please sign in to comment.