Skip to content

Commit

Permalink
test disable metadata on windows in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaroby committed Jul 12, 2022
1 parent baf740d commit d750801
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 53 deletions.
14 changes: 8 additions & 6 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ load("//rust:defs.bzl", "rust_common")
load("//rust/private:rustc.bzl", "rustc_compile_action")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "compute_crate_name", "determine_output_hash", "find_toolchain", "transform_deps")
load("//rust/private:utils.bzl", "can_build_metadata", "compute_crate_name", "determine_output_hash", "find_toolchain", "transform_deps")

RustProtoInfo = provider(
doc = "Rust protobuf provider info",
Expand Down Expand Up @@ -212,11 +212,13 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
crate_name,
output_hash,
))
rust_metadata = ctx.actions.declare_file("%s/lib%s-%s.rmeta" % (
output_dir,
crate_name,
output_hash,
))
rust_metadata = None
if can_build_metadata(toolchain, ctx, "rlib"):
rust_metadata = ctx.actions.declare_file("%s/lib%s-%s.rmeta" % (
output_dir,
crate_name,
output_hash,
))

# Gather all dependencies for compilation
compile_action_deps = depset(
Expand Down
34 changes: 3 additions & 31 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ load("//rust/private:common.bzl", "rust_common")
load("//rust/private:rustc.bzl", "rustc_compile_action")
load(
"//rust/private:utils.bzl",
"can_build_metadata",
"compute_crate_name",
"dedent",
"determine_output_hash",
Expand Down Expand Up @@ -278,28 +279,6 @@ def _rust_proc_macro_impl(ctx):
"""
return _rust_library_common(ctx, "proc-macro")

def _can_build_metadata(toolchain, ctx, crate_type):
"""Can we build metadata for this rust_library?
Args:
toolchain (toolchain): The rust toolchain
ctx (ctx): The rule's context object
crate_type (String): one of lib|rlib|dylib|staticlib|cdylib|proc-macro
Returns:
bool: whether we can build metadata for this rule.
"""

# In order to enable pipelined compilation we require that:
# 1) The _pipelined_compilation flag is enabled,
# 2) the OS running the rule is something other than windows as we require sandboxing (for now),
# 3) process_wrapper is enabled (this is disabled when compiling process_wrapper itself),
# 4) the crate_type is rlib or lib.
return toolchain._pipelined_compilation and \
toolchain.os != "windows" and \
ctx.attr._process_wrapper and \
crate_type in ("rlib", "lib")

def _rust_library_common(ctx, crate_type):
"""The common implementation of the library-like rules.
Expand Down Expand Up @@ -337,16 +316,9 @@ 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 = _can_build_metadata(toolchain, ctx, crate_type)

# can_build_metadata = ctx.attr._process_wrapper and crate_type in ("rlib", "lib")
if can_build_metadata:
rust_metadata = None
if can_build_metadata(toolchain, ctx, crate_type):
rust_metadata = ctx.actions.declare_file(
paths.replace_extension(rust_lib_name, ".rmeta"),
sibling = rust_lib,
Expand Down
42 changes: 27 additions & 15 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,6 @@ def _should_use_pic(cc_toolchain, feature_configuration, crate_type):
def _is_proc_macro(crate_info):
return "proc-macro" in (crate_info.type, crate_info.wrapped_crate_type)

def _requires_object(crate_info):
return crate_info.type not in ("lib", "rlib")

def collect_deps(
deps,
proc_macro_deps,
Expand Down Expand Up @@ -489,6 +486,30 @@ def _disambiguate_libs(actions, toolchain, crate_info, dep_info, use_pic):
visited_libs[name] = artifact
return ambiguous_libs

def _depend_on_metadata(crate_info, force_depend_on_objects):
"""Determines if we can depend on metadata for this crate.
By default (when pipelining is disabled or when the crate type needs to link against
objects) we depend on the set of object files (.rlib).
When pipelining is enabled and the crate type supports depending on metadata,
we depend on metadata files only (.rmeta).
In some rare cases, even if both of those conditions are true, we still want to
depend on objects. This is what force_depend_on_objects is.
Args:
crate_info (CrateInfo): The Crate to determine this for.
force_depend_on_objects (bool): if set we will not depend on metadata.
Returns:
Whether we can depend on metadata for this crate.
"""
if force_depend_on_objects:
return False

is_lib = crate_info.type in ("rlib", "lib")
build_metadata = crate_info.metadata != None
return is_lib and build_metadata

def collect_inputs(
ctx,
file,
Expand Down Expand Up @@ -559,19 +580,9 @@ def collect_inputs(
# change.
linkstamp_outs = []

# print(ctx.label)
# print(crate_info.name, " crate out: ", dep_info.transitive_crate_outputs)
# print(crate_info.name, "meta out: ", dep_info.transitive_metadata_outputs)

transitive_crate_outputs = dep_info.transitive_crate_outputs
depend_on_metadata = not _requires_object(crate_info) and not force_depend_on_objects
if depend_on_metadata:
if _depend_on_metadata(crate_info, force_depend_on_objects):
transitive_crate_outputs = dep_info.transitive_metadata_outputs
# print(crate_info.name, "depending on metadata")

else:
# print(crate_info.name, "depending on object", "(forced)" if force_depend_on_objects else "")
pass

nolinkstamp_compile_inputs = depset(
getattr(files, "data", []) +
Expand Down Expand Up @@ -870,8 +881,9 @@ def construct_arguments(

_add_native_link_flags(rustc_flags, dep_info, linkstamp_outs, ambiguous_libs, crate_info.type, toolchain, cc_toolchain, feature_configuration)

link_to_metadata = _depend_on_metadata(crate_info, force_link_to_objects)

# These always need to be added, even if not linking this crate.
link_to_metadata = not force_link_to_objects and not _requires_object(crate_info)
add_crate_link_flags(rustc_flags, dep_info, force_all_deps_direct, link_to_metadata = link_to_metadata)

needs_extern_proc_macro_flag = _is_proc_macro(crate_info) and crate_info.edition != "2015"
Expand Down
22 changes: 22 additions & 0 deletions rust/private/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,25 @@ def _replace_all(string, substitutions):
string = string[:pattern_start] + replacement + string[after_pattern:]

return string

def can_build_metadata(toolchain, ctx, crate_type):
"""Can we build metadata for this rust_library?
Args:
toolchain (toolchain): The rust toolchain
ctx (ctx): The rule's context object
crate_type (String): one of lib|rlib|dylib|staticlib|cdylib|proc-macro
Returns:
bool: whether we can build metadata for this rule.
"""

# In order to enable pipelined compilation we require that:
# 1) The _pipelined_compilation flag is enabled,
# 2) the OS running the rule is something other than windows as we require sandboxing (for now),
# 3) process_wrapper is enabled (this is disabled when compiling process_wrapper itself),
# 4) the crate_type is rlib or lib.
return toolchain._pipelined_compilation and \
toolchain.os != "windows" and \
ctx.attr._process_wrapper and \
crate_type in ("rlib", "lib")
7 changes: 6 additions & 1 deletion test/unit/force_all_deps_direct/generator.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ load("//rust/private:providers.bzl", "BuildInfo", "CrateInfo", "DepInfo", "DepVa
# buildifier: disable=bzl-visibility
load("//rust/private:rustc.bzl", "rustc_compile_action")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "can_build_metadata")

def _generator_impl(ctx):
rs_file = ctx.actions.declare_file(ctx.label.name + "_generated.rs")
ctx.actions.run_shell(
Expand Down Expand Up @@ -54,7 +57,9 @@ EOF
) for dep in ctx.attr.deps]

rust_lib = ctx.actions.declare_file(rust_lib_name)
rust_metadata = ctx.actions.declare_file(rust_metadata_name)
rust_metadata = None
if can_build_metadata(toolchain, ctx, crate_type):
rust_metadata = ctx.actions.declare_file(rust_metadata_name)
return rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
Expand Down

0 comments on commit d750801

Please sign in to comment.