build.rs (3325B)
1 use cmake::Config; 2 use std::env; 3 use std::path::{Path, PathBuf}; 4 5 macro_rules! debug_log { 6 ($($arg:tt)*) => { 7 if std::env::var("BUILD_DEBUG").is_ok() { 8 println!("cargo:warning=[DEBUG] {}", format!($($arg)*)); 9 } 10 }; 11 } 12 13 fn copy_folder(src: &Path, dst: &Path) { 14 std::fs::create_dir_all(dst).expect("Failed to create dst directory"); 15 if cfg!(unix) { 16 std::process::Command::new("cp") 17 .arg("-rf") 18 .arg(src) 19 .arg(dst.parent().expect("no parent")) 20 .status() 21 .expect("Failed to execute cp command"); 22 } 23 24 if cfg!(windows) { 25 std::process::Command::new("robocopy.exe") 26 .arg("/e") 27 .arg(src) 28 .arg(dst) 29 .status() 30 .expect("Failed to execute robocopy command"); 31 } 32 } 33 34 fn main() { 35 let target = env::var("TARGET").expect("no target"); 36 let out_dir = PathBuf::from(env::var("OUT_DIR").expect("no out dir")); 37 let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get CARGO_MANIFEST_DIR"); 38 let knf_src = Path::new(&manifest_dir).join("knf"); 39 let knf_dst = out_dir.join("knf"); 40 let knfc_src = Path::new(&manifest_dir).join("knfc"); 41 let knfc_dst = out_dir.join("knfc"); 42 let static_crt = env::var("KNF_STATIC_CRT").map(|v| v == "1").unwrap_or(false); 43 44 let profile = if cfg!(debug_assertions) { 45 "Debug" 46 } else { 47 "Release" 48 }; 49 50 debug_log!("TARGET: {}", target); 51 debug_log!("CARGO_MANIFEST_DIR: {}", manifest_dir); 52 debug_log!("OUT_DIR: {}", out_dir.display()); 53 54 if !knf_dst.exists() { 55 debug_log!("Copy {} to {}", knf_src.display(), knf_dst.display()); 56 copy_folder(&knf_src, &knf_dst); 57 } 58 59 if !knfc_dst.exists() { 60 debug_log!("Copy {} to {}", knfc_src.display(), knfc_dst.display()); 61 copy_folder(&knfc_src, &knfc_dst); 62 } 63 64 // Bindings 65 let bindings = bindgen::Builder::default() 66 .header("wrapper.hpp") 67 .clang_arg(format!("-I{}", knfc_dst.display())) 68 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) 69 .generate() 70 .expect("Failed to generate bindings"); 71 72 // Write the generated bindings to an output file 73 let bindings_path = out_dir.join("bindings.rs"); 74 bindings 75 .write_to_file(bindings_path) 76 .expect("Failed to write bindings"); 77 78 println!("cargo:rerun-if-changed=./knf"); 79 println!("cargo:rerun-if-changed=./knfc"); 80 println!("cargo:rerun-if-changed=wrapper.h"); 81 82 debug_log!("Bindings Created"); 83 84 let mut config = Config::new(&knfc_dst); 85 86 if cfg!(windows) { 87 config.static_crt(static_crt); 88 debug_log!("STATIC_CRT: {}", static_crt); 89 } 90 91 config 92 .profile(profile) 93 .very_verbose(std::env::var("CMAKE_VERBOSE").is_ok()) // Not verbose by default 94 .always_configure(false) 95 .build(); 96 97 println!("cargo:rustc-link-search={}", out_dir.join("lib").display()); 98 99 // Link 100 if cfg!(target_os = "macos") { 101 println!("cargo:rustc-link-lib=c++"); 102 } 103 104 if cfg!(target_os = "linux") { 105 println!("cargo:rustc-link-lib=stdc++"); 106 } 107 108 println!("cargo:rustc-link-lib=static={}", "knfc"); 109 println!("cargo:rustc-link-lib=static={}", "kaldi-native-fbank-core"); 110 }