Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build.rs #1650

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ extern crate pkg_config;
use std::env;
use std::path::PathBuf;

fn main() {
const RUSTIFIED_ENUMS: &[&str] = &[
"dtvcc_(window|pen)_.*",
"ccx_output_format",
"ccx_output_date_format",
];

fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut allowlist_functions = Vec::new();
allowlist_functions.extend_from_slice(&[
".*(?i)_?dtvcc_.*",
Expand All @@ -13,15 +19,15 @@ fn main() {
"version",
"set_binary_mode",
]);

#[cfg(feature = "hardsubx_ocr")]
allowlist_functions.extend_from_slice(&[
"edit_distance",
"convert_pts_to_.*",
"av_rescale_q",
"mprint",
]);

let mut allowlist_types = Vec::new();
allowlist_types.extend_from_slice(&[
".*(?i)_?dtvcc_.*",
Expand All @@ -41,54 +47,49 @@ fn main() {
"uint8_t",
"word_list",
]);

#[cfg(feature = "hardsubx_ocr")]
allowlist_types.extend_from_slice(&["AVRational", "AVPacket", "AVFrame"]);

let mut builder = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header("wrapper.h");

// enable hardsubx if and only if the feature is on
#[cfg(feature = "hardsubx_ocr")]
{
builder = builder.clang_arg("-DENABLE_HARDSUBX");
}

// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
builder = builder.parse_callbacks(Box::new(bindgen::CargoCallbacks));

for type_name in allowlist_types {
builder = builder.allowlist_type(type_name);
}

for fn_name in allowlist_functions {
builder = builder.allowlist_function(fn_name);
}

for rust_enum in RUSTIFIED_ENUMS {
builder = builder.rustified_enum(rust_enum);
}

let bindings = builder
.derive_default(true)
.no_default("dtvcc_pen_attribs|dtvcc_pen_color|dtvcc_symbol")
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Convert to Result
.map_err(|e| format!("Unable to generate bindings: {}", e))?;
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let out_path = PathBuf::from(env::var("OUT_DIR")?);
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
.map_err(|e| format!("Couldn't write bindings: {}", e))?;

Ok(())
}

const RUSTIFIED_ENUMS: &[&str] = &[
"dtvcc_(window|pen)_.*",
"ccx_output_format",
"ccx_output_date_format",
];
Loading