forked from FirePlank/infinite-chess-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_mt.js
More file actions
93 lines (83 loc) · 2.98 KB
/
build_mt.js
File metadata and controls
93 lines (83 loc) · 2.98 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
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
const configPath = path.join(__dirname, '.cargo', 'config.toml');
const backupPath = configPath + '.bak';
// 1. Backup and modify config.toml
// wasm-pack doesn't pass -Z flags well, so we put them in the config temporarily.
let configChanged = false;
try {
if (fs.existsSync(configPath)) {
fs.copyFileSync(configPath, backupPath);
let content = fs.readFileSync(configPath, 'utf8');
if (!content.includes('build-std')) {
content += '\n\n[unstable]\nbuild-std = ["panic_abort", "std"]\n';
fs.writeFileSync(configPath, content);
configChanged = true;
console.log("Temporarily enabled build-std in .cargo/config.toml");
}
} else {
const dir = path.dirname(configPath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(configPath, '[unstable]\nbuild-std = ["panic_abort", "std"]\n');
configChanged = true;
console.log("Temporarily created .cargo/config.toml with build-std");
}
} catch (err) {
console.error("Failed to modify .cargo/config.toml:", err.message);
}
function restoreConfig() {
if (configChanged) {
configChanged = false;
try {
if (fs.existsSync(backupPath)) {
fs.copyFileSync(backupPath, configPath);
fs.unlinkSync(backupPath);
console.log("Restored original .cargo/config.toml content");
} else {
// Only delete if we were the ones who created the file from scratch
fs.unlinkSync(configPath);
console.log("Removed temporary .cargo/config.toml");
}
} catch (err) {
console.error("Failed to restore .cargo/config.toml:", err.message);
}
}
}
// Ensure restoration on exit
process.on('SIGINT', () => { restoreConfig(); process.exit(); });
process.on('exit', restoreConfig);
// 2. Set environment variables
const env = { ...process.env };
env.RUSTFLAGS = [
"-C", "target-feature=+atomics,+bulk-memory,+mutable-globals,+simd128",
"-C", "link-arg=--shared-memory",
"-C", "link-arg=--max-memory=1073741824",
"-C", "link-arg=--import-memory",
"-C", "link-arg=--export=__wasm_init_tls",
"-C", "link-arg=--export=__tls_size",
"-C", "link-arg=--export=__tls_align",
"-C", "link-arg=--export=__tls_base"
].join(" ");
env.RUSTUP_TOOLCHAIN = "nightly";
console.log("Building Multi-threaded (Lazy SMP) with wasm-pack...");
const args = [
"build",
"--target", "web",
"--release",
"--features", "multithreading"
];
const build = spawn("wasm-pack", args, {
env: env,
shell: true,
stdio: 'inherit'
});
build.on('close', (code) => {
restoreConfig();
if (code !== 0) {
console.error(`Build failed with code ${code}`);
process.exit(code);
} else {
console.log("Build completed successfully!");
}
});