forked from d07RiV/devilution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
160 lines (143 loc) · 4.84 KB
/
build.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const fs = require('fs');
const fsp = fs.promises;
const path = require('path');
const { exec } = require('child_process');
function async_limit(func, limit) {
const executing = [];
return async function(...args) {
while (executing.length >= limit) {
await Promise.race(executing).catch(() => null);
}
const p = Promise.resolve().then(() => func(...args));
const e = p.finally(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
return p;
};
}
const execute = async_limit((cmd, cb) => new Promise((resolve, reject) => {
if (cb) cb();
exec(cmd, function(err, stdout, stderr) {
if (err) {
reject(err);
} else {
resolve({stdout, stderr});
}
});
}), 6);
function isDir(p) {
try {
let stat = fs.statSync(p);
return stat.isDirectory() ? 1 : -1;
} catch (e) {
return 0;
}
}
function mkdirsSync(p) {
const status = isDir(p);
if (status === 1) {
return;
} else if (status === -1) {
throw new Error(`path ${p} is not a directory`);
} else {
const par = path.dirname(p);
if (par === p) {
return; // how?
}
mkdirsSync(par);
fs.mkdirSync(p);
}
}
async function run_build(flags, oname, dirs) {
const out_dir = `./emcc/${oname}`;
let rebuild = true;
if (fs.existsSync(`${out_dir}/args.txt`)) {
if (fs.readFileSync(`${out_dir}/args.txt`, 'utf8') === flags) {
rebuild = false;
}
}
const link_list = [];
let firstFile = true;
let maxTime = null;
let fileTime = new Map();
function file_time(name) {
if (fileTime.has(name)) {
return fileTime.get(name);
}
const time = (async () => {
const data = await fsp.readFile(name, 'utf8');
const reg = /^\s*#include "(.*)"/mg;
let m;
const includes = [];
const dir = path.dirname(name);
while (m = reg.exec(data)) {
includes.push(path.join(dir, m[1]));
}
const incl = includes.map(n => file_time(n));
const times = await Promise.all(incl);
const time = times.reduce((res, t) => (res > t ? res : t), (await fsp.stat(name)).mtime);
return time;
})();
fileTime.set(name, time);
return time;
}
async function handle_file(name) {
const statSrc = await fsp.stat(name);
if (statSrc.isDirectory()) {
const list = await fsp.readdir(name);
await Promise.all(list.map(fn => handle_file(`${name}/${fn}`)));
} else if (name.match(/\.(?:c|cpp|cc)$/i)) {
const out = `${out_dir}/${name}.o`;
const srcTime = await file_time(name);
let statDst = null;
if (fs.existsSync(out)) {
statDst = await fsp.stat(out);
} else {
mkdirsSync(path.dirname(out));
}
if (rebuild || !statDst || srcTime > statDst.mtime) {
if (firstFile) {
console.log('Compiling...');
firstFile = false;
}
const cmd = `emcc -c ${name} -o ${out} ${name.match(/\.(?:cpp|cc)$/i) ? "--std=c++17 " : ""}-DNO_SYSTEM -DEMSCRIPTEN -Wno-logical-op-parentheses ${flags} -I.`; //-fPIC
try {
const {stderr} = await execute(cmd, () => console.log(` ${name}`));
if (stderr) {
console.error(stderr);
}
} catch (e) {
if (fs.existsSync(out)) {
await fsp.unlink(out);
}
throw e;
}
}
if (!maxTime || srcTime > maxTime) {
maxTime = srcTime;
}
link_list.push(out);
}
}
await Promise.all(dirs.map(dir => handle_file(dir)));
mkdirsSync(out_dir);
fs.writeFileSync(`${out_dir}/args.txt`, flags);
let wasmTime = null;
if (fs.existsSync(oname + '.wasm')) {
wasmtime = fs.statSync(oname + '.wasm').mtime;
}
if (!rebuild && (!maxTime || (wasmTime && maxTime <= wasmTime))) {
console.log('Everything is up to date');
return;
}
console.log(`Linking ${oname}`);
const cmd = `emcc ${link_list.join(" ")} -o ${oname}.js -s EXPORT_NAME="${oname}" ${flags} -s WASM=1 -s MODULARIZE=1 -s NO_FILESYSTEM=1 --post-js ./module-post.js -s ALLOW_MEMORY_GROWTH=1 -s TOTAL_MEMORY=134217728 -s DISABLE_EXCEPTION_CATCHING=0`; //-s BINARYEN_TRAP_MODE='clamp' -s RELOCATABLE=1 -s EXPORTED_FUNCTIONS=_DApi_Char
// const cmd = `emcc ${link_list.join(" ")} -o ${oname}.js -s EXPORT_NAME="${oname}" ${flags} -s WASM=1 -s MODULARIZE=1 -s ASSERTIONS=2 -s DEMANGLE_SUPPORT=1 --post-js ./module-post.js -s TOTAL_MEMORY=536870912 -s DISABLE_EXCEPTION_CATCHING=0 -s BINARYEN_TRAP_MODE='clamp'`;
const {stderr} = await execute(cmd);
if (stderr) {
console.error(stderr);
}
fs.renameSync(oname + '.js', oname + '.jscc');
}
run_build('-O3 -DZ_SOLO', 'Diablo', ['Source']).catch(e => console.error(e.message));
//run_build('-O3 -DZ_SOLO -DSPAWN', 'DiabloSpawn', ['Source']).catch(e => console.error(e.message));
//run_build('-O3 -DZ_SOLO', 'MpqCmp', ['Source/rmpq', 'Source/zlib', 'mpqcmp']).catch(e => console.error(e.message));