Skip to content

Commit

Permalink
[eclipse-iceoryx#349] Make 'posix_generated' available for external b…
Browse files Browse the repository at this point in the history
…azel builds
  • Loading branch information
elBoberido committed Oct 11, 2024
1 parent 50f0d12 commit 89c4666
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
18 changes: 13 additions & 5 deletions iceoryx2-pal/posix/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package(default_visibility = ["//visibility:public"])

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

# Generate the binding lib
Expand All @@ -36,25 +37,32 @@ genrule(
"src/c/posix.h",
"@bindgen//:bindgen-cli",
],
outs = ["src/posix_generated.rs"],
outs = ["posix_generated.rs"],
cmd = "$(execpath @bindgen//:bindgen-cli) --use-core --blocklist-type max_align_t $(location src/c/posix.h) --output $(OUTS)",
)

cargo_build_script(
name = "bazel_bindgen_workaround",
srcs = ["bazel_bindgen_workaround.rs"],
)

# Create the actual crate
rust_library(
name = "iceoryx2-pal-posix",
srcs = glob(["src/**/*.rs"]) + [ ":src/posix_generated.rs" ],
srcs = glob(["src/**/*.rs"]),
compile_data = [ "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",
":bazel_bindgen_workaround",
],
rustc_env = {
# - 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": "."
# - 'OUT_DIR' is set by 'bazel_bindgen_workaround' and in the same folder like 'posix_generated.rs'
# - therefore the path needs to be corrected
"BAZEL_BINDGEN_PATH_CORRECTION": "/.."
},
)

Expand Down
17 changes: 17 additions & 0 deletions iceoryx2-pal/posix/bazel_bindgen_workaround.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT


// This file is used to make bazel generate the `OUT_DIR` environment variable
// to let rustc find the `posix_generated.rs` file

fn main() {}
3 changes: 3 additions & 0 deletions iceoryx2-pal/posix/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ fn main() {
}
};

// needed for bazel but can be empty for cargo builds
println!("cargo:rustc-env=BAZEL_BINDGEN_PATH_CORRECTION=");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("posix_generated.rs"))
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.rs"));
include!(concat!(env!("OUT_DIR"), env!("BAZEL_BINDGEN_PATH_CORRECTION"), "/posix_generated.rs"));

pub const ESUCCES: u32 = 0;
}
Expand Down

0 comments on commit 89c4666

Please sign in to comment.