-
Notifications
You must be signed in to change notification settings - Fork 109
/
nobuild.c
268 lines (243 loc) · 10.3 KB
/
nobuild.c
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
#define NOBUILD_IMPLEMENTATION
#include "./nobuild.h"
#define COMMON_CFLAGS "-Wall", "-Wextra", "-pedantic", "-std=c11", "-ggdb", "-I.", "-I./build/", "-I./dev-deps/"
void build_tools(void)
{
MKDIRS("build", "tools");
CMD("clang", COMMON_CFLAGS, "-o", "./build/tools/png2c", "./tools/png2c.c", "-lm");
CMD("clang", COMMON_CFLAGS, "-o", "./build/tools/obj2c", "./tools/obj2c.c", "-lm");
}
void build_assets(void)
{
MKDIRS("build", "assets");
CMD("./build/tools/png2c", "-n", "tsodinPog", "-o", "./build/assets/tsodinPog.c", "./assets/tsodinPog.png");
CMD("./build/tools/png2c", "-n", "tsodinCup", "-o", "./build/assets/tsodinCup.c", "./assets/tsodinCup.png");
CMD("./build/tools/png2c", "-n", "oldstone", "-o", "./build/assets/oldstone.c", "./assets/oldstone.png");
CMD("./build/tools/png2c", "-n", "lavastone", "-o", "./build/assets/lavastone.c", "./assets/lavastone.png");
CMD("./build/tools/obj2c", "-o", "./build/assets/tsodinCupLowPoly.c", "./assets/tsodinCupLowPoly.obj");
CMD("./build/tools/obj2c", "-s", "0.40", "-o", "./build/assets/utahTeapot.c", "./assets/utahTeapot.obj");
CMD("./build/tools/obj2c", "-s", "1.5", "-o", "./build/assets/penger.c", "./assets/penger_obj/penger.obj");
}
void build_tests(void)
{
CMD("clang", COMMON_CFLAGS, "-fsanitize=memory", "-o", "./build/test", "test.c", "-lm");
}
// TODO: move copy_file to nobuild.h
// Maybe even use platform dependent APIs to make it better.
void copy_file(const char *src_file_path, const char *dst_file_path)
{
INFO("Copying %s -> %s", src_file_path, dst_file_path);
size_t buffer_sz = 32*1024;
char *buffer = malloc(buffer_sz);
if (buffer == NULL) {
PANIC("Could not allocate memory to copy file %s -> %s", src_file_path, dst_file_path);
}
FILE *src = fopen(src_file_path, "rb");
if (src == NULL) {
PANIC("Could not open file %s for reading: %s", src_file_path, strerror(errno));
}
FILE *dst = fopen(dst_file_path, "wb");
if (dst == NULL) {
PANIC("Could not open file %s for writing: %s", dst_file_path, strerror(errno));
}
while (!feof(src)) {
size_t n = fread(buffer, 1, buffer_sz, src);
if (ferror(src)) {
PANIC("Could not read from file %s: %s", src_file_path, strerror(errno));
}
size_t m = 0;
while (m < n) {
m += fwrite(buffer + m, 1, n - m, dst);
if (ferror(dst)) {
PANIC("Could not write to file %s: %s", dst_file_path, strerror(errno));
}
}
}
if (fclose(dst) < 0) PANIC("Could not close file %s: %s", dst_file_path, strerror(errno));
if (fclose(src) < 0) PANIC("Could not close file %s: %s", src_file_path, strerror(errno));
}
Pid build_wasm_demo(const char *name)
{
Cmd cmd = {
.line = cstr_array_make("clang", COMMON_CFLAGS, "-O2", "-fno-builtin", "--target=wasm32", "--no-standard-libraries", "-Wl,--no-entry", "-Wl,--export=vc_render", "-Wl,--export=__heap_base", "-Wl,--allow-undefined", "-o", CONCAT("./build/demos/", name, ".wasm"), "-DVC_PLATFORM=VC_WASM_PLATFORM", CONCAT("./demos/", name, ".c"), NULL)
};
INFO("CMD: %s", cmd_show(cmd));
return cmd_run_async(cmd, NULL, NULL);
}
Pid build_term_demo(const char *name)
{
Cmd cmd = {
.line = cstr_array_make("clang", COMMON_CFLAGS, "-O2", "-o", CONCAT("./build/demos/", name, ".term"), "-DVC_PLATFORM=VC_TERM_PLATFORM", "-D_XOPEN_SOURCE=600", CONCAT("./demos/", name, ".c"), "-lm", NULL)
};
INFO("CMD: %s", cmd_show(cmd));
return cmd_run_async(cmd, NULL, NULL);
}
Pid build_sdl_demo(const char *name)
{
Cmd cmd = {
.line = cstr_array_make("clang", COMMON_CFLAGS, "-O2", "-o", CONCAT("./build/demos/", name, ".sdl"), "-DVC_PLATFORM=VC_SDL_PLATFORM", CONCAT("./demos/", name, ".c"), "-lm", "-lSDL2", NULL)
};
INFO("CMD: %s", cmd_show(cmd));
return cmd_run_async(cmd, NULL, NULL);
}
// TODO: move struct Pids, pids_wait() and da_append() to nobuild.h
typedef struct {
Pid *items;
size_t count;
size_t capacity;
} Pids;
void pids_wait(Pids pids)
{
for (size_t i = 0; i < pids.count; ++i) {
pid_wait(pids.items[i]);
}
}
#define DA_INIT_CAPACITY 8192
#define DA_REALLOC(oldptr, oldsz, newsz) realloc(oldptr, newsz)
#define da_append(da, item) \
do { \
if ((da)->count >= (da)->capacity) { \
size_t new_capacity = (da)->capacity*2; \
if (new_capacity == 0) { \
new_capacity = DA_INIT_CAPACITY; \
} \
\
(da)->items = DA_REALLOC((da)->items, \
(da)->capacity*sizeof((da)->items[0]), \
new_capacity*sizeof((da)->items[0])); \
(da)->capacity = new_capacity; \
} \
\
(da)->items[(da)->count++] = (item); \
} while (0)
void build_vc_demo(const char *name, Pids *pids)
{
da_append(pids, build_wasm_demo(name));
da_append(pids, build_term_demo(name));
da_append(pids, build_sdl_demo(name));
}
void build_all_vc_demos(void)
{
MKDIRS("build", "demos");
const char *names[] = {
"triangle",
"dots3d",
"squish",
"triangle3d",
"triangleTex",
"triangle3dTex",
"cup3d",
"teapot3d",
"penger3d",
};
size_t names_sz = sizeof(names)/sizeof(names[0]);
size_t thread_count = 6;
Pids pids = {0};
for (size_t i = 0; i < names_sz; ++i) {
build_vc_demo(names[i], &pids);
if (pids.count >= thread_count) {
pids_wait(pids);
pids.count = 0;
}
}
pids_wait(pids);
for (size_t i = 0; i < names_sz; ++i) {
copy_file(CONCAT("./build/demos/", names[i], ".wasm"), CONCAT("./wasm/", names[i], ".wasm"));
}
}
void usage(const char *program)
{
INFO("Usage: %s [<subcommand>]", program);
INFO("Subcommands:");
INFO(" tools");
INFO(" Build all the tools. Things like png2c, obj2c, etc.");
INFO(" assets");
INFO(" Build the assets in the assets/ folder.");
INFO(" Basically convert their data to C code so we can bake them in demos.");
INFO(" test[s] [<args>]");
INFO(" Build and run test.c");
INFO(" If <args> are provided the test utility is run with them.");
INFO(" demos [<platform>] [run]");
INFO(" Build demos.");
INFO(" Available platforms are: sdl, term, or wasm.");
INFO(" Optional [run] runs the demo after the build.");
INFO(" [run] is not available for wasm platform.");
INFO(" help");
INFO(" Print this message");
}
int main(int argc, char **argv)
{
GO_REBUILD_URSELF(argc, argv);
const char *program = shift_args(&argc, &argv);
if (argc > 0) {
const char *subcmd = shift_args(&argc, &argv);
if (strcmp(subcmd, "tools") == 0) {
build_tools();
} else if (strcmp(subcmd, "assets") == 0) {
build_assets();
} else if (strcmp(subcmd, "tests") == 0 || strcmp(subcmd, "test") == 0) {
build_tests();
if (argc > 0) {
Cmd cmd = {0};
cmd.line = cstr_array_append(cmd.line, "./build/test");
for (int i = 0; i < argc; ++i) {
cmd.line = cstr_array_append(cmd.line, argv[i]);
}
cmd.line = cstr_array_append(cmd.line, NULL);
cmd_run_sync(cmd);
}
} else if (strcmp(subcmd, "demos") == 0) {
if (argc > 0) {
const char *name = shift_args(&argc, &argv);
if (argc > 0) {
const char *platform = shift_args(&argc, &argv);
if (strcmp(platform, "sdl") == 0) {
pid_wait(build_sdl_demo(name));
if (argc > 0) {
const char *run = shift_args(&argc, &argv);
if (strcmp(run, "run") != 0) {
usage(program);
PANIC("unknown action `%s` for SDL demo: %s", run, name);
}
CMD(CONCAT("./build/demos/", name, ".sdl"));
}
} else if (strcmp(platform, "term") == 0) {
pid_wait(build_term_demo(name));
if (argc > 0) {
const char *run = shift_args(&argc, &argv);
if (strcmp(run, "run") != 0) {
usage(program);
PANIC("unknown action `%s` for Terminal demo: %s", run, name);
}
CMD(CONCAT("./build/demos/", name, ".term"));
}
} else if (strcmp(platform, "wasm") == 0) {
pid_wait(build_wasm_demo(name));
copy_file(CONCAT("./build/demos/", name, ".wasm"), CONCAT("./wasm/", name, ".wasm"));
} else {
usage(program);
PANIC("unknown demo platform %s", platform);
}
} else {
Pids pids = {0};
build_vc_demo(name, &pids);
pids_wait(pids);
copy_file(CONCAT("./build/demos/", name, ".wasm"), CONCAT("./wasm/", name, ".wasm"));
}
} else {
build_all_vc_demos();
}
} else if(strcmp(subcmd, "help") == 0) {
usage(program);
} else {
usage(program);
PANIC("Unknown command `%s`", subcmd);
}
} else {
build_tools();
build_assets();
build_tests();
build_all_vc_demos();
}
return 0;
}