Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: avoid Windows ORT release downloads

Richie McIlroy9a34f814a3d8
diff --git a/apps/desktop/scripts/prepare.js b/apps/desktop/scripts/prepare.js
index 35073e977..dc3e28d9a 100644
--- a/apps/desktop/scripts/prepare.js
+++ b/apps/desktop/scripts/prepare.js
@@ -87,6 +87,7 @@ export async function createTauriPlatformConfigs(
],
resources: {
"../../../target/ffmpeg/bin/*.dll": "./",
+ "../../../target/native-deps/onnxruntime/lib/*.dll": "./",
},
windows: {
wix: {
diff --git a/crates/camera-effects/Cargo.toml b/crates/camera-effects/Cargo.toml
index b2d999d42..48975431f 100644
--- a/crates/camera-effects/Cargo.toml
+++ b/crates/camera-effects/Cargo.toml
@@ -23,4 +23,4 @@ ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "n
ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "coreml", "api-23"] }
[target.'cfg(target_os = "windows")'.dependencies]
-ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "download-binaries", "copy-dylibs", "tls-native", "directml"] }
+ort = { version = "2.0.0-rc.12", default-features = false, features = ["std", "ndarray", "load-dynamic", "directml", "api-23"] }
diff --git a/crates/camera-effects/src/segmentation.rs b/crates/camera-effects/src/segmentation.rs
index bf8a7ca56..82ba2c980 100644
--- a/crates/camera-effects/src/segmentation.rs
+++ b/crates/camera-effects/src/segmentation.rs
@@ -1,13 +1,15 @@
use anyhow::Context;
use ort::session::Session;
use ort::value::Value;
-#[cfg(any(target_os = "macos", target_os = "linux"))]
+#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
use std::path::PathBuf;
#[cfg(target_os = "macos")]
const ORT_LIBRARY_NAME: &str = "libonnxruntime.dylib";
#[cfg(target_os = "linux")]
const ORT_LIBRARY_NAME: &str = "libonnxruntime.so";
+#[cfg(target_os = "windows")]
+const ORT_LIBRARY_NAME: &str = "onnxruntime.dll";
const MODEL_BYTES: &[u8] = include_bytes!("../assets/selfie_segmentation.onnx");
const MODEL_INPUT_SIZE: usize = 256;
@@ -90,7 +92,7 @@ fn create_session() -> anyhow::Result<Session> {
Ok(session)
}
-#[cfg(any(target_os = "macos", target_os = "linux"))]
+#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
fn init_runtime() -> anyhow::Result<()> {
let path = std::env::var_os("ORT_DYLIB_PATH")
.map(PathBuf::from)
@@ -108,12 +110,12 @@ fn init_runtime() -> anyhow::Result<()> {
Ok(())
}
-#[cfg(not(any(target_os = "macos", target_os = "linux")))]
+#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn init_runtime() -> anyhow::Result<()> {
Ok(())
}
-#[cfg(any(target_os = "macos", target_os = "linux"))]
+#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
fn onnx_runtime_candidates() -> Vec<PathBuf> {
let mut candidates = Vec::new();
diff --git a/scripts/setup.js b/scripts/setup.js
index 96f5e328d..9b42839ea 100644
--- a/scripts/setup.js
+++ b/scripts/setup.js
@@ -24,6 +24,10 @@ const FFMPEG_CARGO_ENV = `[env]
FFMPEG_DIR = { relative = true, force = true, value = "target/native-deps" }
`;
+function cargoConfigPath(value) {
+ return value.replaceAll("\\", "/");
+}
+
async function main() {
await fs.mkdir(targetDir, { recursive: true });
@@ -190,6 +194,11 @@ async function main() {
);
console.log("Copied ffmpeg/lib and ffmpeg/include to target/native-deps");
+ const onnxRuntimePath = await setupWindowsOnnxRuntime();
+ cargoConfigContents += `ORT_DYLIB_PATH = { relative = true, force = true, value = "${cargoConfigPath(
+ path.relative(__root, onnxRuntimePath),
+ )}" }\n`;
+
const { stdout: vcInstallDir } = await exec(
// biome-ignore lint/suspicious/noTemplateCurlyInString: PowerShell syntax, not JS template literal
'$(& "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe" -latest -property installationPath)',
@@ -403,6 +412,81 @@ async function setupMacOSOnnxRuntime() {
return outputPath;
}
+async function setupWindowsOnnxRuntime() {
+ const assets = {
+ x86_64: {
+ version: "1.24.2",
+ name: "onnxruntime-win-x64-1.24.2.zip",
+ },
+ aarch64: {
+ version: "1.24.2",
+ name: "onnxruntime-win-arm64-1.24.2.zip",
+ },
+ };
+ const asset = assets[arch];
+ if (!asset)
+ throw new Error(`Unsupported Windows arch for ONNX Runtime: ${arch}`);
+
+ const url = `https://github.com/microsoft/onnxruntime/releases/download/v${asset.version}/${asset.name}`;
+ const archivePath = path.join(targetDir, asset.name);
+ const extractDir = path.join(targetDir, asset.name.replace(/\.zip$/, ""));
+ const outputDir = path.join(targetDir, "native-deps", "onnxruntime", "lib");
+ const outputPath = path.join(outputDir, "onnxruntime.dll");
+ const markerPath = path.join(outputDir, "asset.txt");
+ const marker = await fs
+ .readFile(markerPath, "utf-8")
+ .then((value) => value.trim())
+ .catch(() => null);
+
+ if (!(await fileExists(archivePath))) {
+ console.log(`Downloading ${asset.name}`);
+ const bytes = await fetch(url)
+ .then((r) => r.blob())
+ .then((b) => b.arrayBuffer());
+ await fs.writeFile(archivePath, Buffer.from(bytes));
+ console.log(`Downloaded ${asset.name}`);
+ } else console.log(`Using cached ${asset.name}`);
+
+ if (!(await fileExists(outputPath)) || marker !== asset.name) {
+ await fs.rm(extractDir, { recursive: true, force: true }).catch(() => {});
+ await exec(
+ `Expand-Archive -Path "${archivePath}" -DestinationPath "${targetDir}" -Force`,
+ { shell: "powershell.exe" },
+ );
+ await fs.rm(outputDir, { recursive: true, force: true }).catch(() => {});
+ await fs.mkdir(outputDir, { recursive: true });
+ const libDir = path.join(extractDir, "lib");
+ const dllNames = (await fs.readdir(libDir)).filter((name) =>
+ name.toLowerCase().endsWith(".dll"),
+ );
+ if (!dllNames.includes("onnxruntime.dll"))
+ throw new Error(`ONNX Runtime archive is missing onnxruntime.dll`);
+
+ for (const name of dllNames) {
+ await fs.copyFile(path.join(libDir, name), path.join(outputDir, name));
+ }
+ await fs.writeFile(markerPath, asset.name);
+ console.log("Prepared ONNX Runtime DLLs");
+ } else console.log("Using cached ONNX Runtime DLLs");
+
+ const dllNames = (await fs.readdir(outputDir)).filter((name) =>
+ name.toLowerCase().endsWith(".dll"),
+ );
+ for (const profile of ["debug", "release"]) {
+ const profileDir = path.join(targetDir, profile);
+ await fs.mkdir(profileDir, { recursive: true });
+ for (const name of dllNames) {
+ await fs.copyFile(
+ path.join(outputDir, name),
+ path.join(profileDir, name),
+ );
+ }
+ }
+ console.log("Copied ONNX Runtime DLLs to target/debug and target/release");
+
+ return outputPath;
+}
+
async function writeFileIfChanged(filePath, contents) {
const currentContents = await fs
.readFile(filePath, "utf-8")