-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathwasmCompile.mjs
More file actions
73 lines (67 loc) · 2.2 KB
/
wasmCompile.mjs
File metadata and controls
73 lines (67 loc) · 2.2 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
// Copyright (c) 2021-2026 Littleton Robotics
// http://github.com/Mechanical-Advantage
//
// Use of this source code is governed by a BSD
// license that can be found in the LICENSE file
// at the root directory of this project.
import { exec } from "child_process";
import fs from "fs";
const EMSCRIPTEN_VERSION = "4.0.12";
try {
await new Promise(async (resolve, reject) => {
// Check Emscripten version
try {
await new Promise((resolve, reject) => {
exec("emcc --version", (error, stdout) => {
if (error !== null) {
reject("Failed to invoke emcc. Is Emscripten installed?");
}
if (!stdout.includes(EMSCRIPTEN_VERSION)) {
reject(
`Emscripten version is not ${EMSCRIPTEN_VERSION}. Please install the correct version and try again.`
);
}
resolve();
});
});
} catch (error) {
console.error(error);
reject();
return;
}
// Create directories
if (!fs.existsSync("bundles")) {
fs.mkdirSync("bundles");
}
if (!fs.existsSync("lite/static/bundles")) {
fs.mkdirSync("lite/static/bundles");
}
// Compile wasm
let inPath, outPath;
if (process.platform === "win32") {
inPath = "src\\hub\\dataSources\\wpilog\\indexer\\wpilogIndexer.c";
outPath = "bundles\\hub$wpilogIndexer.js";
} else {
inPath = "'src/hub/dataSources/wpilog/indexer/wpilogIndexer.c'";
outPath = "'bundles/hub$wpilogIndexer.js' ";
}
exec(
`emcc ${inPath} -o ${outPath} -sEXPORTED_FUNCTIONS=_run,_malloc -sEXPORTED_RUNTIME_METHODS=HEAPU8,HEAPF64,HEAPU32 -sALLOW_MEMORY_GROWTH -sMAXIMUM_MEMORY=4294967296 -O3`,
(error, stdout, stderr) => {
console.log(stdout);
console.error(stderr);
if (error === null) {
// Copy to Lite bundles
fs.copyFileSync("bundles/hub$wpilogIndexer.js", "lite/static/bundles/hub$wpilogIndexer.js");
fs.copyFileSync("bundles/hub$wpilogIndexer.wasm", "lite/static/bundles/hub$wpilogIndexer.wasm");
// Exit successfully
resolve();
} else {
reject();
}
}
);
});
} catch (exception) {
process.exit(1);
}