Skip to content

Commit

Permalink
Made the use of attr explicit for rustc_compile_action
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Aug 12, 2021
1 parent 8878853 commit 98c223b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _build_script_impl(ctx):
# Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version).
# We hope that the linker env is sufficient for the whole cc_toolchain.
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
_, _, linker_env = get_linker_and_args(ctx, cc_toolchain, feature_configuration, None)
_, _, linker_env = get_linker_and_args(ctx, ctx.attr, cc_toolchain, feature_configuration, None)
env.update(**linker_env)

# MSVC requires INCLUDE to be set
Expand Down
1 change: 1 addition & 0 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr

return rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
toolchain = find_toolchain(ctx),
crate_info = rust_common.create_crate_info(
name = crate_name,
Expand Down
3 changes: 3 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def _rust_library_common(ctx, crate_type):

return rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
toolchain = toolchain,
crate_info = rust_common.create_crate_info(
name = crate_name,
Expand Down Expand Up @@ -287,6 +288,7 @@ def _rust_binary_impl(ctx):

return rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
toolchain = toolchain,
crate_info = rust_common.create_crate_info(
name = crate_name,
Expand Down Expand Up @@ -447,6 +449,7 @@ def _rust_test_common(ctx, toolchain, output):

providers = rustc_compile_action(
ctx = ctx,
attr = ctx.attr,
toolchain = toolchain,
crate_info = crate_info,
rust_flags = ["--test"] if ctx.attr.use_libtest_harness else ["--cfg", "test"],
Expand Down
44 changes: 27 additions & 17 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,15 @@ def get_cc_user_link_flags(ctx):
"""
return ctx.fragments.cpp.linkopts

def get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths, attrs = None):
def get_linker_and_args(ctx, attr, cc_toolchain, feature_configuration, rpaths):
"""Gathers cc_common linker information
Args:
ctx (ctx): The current target's context object
attr (struct): Attributes to use in gathering linker args
cc_toolchain (CcToolchain): cc_toolchain for which we are creating build variables.
feature_configuration (FeatureConfiguration): Feature configuration to be queried.
rpaths (depset): Depset of directories where loader will look for libraries at runtime.
attrs (struct, optional): Attributes to use instead of `ctx.attr`.
Returns:
Expand All @@ -202,14 +202,11 @@ def get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths, attrs
- (sequence): A flattened command line flags for given action.
- (dict): Environment variables to be set for given action.
"""
if not attrs:
attrs = ctx.attr

user_link_flags = get_cc_user_link_flags(ctx)

# Add linkopt's from dependencies. This includes linkopts from transitive
# dependencies since they get merged up.
for dep in getattr(attrs, "deps", []):
for dep in getattr(attr, "deps", []):
if CcInfo in dep and dep[CcInfo].linking_context:
for linker_input in dep[CcInfo].linking_context.linker_inputs.to_list():
for flag in linker_input.user_link_flags:
Expand Down Expand Up @@ -487,7 +484,7 @@ def construct_arguments(
# linker since it won't understand.
if toolchain.target_arch != "wasm32":
rpaths = _compute_rpaths(toolchain, output_dir, dep_info)
ld, link_args, link_env = get_linker_and_args(ctx, cc_toolchain, feature_configuration, rpaths, attr)
ld, link_args, link_env = get_linker_and_args(ctx, attr, cc_toolchain, feature_configuration, rpaths)
env.update(link_env)
rustc_flags.add("--codegen=linker=" + ld)
rustc_flags.add_joined("--codegen", link_args, join_with = " ", format_joined = "link-args=%s")
Expand Down Expand Up @@ -533,6 +530,7 @@ def construct_arguments(

def rustc_compile_action(
ctx,
attr,
toolchain,
crate_info,
output_hash = None,
Expand All @@ -542,6 +540,7 @@ def rustc_compile_action(
Args:
ctx (ctx): The rule's context object
attr (struct): Attributes to use for the rust compile action
toolchain (rust_toolchain): The current `rust_toolchain`
crate_info (CrateInfo): The CrateInfo provider for the current target.
output_hash (str, optional): The hashed path of the crate root. Defaults to None.
Expand Down Expand Up @@ -576,7 +575,7 @@ def rustc_compile_action(

args, env = construct_arguments(
ctx = ctx,
attr = ctx.attr,
attr = attr,
file = ctx.file,
toolchain = toolchain,
tool_path = toolchain.rustc.path,
Expand All @@ -591,8 +590,8 @@ def rustc_compile_action(
build_flags_files = build_flags_files,
)

if hasattr(ctx.attr, "version") and ctx.attr.version != "0.0.0":
formatted_version = " v{}".format(ctx.attr.version)
if hasattr(attr, "version") and attr.version != "0.0.0":
formatted_version = " v{}".format(attr.version)
else:
formatted_version = ""

Expand All @@ -618,9 +617,9 @@ def rustc_compile_action(
collect_data = True,
)

out_binary = False
if hasattr(ctx.attr, "out_binary"):
out_binary = getattr(ctx.attr, "out_binary")
# TODO: Remove after some resolution to
# https://github.com/bazelbuild/rules_rust/issues/771
out_binary = getattr(attr, "out_binary", False)

providers = [
crate_info,
Expand All @@ -633,18 +632,19 @@ def rustc_compile_action(
),
]
if toolchain.target_arch != "wasm32":
providers += establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configuration)
providers += establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration)

return providers

def _is_dylib(dep):
return not bool(dep.static_library or dep.pic_static_library)

def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configuration):
def establish_cc_info(ctx, attr, crate_info, toolchain, cc_toolchain, feature_configuration):
"""If the produced crate is suitable yield a CcInfo to allow for interop with cc rules
Args:
ctx (ctx): The rule's context object
attr (struct): Attributes to use in gathering CcInfo
crate_info (CrateInfo): The CrateInfo provider of the target crate
toolchain (rust_toolchain): The current `rust_toolchain`
cc_toolchain (CcToolchainInfo): The current `CcToolchainInfo`
Expand All @@ -654,7 +654,17 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
list: A list containing the CcInfo provider
"""

if crate_info.is_test or crate_info.type not in ("staticlib", "cdylib", "rlib", "lib") or getattr(ctx.attr, "out_binary", False):
# A test will not need to produce CcInfo as nothing can depend on test targets
if crate_info.is_test:
return []

# Only generate CcInfo for particular crate types
if crate_info.type not in ("staticlib", "cdylib", "rlib", "lib"):
return []

# TODO: Remove after some resolution to
# https://github.com/bazelbuild/rules_rust/issues/771
if getattr(attr, "out_binary", False):
return []

if crate_info.type == "staticlib":
Expand Down Expand Up @@ -703,7 +713,7 @@ def establish_cc_info(ctx, crate_info, toolchain, cc_toolchain, feature_configur
)

cc_infos = [CcInfo(linking_context = linking_context)]
for dep in ctx.attr.deps:
for dep in getattr(attr, "deps", []):
if CcInfo in dep:
cc_infos.append(dep[CcInfo])

Expand Down

0 comments on commit 98c223b

Please sign in to comment.