Propagate `--crate-name` into `cc_bindings_from_rs`.
This CL is needed to support running `cc_bindings_from_rs` on any Rust
crate where the name of the crate is not based on the basename of the
crate root (the name of the main `.rs` file).
This CL can be seen as a *small* step toward replicating all `rustc`
cmdline arguments into the invocation of `cc_bindings_from_rs`.
This CL makes the following changes:
* Adds `--crate-name` propagation to
`cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl`
* Adds tests that exercise the `--crate-name` support and would fail
without the change above: `cc_bindings_from_rs/test/bazel/crate_name`
* Moves tests from `cc_bindings_from_rs/test/bazel_unit_tests` to
`cc_bindings_from_rs/test/bazel/unit_tests`. This helps to keep
the Bazel-focused tests under `cc_bindings_from_rs/test/bazel`.
PiperOrigin-RevId: 510499869
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 66c8b77..0928eac 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
@@ -125,15 +125,17 @@
def _cc_bindings_from_rust_rule_impl(ctx):
basename = ctx.attr.crate.label.name
- crate_root = ctx.attr.crate[CrateInfo].root
+ crate_info = ctx.attr.crate[CrateInfo]
+ crate_root = crate_info.root
# TODO(b/258449205): Extract `rustc_args` from the target `crate` (instead
- # of figouring out the `crate_root` and hard-coding `--crate-type`,
+ # of figuring out the `crate_root` and hard-coding `--crate-type`,
# `panic=abort`, etc.). It seems that `BuildInfo` from
# @rules_rust//rust/private/providers.bzl is not
# exposed publicly?
rustc_args = ctx.actions.args()
rustc_args.add(crate_root)
+ rustc_args.add("--crate-name", crate_info.name)
rustc_args.add("--crate-type", "lib")
rustc_args.add("--codegen", "panic=abort")
diff --git a/cc_bindings_from_rs/test/bazel/crate_name/BUILD b/cc_bindings_from_rs/test/bazel/crate_name/BUILD
new file mode 100644
index 0000000..0db7b5c
--- /dev/null
+++ b/cc_bindings_from_rs/test/bazel/crate_name/BUILD
@@ -0,0 +1,38 @@
+"""End-to-end tests of `cc_bindings_from_rs`, focusing on how
+Bazel<->cc_bindings_from_rs integration handles a custom `name` in a
+`rust_library` target."""
+
+load(
+ "@rules_rust//rust:defs.bzl",
+ "rust_library",
+)
+load(
+ "//cc_bindings_from_rs/bazel_support:cc_bindings_from_rust_rule.bzl",
+ "cc_bindings_from_rust",
+)
+
+licenses(["notice"])
+
+rust_library(
+ name = "custom_crate_name", # <- This is an important part of this test.
+ testonly = 1,
+ srcs = ["lib.rs"],
+ deps = [
+ "//common:rust_allocator_shims",
+ ],
+)
+
+cc_bindings_from_rust(
+ name = "custom_crate_name_cc_api",
+ testonly = 1,
+ crate = ":custom_crate_name",
+)
+
+cc_test(
+ name = "crate_name_test",
+ srcs = ["crate_name_test.cc"],
+ deps = [
+ ":custom_crate_name_cc_api",
+ "@com_google_googletest//:gtest_main",
+ ],
+)
diff --git a/cc_bindings_from_rs/test/bazel/crate_name/crate_name_test.cc b/cc_bindings_from_rs/test/bazel/crate_name/crate_name_test.cc
new file mode 100644
index 0000000..9238ebc
--- /dev/null
+++ b/cc_bindings_from_rs/test/bazel/crate_name/crate_name_test.cc
@@ -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
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "cc_bindings_from_rs/test/bazel/crate_name/custom_crate_name_cc_api.h"
+
+namespace crubit {
+namespace {
+
+TEST(CrateNameTests, BasicEndToEndTest) {
+ EXPECT_EQ(42, custom_crate_name::get_the_answer());
+}
+
+} // namespace
+} // namespace crubit
diff --git a/cc_bindings_from_rs/test/bazel/crate_name/lib.rs b/cc_bindings_from_rs/test/bazel/crate_name/lib.rs
new file mode 100644
index 0000000..e19a19d
--- /dev/null
+++ b/cc_bindings_from_rs/test/bazel/crate_name/lib.rs
@@ -0,0 +1,30 @@
+// 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
+
+//! This crate is used as a test input for `cc_bindings_from_rs` and the
+//! generated C++ bindings are then tested via `crate_name_test.cc`.
+//!
+//! The file name (i.e. `lib.rs`) is typical for Cargo crates. The crate
+//! is given a real name via Bazel BUILD file:
+//!
+//! ```
+//! rust_library(
+//! name = "custom_crate_name",
+//! srcs = ["lib.rs"],
+//! )
+//! ```
+//!
+//! This test verifies that `cc_bindings_from_rs` is invoked with
+//! `--crate_name=custom_crate_name` - without the cmdline argument
+//! `cc_bindings_from_rs` would think that the crate name is `lib`.
+//! - `lib::get_the_answer()` wouldn't compile if used in the generated
+//! `...cc_api_impl.rs`.
+//! - `namespace lib` is also undesirable in the generated `...cc_api.h` (this
+//! is slightly less important because in the long-term the C++ namespace
+//! might not depend on the crate name - see
+//! <internal link>).
+
+pub fn get_the_answer() -> i32 {
+ 42
+}
diff --git a/cc_bindings_from_rs/test/bazel_unit_tests/BUILD b/cc_bindings_from_rs/test/bazel/unit_tests/BUILD
similarity index 100%
rename from cc_bindings_from_rs/test/bazel_unit_tests/BUILD
rename to cc_bindings_from_rs/test/bazel/unit_tests/BUILD
diff --git a/cc_bindings_from_rs/test/bazel_unit_tests/generating_files/BUILD b/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/BUILD
similarity index 100%
rename from cc_bindings_from_rs/test/bazel_unit_tests/generating_files/BUILD
rename to cc_bindings_from_rs/test/bazel/unit_tests/generating_files/BUILD
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
similarity index 100%
rename from cc_bindings_from_rs/test/bazel_unit_tests/generating_files/generating_files_test.bzl
rename to cc_bindings_from_rs/test/bazel/unit_tests/generating_files/generating_files_test.bzl
diff --git a/cc_bindings_from_rs/test/bazel_unit_tests/generating_files/rusty_lib_crate_root.rs b/cc_bindings_from_rs/test/bazel/unit_tests/generating_files/rusty_lib_crate_root.rs
similarity index 100%
rename from cc_bindings_from_rs/test/bazel_unit_tests/generating_files/rusty_lib_crate_root.rs
rename to cc_bindings_from_rs/test/bazel/unit_tests/generating_files/rusty_lib_crate_root.rs