-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
77 lines (68 loc) · 3.47 KB
/
Copy pathbuild.rs
File metadata and controls
77 lines (68 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::env;
use std::path::PathBuf;
fn main() {
println!("cargo:rerun-if-env-changed=ECHOLET_NATIVE_LIB_DIR");
println!("cargo:rerun-if-env-changed=ECHOLET_BUNDLE_BUILD");
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
// 1. Determine native library directory
let native_lib_dir = if let Ok(custom_dir) = env::var("ECHOLET_NATIVE_LIB_DIR") {
PathBuf::from(custom_dir)
} else {
manifest_dir.join(".local-runtime/runtime/lib")
};
// 2. Validate that required native shared libraries / import libraries exist
let (lib_name, prep_script) = if target_os == "macos" {
("libsherpa-onnx-c-api.dylib", "./scripts/macos/prepare-assets.sh")
} else if target_os == "windows" {
("sherpa-onnx-c-api.lib", ".\\scripts\\windows\\prepare-assets.ps1")
} else {
("libsherpa-onnx-c-api.so", "./scripts/prepare-local-assets.sh")
};
let sherpa_c_api = native_lib_dir.join(lib_name);
if !sherpa_c_api.exists() {
panic!(
"\n========================================================================\n\
[Build Error] Echolet native runtime not found at:\n {:?}\n\n\
Please run the local asset preparation script first:\n\
{}\n\n\
Or specify a custom native library directory:\n\
export ECHOLET_NATIVE_LIB_DIR=/path/to/runtime/lib\n\
========================================================================\n",
native_lib_dir, prep_script
);
}
// 3. Link against shared libraries
println!("cargo:rustc-link-search=native={}", native_lib_dir.display());
println!("cargo:rustc-link-lib=dylib=sherpa-onnx-c-api");
println!("cargo:rustc-link-lib=dylib=onnxruntime");
// 4. Inject RPATH & OS-specific link flags:
let is_bundle_build = env::var("ECHOLET_BUNDLE_BUILD")
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if target_os == "macos" {
println!("cargo:rustc-link-lib=framework=Carbon");
println!("cargo:rustc-link-lib=framework=Cocoa");
println!("cargo:rustc-link-lib=framework=CoreGraphics");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=ApplicationServices");
if is_bundle_build {
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../Frameworks");
} else {
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/../Frameworks");
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", native_lib_dir.display());
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/../../.local-runtime/runtime/lib");
println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/../../../.local-runtime/runtime/lib");
}
} else if target_os == "linux" {
if is_bundle_build {
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/runtime/lib");
} else {
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/runtime/lib");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../runtime/lib");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../../.local-runtime/runtime/lib");
println!("cargo:rustc-link-arg=-Wl,-rpath,$ORIGIN/../../../.local-runtime/runtime/lib");
}
println!("cargo:rustc-link-arg=-Wl,-z,origin");
}
}