Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove failure when bindgen cc_lib does not contain static library #2742

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
5 changes: 0 additions & 5 deletions bindgen/private/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,6 @@ def _generate_cc_link_build_info(ctx, cc_lib):
linker_search_paths.append(lib.pic_static_library.dirname)
compile_data.append(lib.pic_static_library)

if not compile_data:
fail("No static libraries found in {}".format(
cc_lib.label,
))

rustc_flags_file = ctx.actions.declare_file("{}.rustc_flags".format(ctx.label.name))
ctx.actions.write(
output = rustc_flags_file,
Expand Down
21 changes: 20 additions & 1 deletion examples/bindgen/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,30 @@ rust_bindgen_library(

rust_binary(
name = "simple_example",
srcs = ["main.rs"],
srcs = ["simple.rs"],
deps = [":simple_bindgen"],
)

rust_test(
name = "simple_test",
crate = ":simple_example",
)

rust_bindgen_library(
name = "hello_c_bindgen",
bindgen_flags = [
"--allowlist-function=hello",
],
cc_lib = "//bindgen/shared:hello_c",
crate_name = "hello_c",
header = "//bindgen/shared:hello.h",
)

rust_binary(
name = "shared_example",
srcs = ["shared.rs"],
deps = [
":hello_c_bindgen",
"//bindgen/shared:hello_rs",
],
)
4 changes: 4 additions & 0 deletions examples/bindgen/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
hello_rs::hello();
unsafe { hello_c::hello() };
}
36 changes: 36 additions & 0 deletions examples/bindgen/shared/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen")
load("@rules_rust//rust:defs.bzl", "rust_library")

package(default_visibility = ["//bindgen:__pkg__"])

cc_library(
name = "greeting",
hdrs = ["greeting.h"],
)

rust_bindgen(
name = "greeting_bindgen",
bindgen_flags = [
"--allowlist-var=GREETING",
"--generate-cstr",
],
cc_lib = ":greeting",
header = "greeting.h",
)

rust_library(
name = "hello_rs",
srcs = [
"hello.rs",
":greeting_bindgen",
],
crate_root = "hello.rs",
)

cc_library(
name = "hello_c",
srcs = ["hello.c"],
hdrs = ["hello.h"],
deps = [":greeting"],
)
6 changes: 6 additions & 0 deletions examples/bindgen/shared/greeting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __FFI_H_INCLUDE__
#define __FFI_H_INCLUDE__

#define GREETING "Hello"

#endif // __FFI_H_INCLUDE__
5 changes: 5 additions & 0 deletions examples/bindgen/shared/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

#include "greeting.h"

void hello(void) { printf("%s from C!\n", GREETING); }
6 changes: 6 additions & 0 deletions examples/bindgen/shared/hello.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef HELLO_H_INCLUDE__
#define HELLO_H_INCLUDE__

void hello(void);

#endif // HELLO_H_INCLUDE__
6 changes: 6 additions & 0 deletions examples/bindgen/shared/hello.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mod greeting_bindgen;

pub fn hello() {
let greeting = greeting_bindgen::GREETING.to_str().unwrap();
println!("{greeting} from Rust!");
}
File renamed without changes.