Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

beforebuild cleanup

laurenspriemdcb7c95d3be3
diff --git a/desktop/scripts/beforeBuild.js b/desktop/scripts/beforeBuild.js
index f3146dc166..d6e9892efd 100755
--- a/desktop/scripts/beforeBuild.js
+++ b/desktop/scripts/beforeBuild.js
@@ -1,10 +1,14 @@
const fsp = require("fs/promises");
+const { stageMLAddons } = require("./ml-native");
+const { ensureONNXRuntime } = require("./ort");
/**
* This hook is invoked during the initial build (e.g. when triggered by
* "npm run build"), and importantly, on each rebuild for a different
- * architecture during the build. We use it to ensure the vips binary matches
- * the current architecture. See "[Note: vips]" for more details.
+ * architecture during the build. We use it to ensure the vips binary, the
+ * ONNX Runtime library, and the Rust ML addon match the current architecture.
+ * See "[Note: vips]", "[Note: ONNX Runtime binaries]" and "[Note: Packaging
+ * the ML addon]" for more details.
*
* The documentation for this hook is at:
* https://www.electron.build/app-builder-lib.interface.configuration#beforebuild
@@ -30,6 +34,13 @@ const fsp = require("fs/promises");
module.exports = async (context) => {
const { appDir, platform, arch } = context;
+ // Match the ONNX Runtime library and the staged Rust ML addon to the
+ // architecture being packaged. Unlike the vips handling below, this also
+ // has work to do when the build arch is the host arch: the addon must be
+ // staged from `rust-bindings/` into `build/` in all cases.
+ await ensureONNXRuntime(platform.nodeName, arch, appDir);
+ await stageMLAddons(appDir, platform.nodeName, arch);
+
// The arch used by Electron Builder is not the same as the arch used by
// Node's process, but for the two cases that we care about, "x64" and
// "arm64", both of them use the string constant and thus can be compared.
diff --git a/desktop/scripts/ort.js b/desktop/scripts/ort.js
index 23eba1c093..129d6b56ca 100644
--- a/desktop/scripts/ort.js
+++ b/desktop/scripts/ort.js
@@ -85,14 +85,31 @@ const downloadONNXRuntimeIfNeeded = async (platform, arch, appDir) => {
await fsp.writeFile(stampPath, asset);
};
-const main = async () => {
- const arches =
- process.platform == "darwin" ? ["arm64", "x64"] : [process.arch];
- for (const arch of arches)
- await downloadONNXRuntimeIfNeeded(process.platform, arch, ".");
+/**
+ * Ensure `<appDir>/build/onnxruntime` contains exactly the ONNX Runtime
+ * libraries needed for the given OS/arch combination.
+ *
+ * On macOS the packaged app is universal, so both architectures are kept. On
+ * Linux and Windows only the given arch is kept: unlike vips (a single file
+ * that gets overwritten in place per arch), the libraries live in per-arch
+ * directories, so directories left over from earlier passes for other
+ * architectures need to be removed lest they also get copied into the
+ * package.
+ */
+const ensureONNXRuntime = async (platform, arch, appDir) => {
+ const arches = platform == "darwin" ? ["arm64", "x64"] : [arch];
+ for (const a of arches)
+ await downloadONNXRuntimeIfNeeded(platform, a, appDir);
+
+ const outDir = path.join(appDir, "build", "onnxruntime");
+ for (const entry of await fsp.readdir(outDir))
+ if (!arches.includes(entry))
+ await fsp.rm(path.join(outDir, entry), { recursive: true });
};
-module.exports = { downloadONNXRuntimeIfNeeded };
+const main = () => ensureONNXRuntime(process.platform, process.arch, ".");
+
+module.exports = { ensureONNXRuntime };
if (require.main === module)
main().catch((e) => {