diff --git a/crates/sherpa-rs-sys/build.rs b/crates/sherpa-rs-sys/build.rs index b8cbaff..80b9b5e 100644 --- a/crates/sherpa-rs-sys/build.rs +++ b/crates/sherpa-rs-sys/build.rs @@ -72,15 +72,6 @@ fn get_cargo_target_dir() -> Result = Vec::new(); - - // Download libraries, cache and set SHERPA_LIB_PATH - #[cfg(feature = "download-binaries")] let mut optional_dist: Option = None; + let mut sherpa_libs: Vec = Vec::new(); + #[cfg(feature = "download-binaries")] { + // Download libraries, cache and set SHERPA_LIB_PATH use download_binaries::{extract_tbz, fetch_file, get_cache_dir, sha256, DIST_TABLE}; debug_log!("Download binaries enabled"); // debug_log!("Dist table: {:?}", DIST_TABLE.targets); @@ -434,22 +377,7 @@ fn main() { sherpa_libs = libs .iter() - .map(|p| { - Path::new(p) - .file_name() - .unwrap() - .to_string_lossy() - // Remove lib prefix - .strip_prefix("lib") - .unwrap_or_else(|| p) - // Remove .so - .replace(".so", "") - // Remove .dylib - .replace(".dylib", "") - // Remove .a - .replace(".a", "") - .to_string() - }) + .map(|p| download_binaries::extract_lib_name(p)) .collect(); } else { sherpa_libs = extract_lib_names(&lib_dir, is_dynamic, &target_os); @@ -469,16 +397,64 @@ fn main() { } } else { // Build with CMake - let bindings_dir = config.build(); - add_search_path(&bindings_dir); + let profile = env::var("SHERPA_LIB_PROFILE").unwrap_or("Release".to_string()); + let static_crt = env::var("SHERPA_STATIC_CRT") + .map(|v| v == "1") + .unwrap_or(true); + let mut config = Config::new(&sherpa_dst); + + config + .define("SHERPA_ONNX_ENABLE_C_API", "ON") + .define("SHERPA_ONNX_ENABLE_BINARY", "OFF") + .define("BUILD_SHARED_LIBS", if is_dynamic { "ON" } else { "OFF" }) + .define("SHERPA_ONNX_ENABLE_WEBSOCKET", "OFF") + .define("SHERPA_ONNX_ENABLE_TTS", "OFF") + .define("SHERPA_ONNX_BUILD_C_API_EXAMPLES", "OFF"); + + if target_os == "windows" { + config.static_crt(static_crt); + } + + // TTS + if cfg!(feature = "tts") { + config.define("SHERPA_ONNX_ENABLE_TTS", "ON"); + } + + // Cuda https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html + if cfg!(feature = "cuda") { + debug_log!("Cuda enabled"); + config.define("SHERPA_ONNX_ENABLE_GPU", "ON"); + config.define("BUILD_SHARED_LIBS", "ON"); + } + + // DirectML https://onnxruntime.ai/docs/execution-providers/DirectML-ExecutionProvider.html + if cfg!(feature = "directml") { + debug_log!("DirectML enabled"); + config.define("SHERPA_ONNX_ENABLE_DIRECTML", "ON"); + config.define("BUILD_SHARED_LIBS", "ON"); + } + + if target_os == "windows" || target_os == "linux" || target == "android" { + config.define("SHERPA_ONNX_ENABLE_PORTAUDIO", "ON"); + } + + // General + config + .profile(&profile) + .very_verbose(std::env::var("CMAKE_VERBOSE").is_ok()) // Not verbose by default + .always_configure(false); + + let build_dir = config.build(); + add_search_path(&build_dir); // Extract libs on desktop platforms if !is_mobile { - sherpa_libs = extract_lib_names(&bindings_dir, is_dynamic, &target_os); + sherpa_libs = extract_lib_names(&build_dir, is_dynamic, &target_os); } } - // Search paths + // Linking + debug_log!("Sherpa libs: {:?}", sherpa_libs); add_search_path(out_dir.join("lib")); @@ -518,13 +494,8 @@ fn main() { } } - // TODO: add rpath for Android and iOS so it can find its dependencies in the same directory of executable - // if is_mobile { - // // Add rpath for Android and iOS so that the shared library can find its dependencies in the same directory as well - // println!("cargo:rustc-link-arg=-Wl,-rpath,'$ORIGIN'"); - // } + // Copy dynamic libraries - // copy DLLs to target if is_dynamic { let mut libs_assets = extract_lib_assets(&out_dir, &target_os); if let Ok(sherpa_lib_path) = env::var("SHERPA_LIB_PATH") { @@ -567,5 +538,11 @@ fn main() { copy_file(asset.clone(), dst); } } + + // TODO: add rpath for Android and iOS so it can find its dependencies in the same directory of executable + // if is_mobile { + // // Add rpath for Android and iOS so that the shared library can find its dependencies in the same directory as well + // println!("cargo:rustc-link-arg=-Wl,-rpath,'$ORIGIN'"); + // } } } diff --git a/crates/sherpa-rs-sys/src/download_binaries.rs b/crates/sherpa-rs-sys/src/download_binaries.rs index ef6d0a9..0452c90 100644 --- a/crates/sherpa-rs-sys/src/download_binaries.rs +++ b/crates/sherpa-rs-sys/src/download_binaries.rs @@ -252,3 +252,17 @@ pub fn extract_tbz(buf: &[u8], output: &Path) { let mut archive = tar::Archive::new(tar); archive.unpack(output).expect("Failed to extract .tbz file"); } + +pub fn extract_lib_name>(path: P) -> String { + path.as_ref() + .file_name() + .and_then(|name| name.to_str()) + .map(|name| { + name.strip_prefix("lib") + .unwrap_or(name) + .replace(".so", "") + .replace(".dylib", "") + .replace(".a", "") + }) + .unwrap_or_else(|| "".to_string()) +} diff --git a/examples/vad.rs b/examples/vad.rs index b3a388f..6e45c37 100644 --- a/examples/vad.rs +++ b/examples/vad.rs @@ -1,6 +1,6 @@ /* Detect voice in audio file and mark start and stop. -s + wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx wget https://github.com/k2-fsa/sherpa-onnx/releases/download/speaker-recongition-models/nemo_en_speakerverification_speakernet.onnx wget https://github.com/thewh1teagle/sherpa-rs/releases/download/v0.1.0/motivation.wav -O motivation.wav