diff --git a/.bazelrc b/.bazelrc index 0667486fb5..4b35be4873 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 e33e5614ae..7727febbd5 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -901,6 +901,7 @@ rust_static_library = rule( str(Label("//rust:toolchain_type")), "@bazel_tools//tools/cpp:toolchain_type", ], + provides = [CcInfo], doc = dedent("""\ Builds a Rust static library. @@ -948,6 +949,7 @@ rust_shared_library = rule( str(Label("//rust:toolchain_type")), "@bazel_tools//tools/cpp:toolchain_type", ], + 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..82e1fb1d65 --- /dev/null +++ b/test/cc_shared_library/BUILD.bazel @@ -0,0 +1,30 @@ +load("@rules_cc//cc:defs.bzl", "cc_library", "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"], + linkstatic = True, + 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..68024e823b --- /dev/null +++ b/test/cc_shared_library/lib.c @@ -0,0 +1,5 @@ +#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..d5d3cd6ca8 --- /dev/null +++ b/test/cc_shared_library/lib.h @@ -0,0 +1,9 @@ +#include + +#ifdef _WIN32 +#define DllExport __declspec(dllexport) +#else +#define DllExport +#endif + +DllExport 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..12dc69b864 --- /dev/null +++ b/test/cc_shared_library/main.c @@ -0,0 +1,9 @@ +#include +#include + +#include "lib.h" + +int main(int argc, char** argv) { + assert(foo() == 4); + return 0; +}