forked from ukyo/zlib-wasm-without-emscripten-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
104 lines (94 loc) · 2.76 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
const path = require("path");
const mkdirp = require("mkdirp");
const waCliUtil = require("webassembly/cli/util");
const OPTIMIZATION_LEVEL = process.argv[2] || "-Oz";
const zlibSources = [
"adler32",
"crc32",
"deflate",
"infback",
"inffast",
"inflate",
"inftrees",
"trees",
"zutil",
];
const builddir = path.join(__dirname, "build");
const srcdir = path.join(__dirname, "src");
const zlibdir = path.join(__dirname, "zlib");
mkdirp.sync(path.join(__dirname, "build"));
(async () => {
await waCliUtil.run(path.join(waCliUtil.bindir, "clang"), [
zlibSources.map(s => path.join(zlibdir, `${s}.c`)),
OPTIMIZATION_LEVEL,
"-c",
"--target=wasm32-unknown-unknown",
"-D", "WEBASSEMBLY",
"-emit-llvm",
"-nostdinc",
"-nostdlib",
"-fno-builtin",
"-isystem", path.join(waCliUtil.basedir, "include"),
"-isystem", path.join(waCliUtil.basedir, "lib/musl-wasm32/include"),
"-isystem", path.join(srcdir, "include"),
`-I${zlibdir}`,
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "llvm-link"), [
zlibSources.map(s => `${s}.bc`),
"-o", "zlib.bc",
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "clang"), [
path.join(srcdir, "zlib-sample.c"),
OPTIMIZATION_LEVEL,
"-c",
"--target=wasm32-unknown-unknown",
"-D", "WEBASSEMBLY",
"-emit-llvm",
"-nostdinc",
"-nostdlib",
"-fno-builtin",
"-isystem", path.join(waCliUtil.basedir, "include"),
"-isystem", path.join(waCliUtil.basedir, "lib/musl-wasm32/include"),
"-isystem", path.join(srcdir, "include"),
`-I${zlibdir}`,
`-I${path.join(srcdir, "import.h")}`,
"-o", "tmp-zlib-sample.bc",
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "llvm-link"), [
"tmp-zlib-sample.bc",
"zlib.bc",
path.join(waCliUtil.basedir, "lib", "webassembly.bc"),
"-only-needed",
"-o", "zlib-sample.bc",
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "llc"), [
"zlib-sample.bc",
"-march=wasm32",
"-filetype=asm",
"-asm-verbose=false",
"-thread-model=single",
"-data-sections",
"-function-sections",
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "s2wasm"), [
"zlib-sample.s",
"--import-memory",
"--allocate-stack", "10000",
"-o", "zlib-sample.wat",
], { cwd: builddir });
await waCliUtil.run(path.join(waCliUtil.bindir, "wasm-opt"), [
"zlib-sample.wat",
"-O3",
"--coalesce-locals-learning",
"--dce",
"--duplicate-function-elimination",
"--inlining",
"--local-cse",
"--merge-blocks",
"--optimize-instructions",
"--reorder-functions",
"--reorder-locals",
"--vacuum",
"-o", "zlib-sample.wasm",
], { cwd: builddir });
})();