From b957f98a304bb870c5715c5bfe8a5d71525706c3 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Fri, 5 Apr 2024 12:31:48 +0200 Subject: [PATCH] generate opaque types data inside build.rs instead of cmake --- CMakeLists.txt | 5 ----- build.rs | 28 ++++++++++++++++++++++++++-- src/collections.rs | 17 ++++++----------- src/commons.rs | 14 ++++++++++---- src/opaque_types/mod.rs | 15 --------------- 5 files changed, 42 insertions(+), 37 deletions(-) delete mode 100644 src/opaque_types/mod.rs diff --git a/CMakeLists.txt b/CMakeLists.txt index aac66278b..00aa5cac2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -179,10 +179,6 @@ set_genexpr_condition(cargo_flags 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 $ - "--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) @@ -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 diff --git a/build.rs b/build.rs index 03a271817..b14bdc4cd 100644 --- a/build.rs +++ b/build.rs @@ -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"; @@ -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()) @@ -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(); @@ -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}], }} " ); diff --git a/src/collections.rs b/src/collections.rs index 2990f1a3a..363c30a89 100644 --- a/src/collections.rs +++ b/src/collections.rs @@ -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(); @@ -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()); } } @@ -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) }) } } @@ -194,7 +190,6 @@ pub extern "C" fn z_bytes_check(b: &z_owned_bytes_t) -> bool { !b.start.is_null() } - impl From for z_bytes_t { #[inline] fn from(pid: ZenohId) -> Self { diff --git a/src/commons.rs b/src/commons.rs index e8a6bc5d6..5f8ede970 100644 --- a/src/commons.rs +++ b/src/commons.rs @@ -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(); @@ -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(); @@ -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 {} diff --git a/src/opaque_types/mod.rs b/src/opaque_types/mod.rs deleted file mode 100644 index 0327b0943..000000000 --- a/src/opaque_types/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// A split buffer that owns all of its data. -/// -/// To minimize copies and reallocations, Zenoh may provide you data in split buffers. -#[repr(C, align(8))] -pub struct z_owned_buffer_t { - _0: [u8; 40] -} -/// An owned sample. -/// -/// This is a read only type that can only be constructed by cloning a `z_sample_t`. -/// Like all owned types, its memory must be freed by passing a mutable reference to it to `zc_sample_drop`. -#[repr(C, align(8))] -pub struct zc_owned_sample_t { - _0: [u8; 224] -}