-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathbuild.js
More file actions
196 lines (181 loc) · 5.33 KB
/
build.js
File metadata and controls
196 lines (181 loc) · 5.33 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
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
// @ts-check
const { spawn } = require('child_process');
const { promisify } = require('util');
const { mkdir, mkdtemp } = require('fs/promises');
const BUILD_FOLDER = 'build/';
const SRC_FOLDER = 'src/';
/**
* TODO: this signature is outdated
*
* @param {string} program
* @param {string[]} args
* @returns {ReturnType<typeof spawn>}
*/
function cmd(program, args, callback) {
const spawnOptions = { "shell": true };
console.log('CMD:', program, args.flat(), spawnOptions);
const p = spawn(program, args.flat(), spawnOptions); // NOTE: flattening the args array enables you to group related arguments for better self-documentation of the running command
// @ts-ignore [stdout may be null?]
p.stdout.on('data', (data) => process.stdout.write(data));
// @ts-ignore [stderr may be null?]
p.stderr.on('data', (data) => process.stderr.write(data));
p.on('exit', (code, signal) => callback(code || signal || null))
p.on('error', callback)
return p;
}
const cmdAsync = promisify(cmd);
function TODO(message) {
throw new Error(`TODO: ${message}`)
}
function buildJs() {
return cmdAsync("./node_modules/.bin/tsc", []);
}
async function buildClient() {
await cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-I"+SRC_FOLDER,
"-I"+SRC_FOLDER+"cws/",
"-o", BUILD_FOLDER+"packer",
SRC_FOLDER+"packer.c",
"-lm",
]);
await cmdAsync(BUILD_FOLDER+"packer", [
BUILD_FOLDER+"pack.h",
]);
await Promise.all([
cmdAsync("clang", [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-c", SRC_FOLDER+"common.c",
"-o", BUILD_FOLDER+"common.wasm.o",
]),
cmdAsync("clang", [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-I", BUILD_FOLDER,
"-c", SRC_FOLDER+"client.c",
"-o", BUILD_FOLDER+"client.wasm.o",
]),
cmdAsync("clang", [
"-Wall", "-Wextra",
"--target=wasm32",
"-I", SRC_FOLDER+"cws/",
"-c", SRC_FOLDER+"sort.c",
"-o", BUILD_FOLDER+"sort.wasm.o",
]),
])
return cmdAsync("c3c", [
"compile",
"-D", "PLATFORM_WEB",
"--reloc=none",
"--target", "wasm32",
"-O5", "-g0", "--link-libc=no", "--no-entry",
"--trust=full",
"-o", "client",
"-z", "--export-table",
"-z", "--allow-undefined",
"-z", "--export=pixels_of_display",
"-z", "--export=key_up",
"-z", "--export=key_down",
BUILD_FOLDER+"common.wasm.o",
BUILD_FOLDER+"client.wasm.o",
BUILD_FOLDER+"sort.wasm.o",
SRC_FOLDER+"client.c3",
SRC_FOLDER+"common.c3",
])
}
async function buildCWS() {
await Promise.all([
cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-o", BUILD_FOLDER+"coroutine.o",
"-fsanitize=address",
"-c",
SRC_FOLDER+"cws/coroutine.c"
]),
cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-o", BUILD_FOLDER+"cws.o",
"-fsanitize=address",
"-c",
SRC_FOLDER+"cws/cws.c"
]),
])
await cmdAsync("ar", [
"-rcs",
BUILD_FOLDER+"libcws.a",
BUILD_FOLDER+"coroutine.o",
BUILD_FOLDER+"cws.o"
])
}
async function buildServer() {
await Promise.all([
buildCWS(),
cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-I", SRC_FOLDER+"cws/",
"-fsanitize=address",
"-c", SRC_FOLDER+"server.c",
"-o", BUILD_FOLDER+"server.o",
]),
cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-I", SRC_FOLDER+"cws/",
"-fsanitize=address",
"-c", SRC_FOLDER+"common.c",
"-o", BUILD_FOLDER+"common.o",
]),
cmdAsync("clang", [
"-Wall", "-Wextra", "-ggdb",
"-I", SRC_FOLDER+"cws/",
"-fsanitize=address",
"-c", SRC_FOLDER+"stats.c",
"-o", BUILD_FOLDER+"stats.o",
]),
])
await cmdAsync("clang", [
"-ggdb",
"-fsanitize=address",
"-o", BUILD_FOLDER+"server",
BUILD_FOLDER+"server.o",
BUILD_FOLDER+"common.o",
BUILD_FOLDER+"stats.o",
BUILD_FOLDER+"libcws.a",
"-lm"
]);
}
function mkdirp(path) {
console.log(`MKDIR: ${path}`)
return mkdir(path, {
mode: 0o755,
recursive: true
});
}
async function main () {
await mkdirp(BUILD_FOLDER)
await Promise.all([
buildJs(),
// Running all the C3 related builds sequentually because c3c is completely unparallelizable
(async () => {
const args = process.argv.slice(2);
const target = args.shift()
switch (target) {
case undefined:
await buildClient();
await buildServer();
break;
case 'client':
await buildClient();
break;
case 'server':
await buildServer();
break;
default:
throw new Error(`unknown target \`${target}\``)
}
})()
])
}
main()