Skip to content

Commit

Permalink
[eclipse-iceoryx#439] Use prebuilt bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
elBoberido committed Oct 11, 2024
1 parent 6d2b91e commit 87e1d7d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
24 changes: 14 additions & 10 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ rust_register_toolchains(
)


# Load bindgen rules
load("@rules_rust//bindgen:repositories.bzl", "rust_bindgen_dependencies", "rust_bindgen_register_toolchains")

rust_bindgen_dependencies()

rust_bindgen_register_toolchains()

load("@rules_rust//bindgen:transitive_repositories.bzl", "rust_bindgen_transitive_dependencies")

rust_bindgen_transitive_dependencies()
# Load prebuilt bindgen
http_archive(
name = "bindgen",
sha256 = "b7e2321ee8c617f14ccc5b9f39b3a804db173ee217e924ad93ed16af6bc62b1d",
strip_prefix = "bindgen-cli-x86_64-unknown-linux-gnu",
urls = ["https://github.com/rust-lang/rust-bindgen/releases/download/v0.69.5/bindgen-cli-x86_64-unknown-linux-gnu.tar.xz"],
build_file_content = """
filegroup(
name = "bindgen-cli",
srcs = ["bindgen"],
visibility = ["//visibility:public"],
)
""",
)


# Load external crates
Expand Down
23 changes: 12 additions & 11 deletions iceoryx2-pal/posix/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package(default_visibility = ["//visibility:public"])

load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//rust:defs.bzl", "rust_library")
load("@rules_rust//bindgen:defs.bzl", "rust_bindgen_library")

# Generate the binding lib
cc_library(
Expand All @@ -31,29 +30,31 @@ cc_library(
)

# Generate the Rust binding file
# This will generate 'posix_generated__bindgen.rs'
rust_bindgen_library(
name = "posix_generated",
bindgen_flags = [
"--blocklist-type=max_align_t",
genrule(
name = "iceoryx2-pal-posix-bindgen",
srcs = [
"src/c/posix.h",
"@bindgen//:bindgen-cli",
],
cc_lib = "iceoryx2-pal-posix-c-lib",
header = "src/c/posix.h",
wrap_static_fns = True,
outs = ["src/posix_generated.rs"],
cmd = "$(execpath @bindgen//:bindgen-cli) --use-core --blocklist-type max_align_t $(location src/c/posix.h) --output $(OUTS)",
)

# Create the actual crate
rust_library(
name = "iceoryx2-pal-posix",
srcs = glob(["src/**/*.rs"]) + [ ":posix_generated__bindgen.rs" ],
srcs = glob(["src/**/*.rs"]) + [ ":src/posix_generated.rs" ],
deps = [
"//iceoryx2-pal/concurrency-sync:iceoryx2-pal-concurrency-sync",
"//iceoryx2-pal/configuration:iceoryx2-pal-configuration",
"@crate_index//:lazy_static",
":iceoryx2-pal-posix-c-lib",
],
rustc_env = {
"OUT_DIR": ".." # the file is generated in 'posix', not in 'posix/src' where the 'lib.rs' is located; therefore the relative path of '..'
# - the file is generated in 'posix/src'
# - 'lib.rs' expects the location at 'env!("OUT_DIR")/posix_generated.rs'
# - therefore 'OUT_DIR' needs to point to the same dir where 'lib.rs' is located
"OUT_DIR": "."
},
)

Expand Down
5 changes: 1 addition & 4 deletions iceoryx2-pal/posix/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,8 @@ fn main() {
};

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
// bazel generates 'posix_generated__bindgen.rs' and there is no way to set the name
// the simplest solution is to use the same name here; the workaround in bazel would
// require to have a 'genrule' and copy the file
bindings
.write_to_file(out_path.join("posix_generated__bindgen.rs"))
.write_to_file(out_path.join("posix_generated.rs"))
.expect("Couldn't write bindings!");

println!("cargo:rerun-if-changed=src/c/sigaction.c");
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2-pal/posix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) mod internal {
#![allow(improper_ctypes)]
#![allow(unknown_lints)]
#![allow(clippy::all)]
include!(concat!(env!("OUT_DIR"), "/posix_generated__bindgen.rs"));
include!(concat!(env!("OUT_DIR"), "/posix_generated.rs"));

pub const ESUCCES: u32 = 0;
}
Expand Down

0 comments on commit 87e1d7d

Please sign in to comment.