From 65b558cf0304aa0f9c599d0c1c60f0f956b24b25 Mon Sep 17 00:00:00 2001 From: scentini Date: Thu, 2 Dec 2021 14:15:57 +0100 Subject: [PATCH] Don't add dylibs to runfiles manually (#1052) We actually don't need to manually add dylibs to runfiles at all, as we create runfiles with `collect_data = True`. --- rust/private/rustc.bzl | 4 +- test/unit/check_runfiles/BUILD.bazel | 4 + test/unit/check_runfiles/bar.cc | 1 + .../check_runfiles/check_runfiles_test.bzl | 97 +++++++++++++++++++ test/unit/check_runfiles/foo.rs | 1 + test/unit/check_runfiles/foo_main.rs | 1 + 6 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 test/unit/check_runfiles/BUILD.bazel create mode 100644 test/unit/check_runfiles/bar.cc create mode 100644 test/unit/check_runfiles/check_runfiles_test.bzl create mode 100644 test/unit/check_runfiles/foo.rs create mode 100644 test/unit/check_runfiles/foo_main.rs diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 4021122d4a..f5c8256392 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -774,10 +774,8 @@ def rustc_compile_action( ), ) - dylibs = [get_preferred_artifact(lib) for linker_input in dep_info.transitive_noncrates.to_list() for lib in linker_input.libraries if _is_dylib(lib)] - runfiles = ctx.runfiles( - files = dylibs + getattr(ctx.files, "data", []), + files = getattr(ctx.files, "data", []), collect_data = True, ) diff --git a/test/unit/check_runfiles/BUILD.bazel b/test/unit/check_runfiles/BUILD.bazel new file mode 100644 index 0000000000..0dfcfcc972 --- /dev/null +++ b/test/unit/check_runfiles/BUILD.bazel @@ -0,0 +1,4 @@ +load(":check_runfiles_test.bzl", "check_runfiles_test_suite") + +############################ UNIT TESTS ############################# +check_runfiles_test_suite(name = "check_runfiles_test_suite") diff --git a/test/unit/check_runfiles/bar.cc b/test/unit/check_runfiles/bar.cc new file mode 100644 index 0000000000..237c8ce181 --- /dev/null +++ b/test/unit/check_runfiles/bar.cc @@ -0,0 +1 @@ +int main() {} diff --git a/test/unit/check_runfiles/check_runfiles_test.bzl b/test/unit/check_runfiles/check_runfiles_test.bzl new file mode 100644 index 0000000000..94ee3d7aeb --- /dev/null +++ b/test/unit/check_runfiles/check_runfiles_test.bzl @@ -0,0 +1,97 @@ +"""Unittests for rust rules.""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load( + "//rust:defs.bzl", + "rust_binary", + "rust_library", + "rust_shared_library", + "rust_static_library", +) + +def _check_runfiles_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + runfiles = tut[DefaultInfo].default_runfiles.files.to_list() + + asserts.true(env, _is_in_runfiles("libbar.so", runfiles)) + + return analysistest.end(env) + +def _is_in_runfiles(name, runfiles): + for file in runfiles: + if file.basename == name: + return True + return False + +check_runfiles_test = analysistest.make(_check_runfiles_test_impl) + +def _check_runfiles_test(): + rust_library( + name = "foo_lib", + srcs = ["foo.rs"], + deps = [":libbar.so"], + ) + + rust_binary( + name = "foo_bin", + srcs = ["foo_main.rs"], + deps = [":libbar.so"], + ) + + rust_shared_library( + name = "foo_dylib", + srcs = ["foo.rs"], + deps = [":libbar.so"], + ) + + rust_static_library( + name = "foo_static", + srcs = ["foo.rs"], + deps = [":libbar.so"], + ) + + # buildifier: disable=native-cc + native.cc_binary( + name = "libbar.so", + srcs = ["bar.cc"], + linkshared = True, + ) + + check_runfiles_test( + name = "check_runfiles_lib_test", + target_under_test = ":foo_lib", + ) + + check_runfiles_test( + name = "check_runfiles_bin_test", + target_under_test = ":foo_bin", + ) + + check_runfiles_test( + name = "check_runfiles_dylib_test", + target_under_test = ":foo_dylib", + ) + + check_runfiles_test( + name = "check_runfiles_static_test", + target_under_test = ":foo_static", + ) + +def check_runfiles_test_suite(name): + """Entry-point macro called from the BUILD file. + + Args: + name: Name of the macro. + """ + _check_runfiles_test() + + native.test_suite( + name = name, + tests = [ + ":check_runfiles_lib_test", + ":check_runfiles_bin_test", + ":check_runfiles_dylib_test", + ":check_runfiles_static_test", + ], + ) diff --git a/test/unit/check_runfiles/foo.rs b/test/unit/check_runfiles/foo.rs new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/test/unit/check_runfiles/foo.rs @@ -0,0 +1 @@ + diff --git a/test/unit/check_runfiles/foo_main.rs b/test/unit/check_runfiles/foo_main.rs new file mode 100644 index 0000000000..f328e4d9d0 --- /dev/null +++ b/test/unit/check_runfiles/foo_main.rs @@ -0,0 +1 @@ +fn main() {}