From 82f0a0afad932da5ad64d010c5c324b4b48f0aa3 Mon Sep 17 00:00:00 2001 From: Rahul Date: Wed, 22 May 2024 15:21:11 -0700 Subject: [PATCH] Replace variadic arguments with fixed (#112) * variadic to fixed args * remove cargo fmt from generated files * remove extra tab * replace tabs by spaces for CI format failures * Update crates/xtask/src/codegen.rs Co-authored-by: Andrew Brown * Update crates/xtask/Cargo.toml Co-authored-by: Andrew Brown * cargo lock update --------- Co-authored-by: Andrew Brown --- Cargo.lock | 13 +++++---- .../openvino-sys/src/generated/functions.rs | 6 ++-- crates/xtask/Cargo.toml | 1 + crates/xtask/src/codegen.rs | 29 ++++++++++++++----- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bfc694e..a655136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -449,9 +449,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.5" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -461,9 +461,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" dependencies = [ "aho-corasick", "memchr", @@ -472,9 +472,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.5" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "rustc-hash" @@ -753,6 +753,7 @@ dependencies = [ "anyhow", "bindgen", "clap", + "regex", "semver", "toml", ] diff --git a/crates/openvino-sys/src/generated/functions.rs b/crates/openvino-sys/src/generated/functions.rs index 1985ee0..af277a1 100644 --- a/crates/openvino-sys/src/generated/functions.rs +++ b/crates/openvino-sys/src/generated/functions.rs @@ -640,7 +640,8 @@ extern "C" { #[doc = " @brief Sets properties for a device, acceptable keys can be found in ov_property_key_xxx.\n @ingroup ov_compiled_model_c_api\n @param compiled_model A pointer to the ov_compiled_model_t.\n @param ... variadic paramaters The format is .\n Supported property key please see ov_property.h.\n @return Status code of the operation: OK(0) for success."] pub fn ov_compiled_model_set_property( compiled_model: *const ov_compiled_model_t, - ... + property_key: *const ::std::os::raw::c_char, + property_value: *const ::std::os::raw::c_char ) -> ov_status_e; } extern "C" { @@ -765,7 +766,8 @@ extern "C" { pub fn ov_core_set_property( core: *const ov_core_t, device_name: *const ::std::os::raw::c_char, - ... + property_key: *const ::std::os::raw::c_char, + property_value: *const ::std::os::raw::c_char ) -> ov_status_e; } extern "C" { diff --git a/crates/xtask/Cargo.toml b/crates/xtask/Cargo.toml index fe14750..b6e542f 100644 --- a/crates/xtask/Cargo.toml +++ b/crates/xtask/Cargo.toml @@ -13,3 +13,4 @@ bindgen = "0.68.1" clap = { version = "4.4.4", features = ["derive"] } toml = "0.8.0" semver = "1.0" +regex = "1.10.4" diff --git a/crates/xtask/src/codegen.rs b/crates/xtask/src/codegen.rs index ae5ea01..9f39ddf 100644 --- a/crates/xtask/src/codegen.rs +++ b/crates/xtask/src/codegen.rs @@ -1,6 +1,7 @@ use crate::util::path_to_crates; use anyhow::{anyhow, ensure, Context, Result}; use clap::Args; +use regex::Regex; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::{Path, PathBuf}; @@ -38,6 +39,20 @@ impl CodegenCommand { // Generate the function bindings into `.../functions.rs`, with a prefix and suffix. let function_bindings = Self::generate_function_bindings(&header_file)?; + + // Runtime linking doesn't work yet with variadic args (...), so we need to convert them + // to a fixed pair of args (property_key, property_value) for a few select functions. + // This is a workaround until the runtime linking is updated to support variadic args. + let functions_to_modify = vec!["ov_core_set_property", "ov_compiled_model_set_property"]; + let mut function_bindings_string = function_bindings.to_string(); + for function in &functions_to_modify { + let re = Regex::new(&format!(r"(?s){}.*?\.\.\.", function)).unwrap(); + if re.is_match(&function_bindings_string) { + function_bindings_string = re.replace(&function_bindings_string, |caps: ®ex::Captures| { + caps[0].replace("...", "property_key: *const ::std::os::raw::c_char,\n property_value: *const ::std::os::raw::c_char") + }).to_string(); + } + } let function_bindings_path = output_directory.join(FUNCTIONS_FILE); { let mut function_bindings_file = Box::new(File::create(&function_bindings_path)?); @@ -46,14 +61,12 @@ impl CodegenCommand { function_bindings_file.write_all(b"type wchar_t = ::std::os::raw::c_char;\n")?; function_bindings_file.write_all(b"link! {\n")?; function_bindings_file.write_all(b"\n")?; - function_bindings - .write(function_bindings_file) - .with_context(|| { - format!( - "Failed to write functions to: {}", - &function_bindings_path.display() - ) - })?; + function_bindings_file + .write_all(function_bindings_string.as_bytes()) + .context(format!( + "Failed to write functions to: {}", + &function_bindings_path.display() + ))?; } let mut function_bindings_file = OpenOptions::new()