diff --git a/.bazelrc b/.bazelrc index 65d3e9795c..6e6b213d8e 100644 --- a/.bazelrc +++ b/.bazelrc @@ -20,6 +20,10 @@ coverage --combined_report=lcov # https://bazel.build/reference/command-line-reference#flag--experimental_fetch_all_coverage_outputs coverage --experimental_fetch_all_coverage_outputs +# Required for some of the tests +# https://bazel.build/reference/command-line-reference#flag--experimental_cc_shared_library +common --experimental_cc_shared_library + ############################################################################### ## Unique configuration groups ############################################################################### diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index df5095e01f..66e64efa3c 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -938,6 +938,7 @@ rust_static_library = rule( "@bazel_tools//tools/cpp:toolchain_type", ], incompatible_use_toolchain_transition = True, + provides = [CcInfo], doc = dedent("""\ Builds a Rust static library. @@ -970,6 +971,7 @@ rust_shared_library = rule( "@bazel_tools//tools/cpp:toolchain_type", ], incompatible_use_toolchain_transition = True, + provides = [CcInfo], doc = dedent("""\ Builds a Rust shared library. diff --git a/test/cc_shared_library/BUILD.bazel b/test/cc_shared_library/BUILD.bazel new file mode 100644 index 0000000000..bcfd9122c7 --- /dev/null +++ b/test/cc_shared_library/BUILD.bazel @@ -0,0 +1,29 @@ +load("@rules_cc//cc:defs.bzl", "cc_test") +load("@rules_rust//rust:defs.bzl", "rust_static_library") + +rust_static_library( + name = "rust_lib", + srcs = ["lib.rs"], + edition = "2021", +) + +cc_library( + name = "c_lib", + srcs = ["lib.c"], + hdrs = ["lib.h"], + deps = [":rust_lib"], +) + +# Tests that cc_shared_library correctly traverses into +# `rust_static_library` when linking. +cc_shared_library( + name = "shared", + deps = [":c_lib"], +) + +cc_test( + name = "test", + srcs = ["main.c"], + dynamic_deps = [":shared"], + deps = [":c_lib"], +) diff --git a/test/cc_shared_library/lib.c b/test/cc_shared_library/lib.c new file mode 100644 index 0000000000..596ca33ca8 --- /dev/null +++ b/test/cc_shared_library/lib.c @@ -0,0 +1,7 @@ +#include "lib.h" + +extern int32_t bar(); + +int32_t foo() { + return bar(); +} diff --git a/test/cc_shared_library/lib.h b/test/cc_shared_library/lib.h new file mode 100644 index 0000000000..5d10ec869e --- /dev/null +++ b/test/cc_shared_library/lib.h @@ -0,0 +1,3 @@ +#include + +int32_t foo(); diff --git a/test/cc_shared_library/lib.rs b/test/cc_shared_library/lib.rs new file mode 100644 index 0000000000..9c7de6890c --- /dev/null +++ b/test/cc_shared_library/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern "C" fn bar() -> i32 { + 4 +} diff --git a/test/cc_shared_library/main.c b/test/cc_shared_library/main.c new file mode 100644 index 0000000000..d277feb28d --- /dev/null +++ b/test/cc_shared_library/main.c @@ -0,0 +1,8 @@ +#include +#include +#include "lib.h" + +int main(int argc, char** argv) { + assert(foo() == 4); + return 0; +}