2024-06-08 22:12:50 +00:00
|
|
|
import { execFileSync } from "child_process";
|
|
|
|
import { copyFileSync, mkdirSync, rmSync } from "fs";
|
|
|
|
import * as process from "process";
|
2023-07-15 09:23:08 +00:00
|
|
|
|
2024-06-08 22:12:50 +00:00
|
|
|
function runWasmOpt({ path, flags }: { path: string; flags?: string[] }) {
|
2023-07-15 09:23:08 +00:00
|
|
|
let args = ["-o", path, "-O", "-g", path];
|
|
|
|
if (flags) {
|
|
|
|
args = args.concat(flags);
|
|
|
|
}
|
|
|
|
execFileSync("wasm-opt", args, {
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
}
|
2024-06-08 22:12:50 +00:00
|
|
|
function runWasmBindgen({
|
|
|
|
path,
|
|
|
|
outName,
|
|
|
|
flags,
|
|
|
|
dir,
|
|
|
|
}: {
|
|
|
|
path: string;
|
|
|
|
outName: string;
|
|
|
|
flags?: string[];
|
|
|
|
dir: string;
|
|
|
|
}) {
|
2023-07-15 09:23:08 +00:00
|
|
|
let args = [
|
|
|
|
path,
|
|
|
|
"--target",
|
|
|
|
"web",
|
|
|
|
"--out-dir",
|
|
|
|
dir,
|
|
|
|
"--out-name",
|
|
|
|
outName,
|
|
|
|
];
|
|
|
|
if (flags) {
|
|
|
|
args = args.concat(flags);
|
|
|
|
}
|
|
|
|
execFileSync("wasm-bindgen", args, {
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
}
|
2024-06-08 22:12:50 +00:00
|
|
|
function cargoBuild({
|
|
|
|
profile,
|
|
|
|
features,
|
|
|
|
rustFlags,
|
|
|
|
}: {
|
|
|
|
profile?: string;
|
|
|
|
features?: string[];
|
|
|
|
rustFlags?: string[];
|
|
|
|
}) {
|
2023-10-28 00:07:13 +00:00
|
|
|
let args = ["build", "--locked", "--target", "wasm32-unknown-unknown"];
|
2023-07-15 09:23:08 +00:00
|
|
|
if (profile) {
|
|
|
|
args.push("--profile", profile);
|
|
|
|
}
|
|
|
|
if (process.env["CARGO_FEATURES"]) {
|
|
|
|
features = (features || []).concat(
|
2023-07-20 11:19:39 +00:00
|
|
|
process.env["CARGO_FEATURES"].split(","),
|
2023-07-15 09:23:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if (features) {
|
|
|
|
args.push("--features", features.join(","));
|
|
|
|
}
|
|
|
|
let totalRustFlags = process.env["RUSTFLAGS"] || "";
|
|
|
|
if (rustFlags) {
|
|
|
|
if (totalRustFlags) {
|
|
|
|
totalRustFlags += ` ${rustFlags.join(" ")}`;
|
|
|
|
} else {
|
|
|
|
totalRustFlags = rustFlags.join(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (process.env["CARGO_FLAGS"]) {
|
|
|
|
args = args.concat(process.env["CARGO_FLAGS"].split(" "));
|
|
|
|
}
|
|
|
|
execFileSync("cargo", args, {
|
|
|
|
env: Object.assign(Object.assign({}, process.env), {
|
|
|
|
RUSTFLAGS: totalRustFlags,
|
|
|
|
}),
|
|
|
|
stdio: "inherit",
|
|
|
|
});
|
|
|
|
}
|
2024-06-08 22:12:50 +00:00
|
|
|
function buildWasm(
|
|
|
|
profile: string,
|
|
|
|
filename: string,
|
|
|
|
optimise: boolean,
|
|
|
|
extensions: boolean,
|
|
|
|
wasmSource: string,
|
|
|
|
) {
|
2024-06-12 10:32:07 +00:00
|
|
|
const rustFlags = ["--cfg=web_sys_unstable_apis", "-Aunknown_lints"];
|
2023-07-15 09:23:08 +00:00
|
|
|
const wasmBindgenFlags = [];
|
|
|
|
const wasmOptFlags = [];
|
|
|
|
const flavor = extensions ? "extensions" : "vanilla";
|
|
|
|
if (extensions) {
|
|
|
|
rustFlags.push(
|
|
|
|
"-C",
|
2023-07-20 11:19:39 +00:00
|
|
|
"target-feature=+bulk-memory,+simd128,+nontrapping-fptoint,+sign-ext,+reference-types",
|
2023-07-15 09:23:08 +00:00
|
|
|
);
|
|
|
|
wasmBindgenFlags.push("--reference-types");
|
|
|
|
wasmOptFlags.push("--enable-reference-types");
|
|
|
|
}
|
2024-02-22 00:26:47 +00:00
|
|
|
let originalWasmPath;
|
|
|
|
if (wasmSource === "cargo" || wasmSource === "cargo_and_store") {
|
|
|
|
console.log(`Building ${flavor} with cargo...`);
|
|
|
|
cargoBuild({
|
|
|
|
profile,
|
|
|
|
rustFlags,
|
|
|
|
});
|
|
|
|
originalWasmPath = `../../../target/wasm32-unknown-unknown/${profile}/ruffle_web.wasm`;
|
|
|
|
if (wasmSource === "cargo_and_store") {
|
|
|
|
copyFileSync(originalWasmPath, `../../dist/${filename}.wasm`);
|
|
|
|
}
|
|
|
|
} else if (wasmSource === "existing") {
|
|
|
|
originalWasmPath = `../../dist/${filename}.wasm`;
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
"Invalid wasm source: must be one of 'cargo', 'cargo_and_store' or 'existing'",
|
|
|
|
);
|
|
|
|
}
|
2023-07-15 09:23:08 +00:00
|
|
|
console.log(`Running wasm-bindgen on ${flavor}...`);
|
|
|
|
runWasmBindgen({
|
2024-02-22 00:26:47 +00:00
|
|
|
path: originalWasmPath,
|
2023-07-15 09:23:08 +00:00
|
|
|
outName: filename,
|
|
|
|
dir: "dist",
|
|
|
|
flags: wasmBindgenFlags,
|
|
|
|
});
|
|
|
|
if (optimise) {
|
2023-07-19 15:13:25 +00:00
|
|
|
console.log(`Running wasm-opt on ${flavor}...`);
|
2023-07-15 09:23:08 +00:00
|
|
|
runWasmOpt({
|
|
|
|
path: `dist/${filename}_bg.wasm`,
|
|
|
|
flags: wasmOptFlags,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-06-08 22:12:50 +00:00
|
|
|
function copyStandIn(from: string, to: string) {
|
2023-07-15 09:23:08 +00:00
|
|
|
const suffixes = [`_bg.wasm`, `_bg.wasm.d.ts`, `.js`, `.d.ts`];
|
|
|
|
console.log(`Copying ${from} as a stand-in for ${to}...`);
|
|
|
|
for (const suffix of suffixes) {
|
|
|
|
copyFileSync(`dist/${from}${suffix}`, `dist/${to}${suffix}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function detectWasmOpt() {
|
|
|
|
try {
|
|
|
|
execFileSync("wasm-opt", ["--version"]);
|
|
|
|
return true;
|
|
|
|
} catch (_a) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const buildExtensions = !!process.env["ENABLE_WASM_EXTENSIONS"];
|
2024-02-22 00:26:47 +00:00
|
|
|
const wasmSource = process.env["WASM_SOURCE"] || "cargo";
|
2023-07-15 09:23:08 +00:00
|
|
|
const hasWasmOpt = detectWasmOpt();
|
|
|
|
if (!hasWasmOpt) {
|
|
|
|
console.log(
|
2023-07-20 11:19:39 +00:00
|
|
|
"NOTE: Since wasm-opt could not be found (or it failed), the resulting module might not perform that well, but it should still work.",
|
2023-07-15 09:23:08 +00:00
|
|
|
);
|
|
|
|
}
|
2024-02-22 00:26:47 +00:00
|
|
|
if (wasmSource === "cargo_and_store") {
|
|
|
|
rmSync("../../dist", { recursive: true, force: true });
|
|
|
|
mkdirSync("../../dist");
|
|
|
|
}
|
|
|
|
buildWasm("web-vanilla-wasm", "ruffle_web", hasWasmOpt, false, wasmSource);
|
2023-07-15 09:23:08 +00:00
|
|
|
if (buildExtensions) {
|
|
|
|
buildWasm(
|
|
|
|
"web-wasm-extensions",
|
|
|
|
"ruffle_web-wasm_extensions",
|
|
|
|
hasWasmOpt,
|
2023-07-20 11:19:39 +00:00
|
|
|
true,
|
2024-02-22 00:26:47 +00:00
|
|
|
wasmSource,
|
2023-07-15 09:23:08 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
copyStandIn("ruffle_web", "ruffle_web-wasm_extensions");
|
|
|
|
}
|