Skip to content

Commit

Permalink
add target option to com-scrape and vst3-bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Feb 4, 2024
1 parent a6f29d5 commit 83fc872
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let bindings = File::create(Path::new(&out_dir).join("bindings.rs")).unwrap();
let sink = BufWriter::new(bindings);

if let Err(err) = generate(Path::new(&vst3_sdk_dir), sink) {
if let Err(err) = generate(Path::new(&vst3_sdk_dir), None, sink) {
eprintln!("{}", err);
process::exit(1);
}
Expand Down
12 changes: 11 additions & 1 deletion com-scrape/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn rust_to_clang_target(rust_target: &str) -> String {
/// Builder struct for configuring and generating bindings.
pub struct Generator {
pub(crate) include_paths: Vec<PathBuf>,
pub(crate) target: Option<String>,
pub(crate) skip_types: HashSet<String>,
pub(crate) skip_interface_traits: HashSet<String>,
pub(crate) constant_parser: Option<Box<dyn Fn(&[String]) -> Option<String>>>,
Expand All @@ -37,6 +38,7 @@ impl Default for Generator {
fn default() -> Generator {
Generator {
include_paths: Vec::new(),
target: None,
skip_types: HashSet::new(),
skip_interface_traits: HashSet::new(),
constant_parser: None,
Expand All @@ -55,6 +57,12 @@ impl Generator {
self
}

/// Specify the target triple for which bindings should be generated.
pub fn target<T: AsRef<str>>(mut self, target: T) -> Self {
self.target = Some(target.as_ref().to_string());
self
}

/// Do not generate bindings for `type_`.
pub fn skip_type<T: AsRef<str>>(mut self, type_: T) -> Self {
self.skip_types.insert(type_.as_ref().to_string());
Expand Down Expand Up @@ -145,7 +153,9 @@ impl Generator {
clang_sys::load()?;
}

let rust_target = if let Ok(target) = env::var("TARGET") {
let rust_target = if let Some(target) = &self.target {
Cow::from(target)
} else if let Ok(target) = env::var("TARGET") {
Cow::from(target)
} else {
Cow::from(HOST_TARGET)
Expand Down
19 changes: 14 additions & 5 deletions vst3-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ fn parse_iid(tokens: &[String]) -> Option<String> {
}

/// Generates Rust bindings given a path to the VST 3 SDK.
pub fn generate(sdk_dir: &Path, mut sink: impl Write) -> Result<(), Box<dyn Error>> {
pub fn generate(
sdk_dir: &Path,
target: Option<&str>,
mut sink: impl Write,
) -> Result<(), Box<dyn Error>> {
let pluginterfaces_path = sdk_dir.join("pluginterfaces");
let headers = find_headers(&pluginterfaces_path)?;

Expand All @@ -59,7 +63,7 @@ pub fn generate(sdk_dir: &Path, mut sink: impl Write) -> Result<(), Box<dyn Erro

let mut source = String::new();
for header in &headers {
let relative = header.strip_prefix(&sdk_dir).unwrap();
let relative = header.strip_prefix(sdk_dir).unwrap();
if skip_headers.contains(relative) {
continue;
}
Expand All @@ -75,7 +79,7 @@ pub fn generate(sdk_dir: &Path, mut sink: impl Write) -> Result<(), Box<dyn Erro

writeln!(sink, "{}", include_str!("support.rs"))?;

com_scrape::Generator::default()
let mut generator = com_scrape::Generator::default()
.skip_types(&[
"Adopt",
"ConstStringTable",
Expand All @@ -89,8 +93,13 @@ pub fn generate(sdk_dir: &Path, mut sink: impl Write) -> Result<(), Box<dyn Erro
.query_interface_fn("crate::__bindings::FUnknown_query_interface")
.add_ref_fn("crate::__bindings::FUnknown_add_ref")
.release_fn("crate::__bindings::FUnknown_release")
.include_path(&sdk_dir)
.generate(source, &mut sink)?;
.include_path(sdk_dir);

if let Some(target) = target {
generator = generator.target(target);
}

generator.generate(source, &mut sink)?;

writeln!(sink)?;
writeln!(sink, "}}")?;
Expand Down

0 comments on commit 83fc872

Please sign in to comment.