Skip to content

Commit

Permalink
Advertise CcInfo provider on rules (#2126)
Browse files Browse the repository at this point in the history
The aspect used in the `cc_shared_library` implementation in bazel 6.3
and 7 only traverses attributes that advertise the `CcInfo` provider via
the `provides` attribute. This means in these versions of bazel, rust
libraries are currently omitted from libraries built with
`cc_shared_library`.

Fixes #2101.
  • Loading branch information
cameron-martin authored Jan 5, 2024
1 parent 0a4523e commit 95e5d44
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
###############################################################################
Expand Down
2 changes: 2 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions test/cc_shared_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
5 changes: 5 additions & 0 deletions test/cc_shared_library/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "lib.h"

extern int32_t bar();

int32_t foo() { return bar(); }
9 changes: 9 additions & 0 deletions test/cc_shared_library/lib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdint.h>

#ifdef _WIN32
#define DllExport __declspec(dllexport)
#else
#define DllExport
#endif

DllExport int32_t foo();
4 changes: 4 additions & 0 deletions test/cc_shared_library/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[no_mangle]
pub extern "C" fn bar() -> i32 {
4
}
9 changes: 9 additions & 0 deletions test/cc_shared_library/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <assert.h>
#include <stdint.h>

#include "lib.h"

int main(int argc, char** argv) {
assert(foo() == 4);
return 0;
}

0 comments on commit 95e5d44

Please sign in to comment.