-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathnob.c
230 lines (198 loc) · 9.09 KB
/
nob.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
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#define NOB_EXPERIMENTAL_DELETE_OLD
#include "./dev-deps/nob.h"
#define COMMON_CFLAGS "-Wall", "-Wextra", "-pedantic", "-ggdb", "-I.", "-I./build/", "-I./dev-deps/"
bool build_tools(Cmd *cmd)
{
if (!mkdir_if_not_exists("build")) return false;
if (!mkdir_if_not_exists("build/tools")) return false;
cmd_append(cmd, "clang", COMMON_CFLAGS, "-o", "./build/tools/png2c", "./tools/png2c.c", "-lm");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "clang", COMMON_CFLAGS, "-o", "./build/tools/obj2c", "./tools/obj2c.c", "-lm");
if (!cmd_run_sync_and_reset(cmd)) return false;
return true;
}
bool build_assets(Cmd *cmd)
{
if (!mkdir_if_not_exists("build")) return false;
if (!mkdir_if_not_exists("build/assets")) return false;
cmd_append(cmd, "./build/tools/png2c", "-n", "tsodinPog", "-o", "./build/assets/tsodinPog.c", "./assets/tsodinPog.png");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/png2c", "-n", "tsodinCup", "-o", "./build/assets/tsodinCup.c", "./assets/tsodinCup.png");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/png2c", "-n", "oldstone", "-o", "./build/assets/oldstone.c", "./assets/oldstone.png");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/png2c", "-n", "lavastone", "-o", "./build/assets/lavastone.c", "./assets/lavastone.png");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/obj2c", "-o", "./build/assets/tsodinCupLowPoly.c", "./assets/tsodinCupLowPoly.obj");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/obj2c", "-s", "0.40", "-o", "./build/assets/utahTeapot.c", "./assets/utahTeapot.obj");
if (!cmd_run_sync_and_reset(cmd)) return false;
cmd_append(cmd, "./build/tools/obj2c", "-s", "1.5", "-o", "./build/assets/penger.c", "./assets/penger_obj/penger.obj");
if (!cmd_run_sync_and_reset(cmd)) return false;
return true;
}
bool build_tests(Cmd *cmd)
{
cmd_append(cmd, "clang", COMMON_CFLAGS, "-fsanitize=memory", "-o", "./build/test", "test.c", "-lm");
if (!cmd_run_sync_and_reset(cmd)) return false;
return true;
}
void build_wasm_demo(Cmd *cmd, Procs *procs, const char *name)
{
cmd_append(cmd, "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", temp_sprintf("./build/demos/%s.wasm", name), "-DVC_PLATFORM=VC_WASM_PLATFORM", temp_sprintf("./demos/%s.c", name));
da_append(procs, cmd_run_async_and_reset(cmd));
}
void build_term_demo(Cmd *cmd, Procs *procs, const char *name)
{
cmd_append(cmd, "clang", COMMON_CFLAGS, "-O2", "-o", temp_sprintf("./build/demos/%s.term", name), "-DVC_PLATFORM=VC_TERM_PLATFORM", "-D_XOPEN_SOURCE=600", temp_sprintf("./demos/%s.c", name), "-lm");
da_append(procs, cmd_run_async_and_reset(cmd));
}
void build_sdl_demo(Cmd *cmd, Procs *procs, const char *name)
{
cmd_append(cmd, "clang", COMMON_CFLAGS, "-O2", "-o", temp_sprintf("./build/demos/%s.sdl", name), "-DVC_PLATFORM=VC_SDL_PLATFORM", temp_sprintf("./demos/%s.c", name), "-lm", "-lSDL2", NULL);
da_append(procs, cmd_run_async_and_reset(cmd));
}
void build_vc_demo(Cmd *cmd, Procs *procs, const char *name)
{
build_wasm_demo(cmd, procs, name);
build_term_demo(cmd, procs, name);
build_sdl_demo(cmd, procs, name);
}
bool build_all_vc_demos(Cmd *cmd, Procs *procs)
{
if (!mkdir_if_not_exists("build")) return false;
if (!mkdir_if_not_exists("build/demos")) return false;
const char *names[] = {
"triangle",
"dots3d",
"squish",
"triangle3d",
"triangleTex",
"triangle3dTex",
"cup3d",
"teapot3d",
"penger3d",
};
size_t thread_count = 6;
for (size_t i = 0; i < ARRAY_LEN(names); ++i) {
build_vc_demo(cmd, procs, names[i]);
if (procs->count >= thread_count) {
if (!nob_procs_wait_and_reset(procs)) return false;
}
}
if (!nob_procs_wait_and_reset(procs)) return false;
for (size_t i = 0; i < ARRAY_LEN(names); ++i) {
const char *src_path = temp_sprintf("./build/demos/%s.wasm", names[i]);
const char *dst_path = temp_sprintf("./wasm/%s.wasm", names[i]);
if (!copy_file(src_path, dst_path)) return false;
}
return true;
}
void usage(const char *program)
{
nob_log(INFO, "Usage: %s [<subcommand>]", program);
nob_log(INFO, "Subcommands:");
nob_log(INFO, " tools");
nob_log(INFO, " Build all the tools. Things like png2c, obj2c, etc.");
nob_log(INFO, " assets");
nob_log(INFO, " Build the assets in the assets/ folder.");
nob_log(INFO, " Basically convert their data to C code so we can bake them in demos.");
nob_log(INFO, " test[s] [<args>]");
nob_log(INFO, " Build and run test.c");
nob_log(INFO, " If <args> are provided the test utility is run with them.");
nob_log(INFO, " demos [<platform>] [run]");
nob_log(INFO, " Build demos.");
nob_log(INFO, " Available platforms are: sdl, term, or wasm.");
nob_log(INFO, " Optional [run] runs the demo after the build.");
nob_log(INFO, " [run] is not available for wasm platform.");
nob_log(INFO, " help");
nob_log(INFO, " Print this message");
}
int main(int argc, char **argv)
{
NOB_GO_REBUILD_URSELF(argc, argv);
Cmd cmd = {0};
Procs procs = {0};
const char *program = shift_args(&argc, &argv);
if (argc > 0) {
const char *subcmd = shift_args(&argc, &argv);
if (strcmp(subcmd, "tools") == 0) {
if (!build_tools(&cmd)) return 1;
} else if (strcmp(subcmd, "assets") == 0) {
if (!build_assets(&cmd)) return 1;
} else if (strcmp(subcmd, "tests") == 0 || strcmp(subcmd, "test") == 0) {
if (!build_tests(&cmd)) return 1;
if (argc > 0) {
cmd_append(&cmd, "./build/test");
da_append_many(&cmd, argv, argc);
if (!cmd_run_sync_and_reset(&cmd)) return 1;
}
} else if (strcmp(subcmd, "demos") == 0) {
if (argc <= 0) {
if (!build_all_vc_demos(&cmd, &procs)) return 1;
return 0;
}
const char *name = shift(argv, argc);
if (argc <= 0) {
build_vc_demo(&cmd, &procs, name);
if (!procs_wait_and_reset(&procs)) return 1;
const char *src_path = temp_sprintf("./build/demos/%s.wasm", name);
const char *dst_path = temp_sprintf("./wasm/%s.wasm", name);
if (!copy_file(src_path, dst_path)) return 1;
return 0;
}
const char *platform = shift(argv, argc);
if (strcmp(platform, "sdl") == 0) {
build_sdl_demo(&cmd, &procs, name);
if (!procs_wait_and_reset(&procs)) return 1;
if (argc <= 0) return 0;
const char *run = shift(argv, argc);
if (strcmp(run, "run") != 0) {
usage(program);
nob_log(ERROR, "unknown action `%s` for SDL demo: %s", run, name);
return 1;
}
cmd_append(&cmd, temp_sprintf("./build/demos/%s.sdl", name));
if (!cmd_run_sync_and_reset(&cmd)) return 1;
return 0;
} else if (strcmp(platform, "term") == 0) {
build_term_demo(&cmd, &procs, name);
if (!procs_wait_and_reset(&procs)) return 1;
if (argc <= 0) return 0;
const char *run = shift(argv, argc);
if (strcmp(run, "run") != 0) {
usage(program);
nob_log(ERROR, "unknown action `%s` for Terminal demo: %s", run, name);
return 1;
}
cmd_append(&cmd, temp_sprintf("./build/demos/%s.term", name));
if (!cmd_run_sync_and_reset(&cmd)) return 1;
return 0;
} else if (strcmp(platform, "wasm") == 0) {
build_wasm_demo(&cmd, &procs, name);
if (!procs_wait_and_reset(&procs)) return 1;
const char *src_path = temp_sprintf("./build/demos/%s.wasm", name);
const char *dst_path = temp_sprintf("./wasm/%s.wasm", name);
if (!copy_file(src_path, dst_path)) return 1;
} else {
usage(program);
nob_log(ERROR, "unknown demo platform %s", platform);
return 1;
}
} else if(strcmp(subcmd, "help") == 0) {
usage(program);
} else {
usage(program);
nob_log(ERROR, "Unknown command `%s`", subcmd);
return 1;
}
} else {
if (!build_tools(&cmd)) return 1;
if (!build_assets(&cmd)) return 1;
if (!build_tests(&cmd)) return 1;
if (!build_all_vc_demos(&cmd, &procs)) return 1;
}
return 0;
}