Skip to content
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

propagate additional inputs from native dependencies #858

Merged
merged 9 commits into from
Jul 30, 2021
1 change: 1 addition & 0 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _clippy_aspect_impl(target, ctx):
ctx.rule.files,
toolchain,
cc_toolchain,
crate_type,
crate_info,
dep_info,
build_info,
Expand Down
16 changes: 16 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def collect_inputs(
files,
toolchain,
cc_toolchain,
crate_type,
krasimirgg marked this conversation as resolved.
Show resolved Hide resolved
crate_info,
dep_info,
build_info):
Expand All @@ -279,6 +280,7 @@ def collect_inputs(
files (list): A list of all inputs (`ctx.files`).
toolchain (rust_toolchain): The current `rust_toolchain`.
cc_toolchain (CcToolchainInfo): The current `cc_toolchain`.
crate_type (str): Crate type of the current target.
crate_info (CrateInfo): The Crate information of the crate to process build scripts for.
dep_info (DepInfo): The target Crate's dependency information.
build_info (BuildInfo): The target Crate's build settings.
Expand All @@ -290,6 +292,18 @@ def collect_inputs(

linker_depset = cc_toolchain.all_files

# Pass linker additional inputs (e.g., linker scripts) only for linking-like
# actions, not for example where the output is rlib. This avoids quadratic
# behavior where transitive noncrates are flattened on each transitive
# rust_library dependency.
additional_transitive_inputs = []
if crate_type == "bin":
additional_transitive_inputs = [
additional_input
for linker_input in dep_info.transitive_noncrates.to_list()
for additional_input in linker_input.additional_inputs
]

compile_inputs = depset(
getattr(files, "data", []) +
[toolchain.rustc] +
Expand All @@ -303,6 +317,7 @@ def collect_inputs(
linker_depset,
crate_info.srcs,
dep_info.transitive_libs,
depset(additional_transitive_inputs),
crate_info.compile_data,
],
)
Expand Down Expand Up @@ -555,6 +570,7 @@ def rustc_compile_action(
ctx.files,
toolchain,
cc_toolchain,
crate_type,
crate_info,
dep_info,
build_info,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/native_deps/dynamic.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
};
31 changes: 31 additions & 0 deletions test/unit/native_deps/native_deps_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,35 @@ def _linkopts_test():
target_under_test = ":linkopts_rust_bin",
)

def _additional_deps_test_impl(ctx):
env = analysistest.begin(ctx)
tut = analysistest.target_under_test(env)
action = tut.actions[0]
additional_inputs = [inp.basename for inp in action.inputs.to_list()]
asserts.true(env, "dynamic.lds" in additional_inputs)
return analysistest.end(env)

additional_deps_test = analysistest.make(_additional_deps_test_impl)

def _additional_deps_test():
rust_binary(
name = "additional_deps_rust_bin",
srcs = ["bin_using_native_dep.rs"],
deps = [":additional_deps_cc"],
)

cc_library(
name = "additional_deps_cc",
srcs = ["native_dep.cc"],
linkopts = ["-L$(location :dynamic.lds)"],
deps = [":dynamic.lds"],
)

additional_deps_test(
name = "additional_deps_test",
target_under_test = ":additional_deps_rust_bin",
)

def native_deps_test_suite(name):
"""Entry-point macro called from the BUILD file.

Expand All @@ -301,6 +330,7 @@ def native_deps_test_suite(name):
"""
_native_dep_test()
_linkopts_test()
_additional_deps_test()

native.test_suite(
name = name,
Expand All @@ -313,5 +343,6 @@ def native_deps_test_suite(name):
":bin_has_native_dep_and_alwayslink_test",
":cdylib_has_native_dep_and_alwayslink_test",
":native_linkopts_propagate_test",
":additional_deps_test",
],
)