diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 6dbb258bd2..dd3d816cac 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -240,6 +240,7 @@ def collect_deps( crate_deps.append(struct( crate_info = dep_variant_info.crate_info, dep_info = dep_variant_info.dep_info, + cc_info = dep_variant_info.cc_info, )) aliases = {k.label: v for k, v in aliases.items()} diff --git a/test/unit/cc_info/cc_info_test.bzl b/test/unit/cc_info/cc_info_test.bzl index 2848ab3c7e..cdc46407ac 100644 --- a/test/unit/cc_info/cc_info_test.bzl +++ b/test/unit/cc_info/cc_info_test.bzl @@ -1,7 +1,8 @@ """Unittests for rust rules.""" load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") -load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_macro", "rust_shared_library", "rust_static_library") +load("@rules_cc//cc:defs.bzl", "cc_library") +load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_library", "rust_proc_macro", "rust_shared_library", "rust_static_library") def _is_dylib_on_windows(ctx): return ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]) @@ -90,6 +91,16 @@ def _staticlib_provides_cc_info_test_impl(ctx): _assert_cc_info_has_library_to_link(env, tut, "staticlib", 2) return analysistest.end(env) +def _crate_group_info_provides_cc_info_test_impl(ctx): + env = analysistest.begin(ctx) + tut = analysistest.target_under_test(env) + asserts.true( + env, + len(tut[rust_common.dep_info].transitive_noncrates.to_list()) == 1, + "crate_group_info should provide 1 non-crate transitive dependency", + ) + return analysistest.end(env) + rlib_provides_cc_info_test = analysistest.make(_rlib_provides_cc_info_test_impl) rlib_with_dep_only_has_stdlib_linkflags_once_test = analysistest.make( _rlib_with_dep_only_has_stdlib_linkflags_once_test_impl, @@ -101,6 +112,29 @@ cdylib_provides_cc_info_test = analysistest.make(_cdylib_provides_cc_info_test_i }) proc_macro_does_not_provide_cc_info_test = analysistest.make(_proc_macro_does_not_provide_cc_info_test_impl) +crate_group_info_provides_cc_info_test = analysistest.make(_crate_group_info_provides_cc_info_test_impl) + +def _rust_cc_injection_impl(ctx): + dep_variant_info = rust_common.dep_variant_info( + cc_info = ctx.attr.cc_dep[CcInfo], + crate_info = None, + dep_info = None, + ) + return [ + rust_common.crate_group_info( + dep_variant_infos = depset([dep_variant_info]), + ), + ] + +rust_cc_injection = rule( + attrs = { + "cc_dep": attr.label( + providers = [CcInfo], + ), + }, + implementation = _rust_cc_injection_impl, +) + def _cc_info_test(): rust_library( name = "rlib", @@ -140,6 +174,23 @@ def _cc_info_test(): deps = ["//test/unit/native_deps:native_dep"], ) + cc_library( + name = "cc_lib", + srcs = ["foo.cc"], + ) + + rust_cc_injection( + name = "cc_lib_injected", + cc_dep = ":cc_lib", + ) + + rust_library( + name = "rust_lib_with_cc_lib_injected", + srcs = ["foo.rs"], + deps = [":cc_lib_injected"], + edition = "2018", + ) + rlib_provides_cc_info_test( name = "rlib_provides_cc_info_test", target_under_test = ":rlib", @@ -164,6 +215,10 @@ def _cc_info_test(): name = "proc_macro_does_not_provide_cc_info_test", target_under_test = ":proc_macro", ) + crate_group_info_provides_cc_info_test( + name = "crate_group_info_provides_cc_info_test", + target_under_test = ":rust_lib_with_cc_lib_injected", + ) def cc_info_test_suite(name): """Entry-point macro called from the BUILD file. @@ -182,5 +237,6 @@ def cc_info_test_suite(name): ":cdylib_provides_cc_info_test", ":proc_macro_does_not_provide_cc_info_test", ":bin_does_not_provide_cc_info_test", + ":crate_group_info_provides_cc_info_test", ], ) diff --git a/test/unit/cc_info/foo.cc b/test/unit/cc_info/foo.cc new file mode 100644 index 0000000000..bf7759e11e --- /dev/null +++ b/test/unit/cc_info/foo.cc @@ -0,0 +1 @@ +int foo() { return 42; }