Skip to content

Commit

Permalink
rules_rust: enable pipelined compilation.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gigaroby committed Apr 20, 2022
1 parent 34fd467 commit 711df10
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 73 deletions.
2 changes: 2 additions & 0 deletions rust/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def _create_crate_info(**kwargs):
"""
if not "wrapped_crate_type" in kwargs:
kwargs.update({"wrapped_crate_type": None})
if not "metadata" in kwargs:
kwargs.update({"metadata": None})
return CrateInfo(**kwargs)

rust_common = struct(
Expand Down
2 changes: 2 additions & 0 deletions rust/private/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CrateInfo = provider(
"is_test": "bool: If the crate is being compiled in a test context",
"name": "str: The name of this crate.",
"output": "File: The output File that will be produced, depends on crate type.",
"metadata": "File: The rmeta file produced for this crate. It is optional.",
"owner": "Label: The label of the target that produced this CrateInfo",
"proc_macro_deps": "depset[DepVariantInfo]: This crate's rust proc_macro dependencies' providers.",
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
Expand All @@ -48,6 +49,7 @@ DepInfo = provider(
"link_search_path_files": "depset[File]: All transitive files containing search paths to pass to the linker",
"transitive_build_infos": "depset[BuildInfo]",
"transitive_crate_outputs": "depset[File]: All transitive crate outputs.",
"transitive_metadata_outputs": "depset[File]: All transitive crate metadata outputs.",
"transitive_crates": "depset[CrateInfo]",
"transitive_noncrates": "depset[LinkerInput]: All transitive dependencies that aren't crates.",
},
Expand Down
16 changes: 15 additions & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ load(
"name_to_crate_name",
"transform_deps",
)

load("@bazel_skylib//lib:paths.bzl", "paths")
# TODO(marco): Separate each rule into its own file.

def _assert_no_deprecated_attributes(_ctx):
Expand Down Expand Up @@ -263,6 +263,19 @@ def _rust_library_common(ctx, crate_type):
output_hash,
)
rust_lib = ctx.actions.declare_file(rust_lib_name)
rust_metadata = None

# We can only build metadata iif
# - Pipelined compilation is enabled,
# - we are building with process wrapper, and
# - the current crate is a lib or rlib
can_build_metadata = toolchain._pipelined_compilation and ctx.attr._process_wrapper and crate_type in ("rlib", "lib")
# can_build_metadata = ctx.attr._process_wrapper and crate_type in ("rlib", "lib")
if can_build_metadata:
rust_metadata = ctx.actions.declare_file(
paths.replace_extension(rust_lib_name, ".rmeta"),
sibling = rust_lib,
)

deps = transform_deps(ctx.attr.deps)
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
Expand All @@ -280,6 +293,7 @@ def _rust_library_common(ctx, crate_type):
proc_macro_deps = depset(proc_macro_deps),
aliases = ctx.attr.aliases,
output = rust_lib,
metadata = rust_metadata,
edition = get_edition(ctx.attr, toolchain),
rustc_env = ctx.attr.rustc_env,
is_test = False,
Expand Down
Loading

0 comments on commit 711df10

Please sign in to comment.