-
Notifications
You must be signed in to change notification settings - Fork 290
Expand file tree
/
Copy pathbuild.rs
More file actions
271 lines (230 loc) · 8.48 KB
/
build.rs
File metadata and controls
271 lines (230 loc) · 8.48 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
use std::env;
use std::fs;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
const NUGET_FEED: &str = "https://api.nuget.org/v3/index.json";
// const ORT_NIGHTLY_FEED: &str =
// "https://pkgs.dev.azure.com/aiinfra/PublicPackages/_packaging/ORT-Nightly/nuget/v3/index.json";
const CORE_VERSION: &str = "1.0.0";
const ORT_VERSION: &str = "1.24.4";
const GENAI_VERSION: &str = "0.13.1";
const WINML_ORT_VERSION: &str = "1.23.2.3";
struct NuGetPackage {
name: &'static str,
version: String,
feed_url: &'static str,
}
fn get_rid() -> Option<&'static str> {
let os = env::consts::OS;
let arch = env::consts::ARCH;
match (os, arch) {
("windows", "x86_64") => Some("win-x64"),
("windows", "aarch64") => Some("win-arm64"),
("linux", "x86_64") => Some("linux-x64"),
("macos", "aarch64") => Some("osx-arm64"),
_ => None,
}
}
fn native_lib_extension() -> &'static str {
match env::consts::OS {
"windows" => "dll",
"linux" => "so",
"macos" => "dylib",
_ => "so",
}
}
fn get_packages(rid: &str) -> Vec<NuGetPackage> {
let winml = env::var("CARGO_FEATURE_WINML").is_ok();
let is_linux = rid.starts_with("linux");
// Use pinned versions directly — dynamic resolution via resolve_latest_version
// is unreliable (feed returns versions in unexpected order, and some old versions
// require authentication).
let mut packages = Vec::new();
if winml {
packages.push(NuGetPackage {
name: "Microsoft.AI.Foundry.Local.Core.WinML",
version: CORE_VERSION.to_string(),
feed_url: NUGET_FEED,
});
packages.push(NuGetPackage {
name: "Microsoft.ML.OnnxRuntime.Foundry",
version: WINML_ORT_VERSION.to_string(),
feed_url: NUGET_FEED,
});
packages.push(NuGetPackage {
name: "Microsoft.ML.OnnxRuntimeGenAI.Foundry",
version: GENAI_VERSION.to_string(),
feed_url: NUGET_FEED,
});
} else {
packages.push(NuGetPackage {
name: "Microsoft.AI.Foundry.Local.Core",
version: CORE_VERSION.to_string(),
feed_url: NUGET_FEED,
});
if is_linux {
packages.push(NuGetPackage {
name: "Microsoft.ML.OnnxRuntime.Gpu.Linux",
version: ORT_VERSION.to_string(),
feed_url: NUGET_FEED,
});
} else {
packages.push(NuGetPackage {
name: "Microsoft.ML.OnnxRuntime.Foundry",
version: ORT_VERSION.to_string(),
feed_url: NUGET_FEED,
});
}
packages.push(NuGetPackage {
name: "Microsoft.ML.OnnxRuntimeGenAI.Foundry",
version: GENAI_VERSION.to_string(),
feed_url: NUGET_FEED,
});
}
packages
}
/// Resolve the PackageBaseAddress from a NuGet v3 service index.
fn resolve_base_address(feed_url: &str) -> Result<String, String> {
let body: String = ureq::get(feed_url)
.call()
.map_err(|e| format!("Failed to fetch NuGet feed index at {feed_url}: {e}"))?
.body_mut()
.read_to_string()
.map_err(|e| format!("Failed to read feed index response: {e}"))?;
let index: serde_json::Value =
serde_json::from_str(&body).map_err(|e| format!("Failed to parse feed index JSON: {e}"))?;
let resources = index["resources"]
.as_array()
.ok_or("Feed index missing 'resources' array")?;
for resource in resources {
let rtype = resource["@type"].as_str().unwrap_or("");
if rtype == "PackageBaseAddress/3.0.0" {
if let Some(id) = resource["@id"].as_str() {
let base = if id.ends_with('/') {
id.to_string()
} else {
format!("{id}/")
};
return Ok(base);
}
}
}
Err(format!(
"Could not find PackageBaseAddress/3.0.0 in feed {feed_url}"
))
}
/// Download a .nupkg and extract native libraries for the given RID into `out_dir`.
fn download_and_extract(pkg: &NuGetPackage, rid: &str, out_dir: &Path) -> Result<(), String> {
let base_address = resolve_base_address(pkg.feed_url)?;
let lower_name = pkg.name.to_lowercase();
let lower_version = pkg.version.to_lowercase();
let url =
format!("{base_address}{lower_name}/{lower_version}/{lower_name}.{lower_version}.nupkg");
println!(
"cargo:warning=Downloading {name} {ver} from {feed}",
name = pkg.name,
ver = pkg.version,
feed = if pkg.feed_url == NUGET_FEED {
"NuGet.org"
} else {
"ORT-Nightly"
},
);
let mut response = ureq::get(&url)
.call()
.map_err(|e| format!("Failed to download {}: {e}", pkg.name))?;
let mut bytes = Vec::new();
response
.body_mut()
.as_reader()
.read_to_end(&mut bytes)
.map_err(|e| format!("Failed to read response body for {}: {e}", pkg.name))?;
let ext = native_lib_extension();
let prefix = format!("runtimes/{rid}/native/");
let cursor = io::Cursor::new(&bytes);
let mut archive = zip::ZipArchive::new(cursor)
.map_err(|e| format!("Failed to open nupkg as zip for {}: {e}", pkg.name))?;
let mut extracted = 0usize;
for i in 0..archive.len() {
let mut file = archive
.by_index(i)
.map_err(|e| format!("Failed to read zip entry: {e}"))?;
let name = file.name().to_string();
if !name.starts_with(&prefix) {
continue;
}
if !name.ends_with(&format!(".{ext}")) {
continue;
}
let file_name = Path::new(&name)
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();
if file_name.is_empty() {
continue;
}
let dest = out_dir.join(&file_name);
let mut out_file = fs::File::create(&dest)
.map_err(|e| format!("Failed to create {}: {e}", dest.display()))?;
io::copy(&mut file, &mut out_file)
.map_err(|e| format!("Failed to write {}: {e}", dest.display()))?;
println!("cargo:warning= Extracted {file_name}");
extracted += 1;
}
if extracted == 0 {
println!(
"cargo:warning= No native libraries found for RID '{rid}' in {} {}",
pkg.name, pkg.version
);
}
Ok(())
}
/// Check whether the core native library is already present in `out_dir`.
fn libs_already_present(out_dir: &Path) -> bool {
let core_lib = match env::consts::OS {
"windows" => "Microsoft.AI.Foundry.Local.Core.dll",
"linux" => "Microsoft.AI.Foundry.Local.Core.so",
"macos" => "Microsoft.AI.Foundry.Local.Core.dylib",
_ => return false,
};
out_dir.join(core_lib).exists()
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR not set"));
let rid = match get_rid() {
Some(r) => r,
None => {
println!(
"cargo:warning=Unsupported platform: {} {}. Native libraries will not be downloaded.",
env::consts::OS,
env::consts::ARCH,
);
return;
}
};
// Skip download if libraries already exist
if libs_already_present(&out_dir) {
println!("cargo:warning=Native libraries already present in OUT_DIR, skipping download.");
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-env=FOUNDRY_NATIVE_DIR={}", out_dir.display());
#[cfg(windows)]
println!("cargo:rustc-link-lib=kernel32");
return;
}
let packages = get_packages(rid);
for pkg in &packages {
if let Err(e) = download_and_extract(pkg, rid, &out_dir) {
println!("cargo:warning=Error downloading {}: {e}", pkg.name);
println!("cargo:warning=Build will continue, but runtime loading may fail.");
println!(
"cargo:warning=You can manually place native libraries in the output directory."
);
}
}
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-env=FOUNDRY_NATIVE_DIR={}", out_dir.display());
// LocalFree (used to free native-allocated buffers) lives in kernel32.lib on Windows.
#[cfg(windows)]
println!("cargo:rustc-link-lib=kernel32");
}