Skip to content

Commit 711df10

Browse files
committed
rules_rust: enable pipelined compilation.
Pipelined compilation allows better parallelism during builds as it allows libraries to generate lightweight metadata files to unlock other depencies. These metadata files (.rmeta) can only be used to unlock library -> library dependencies and do not affect builds in any other way. This is currently the default in cargo: https://internals.rust-lang.org/t/evaluating-pipelined-rustc-compilation/10199
1 parent 34fd467 commit 711df10

File tree

12 files changed

+317
-73
lines changed

12 files changed

+317
-73
lines changed

rust/private/common.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def _create_crate_info(**kwargs):
4848
"""
4949
if not "wrapped_crate_type" in kwargs:
5050
kwargs.update({"wrapped_crate_type": None})
51+
if not "metadata" in kwargs:
52+
kwargs.update({"metadata": None})
5153
return CrateInfo(**kwargs)
5254

5355
rust_common = struct(

rust/private/providers.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ CrateInfo = provider(
2424
"is_test": "bool: If the crate is being compiled in a test context",
2525
"name": "str: The name of this crate.",
2626
"output": "File: The output File that will be produced, depends on crate type.",
27+
"metadata": "File: The rmeta file produced for this crate. It is optional.",
2728
"owner": "Label: The label of the target that produced this CrateInfo",
2829
"proc_macro_deps": "depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers.",
2930
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
@@ -48,6 +49,7 @@ DepInfo = provider(
4849
"link_search_path_files": "depset[File]: All transitive files containing search paths to pass to the linker",
4950
"transitive_build_infos": "depset[BuildInfo]",
5051
"transitive_crate_outputs": "depset[File]: All transitive crate outputs.",
52+
"transitive_metadata_outputs": "depset[File]: All transitive crate metadata outputs.",
5153
"transitive_crates": "depset[CrateInfo]",
5254
"transitive_noncrates": "depset[LinkerInput]: All transitive dependencies that aren't crates.",
5355
},

rust/private/rust.bzl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load(
2626
"name_to_crate_name",
2727
"transform_deps",
2828
)
29-
29+
load("@bazel_skylib//lib:paths.bzl", "paths")
3030
# TODO(marco): Separate each rule into its own file.
3131

3232
def _assert_no_deprecated_attributes(_ctx):
@@ -263,6 +263,19 @@ def _rust_library_common(ctx, crate_type):
263263
output_hash,
264264
)
265265
rust_lib = ctx.actions.declare_file(rust_lib_name)
266+
rust_metadata = None
267+
268+
# We can only build metadata iif
269+
# - Pipelined compilation is enabled,
270+
# - we are building with process wrapper, and
271+
# - the current crate is a lib or rlib
272+
can_build_metadata = toolchain._pipelined_compilation and ctx.attr._process_wrapper and crate_type in ("rlib", "lib")
273+
# can_build_metadata = ctx.attr._process_wrapper and crate_type in ("rlib", "lib")
274+
if can_build_metadata:
275+
rust_metadata = ctx.actions.declare_file(
276+
paths.replace_extension(rust_lib_name, ".rmeta"),
277+
sibling = rust_lib,
278+
)
266279

267280
deps = transform_deps(ctx.attr.deps)
268281
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
@@ -280,6 +293,7 @@ def _rust_library_common(ctx, crate_type):
280293
proc_macro_deps = depset(proc_macro_deps),
281294
aliases = ctx.attr.aliases,
282295
output = rust_lib,
296+
metadata = rust_metadata,
283297
edition = get_edition(ctx.attr, toolchain),
284298
rustc_env = ctx.attr.rustc_env,
285299
is_test = False,

0 commit comments

Comments
 (0)