Skip to content

Commit

Permalink
Add shared-header bindgen example
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnrt committed Jul 16, 2024
1 parent 8425244 commit 061bbff
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 1 deletion.
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__
6 changes: 6 additions & 0 deletions examples/bindgen/shared/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdio.h>
#include "greeting.h"

void hello(void) {
printf("%s from C!", 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.

0 comments on commit 061bbff

Please sign in to comment.