forked from thewh1teagle/vibe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
138 lines (122 loc) · 4.85 KB
/
build.rs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;
fn copy_file(src: &Path, dst: &Path) {
if dst.exists() {
std::fs::remove_file(dst).unwrap();
}
std::fs::copy(src, dst).unwrap();
}
fn get_cargo_target_dir() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR")?);
let profile = std::env::var("PROFILE")?;
let mut target_dir = None;
let mut sub_path = out_dir.as_path();
while let Some(parent) = sub_path.parent() {
if parent.ends_with(&profile) {
target_dir = Some(parent);
break;
}
sub_path = parent;
}
let target_dir = target_dir.ok_or("not found")?;
Ok(target_dir.to_path_buf())
}
fn macos_link_search_path() -> Option<String> {
let output = Command::new("clang").arg("--print-search-dirs").output().ok()?;
if !output.status.success() {
println!("failed to run 'clang --print-search-dirs', continuing without a link search path");
return None;
}
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() {
if line.contains("libraries: =") {
let path = line.split('=').nth(1)?;
return Some(format!("{}/lib/darwin", path));
}
}
println!("failed to determine link search path, continuing without it");
None
}
fn main() {
let target = env::var("TARGET").unwrap();
// ffmpeg
let ffmpeg_dir = env::var("FFMPEG_DIR").unwrap_or_default();
let ffmpeg_dir = PathBuf::from(ffmpeg_dir);
// openblas
let openblas_dir = env::var("OPENBLAS_PATH").unwrap_or_default();
let openblas_dir = PathBuf::from(openblas_dir);
// Sometimes it doesn't find the libs files after restring Github cache
if cfg!(all(feature = "cuda", windows)) {
let cuda_path = env::var("CUDA_PATH").unwrap_or_default();
let cuda_path = PathBuf::from(cuda_path);
println!("cargo:rustc-link-search={}", cuda_path.join("lib\\x64").display());
}
if ffmpeg_dir.exists() {
if !ffmpeg_dir.exists() {
panic!("Cant find ffmpeg at {}", ffmpeg_dir.canonicalize().unwrap().display());
}
if cfg!(target_os = "macos") {
let target_dir = get_cargo_target_dir().unwrap();
for entry in glob::glob(&format!("{}/*.dylib", ffmpeg_dir.join("lib").to_str().unwrap()))
.unwrap()
.flatten()
{
let dst = Path::new(&target_dir).join(Path::new(entry.file_name().unwrap()));
copy_file(&entry, &dst);
}
for entry in glob::glob(&format!("{}/*", ffmpeg_dir.join("bin").to_str().unwrap()))
.unwrap()
.flatten()
{
let dst = Path::new(&target_dir).join(Path::new(entry.file_name().unwrap()));
copy_file(&entry, &dst);
}
}
if cfg!(target_os = "windows") {
let target_dir = get_cargo_target_dir().unwrap();
let patterns = [
format!("{}\\*.dll", ffmpeg_dir.join("bin\\x64").to_str().unwrap()),
format!("{}\\*.exe", ffmpeg_dir.join("bin\\x64").to_str().unwrap()),
format!("{}\\*.dll", openblas_dir.join("..\\bin").to_str().unwrap()),
];
for pattern in patterns {
for entry in glob::glob(&pattern).unwrap().flatten() {
let dst = Path::new(&target_dir).join(Path::new(entry.file_name().unwrap()));
copy_file(&entry, &dst);
}
}
}
}
if cfg!(target_os = "windows") {
let openblas_path = std::env::var("OPENBLAS_PATH").unwrap().parse::<PathBuf>().unwrap();
let openblas_lib = openblas_path.join("lib");
println!("cargo:rustc-link-search=native={}", openblas_lib.to_str().unwrap());
}
if target.contains("apple") {
// On (older) OSX we need to link against the clang runtime,
// which is hidden in some non-default path.
//
// More details at https://github.com/alexcrichton/curl-rust/issues/279.
if let Some(path) = macos_link_search_path() {
println!("cargo:rustc-link-lib=clang_rt.osx");
println!("cargo:rustc-link-search={}", path);
}
}
// Passed from Github action
println!("cargo:rerun-if-env-changed=CUDA_VERSION");
println!(
"cargo:rustc-env=CUDA_VERSION={}",
std::env::var("INPUT_CUDA_VERSION").unwrap_or_default()
);
// Passed from Github action
println!("cargo:rerun-if-env-changed=ROCM_VERSION");
println!(
"cargo:rustc-env=ROCM_VERSION={}",
std::env::var("INPUT_ROCM_VERSION").unwrap_or_default()
);
// Todo: link correctly in whisper-rs
if cfg!(windows) {
println!("cargo:rustc-link-lib=msvcrt");
}
}