Skip to content

Commit

Permalink
Advertise CcInfo provider on rules
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`.
  • Loading branch information
cameron-martin committed Aug 24, 2023
1 parent 81af2f9 commit 18fe608
Show file tree
Hide file tree
Showing 7 changed files with 57 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 @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions test/cc_shared_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
7 changes: 7 additions & 0 deletions test/cc_shared_library/lib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "lib.h"

extern int32_t bar();

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

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
}
8 changes: 8 additions & 0 deletions test/cc_shared_library/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <assert.h>
#include <stdint.h>
#include "lib.h"

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

0 comments on commit 18fe608

Please sign in to comment.