Skip to content

Commit

Permalink
generate opaque types data inside build.rs instead of cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Apr 5, 2024
1 parent 6d005bb commit b957f98
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 37 deletions.
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ set_genexpr_condition(cargo_flags DEBUG $<CONFIG:Debug>
"--manifest-path=${cargo_toml_dir_debug}/Cargo.toml"
"--release;--manifest-path=${cargo_toml_dir_release}/Cargo.toml")
set(cargo_flags ${cargo_flags} ${ZENOHC_CARGO_FLAGS})
set_genexpr_condition(cargo_dep_flags DEBUG $<CONFIG:Debug>
"--manifest-path=${CMAKE_CURRENT_SOURCE_DIR}/build-resources/opaque-types/Cargo.toml"
"--release;--manifest-path=${CMAKE_CURRENT_SOURCE_DIR}/build-resources/opaque-types/Cargo.toml")
set(cargo_dep_flags ${cargo_dep_flags} ${ZENOHC_CARGO_FLAGS})

if(ZENOHC_BUILD_WITH_LOGGER_AUTOINIT)
set(cargo_flags ${cargo_flags} --features=logger-autoinit)
Expand All @@ -203,7 +199,6 @@ add_custom_command(
OUTPUT ${libs}
COMMAND ${CMAKE_COMMAND} -E echo \"RUSTFLAGS = $$RUSTFLAGS\"
COMMAND ${CMAKE_COMMAND} -E echo \"cargo +${ZENOHC_CARGO_CHANNEL} build ${cargo_flags}\"
COMMAND cargo +${ZENOHC_CARGO_CHANNEL} build ${cargo_dep_flags} &> ${CMAKE_CURRENT_SOURCE_DIR}/.build_resources_opaque_types.txt || echo ""
COMMAND cargo +${ZENOHC_CARGO_CHANNEL} build ${cargo_flags}
VERBATIM
COMMAND_EXPAND_LISTS
Expand Down
28 changes: 26 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use fs2::FileExt;
use regex::Regex;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::{borrow::Cow, collections::HashMap, io::BufWriter, path::Path};

const GENERATION_PATH: &str = "include/zenoh-gen.h";
Expand All @@ -24,6 +26,8 @@ const HEADER: &str = r"//
#endif
";

use std::env;

fn main() {
generate_opaque_types();
cbindgen::generate(std::env::var("CARGO_MANIFEST_DIR").unwrap())
Expand All @@ -42,9 +46,29 @@ fn main() {
println!("cargo:rerun-if-changed=build-resources")
}

fn produce_opaque_types_data() -> PathBuf {
let target = env::var("TARGET").unwrap();
let current_folder = std::env::current_dir().unwrap();
let manifest_path = current_folder.join("./build-resources/opaque-types/Cargo.toml");
let output_file_path = current_folder.join("./.build_resources_opaque_types.txt");
let out_file = std::fs::File::create(output_file_path.clone()).unwrap();
let stdio = Stdio::from(out_file);
let _ = Command::new("cargo")
.arg("build")
.arg("--target")
.arg(target)
.arg("--manifest-path")
.arg(manifest_path)
.stderr(stdio)
.output()
.unwrap();

output_file_path
}

fn generate_opaque_types() {
let current_folder = std::env::current_dir().unwrap();
let path_in = current_folder.join("./.build_resources_opaque_types.txt");
let path_in = produce_opaque_types_data();
let path_out = current_folder.join("./src/opaque_types/mod.rs");

let data_in = std::fs::read_to_string(path_in).unwrap();
Expand All @@ -56,7 +80,7 @@ fn generate_opaque_types() {
let s = format!(
"#[repr(C, align({align}))]
pub struct {type_name} {{
_0: [u8; {size}]
_0: [u8; {size}],
}}
"
);
Expand Down
17 changes: 6 additions & 11 deletions src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Drop for z_owned_bytes_t {
}

impl z_owned_bytes_t {
pub fn new(data: &[u8]) ->z_owned_bytes_t {
if data.len() == 0 {
pub fn new(data: &[u8]) -> z_owned_bytes_t {
if data.is_empty() {
return z_bytes_null();
}
let data = data.to_vec().into_boxed_slice();
Expand All @@ -83,17 +83,13 @@ impl z_owned_bytes_t {
let data = vec![0u8; len].into_boxed_slice();
z_owned_bytes_t {
len,
start: Box::leak(data).as_mut_ptr(),
start: Box::leak(data).as_mut_ptr(),
}
}

#[allow(clippy::missing_safety_doc)]
pub unsafe fn insert_unchecked(&mut self, start: usize, value: &[u8]) {
std::ptr::copy_nonoverlapping(
value.as_ptr(),
(self.start as *mut u8).add(start),
value.len(),
);
std::ptr::copy_nonoverlapping(value.as_ptr(), self.start.add(start), value.len());
}
}

Expand Down Expand Up @@ -182,9 +178,9 @@ pub const extern "C" fn z_bytes_loan(b: &z_owned_bytes_t) -> z_bytes_t {
#[no_mangle]
pub extern "C" fn z_bytes_clone(b: &z_bytes_t) -> z_owned_bytes_t {
if !z_bytes_is_initialized(b) {
return z_bytes_null();
z_bytes_null()
} else {
return z_owned_bytes_t::new(unsafe { std::slice::from_raw_parts(b.start, b.len) } )
z_owned_bytes_t::new(unsafe { std::slice::from_raw_parts(b.start, b.len) })
}
}

Expand All @@ -194,7 +190,6 @@ pub extern "C" fn z_bytes_check(b: &z_owned_bytes_t) -> bool {
!b.start.is_null()
}


impl From<ZenohId> for z_bytes_t {
#[inline]
fn from(pid: ZenohId) -> Self {
Expand Down
14 changes: 10 additions & 4 deletions src/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ pub extern "C" fn zc_payload_clone(payload: zc_payload_t) -> zc_owned_payload_t
/// Decodes payload into null-terminated string
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn zc_payload_decode_into_string(payload: zc_payload_t, cstr: &mut z_owned_str_t) -> i8 {
pub unsafe extern "C" fn zc_payload_decode_into_string(
payload: zc_payload_t,
cstr: &mut z_owned_str_t,
) -> i8 {
let payload: Option<&ZBuf> = payload.into();
if payload.is_none() {
*cstr = z_str_null();
Expand All @@ -154,13 +157,16 @@ pub unsafe extern "C" fn zc_payload_decode_into_string(payload: zc_payload_t, cs
cstr.insert_unchecked(pos, s);
pos += s.len();
}
return 0;
0
}

/// Decodes payload into null-terminated string
#[no_mangle]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn zc_payload_decode_into_bytes(payload: zc_payload_t, b: &mut z_owned_bytes_t) -> i8 {
pub unsafe extern "C" fn zc_payload_decode_into_bytes(
payload: zc_payload_t,
b: &mut z_owned_bytes_t,
) -> i8 {
let payload: Option<&ZBuf> = payload.into();
if payload.is_none() {
*b = z_bytes_null();
Expand All @@ -174,7 +180,7 @@ pub unsafe extern "C" fn zc_payload_decode_into_bytes(payload: zc_payload_t, b:
b.insert_unchecked(pos, s);
pos += s.len();
}
return 0;
0
}

unsafe impl Send for z_bytes_t {}
Expand Down
15 changes: 0 additions & 15 deletions src/opaque_types/mod.rs

This file was deleted.

0 comments on commit b957f98

Please sign in to comment.