[*] Sandbox-friendly build implemented.

* CMake machinery simplified.
* Build artifacts not stored in source directories anymore.
This commit is contained in:
Patsakula Nikita 2023-10-13 14:54:12 +03:00 committed by Patsakula Nikita
parent 7dac364460
commit 0b5a74a16c
No known key found for this signature in database
GPG key ID: 954300EE01218A33
2 changed files with 56 additions and 89 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "whisper-rs-sys" name = "whisper-rs-sys"
version = "0.7.2" version = "0.7.3"
edition = "2021" edition = "2021"
description = "Rust bindings for whisper.cpp (FFI bindings)" description = "Rust bindings for whisper.cpp (FFI bindings)"
license = "Unlicense" license = "Unlicense"
@ -40,8 +40,8 @@ cuda = []
opencl = [] opencl = []
openblas = [] openblas = []
[dependencies]
[build-dependencies] [build-dependencies]
bindgen = "0.64" cmake = "0.1"
bindgen = "0.68"
cfg-if = "1" cfg-if = "1"
fs_extra = "1.3"

View file

@ -2,7 +2,7 @@
extern crate bindgen; extern crate bindgen;
use cfg_if::cfg_if; use cmake::Config;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
@ -22,8 +22,6 @@ fn main() {
} }
} }
println!("cargo:rustc-link-search={}", env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-lib=static=whisper");
#[cfg(feature = "coreml")] #[cfg(feature = "coreml")]
println!("cargo:rustc-link-lib=static=whisper.coreml"); println!("cargo:rustc-link-lib=static=whisper.coreml");
#[cfg(feature = "opencl")] #[cfg(feature = "opencl")]
@ -40,7 +38,7 @@ fn main() {
println!("cargo:rustc-link-lib=cublas"); println!("cargo:rustc-link-lib=cublas");
println!("cargo:rustc-link-lib=cudart"); println!("cargo:rustc-link-lib=cudart");
println!("cargo:rustc-link-lib=cublasLt"); println!("cargo:rustc-link-lib=cublasLt");
cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] { if #[cfg(target_os = "windows")] {
let cuda_path = PathBuf::from(env::var("CUDA_PATH").unwrap()).join("lib/x64"); let cuda_path = PathBuf::from(env::var("CUDA_PATH").unwrap()).join("lib/x64");
println!("cargo:rustc-link-search={}", cuda_path.display()); println!("cargo:rustc-link-search={}", cuda_path.display());
@ -53,17 +51,27 @@ fn main() {
} }
println!("cargo:rerun-if-changed=wrapper.h"); println!("cargo:rerun-if-changed=wrapper.h");
let out = PathBuf::from(env::var("OUT_DIR").unwrap());
let whisper_root = out.join("whisper.cpp/");
if !whisper_root.exists() {
std::fs::create_dir_all(&whisper_root).unwrap();
fs_extra::dir::copy("./whisper.cpp", &out, &Default::default()).unwrap_or_else(|e| {
panic!(
"Failed to copy whisper sources into {}: {}",
whisper_root.display(),
e
)
});
}
if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() { if env::var("WHISPER_DONT_GENERATE_BINDINGS").is_ok() {
let _: u64 = std::fs::copy( let _: u64 = std::fs::copy("src/bindings.rs", out.join("bindings.rs"))
"src/bindings.rs", .expect("Failed to copy bindings.rs");
env::var("OUT_DIR").unwrap() + "/bindings.rs",
)
.expect("Failed to copy bindings.rs");
} else { } else {
let bindings = bindgen::Builder::default() let bindings = bindgen::Builder::default()
.header("wrapper.h") .header("wrapper.h")
.clang_arg("-I./whisper.cpp") .clang_arg("-I./whisper.cpp")
.clang_arg("-I./ggml.c")
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) .parse_callbacks(Box::new(bindgen::CargoCallbacks))
.generate(); .generate();
@ -77,11 +85,8 @@ fn main() {
println!("cargo:warning=Unable to generate bindings: {}", e); println!("cargo:warning=Unable to generate bindings: {}", e);
println!("cargo:warning=Using bundled bindings.rs, which may be out of date"); println!("cargo:warning=Using bundled bindings.rs, which may be out of date");
// copy src/bindings.rs to OUT_DIR // copy src/bindings.rs to OUT_DIR
std::fs::copy( std::fs::copy("src/bindings.rs", out.join("bindings.rs"))
"src/bindings.rs", .expect("Unable to copy bindings.rs");
env::var("OUT_DIR").unwrap() + "/bindings.rs",
)
.expect("Unable to copy bindings.rs");
} }
} }
}; };
@ -91,86 +96,48 @@ fn main() {
return; return;
} }
// build libwhisper.a let mut config = Config::new(&whisper_root);
env::set_current_dir("whisper.cpp").expect("Unable to change directory to whisper.cpp");
_ = std::fs::remove_dir_all("build");
_ = std::fs::create_dir("build");
env::set_current_dir("build").expect("Unable to change directory to whisper.cpp build");
let mut cmd = std::process::Command::new("cmake"); config
cmd.arg("..") .profile("Release")
.arg("-DCMAKE_BUILD_TYPE=Release") .define("BUILD_SHARED_LIBS", "OFF")
.arg("-DBUILD_SHARED_LIBS=OFF") .define("WHISPER_ALL_WARNINGS", "OFF")
.arg("-DWHISPER_ALL_WARNINGS=OFF") .define("WHISPER_ALL_WARNINGS_3RD_PARTY", "OFF")
.arg("-DWHISPER_ALL_WARNINGS_3RD_PARTY=OFF") .define("WHISPER_BUILD_TESTS", "OFF")
.arg("-DWHISPER_BUILD_TESTS=OFF") .define("WHISPER_BUILD_EXAMPLES", "OFF")
.arg("-DWHISPER_BUILD_EXAMPLES=OFF"); .very_verbose(true)
.pic(true);
#[cfg(feature = "coreml")] if cfg!(feature = "coreml") {
cmd.arg("-DWHISPER_COREML=ON") config.define("WHISPER_COREML", "ON");
.arg("-DWHISPER_COREML_ALLOW_FALLBACK=1"); config.define("WHISPER_COREML_ALLOW_FALLBACK", "1");
#[cfg(feature = "cuda")]
cmd.arg("-DWHISPER_CUBLAS=ON");
#[cfg(feature = "openblas")]
cmd.arg("-DWHISPER_OPENBLAS=ON");
#[cfg(feature = "opencl")]
cmd.arg("-DWHISPER_CLBLAST=ON");
cmd.arg("-DCMAKE_POSITION_INDEPENDENT_CODE=ON");
let code = cmd
.status()
.expect("Failed to run `cmake` (is CMake installed?)");
if code.code() != Some(0) {
panic!("Failed to run `cmake` (is CMake installed?)");
} }
let code = std::process::Command::new("cmake") if cfg!(feature = "cuda") {
.arg("--build") config.define("WHISPER_CUBLAS", "ON");
.arg(".")
.arg("--config")
.arg("Release")
.status()
.expect("Failed to build libwhisper.a (is CMake installed?)");
if code.code() != Some(0) {
panic!("Failed to build libwhisper.a (is CMake installed?)");
} }
// move libwhisper.a to where Cargo expects it (OUT_DIR) if cfg!(feature = "openblas") {
cfg_if! { config.define("WHISPER_OPENBLAS", "ON");
if #[cfg(target_os = "windows")] {
std::fs::copy(
"Release/whisper.lib",
format!("{}/whisper.lib", env::var("OUT_DIR").unwrap()),
)
.expect("Failed to copy libwhisper.lib");
} else {
std::fs::copy(
"libwhisper.a",
format!("{}/libwhisper.a", env::var("OUT_DIR").unwrap()),
)
.expect("Failed to copy libwhisper.a");
}
} }
// if on iOS or macOS, with coreml feature enabled, copy libwhisper.coreml.a as well if cfg!(feature = "opencl") {
cfg_if! { config.define("WHISPER_CLBLAST", "ON");
if #[cfg(all(feature = "coreml", any(target_os = "ios", target_os = "macos")))]
{
std::fs::copy(
"libwhisper.coreml.a",
format!("{}/libwhisper.coreml.a", env::var("OUT_DIR").unwrap()),
)
.expect("Failed to copy libwhisper.coreml.a");
}
} }
// clean the whisper build directory to prevent Cargo from complaining during crate publish let destination = config.build();
env::set_current_dir("..").expect("Unable to change directory to whisper.cpp");
_ = std::fs::remove_dir_all("build"); if env::var("TARGET").unwrap().contains("window") {
println!(
"cargo:rustc-link-search={}",
out.join("build").join("Release").display()
);
} else {
println!("cargo:rustc-link-search={}", out.join("build").display());
}
println!("cargo:rustc-link-search=native={}", destination.display());
println!("cargo:rustc-link-lib=static=whisper");
// for whatever reason this file is generated during build and triggers cargo complaining // for whatever reason this file is generated during build and triggers cargo complaining
_ = std::fs::remove_file("bindings/javascript/package.json"); _ = std::fs::remove_file("bindings/javascript/package.json");
} }