-
Notifications
You must be signed in to change notification settings - Fork 133
/
build.js
executable file
·855 lines (758 loc) · 29.2 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#!/usr/bin/env node
//! Chess.com (c) 2024
"use strict";
var runSpawnSync = require("child_process").spawnSync;
var runExecFileSync = require("child_process").execFileSync;
var params = getParams({booleans: [
"no-chesscom", "h", "help", "help-all", "f", "force", "force-linking", "s", "silent", "bin", "colors", "no-color", "no-minify", "v", "verbose", "debug-wasm", "all", "skip-em-check", "single-threaded", "lite", "wasm-debug", "asm-js", "keep-syzygy", "hash", "no-split",
"skip-asm", "skip-single", "skip-lite", "skip-single-lite", "skip-lite-single", "skip-standard",
"only-asm", "only-single", "only-lite", "only-single-lite", "only-lite-single", "only-standard",
]});
var args = ["-j", require("os").cpus().length];
var fs = require("fs");
var p = require("path");
var srcPath = p.join(__dirname, "src");
var stockfishPath = p.join(srcPath, "stockfish");
var stockfishWASMPath = p.join(srcPath, "stockfish.wasm");
var wasmEmbeddedNetsPath;
var data;
var workerData;
var preface;
var postscript;
var buildWithEmscripten;
var child;
var stockfishVersionNumber = "16.1";
var expectedEmscripten = "3.1.7";
var fistRun;
var basename;
var buildingSingleThreaded = false;
function getParams(options, argv)
{
var i,
params = {_: []},
last,
len,
match;
if (Array.isArray(options)) {
args = options;
options = {};
}
options = options || {};
if (!options.booleans) {
options.booleans = [];
}
argv = argv || process.argv;
len = argv.length;
for (i = 2; i < len; i += 1) {
if (argv[i][0] === "-") {
if (argv[i][1] === "-") {
last = argv[i].substr(2);
match = last.match(/([^=]*)=(.*)/);
if (match) {
last = match[1];
params[last] = match[2];
last = "";
} else {
params[last] = true;
}
} else {
/// E.g., -hav should indicate h, a, and v as TRUE.
argv[i].split("").slice(1).forEach(function oneach(letter)
{
params[letter] = true;
last = letter;
});
}
} else if (last) {
params[last] = argv[i];
last = "";
} else {
params._.push(argv[i]);
last = "";
}
/// Handle booleans.
if (last && options.booleans.indexOf(last) > -1) {
last = "";
}
}
return params;
}
function minify(code)
{
var initComment;
if (params["no-minify"] || params["debug-wasm"]) {
return code;
}
initComment = code.match(/\/\*![\s\S]*?\*\//)[0];
try {
code = initComment + require("uglify-js").minify(code).code;
} catch (e) {
warn("Unable to minify JS");
}
return code;
}
function color(color_code, str)
{
if (!params["no-colors"] && (process.stdout.isTTY || params.colors)) {
str = "\u001B[" + color_code + "m" + str + "\u001B[0m";
}
return str;
}
function warn(str)
{
console.warn(color(33, "WARN: " + str));
}
function highlight(str)
{
return color(33, str);
}
function note(str)
{
return color(36, str);
}
function bold(str)
{
return color(1, str);
}
function beep()
{
if (process.stdout.isTTY && !params.s && !params.silent) {
process.stdout.write("\u0007");
}
}
function determineBestArch()
{
var cpuData = "";
var cpuArch = "";
var arch;
try {
cpuArch = execFileSync("uname", ["-m"], {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
} catch (e) {}
if (cpuArch === "i686" || cpuArch === "i386") {
arch = "x86-32";
} else if (cpuArch === "armv7l") {
arch = "armv7";
} else if (cpuArch === "armv8l") {
arch = "armv8";
} else if (cpuArch === "arm64") {
arch = "apple-silicon";
} else {
if (cpuArch !== "x86_64" || cpuArch === "amd64") {
warn("Unrecognized cpu architechure. Defaulting to x86_64.");
}
try {
cpuData = execFileSync("cat", ["/proc/cpuinfo"], {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
} catch (e) {}
if (!cpuData) {
try {
cpuData = execFileSync("lscpu", {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
} catch (e) {}
}
if (!cpuData) {
try {
cpuData = execFileSync("lshw", ["-C", "cpu"], {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
} catch (e) {}
}
if (!cpuData) {
try {
/// FreeBSD & macOS?
cpuData = execFileSync("sysctl", ["-a"], {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
} catch (e) {}
}
if (/\bvnni512\b/i.test(cpuData)) {
arch = "x86-64-vnni512";
} else if (/\bvnni256\b/i.test(cpuData)) {
arch = "x86-64-vnni256";
} else if (/\bavx512\b/i.test(cpuData)) {
arch = "x86-64-avx512";
} else if (/\bavxvnni\b/i.test(cpuData)) {
arch = "x86-64-avxvnni";
} else if (/\bbmi2\b/i.test(cpuData)) {
arch = "x86-64-bmi2";
} else if (/\bavx2\b/i.test(cpuData)) {
arch = "x86-64-avx2";
} else if (/\bpopcnt\b/i.test(cpuData) && /\bsse4_?1\b/i.test(cpuData)) {
arch = "x86-64-sse41-popcnt";
} else if (/\bssse3\b/i.test(cpuData)) {
arch = "x86-64-ssse3";
} else if (/\bpopcnt\b/i.test(cpuData) && /\bsse3\b/i.test(cpuData)) {
arch = "x86-64-sse3-popcnt";
} else {
arch = "x86-64";
}
}
console.log(note("Building " + arch));
args.push("ARCH=" + arch);
}
function spawnSync(command, args, options)
{
if (params.v || params.verbose) {
console.log(highlight(" - Running command: " + command + " " + (args && args.length ? "\"" + args.join("\" \"") + "\"" : "")));
}
return runSpawnSync(command, args, options);
}
function execFileSync(command, args, options)
{
if (params.v || params.verbose) {
console.log(highlight(" - Running command: " + command + " " + (args && args.length ? "\"" + args.join("\" \"") + "\"" : "")));
}
return runExecFileSync(command, args, options);
}
function checkEmscriptenVersion()
{
var versionInfo;
var exec = params.emcc || "emcc";
try {
versionInfo = execFileSync(exec, ["--version"], {encoding: "utf8", env: process.env, cwd: __dirname});
if (versionInfo.indexOf(expectedEmscripten) === -1) {
console.error(highlight("Warning:"));
console.error("\nEmscripten version does not match.\nExpected " + note(expectedEmscripten) + ". See " + note(exec + " --version") + " for your currently installed version.");
console.error("\n" + "To install the expected verions, try:\n\n > " + note("emsdk install " + expectedEmscripten) + "\n > " + note("emsdk activate " + expectedEmscripten));
console.error("\nOr add " + note("--skip-em-check") + " to bypass this check.\n");
}
} catch (e) {
console.error(e);
console.error(highlight("Warning:"));
console.error("\nCould not confirm emscripten version. Set your " + note("emcc") + " path with the " + note("--emcc") +" flag, or add " + note("--skip-em-check") + " to bypass this check.\n");
}
}
function ensureNets()
{
execFileSync(params.make, ["net"], {cwd: srcPath});
}
function getNetPaths()
{
var filename;
if (params["ultra-lite"]) {
filename = "ultra_lite_nets.h";
} else if (params.lite) {
filename = "lite_nets.h";
} else {
filename = "evaluate.h";
}
var code = fs.readFileSync(p.join(srcPath, filename), "utf8");
var match;
var nets = [];
match = code.match(/\#define EvalFileDefaultNameBig "([^"]+)"/);
if (match) {
nets.push({path: match[1], type: "Big"});
} else {
console.error("Cannot find EvalFileDefaultNameBig path");
}
match = code.match(/\#define EvalFileDefaultNameSmall "([^"]*)"/);
if (match) {
nets.push({path: match[1], type: "Small"});
} else {
console.error("Cannot find EvalFileDefaultNameSmall path");
}
return nets;
}
function alreadyEmbedded(nets)
{
var data;
var i;
try {
data = fs.readFileSync(wasmEmbeddedNetsPath, "utf8");
} catch (e) {
return false;
}
for (i = nets.length - 1; i >= 0; --i) {
if (!params.lite || params["ultra-lite"] || nets[i].type !== "small") {
if (data.indexOf(nets[i].path) === -1) {
return false;
}
}
}
return true;
}
function getEmbeddedNetCode(net)
{
/// Read nets or create an empty net for the small net in Lite mode.
var data = net.path ? fs.readFileSync(p.join(srcPath, net.path)) : Buffer.alloc(1);
var out = "";
var prefix = "// Generated from " + net.path + " on " + (new Date).toString() + "\n";
var netVarBase = "gEmbeddedNNUE" + net.type;
var len = data.byteLength;
var i;
var char;
for (i = 0; i < len; ++i) {
char = data[i].toString(16);
if (data[i] < 16) {
char = "0" + char;
}
out += "\\x" + char;
if ((i + 1) % 32 === 0) {
out += "\\\n";
}
}
return prefix +
"extern const unsigned char " + netVarBase + "Data[] = \"\\\n" + out + "\";\n" +
"extern const unsigned int " + netVarBase + "Size = " + len + ";\n" +
"extern const unsigned char* const " + netVarBase + "End = &" + netVarBase + "Data[" + len + "];\n\n"; /// Oddly, Stockfish seems to never actually use this variable.
}
function embedNets()
{
var nets = getNetPaths();
if (!nets.length) {
throw new Error("Cannot find any networks to embed.");
}
/// First, make sure the nets are here.
if (!params.lite && !params["ultra-lite"]) {
ensureNets();
}
if (!alreadyEmbedded(nets)) {
fs.writeFileSync(wasmEmbeddedNetsPath, "");
nets.forEach(function (net)
{
if (net.path) {
console.log(note("Embedding " + net.path));
}
fs.appendFileSync(wasmEmbeddedNetsPath, getEmbeddedNetCode(net));
});
}
}
function makeRelative(from, to)
{
var relDirPath = p.relative(p.dirname(from), p.dirname(to));
var relPath = p.join(relDirPath, p.basename(to));
if (!p.isAbsolute(relPath) && relPath.substr(0, 3) !== "../") {
relPath = "./" + relPath;
}
return relPath;
}
function renameAndSymlink(origPath, newPath)
{
fs.renameSync(origPath, newPath);
makeSymLink(newPath, origPath);
}
function makeSymLink(newPath, origPath)
{
fs.symlinkSync(makeRelative(origPath, newPath), origPath);
}
function fillInBlanks(code)
{
code = code.replace("__YEAR__", (new Date()).getFullYear());
code = code.replace("__VERSION__", stockfishVersionNumber);
return code;
}
function fixUpWASMBuild()
{
var stockfishWASMPath = p.join(srcPath, "stockfish.wasm");
var stockfishWASMLoaderPath = p.join(srcPath, "stockfish.js");
var stockfishWorkerThreadPath = p.join(srcPath, "stockfish.worker.js");
var workerExternPostPath = p.join(srcPath, "emscripten", "worker-extern-post.js");
var workerExternPostData = fs.readFileSync(workerExternPostPath, "utf8");
var workerData = "";
var stockfishWASMLoaderData;
var hashParts;
var finalWasmPath = stockfishWASMPath;
if (!params["single-threaded"]) {
workerData = fs.readFileSync(stockfishWorkerThreadPath, "utf8") + workerExternPostData;
try {
fs.unlinkSync(stockfishWorkerThreadPath);
} catch (e) {}
}
stockfishWASMLoaderData = fs.readFileSync(stockfishWASMLoaderPath, "utf8").replace(/\/\/\/ Insert worker here/, workerData);
stockfishWASMLoaderData = fillInBlanks(stockfishWASMLoaderData);
stockfishWASMLoaderData = minify(stockfishWASMLoaderData);
fs.writeFileSync(stockfishWASMLoaderPath, stockfishWASMLoaderData);
if (!basename && params.hash) {
basename = "stockfish";
}
if (basename) {
/// The hash for all files is the combined hash of both the JS and the WASM.
/// This makes it clear where the WASM file is located based on the JS file's string.
/// That way, we don't have to tell the JS where the WASM file is located.
hashParts = getHashPart([stockfishWASMLoaderPath, stockfishWASMPath]);
finalWasmPath = p.join(srcPath, basename + hashParts + ".wasm");
renameAndSymlink(stockfishWASMLoaderPath, p.join(srcPath, basename + hashParts) + ".js");
renameAndSymlink(stockfishWASMPath, finalWasmPath);
if (params["debug-wasm"]) {
try {
renameAndSymlink(stockfishWASMPath + ".map", p.join(srcPath, basename + hashParts + ".wasm.map"));
} catch (e) {}
}
}
if (params.split && !params["no-split"]) {
splitFile(finalWasmPath, params.split);
fs.unlinkSync(p.join(srcPath, "stockfish.wasm"));
}
console.log("Built " + note(p.basename(finalWasmPath, ".wasm") + ".js"));
}
function splitFile(path, count)
{
var data = fs.readFileSync(path);
///NOTE: We round up to make sure we get all of the bytes on the last parts
var chunkSize = Math.ceil(data.byteLength / count);
var i;
var at;
var ext = p.extname(path);
var basename = path.slice(0, -ext.length);
var newPath;
var origPath;
for (i = 0; i < count; ++i) {
at = i * chunkSize;
newPath = basename + "-part-" + i + ext;
fs.writeFileSync(newPath, data.slice(at, chunkSize + at));
origPath = p.join(srcPath, "stockfish-part-" + i + ext);
makeSymLink(newPath, origPath);
}
fs.unlinkSync(path);
}
function fixUpASMJSBuild()
{
var enginePath = p.join(srcPath, "stockfish.js");
var engineData = fs.readFileSync(enginePath, "utf8");
engineData = fillInBlanks(engineData);
engineData = minify(engineData);
fs.writeFileSync(enginePath, engineData);
if (basename) {
renameAndSymlink(enginePath, p.join(srcPath, basename + getHashPart(enginePath) + ".js"));
}
}
function getVersion()
{
var version = params.version === "string" ? params.version : stockfishVersionNumber;
if (buildWithEmscripten) {
if (params["ultra-lite"]) {
version += " Ultra Lite";
} else if (params.lite) {
version += " Lite";
}
if (!params["asm-js"]) {
version += " WASM";
} else {
version += " ASM.JS";
}
if (!params["single-threaded"]) {
version += " Multithreaded";
}
}
return version;
}
function hashData(data)
{
var crypto = require("crypto");
var hash = crypto.createHash("md5");
hash.setEncoding("hex");
hash.write(data);
hash.end();
return hash.read().substr(0, 7);
}
function hashFiles(paths)
{
var data = Buffer.alloc(0);
if (!Array.isArray(paths)) {
paths = [paths];
}
paths.forEach(function (path)
{
data = Buffer.concat([data, fs.readFileSync(path)]);
});
return hashData(data);
}
function getHashPart(paths)
{
if (!params.hash) {
return "";
}
return "-" + hashFiles(paths);
}
if (params["wasm-debug"]) { /// alias
params["debug-wasm"] = params["wasm-debug"];
}
if (!params.make) {
params.make = "make";
}
if (params.arch) {
if (params.b || params.bin) {
warn("Cannot user --bin (or -b) with --arch");
process.exit(1);
}
args.push("ARCH=" + params.arch);
if (params.arch === "wasm") {
buildWithEmscripten = true;
}
} else if (params.b || params.bin) {
determineBestArch()
} else {
buildWithEmscripten = true;
params.arch = "wasm";
args.push("ARCH=wasm");
}
if (params["asm-js"]) {
args.push("ASMJS=yes");
if (!params.lite) {
if (params["lite"] !== "0" && params["lite"] !== "false") {
params["lite"] = true;
}
}
params["single-threaded"] = true;
}
if (params.help || params["help-all"] || params.h) {
console.log("");
console.log(bold("Build the Stockfish Chess Engine"));
console.log("Usage: ./build.js [" + highlight("options") + "]");
console.log("");
console.log(" " + highlight("--all") + " Build all flavors of emscripten engines");
console.log(" " + highlight("--arch") + " Architecture to build to (default: " + note("wasm") + ")");
console.log( " See " + highlight("--help-all") + " for more options, or use " + highlight("--bin") + " instead");
console.log(" " + highlight("--asm-js") + " Build the ASM.JS version");
console.log(" " + highlight("--basename") + " The filename for the engine (default: " + note ("stockfish") + ")");
console.log( " This will not only rename the files, it will also rewrite the base JS file");
console.log( " to load the correct WASM engine");
console.log(" " + highlight("--bin") + " Attempt to build a binary engine that is the most suitable for this system");
console.log(" " + highlight("--colors") + " Always colorize the output, even through a pipe");
console.log(" " + highlight("--comp") + " Compiler to build C code with");
console.log(" " + highlight("--compcxx") + " Compiler to build C++ code with");
console.log(" " + highlight("--debug-wasm") + " Compile WASM in debug mode");
console.log(" " + highlight("--emcc") + " Path to " + note("emcc") + " (used to ensure version compatibility)");
console.log(" " + highlight("-f --force") + " Always rebuild the entire project");
console.log(" " + highlight("--force-linking") + " Always preforming the final linking step");
console.log(" " + highlight("--hash") + " Add a hash to the filename");
console.log(" " + highlight("-h --help") + " Show build.js's help");
console.log(" " + highlight("--help-all") + " Show Stockfish's Makefile help as well");
console.log(" " + highlight("--keep-syzygy") + " Do not remove syzygy code");
console.log(" " + highlight("--lite") + " Embed small net file");
console.log(" " + highlight("--make") + " Path to program used to make Stockfish (default: " + note("make") + ")");
console.log(" " + highlight("--no-colors") + " Never colorize the output");
console.log(" " + highlight("--no-minify") + " Minification of outer JS code (use " + highlight("--debug-wasm") + " to completely disable minfication)");
console.log(" " + highlight("--no-split") + " Disable WASM splitting, even if " + highlight("--split") + " is present");
console.log(" " + highlight("--only-asm") + " Only build the ASM.JS engine with " + highlight("--all"));
console.log(" " + highlight("--only-lite") + " Only build lite multi-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--only-lite-single") + " Only build lite single-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--only-single") + " Only build non-lite single-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--only-standard") + " Only build standard, multi-threaded engine with " + highlight("--all"));
console.log(" " + highlight("-s --silent") + " Do not beep");
console.log(" " + highlight("--single-threaded") + " Compile the engine without Pthreads");
console.log(" " + highlight("--skip-asm") + " Do not build the ASM.JS engine with " + highlight("--all"));
console.log(" " + highlight("--skip-em-check") + " Do not check Emscripten version (expected version: " + note(expectedEmscripten) + ")");
console.log(" " + highlight("--skip-lite") + " Do not build lite multi-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--skip-lite-single") + " Do not build lite single-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--skip-single") + " Do not build non-lite single-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--skip-standard") + " Do not build standard, multi-threaded engine with " + highlight("--all"));
console.log(" " + highlight("--split") + "=" + note("count") + " Split up WASM binary how many parts");
console.log(" " + highlight("-v --verbose") + " Print extra info");
console.log(" " + highlight("--version") + " Specify Stockfish version number (default: " + note(stockfishVersionNumber) + ")");
console.log("");
console.log("Examples:");
console.log("");
console.log(" Default: build the multi-threaded WASM engine");
console.log(" ./build.js");
console.log("");
console.log(" Build all emscripten engine flavors");
console.log(" ./build.js " + highlight("--all"));
console.log("");
console.log(" Build Native binary Stockfish");
console.log(" ./build.js " + highlight("--bin"));
console.log("");
if (params["help-all"]) {
console.log("");
console.log(bold("******** Makefile Help ********"));
console.log("");
spawnSync(params.make, {stdio: [0,1,2], env: process.env, cwd: srcPath});
}
process.exit();
}
if (buildWithEmscripten && !params["skip-em-check"]) {
checkEmscriptenVersion();
}
if (params.all) {
(function ()
{
var hasOnlyFlag = params["only-asm"] || params["only-single"] || params["only-lite"] || params["only-standard"] || params["only-lite-single"] || params["only-single-lite"];
function removeOld(type)
{
fs.readdirSync(srcPath).forEach(function (path)
{
var regex = new RegExp("^stockfish-\\d+(?:.\\d+)?-?" + type + "-[a-f0-9]+(?:-part-\\d+)?\\.(?:wasm(?:\.map)?|js)$", "i");
if (regex.test(path) || /^stockfish(?:-part-\d+)?\.(?:js|wasm(?:\.map)?)$/.test(path)) {
fs.unlinkSync(p.join(srcPath, path));
}
});
}
var newArgs = [process.argv[1], "--force", "--skip-em-check", "--silent"];
Object.keys(params).forEach(function (key)
{
var val = params[key];
var flag;
if (key === "all" || key === "lite" || key === "ultra-lite" || key === "basename" || key === "no-simd" || key === "non-nested" || key === "arch" || key === "_") {
return;
}
if (key.length === 1) {
flag = "-" + key;
} else {
flag = "--" + key;
}
if (val === true) {
newArgs.push(flag)
} else {
newArgs.push(flag, val);
}
});
if (params._ && params._.length) {
newArgs = newArgs.concat(params._);
}
console.log(highlight(" -- (1/5) Building ASM-JS engine..."));
if (!params["skip-asm"] && (!hasOnlyFlag || params["only-asm"])) {
removeOld("-asm");
spawnSync(process.execPath, newArgs.concat(["--asm-js", "--no-split"]), {encoding: "utf8", env: process.env, cwd: __dirname, stdio: [0,1,2]});
} else {
console.log(note(" Skipping..."));
}
console.log(highlight(" -- (2/5) Building Single-threaded Lite engine..."));
if (!params["skip-lite-single"] && !params["skip-single-lite"] && (!hasOnlyFlag || params["only-lite-single"] || params["only-single-lite"])) {
removeOld("-lite-single");
spawnSync(process.execPath, newArgs.concat(["--lite", "--single-threaded", "--no-split"]), {encoding: "utf8", env: process.env, cwd: __dirname, stdio: [0,1,2]});
} else {
console.log(note(" Skipping..."));
}
console.log(highlight(" -- (3/5) Building Multi-threaded Lite engine..."));
if (!params["skip-lite"] && (!hasOnlyFlag || params["only-lite"])) {
removeOld("-lite");
spawnSync(process.execPath, newArgs.concat(["--lite", "--no-split"]), {encoding: "utf8", env: process.env, cwd: __dirname, stdio: [0,1,2]});
} else {
console.log(note(" Skipping..."));
}
console.log(highlight(" -- (4/5) Building Single-threaded Standard engine..."));
if (!params["skip-single"] && (!hasOnlyFlag || params["only-single"])) {
removeOld("-single");
spawnSync(process.execPath, newArgs.concat(["--single-threaded"]), {encoding: "utf8", env: process.env, cwd: __dirname, stdio: [0,1,2]});
} else {
console.log(note(" Skipping..."));
}
console.log(highlight(" -- (5/5) Building Multi-threaded Standard engine..."));
if (!params["skip-standard"] && (!hasOnlyFlag || params["only-standard"])) {
removeOld("");
spawnSync(process.execPath, newArgs.concat(["--basename=stockfish-" + stockfishVersionNumber]), {encoding: "utf8", env: process.env, cwd: __dirname, stdio: [0,1,2]});
} else {
console.log(note(" Skipping..."));
}
console.log(highlight(" -- Finished building all engines"));
beep();
process.exit();
}());
}
if (typeof params.basename === "string") {
basename = params.basename.replace(/\.(?:js|wasm)$/i, "");
}
if (params.force || params.f) {
args.push("--always-make");
//execFileSync(params.make, ["clean"], {stdio: "pipe", env: process.env, cwd: srcPath});
} else if (params["force-linking"]) {
///NOTE: --force will also link as well, so both are not needed.
if (buildWithEmscripten) {
try {
fs.unlinkSync(stockfishJSWASMPath);
} catch (e) {}
try {
fs.unlinkSync(stockfishJSWASMLoaderPath);
} catch (e) {}
if (basename) {
try {
fs.unlinkSync(p.join(srcPath, basename + ".js"));
} catch (e) {}
}
} else {
try {
fs.unlinkSync(stockfishPath);
} catch (e) {}
}
}
if (params["no-minify"]) {
args.push("NOJSMINIFY=yes");
}
if (!basename && params["asm-js"]) {
basename = "stockfish-" + stockfishVersionNumber + "-asm";
}
if (!basename && params["ultra-lite"] && params["single-threaded"]) {
basename = "stockfish-" + stockfishVersionNumber + "-ultra-lite-single";
}
if (!basename && params.lite && params["single-threaded"]) {
basename = "stockfish-" + stockfishVersionNumber + "-lite-single";
}
if (params.lite) {
args.push("LITE_NET=yes");
if (!basename) {
basename = "stockfish-" + stockfishVersionNumber + "-lite";
}
}
if (params["ultra-lite"]) {
args.push("ULTRA_LITE_NET=yes");
if (!basename) {
basename = "stockfish-" + stockfishVersionNumber + "-ultra-lite";
}
}
if (params["single-threaded"]) {
buildingSingleThreaded = true;
args.push("WASM_SINGLE_THREADED=yes");
if (!basename) {
basename = "stockfish-" + stockfishVersionNumber + "-single";
}
}
if (params["debug-wasm"]) {
if (buildWithEmscripten) {
args.push("WASM_DEBUG=yes");
try {
fs.unlinkSync(stockfishWASMPath + ".map");
} catch (e) {}
} else {
warn("Ignoring --debug-wasm");
}
}
if (params.comp) {
args.push("COMP=" + params.comp);
}
if (params.compcxx) {
args.push("COMPCXX=" + params.compcxx);
}
if (String(params.version).toLowerCase() === "timestamp") {
params.version = Date.now();
}
if (String(params.version).toLowerCase() === "hash") {
params.version = execFileSync("git", ["rev-parse", "--short=0", "HEAD"], {encoding: "utf8", env: process.env, cwd: __dirname}).trim();
}
if (params["ultra-lite"]) {
wasmEmbeddedNetsPath = "wasm_embedded_ultra_lite_network.h";
} else if (params.lite) {
wasmEmbeddedNetsPath = "wasm_embedded_lite_network.h";
} else {
wasmEmbeddedNetsPath = "wasm_embedded_networks.h";
}
wasmEmbeddedNetsPath = p.join(srcPath, "emscripten", wasmEmbeddedNetsPath);
if (buildWithEmscripten && !params["keep-syzygy"]) {
args.push("NO_SYZYGY=yes");
}
args.push("ENGINE_VERSION=\"" + getVersion() + "\"");
if (buildWithEmscripten) {
args.push("build");
embedNets();
} else {
args.push("profile-build");
}
if (params.split && !params["no-split"]) {
if (typeof params.split === "boolean") {
params.split = 6;
}
args.push("SPLIT_WASM=yes");
args.push("WASM_SPLIT_COUNT=" + params.split);
}
///
/// Build
///
child = spawnSync(params.make, args, {stdio: [0,1,2], env: process.env, cwd: srcPath});
/// `make` does not throw an error when encountering errors, so we need to do that manually.
if (Number(child.status) !== 0) {
process.exit(Number(child.status));
}
if (!buildWithEmscripten && basename) {
fs.renameSync(stockfishPath, p.join(srcPath, basename + getHashPart(stockfishPath)));
}
if (buildWithEmscripten) {
if (params["asm-js"]) {
fixUpASMJSBuild();
} else {
fixUpWASMBuild();
}
}
beep();