Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions cc_bindings_from_rs/bazel_support/cc_bindings_from_rust_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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))

Expand All @@ -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(
Expand Down Expand Up @@ -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).
Expand All @@ -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],
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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] + [
Expand Down
1 change: 1 addition & 0 deletions cc_bindings_from_rs/bazel_support/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
)
Expand Down
6 changes: 5 additions & 1 deletion cc_bindings_from_rs/cc_bindings_from_rs_sh_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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=<crubit/support/{header}>\" \
\"--clang-format-exe-path=${CRUBIT_CLANG_FORMAT_EXE_PATH}\" \
Expand All @@ -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 \
Expand Down Expand Up @@ -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=<crubit/support/{header}>\" \
\"--clang-format-exe-path=${CRUBIT_CLANG_FORMAT_EXE_PATH}\" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()]]
Expand Down
2 changes: 2 additions & 0 deletions cc_bindings_from_rs/test/bridging/cc_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <new>

namespace crubit {
namespace test {

Expand Down