diff --git a/sys/Cargo.toml b/sys/Cargo.toml index 0bccdac..16b7ac3 100644 --- a/sys/Cargo.toml +++ b/sys/Cargo.toml @@ -6,6 +6,7 @@ description = "Rust bindings for whisper.cpp (FFI bindings)" license = "Unlicense" documentation = "https://docs.rs/whisper-rs-sys" repository = "https://github.com/tazz4843/whisper-rs" +links = "whisper" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/sys/build.rs b/sys/build.rs index 5858c69..6c9bcc9 100644 --- a/sys/build.rs +++ b/sys/build.rs @@ -12,11 +12,39 @@ fn main() { .header("wrapper.h") .clang_arg("-I./whisper.cpp") .parse_callbacks(Box::new(bindgen::CargoCallbacks)) - .generate() - .expect("Unable to generate bindings"); + .generate(); - let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); - bindings - .write_to_file(out_path.join("bindings.rs")) - .expect("Couldn't write bindings!"); + match bindings { + Ok(b) => { + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + b.write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); + } + Err(e) => { + println!("cargo:warning=Unable to generate bindings: {}", e); + println!("cargo:warning=Using bundled bindings.rs, which may be out of date"); + // copy src/bindings.rs to OUT_DIR + std::fs::copy( + "src/bindings.rs", + env::var("OUT_DIR").unwrap() + "/bindings.rs", + ) + .expect("Unable to copy bindings.rs"); + } + } + + // build libwhisper.a + env::set_current_dir("whisper.cpp").expect("Unable to change directory"); + let code = std::process::Command::new("make") + .arg("libwhisper.a") + .status() + .expect("Failed to build libwhisper.a"); + if code.code() != Some(0) { + panic!("Failed to build libwhisper.a"); + } + // move libwhisper.a to where Cargo expects it (OUT_DIR) + std::fs::copy( + "libwhisper.a", + format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()), + ) + .expect("Failed to copy libwhisper.a"); }