Skip to content

Commit

Permalink
Replace variadic arguments with fixed (#112)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Update crates/xtask/Cargo.toml

Co-authored-by: Andrew Brown <[email protected]>

* cargo lock update

---------

Co-authored-by: Andrew Brown <[email protected]>
  • Loading branch information
rahulchaphalkar and abrown committed May 22, 2024
1 parent 6617fa6 commit 82f0a0a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/openvino-sys/src/generated/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <char *property_key, char* property_value>.\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" {
Expand Down Expand Up @@ -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" {
Expand Down
1 change: 1 addition & 0 deletions crates/xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
29 changes: 21 additions & 8 deletions crates/xtask/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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: &regex::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)?);
Expand All @@ -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()
Expand Down

0 comments on commit 82f0a0a

Please sign in to comment.