Skip to content

Persistent worker in C++ #667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ tasks:
working_directory: examples
test_targets:
- //...
examples-worker:
name: Examples Persistent Worker
platform: ubuntu1804
working_directory: examples
build_flags:
- "--@rules_rust//rust:experimental-use-worker"
- "--worker_verbose"
test_flags:
- "--@rules_rust//rust:experimental-use-worker"
- "--worker_verbose"
test_targets:
- //...
docs_linux:
name: Docs
platform: ubuntu1804
Expand Down
15 changes: 15 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,18 @@ bazel build @examples//hello_world_wasm --platforms=@rules_rust//rust/platform:w

`rust_wasm_bindgen` will automatically transition to the `wasm` platform and can be used when
building WebAssembly code for the host target.

## Persistent Worker support

The rules ship with a persistent worker implementation that uses Rust's [incremental compilation](https://doc.rust-lang.org/edition-guide/rust-2018/the-compiler/incremental-compilation-for-faster-compiles.html) for faster build times when iterating on code.

To enable this:

Enable the protobuf rules by adding this to your WORKSPACE

```
load("@rules_rust//proto:repositories.bzl", "rust_proto_repositories")
rust_proto_repositories()
```

In your build command, use the flag `--@rules_rust//rust:experimental-use-worker`. Optionally you can add this flag to your `.bazelrc` to always use this.
6 changes: 6 additions & 0 deletions rust/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

package(default_visibility = ["//visibility:public"])

Expand All @@ -22,3 +23,8 @@ bzl_library(
"//rust/private:rules",
],
)

bool_flag(
name = "experimental-use-worker",
build_setting_default = False,
)
7 changes: 7 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,19 @@ _common_attrs = {
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
"_error_format": attr.label(default = "//:error_format"),
"_persistent_worker": attr.label(
default = Label("//util/worker"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
"_process_wrapper": attr.label(
default = Label("//util/process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
"_use_worker": attr.label(default = Label("//rust:experimental-use-worker")),
}

_rust_test_attrs = {
Expand Down
30 changes: 28 additions & 2 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

# buildifier: disable=module-docstring
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load(
"@bazel_tools//tools/build_defs/cc:action_names.bzl",
"CPP_LINK_EXECUTABLE_ACTION_NAME",
Expand Down Expand Up @@ -55,6 +56,9 @@ ErrorFormatInfo = provider(
fields = {"error_format": "(string) [" + ", ".join(_error_format_values) + "]"},
)

def _use_worker(ctx):
return hasattr(ctx.attr, "_use_worker") and ctx.attr._use_worker[BuildSettingInfo].value

def _get_rustc_env(ctx, toolchain):
"""Gathers rustc environment variables

Expand Down Expand Up @@ -355,6 +359,9 @@ def construct_arguments(

# Wrapper args first
args = ctx.actions.args()
if _use_worker(ctx):
args.set_param_file_format("multiline")
args.use_param_file("@%s", use_always = True)

for build_env_file in build_env_files:
args.add("--env-file", build_env_file)
Expand Down Expand Up @@ -554,19 +561,38 @@ def rustc_compile_action(
else:
formatted_version = ""

executable = ctx.executable._process_wrapper
tools = []
arguments = [args]
execution_requirements = {}
if _use_worker(ctx):
tools = [executable]
arguments = [
"--compiler", executable.path,
"--compilation_mode", ctx.var["COMPILATION_MODE"],
args,
]
executable = ctx.executable._persistent_worker
execution_requirements = {
"requires-worker-protocol": "proto",
"supports-workers": "1",
}

ctx.actions.run(
executable = ctx.executable._process_wrapper,
executable = executable,
inputs = compile_inputs,
outputs = [crate_info.output],
env = env,
arguments = [args],
tools = tools,
arguments = arguments,
mnemonic = "Rustc",
progress_message = "Compiling Rust {} {}{} ({} files)".format(
crate_info.type,
ctx.label.name,
formatted_version,
len(crate_info.srcs.to_list()),
),
execution_requirements = execution_requirements,
)

dylibs = [get_preferred_artifact(lib) for linker_input in dep_info.transitive_noncrates.to_list() for lib in linker_input.libraries if _is_dylib(lib)]
Expand Down
8 changes: 8 additions & 0 deletions util/process_wrapper/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ cc_binary(
}),
visibility = ["//visibility:public"],
)

exports_files([
"utils.h",
"utils.cc",
"system.h",
"system_windows.cc",
"system_posix.cc",
])
38 changes: 38 additions & 0 deletions util/worker/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_proto//proto:defs.bzl", "proto_library");

cc_binary(
name = "worker",
srcs = [
"worker.cc",
"//util/process_wrapper:system.h",
"//util/process_wrapper:utils.h",
"//util/process_wrapper:utils.cc",
] + select({
"@bazel_tools//src/conditions:host_windows": [
"//util/process_wrapper:system_windows.cc",
],
"//conditions:default": [
"//util/process_wrapper:system_posix.cc",
],
}),
deps = [":worker_cc_proto"],
defines = [] + select({
"@bazel_tools//src/conditions:host_windows": [
"UNICODE",
"_UNICODE",
],
"//conditions:default": [],
}),
visibility = ["//visibility:public"],
)

cc_proto_library(
name = "worker_cc_proto",
deps = [":worker_protocol"],
)

proto_library(
name = "worker_protocol",
srcs = ["worker_protocol.proto"],
)
Loading