Prepare `CcBindingsFromRustInfo` for round-tripped interop.
Specifically, allow for multiple headers per crate, and move it to a separate bazel file which we can use from rs_bindings_from_cc.
This doesn't otherwise do anything -- the binary itself will not accept multiple headers until unknown commit is released (it'll just pick one arbitrarily), and we do not actually _populate_ it with multiple headers (and won't until release), but this change is relatively standalone and helps keeps the changelists small.
PiperOrigin-RevId: 570161088
Change-Id: I7c250cc9d3b7b7a32d9097d5db2518e43073b224
diff --git a/cc_bindings_from_rs/bazel_support/BUILD b/cc_bindings_from_rs/bazel_support/BUILD
index 4407f76..df96892 100644
--- a/cc_bindings_from_rs/bazel_support/BUILD
+++ b/cc_bindings_from_rs/bazel_support/BUILD
@@ -12,3 +12,11 @@
"//visibility:private", # Only private by automation, not intent. Owner may accept CLs adding visibility. See <internal link>.
],
)
+
+bzl_library(
+ name = "providers_bzl",
+ srcs = ["providers.bzl"],
+ visibility = [
+ "//:__subpackages__",
+ ],
+)
diff --git a/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl b/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl
index 3396ed2..440f198 100644
--- a/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl
+++ b/cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl
@@ -36,6 +36,10 @@
"find_toolchain",
)
load(
+ "//cc_bindings_from_rs/bazel_support:providers.bzl",
+ "CcBindingsFromRustInfo",
+)
+load(
"//rs_bindings_from_cc/bazel_support:compile_rust.bzl",
"compile_rust",
)
@@ -49,19 +53,6 @@
targets_to_remove = [
]
-CcBindingsFromRustInfo = provider(
- doc = ("A provider that contains compile and linking information for the generated" +
- " `.rs` and `.h` files."),
- fields = {
- "cc_info": "A CcInfo provider for the API projection.",
- # TODO(b/271857814): A `CRATE_NAME` might not be globally unique - the
- # key needs to also cover a "hash" of the crate version and compilation
- # flags.
- "crate_key": "String with a crate key to use in --bindings-from-dependency",
- "h_out_file": "File object representing the generated ..._cc_api.h.",
- },
-)
-
def _get_dep_bindings_infos(ctx):
"""Returns `CcBindingsFromRustInfo`s of direct, non-transitive dependencies.
@@ -80,7 +71,7 @@
"""
return [
dep[CcBindingsFromRustInfo]
- for dep in ctx.rule.attr.deps
+ for dep in ctx.rule.attr.deps + getattr(ctx.rule.attr, "cc_deps", [])
if CcBindingsFromRustInfo in dep
]
@@ -113,8 +104,9 @@
crubit_args.add("--rustfmt-config-path", ctx.file._rustfmt_cfg)
for dep_bindings_info in _get_dep_bindings_infos(ctx):
- arg = dep_bindings_info.crate_key + "=" + dep_bindings_info.h_out_file.short_path
- crubit_args.add("--bindings-from-dependency", arg)
+ for header in dep_bindings_info.headers:
+ arg = dep_bindings_info.crate_key + "=" + header.short_path
+ crubit_args.add("--bindings-from-dependency", arg)
ctx.actions.run(
outputs = [h_out_file, rs_out_file],
@@ -304,7 +296,7 @@
CcBindingsFromRustInfo(
cc_info = cc_info,
crate_key = crate_info.name,
- h_out_file = h_out_file,
+ headers = [h_out_file],
),
OutputGroupInfo(out = depset([h_out_file, rs_out_file])),
]
diff --git a/cc_bindings_from_rs/bazel_support/providers.bzl b/cc_bindings_from_rs/bazel_support/providers.bzl
new file mode 100644
index 0000000..836e2e4
--- /dev/null
+++ b/cc_bindings_from_rs/bazel_support/providers.bzl
@@ -0,0 +1,17 @@
+# Part of the Crubit project, under the Apache License v2.0 with LLVM
+# Exceptions. See /LICENSE for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+"""Providers used/generated by cc_bindings_from_rs."""
+
+CcBindingsFromRustInfo = provider(
+ doc = ("A provider that contains compile and linking information for the generated" +
+ " `.rs` and `.h` files."),
+ fields = {
+ "cc_info": "A CcInfo provider for the C++ API.",
+ # TODO(b/271857814): A `CRATE_NAME` might not be globally unique - the
+ # key needs to also cover a "hash" of the crate version and compilation
+ # flags.
+ "crate_key": "String with a crate key to use in --other-crate-bindings",
+ "headers": "A list of C++ headers which correspond to this crate.",
+ },
+)
diff --git a/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/generating_files_test.bzl b/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/generating_files_test.bzl
index 6e92c9b..a53a006 100644
--- a/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/generating_files_test.bzl
+++ b/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/generating_files_test.bzl
@@ -10,7 +10,7 @@
)
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
load(
- "//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl",
+ "//cc_bindings_from_rs/bazel_support:providers.bzl",
"CcBindingsFromRustInfo",
)
load(
diff --git a/cc_bindings_from_rs/test/bazel/unit_tests/unit_test_helpers/attach_aspect.bzl b/cc_bindings_from_rs/test/bazel/unit_tests/unit_test_helpers/attach_aspect.bzl
index 42518c7..0630176 100644
--- a/cc_bindings_from_rs/test/bazel/unit_tests/unit_test_helpers/attach_aspect.bzl
+++ b/cc_bindings_from_rs/test/bazel/unit_tests/unit_test_helpers/attach_aspect.bzl
@@ -6,9 +6,12 @@
load(
"//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl",
- "CcBindingsFromRustInfo",
"cc_bindings_from_rust_aspect",
)
+load(
+ "//cc_bindings_from_rs/bazel_support:providers.bzl",
+ "CcBindingsFromRustInfo",
+)
ActionsInfo = provider(
doc = ("A provider that contains compile and linking information for the generated" +