From 99aa889ff7100f4d2177fc8f35dcefb222c8c37b Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Mon, 23 Feb 2026 07:52:05 -0800 Subject: [PATCH] Update rules to thread through cpp file. PiperOrigin-RevId: 874072680 --- .../cc_bindings_from_rust_rule.bzl | 21 ++++++++++++------- .../bazel_support/providers.bzl | 1 + .../cc_bindings_from_rs_sh_test.sh | 6 +++++- .../generating_files_test.bzl | 9 +++++--- cc_bindings_from_rs/test/bridging/cc_type.h | 2 ++ 5 files changed, 28 insertions(+), 11 deletions(-) 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 12c5847b4..1488ecdf0 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 @@ -197,6 +197,7 @@ def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_cra A tuple of (GeneratedBindingsInfo, features, current_config, output_depset). """ h_out_file = ctx.actions.declare_file(basename + ".h") + cpp_out_file = ctx.actions.declare_file(basename + ".cpp") rs_out_file = ctx.actions.declare_file(basename + "_cc_api_impl.rs") if ctx.label in [Label(x) for x in ctx.attr._verbose_log_targets[BuildSettingInfo].value]: @@ -206,6 +207,7 @@ def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_cra crubit_args = ctx.actions.args() crubit_args.add("--h-out", h_out_file) + crubit_args.add("--cpp-out", cpp_out_file) crubit_args.add("--rs-out", rs_out_file) crubit_args.add("--h-out-include-guard", _target_name_to_include_guard(target)) @@ -230,7 +232,7 @@ def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_cra for feature in features: crubit_args.add("--crate-feature", "self=" + feature) - outputs = [h_out_file, rs_out_file] + outputs = [h_out_file, cpp_out_file, rs_out_file] if ctx.attr._generate_error_report[BuildSettingInfo].value: error_report_output = ctx.actions.declare_file(basename + "_cc_api_error_report.json") crubit_args.add( @@ -292,18 +294,23 @@ def _generate_bindings(ctx, target, basename, inputs, args, rustc_env, proto_cra generated_bindings_info = GeneratedBindingsInfo( h_file = h_out_file, + cc_file = cpp_out_file, rust_file = rs_out_file, ) output_depset = [x for x in outputs if x != None] return generated_bindings_info, features, current_config, output_depset -def _make_cc_info_for_h_out_file(ctx, h_out_file, extra_cc_hdrs, extra_cc_srcs, cc_infos): +def _make_cc_info_for_h_out_file(ctx, feature_configuration, h_out_file, cc_out_file, extra_cc_hdrs, extra_cc_srcs, cc_infos): """Creates and returns CcInfo for the generated ..._cc_api.h header file. Args: ctx: The rule context. + feature_configuration: The features enabled for the bindings. h_out_file: The generated "..._cc_api.h" header file + cc_out_file: The generated "..._cc_api.cpp" source file + extra_cc_hdrs: Additional headers to compile with the generated bindings. + extra_cc_srcs: Additional sources to compile with the generated bindings. cc_infos: cc_infos for dependencies of the h_out_file - should include both: 1) the target `crate` and 2) the compiled Rust glue crate (`..._cc_api_impl.rs` file). @@ -316,17 +323,13 @@ def _make_cc_info_for_h_out_file(ctx, h_out_file, extra_cc_hdrs, extra_cc_srcs, for dep in ctx.attr._cc_deps_for_bindings ] + cc_infos) cc_toolchain = find_cpp_toolchain(ctx) - feature_configuration = cc_common.configure_features( - ctx = ctx, - cc_toolchain = cc_toolchain, - ) (compilation_context, compilation_outputs) = cc_common.compile( name = ctx.label.name, actions = ctx.actions, feature_configuration = feature_configuration, cc_toolchain = cc_toolchain, - srcs = extra_cc_srcs, + srcs = [cc_out_file] + extra_cc_srcs, public_hdrs = [h_out_file] + extra_cc_hdrs, compilation_contexts = [cc_info.compilation_context], ) @@ -394,6 +397,8 @@ def _cc_bindings_from_rust_aspect_impl(target, ctx): feature_configuration = cc_common.configure_features( ctx = ctx, cc_toolchain = cc_toolchain, + requested_features = ctx.features, + unsupported_features = ["layering_check"], ) dep_info, build_info, linkstamps = collect_deps( @@ -475,7 +480,9 @@ def _cc_bindings_from_rust_aspect_impl(target, ctx): cc_info = _make_cc_info_for_h_out_file( ctx, + feature_configuration, bindings_info.h_file, + bindings_info.cc_file, extra_cc_hdrs, extra_cc_srcs, cc_infos = [target[CcInfo], dep_variant_info.cc_info] + [ diff --git a/cc_bindings_from_rs/bazel_support/providers.bzl b/cc_bindings_from_rs/bazel_support/providers.bzl index 287aafc5e..3509c6fec 100644 --- a/cc_bindings_from_rs/bazel_support/providers.bzl +++ b/cc_bindings_from_rs/bazel_support/providers.bzl @@ -22,6 +22,7 @@ GeneratedBindingsInfo = provider( doc = "A provider that contains the generated C++ and Rust files.", fields = { "h_file": "The generated C++ header file.", + "cc_file": "The generated C++ source file.", "rust_file": "The generated Rust source file.", }, ) diff --git a/cc_bindings_from_rs/cc_bindings_from_rs_sh_test.sh b/cc_bindings_from_rs/cc_bindings_from_rs_sh_test.sh index 8c47e9ea1..161b971a7 100755 --- a/cc_bindings_from_rs/cc_bindings_from_rs_sh_test.sh +++ b/cc_bindings_from_rs/cc_bindings_from_rs_sh_test.sh @@ -14,9 +14,10 @@ source module gbash_unit.sh readonly STDERR_PATH="${TEST_TMPDIR}/stderr.txt" readonly STDOUT_PATH="${TEST_TMPDIR}/stdout.txt" readonly H_OUT_PATH="${TEST_TMPDIR}/cc_api.h" +readonly CPP_OUT_PATH="${TEST_TMPDIR}/cc_api.cpp" readonly RS_OUT_PATH="${TEST_TMPDIR}/cc_api_impl.rs" function delete_all_test_outputs() { - rm -rf "$STDERR_PATH" "$STDOUT_PATH" "$H_OUT_PATH" "$RS_OUT_PATH" "$TARGET_JSON_PATH" + rm -rf "$STDERR_PATH" "$STDOUT_PATH" "$H_OUT_PATH" "$CPP_OUT_PATH" "$RS_OUT_PATH" "$TARGET_JSON_PATH" } readonly CC_BINDINGS_FROM_RS_PATH="${RUNFILES}/cc_bindings_from_rs/cc_bindings_from_rs" @@ -51,6 +52,7 @@ function test::happy_path() { EXPECT_SUCCEED \ "\"$CC_BINDINGS_FROM_RS_PATH\" >\"$STDOUT_PATH\" 2>\"$STDERR_PATH\" \ \"--h-out=${H_OUT_PATH}\" \ + \"--cpp-out=${CPP_OUT_PATH}\" \ \"--rs-out=${RS_OUT_PATH}\" \ \"--crubit-support-path-format=\" \ \"--clang-format-exe-path=${CRUBIT_CLANG_FORMAT_EXE_PATH}\" \ @@ -64,6 +66,7 @@ function test::happy_path() { EXPECT_STR_EMPTY "$(cat $STDOUT_PATH)" EXPECT_STR_EMPTY "$(cat $STDERR_PATH)" + EXPECT_STR_EMPTY "$(cat $CPP_OUT_PATH)" EXPECT_FILE_NOT_EMPTY "${H_OUT_PATH}" EXPECT_SUCCEED \ @@ -181,6 +184,7 @@ function test::rustc_warnings_are_silenced() { EXPECT_SUCCEED \ "\"$CC_BINDINGS_FROM_RS_PATH\" >\"$STDOUT_PATH\" 2>\"$STDERR_PATH\" \ \"--h-out=${H_OUT_PATH}\" \ + \"--cpp-out=${CPP_OUT_PATH}\" \ \"--rs-out=${RS_OUT_PATH}\" \ \"--crubit-support-path-format=\" \ \"--clang-format-exe-path=${CRUBIT_CLANG_FORMAT_EXE_PATH}\" \ 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 b12013b05..f42e16edf 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 @@ -140,12 +140,15 @@ def _header_generation_test_impl(ctx): # Verify that `CcBindingsFromRust` generates: # 1) `generated_header` ("..._cc_api.h") - # 2) `generated_impl` ("..._cc_api_impl.rs") + # 2) `generated_cpp` ("..._cc_api.cpp") + # 3) `generated_impl` ("..._cc_api_impl.rs") generated_outputs = generate_action.outputs.to_list() - asserts.equals(env, 2, len(generated_outputs)) + asserts.equals(env, 3, len(generated_outputs)) generated_header = generated_outputs[0] asserts.equals(env, "rusty_lib.h", generated_header.basename) - generated_impl = generated_outputs[1] + generated_cpp = generated_outputs[1] + asserts.equals(env, "rusty_lib.cpp", generated_cpp.basename) + generated_impl = generated_outputs[2] asserts.equals(env, "rusty_lib_cc_api_impl.rs", generated_impl.basename) [rustc_action] = [action for action in _find_actions_by_mnemonic(env, "Rustc") if generated_impl.path in [input.path for input in action.inputs.to_list()]] diff --git a/cc_bindings_from_rs/test/bridging/cc_type.h b/cc_bindings_from_rs/test/bridging/cc_type.h index 96b837974..c40524995 100644 --- a/cc_bindings_from_rs/test/bridging/cc_type.h +++ b/cc_bindings_from_rs/test/bridging/cc_type.h @@ -5,6 +5,8 @@ #ifndef THIRD_PARTY_CRUBIT_CC_BINDINGS_FROM_RS_TEST_BRIDGING_CC_TYPE_H_ #define THIRD_PARTY_CRUBIT_CC_BINDINGS_FROM_RS_TEST_BRIDGING_CC_TYPE_H_ +#include + namespace crubit { namespace test {