Skip to content

Commit

Permalink
Fix collecting cc deps in collect_deps (#2699)
Browse files Browse the repository at this point in the history
In my repo mvukov/rules_ros2#316 I have a
use-case that I want to pass some cc-deps via dep_variant_info as in
https://github.com/mvukov/rules_ros2/pull/316/files#diff-58917867447a8263989afcfff01c87ad154b41562efc3f6d789807efb26e298fR231-R253.
This PR enables that.
  • Loading branch information
mvukov authored Jul 18, 2024
1 parent 6a140b2 commit ca7a3df
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
58 changes: 57 additions & 1 deletion test/unit/cc_info/cc_info_test.bzl
Original file line number Diff line number Diff line change
@@ -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])
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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.
Expand All @@ -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",
],
)
1 change: 1 addition & 0 deletions test/unit/cc_info/foo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int foo() { return 42; }

0 comments on commit ca7a3df

Please sign in to comment.