-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnob.c
executable file
·331 lines (267 loc) · 9.61 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
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
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "src/nob/nob.h"
#define EXECUTABLE_NAME "geometryfoss"
#define RAYLIB_VERSION "5.0"
// Stolen from https://github.com/tsoding/musializer
typedef enum {
TARGET_LINUX,
TARGET_WIN32_MINGW,
COUNT_TARGETS
} Target;
static_assert(2 == COUNT_TARGETS, "Amount of targets has changed");
const char *targetNames[] = {
[TARGET_LINUX] = "linux",
[TARGET_WIN32_MINGW] = "win32-mingw",
};
void logAvailableTargets(Log_Level level) {
nob_log(level, "Available targets:");
for (size_t i = 0; i < COUNT_TARGETS; ++i) {
nob_log(level, " %s", targetNames[i]);
}
}
////
static const char* raylibModules[] = {
"rcore",
"rglfw",
"rshapes",
"rtext",
"rtextures",
"utils",
};
bool buildRaylib(Target target) {
// Mostly stolen from https://github.com/tsoding/musializer
bool result = true;
Cmd cmd = {0};
File_Paths objectFiles = {0};
if (!mkdir_if_not_exists("./build/raylib")) {
return_defer(false);
}
Procs procs = {0};
const char* buildPath = temp_sprintf("./build/raylib/%s", ARRAY_GET(targetNames, target));
if (!mkdir_if_not_exists(buildPath)) {
return_defer(false);
}
for (size_t i = 0; i < ARRAY_LEN(raylibModules); ++i) {
const char* inputPath = temp_sprintf("./raylib/raylib-"RAYLIB_VERSION"/src/%s.c", raylibModules[i]);
const char* outputPath = temp_sprintf("%s/%s.o", buildPath, raylibModules[i]);
da_append(&objectFiles, outputPath);
if (needs_rebuild1(outputPath, inputPath)) {
switch (target) {
case TARGET_LINUX: {
cmd_append(&cmd, "gcc");
} break;
case TARGET_WIN32_MINGW: {
cmd_append(&cmd, "x86_64-w64-mingw32-gcc");
} break;
default: NOB_ASSERT(0 && "unreachable");
}
cmd_append(&cmd, "-ggdb", "-DPLATFORM_DESKTOP", "-D_GNU_SOURCE", "-fPIC"); // Remove -ggdb for a miniscule amount of extra performance
cmd_append(&cmd, "-O2");
cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src/external/glfw/include");
cmd_append(&cmd, "-c", inputPath);
cmd_append(&cmd, "-o", outputPath);
Proc proc = cmd_run_async_and_reset(&cmd);
da_append(&procs, proc);
}
}
if (!procs_wait_and_reset(&procs)) return_defer(false);
switch (target) {
case TARGET_WIN32_MINGW:
case TARGET_LINUX: {
const char* libraylibPath = temp_sprintf("%s/libraylib.a", buildPath);
if (needs_rebuild(libraylibPath, objectFiles.items, objectFiles.count)) {
cmd_append(&cmd, "ar", "-crs", libraylibPath);
for (size_t i = 0; i < ARRAY_LEN(raylibModules); ++i) {
const char* inputPath = temp_sprintf("%s/%s.o", buildPath, raylibModules[i]);
cmd_append(&cmd, inputPath);
}
if (!cmd_run_sync_and_reset(&cmd)) return_defer(false);
}
} break;
default: NOB_ASSERT(0 && "unreachable");
}
defer:
cmd_free(cmd);
da_free(objectFiles);
return result;
}
static const char* cFiles[] = {
"geometryfoss.c",
"assets/assets.c",
"assets/font.c",
"camera.c",
"coord.c",
"lib/cJSON/cJSON.c",
"lib/easing/easing.c",
"lib/yxml/yxml.c",
"grid.c",
"ground.c",
"hitbox.c",
"input/keyboard.c",
"input/mouse.c",
"level/level.c",
"level/levelsettings.c",
"object.c",
"player/player.c",
"player/physics/playerphysics.c",
"scene/scenemanager.c",
"scene/sceneswitcher.c",
"scene/sceneloadassets.c",
"scene/scenelevel.c",
"scene/scenelvled.c",
"select.c",
"ui/popup.c",
"ui/text.c",
};
// Kind of stolen from the get_git_hash function in https://github.com/rexim/tore/blob/main/nob.c
bool addGitVersion(Cmd* cmd) {
Cmd tempCmd = {0};
bool result = true;
Fd fdout = fd_open_for_write("./build/gitHash.txt");
if (fdout == INVALID_FD) return_defer(false);
cmd_append(&tempCmd, "git", "describe", "--abbrev=7", "--dirty", "--always", "--tags");
if (!cmd_run_sync_redirect_and_reset(&tempCmd, (Cmd_Redirect) {
.fdout = &fdout,
})) return_defer(false);
String_Builder sb = {0};
if (!read_entire_file("./build/gitHash.txt", &sb)) return_defer(false);
while (sb.count > 0 && isspace(sb.items[sb.count - 1])) --sb.count;
sb_append_null(&sb);
cmd_append(cmd, temp_sprintf("-DGIT_VERSION=\"%s\"", sb.items));
int isDirty = strstr(sb.items, "-dirty") != NULL;
cmd_append(cmd, temp_sprintf("-DGIT_IS_DIRTY=%d", isDirty));
da_free(sb);
defer:
if (!result) {
cmd_append(cmd, "-DGIT_VERSION=\"unknown\"", "-DGIT_IS_DIRTY=0");
}
cmd_free(tempCmd);
return result;
}
bool buildMain(Target target, bool debugMode) {
bool result = true;
Cmd cmd = {0};
switch (target) {
case TARGET_LINUX: {
cmd_append(&cmd, "gcc");
cmd_append(&cmd, "-Wall", "-Wextra", "-ggdb");
if (debugMode)
cmd_append(&cmd, "-DDEBUG");
// Add the git version
addGitVersion(&cmd);
// Disable warnings from raygui
cmd_append(&cmd, "-isystem", "./src/lib/ray");
// Disable some warnings from stb_ds
cmd_append(&cmd, "-isystem", "./src/lib/stb");
cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src");
cmd_append(&cmd, "-I./src");
cmd_append(&cmd, "-I./src/nob");
cmd_append(&cmd, "-o", "./build/"EXECUTABLE_NAME);
for (size_t i = 0; i < ARRAY_LEN(cFiles); ++i) {
cmd_append(&cmd, temp_sprintf("src/%s", cFiles[i]));
}
cmd_append(&cmd,
temp_sprintf("-L./build/raylib/%s", ARRAY_GET(targetNames, target)),
"-l:libraylib.a");
cmd_append(&cmd, "-lm");
if (!cmd_run_sync_and_reset(&cmd)) return_defer(false);
} break;
case TARGET_WIN32_MINGW: {
cmd_append(&cmd, "x86_64-w64-mingw32-gcc");
cmd_append(&cmd, "-Wall", "-Wextra", "-ggdb", "-static");
if (debugMode)
cmd_append(&cmd, "-DDEBUG");
// Add the git version
addGitVersion(&cmd);
// Disable warnings from raygui
cmd_append(&cmd, "-isystem", "./src/lib/ray");
// Disable some warnings from stb_ds
cmd_append(&cmd, "-isystem", "./src/lib/stb");
cmd_append(&cmd, "-I./raylib/raylib-"RAYLIB_VERSION"/src");
cmd_append(&cmd, "-I./src");
cmd_append(&cmd, "-I./src/nob");
cmd_append(&cmd, "-o", "./build/"EXECUTABLE_NAME);
for (size_t i = 0; i < ARRAY_LEN(cFiles); ++i) {
cmd_append(&cmd, temp_sprintf("src/%s", cFiles[i]));
}
cmd_append(&cmd,
temp_sprintf("-L./build/raylib/%s", ARRAY_GET(targetNames, target)),
"-l:libraylib.a");
cmd_append(&cmd, "-lwinmm", "-lgdi32");
cmd_append(&cmd, "-static");
if (!cmd_run_sync_and_reset(&cmd)) return_defer(false);
} break;
default: NOB_ASSERT(0 && "unreachable");
}
defer:
cmd_free(cmd);
return result;
}
bool parseTarget(const char* value, Target* target) {
bool found = false;
for (size_t i = 0; !found && i < COUNT_TARGETS; ++i) {
if (strcmp(targetNames[i], value) == 0) {
*target = i;
found = true;
}
}
if (!found) {
nob_log(ERROR, "Unknown target %s", value);
logAvailableTargets(ERROR);
return false;
}
return true;
}
void logAvailableSubcommands(const char* program, Log_Level level) {
nob_log(level, "Usage: %s <subcommand>", program);
nob_log(level, "Subcommands:");
nob_log(level, " build");
nob_log(level, " help");
}
int main(int argc, char** argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
const char* program = shift(argv, argc);
if (argc < 1) {
logAvailableSubcommands(program, ERROR);
return 1;
}
const char* subcommand = shift(argv, argc);
if (strcmp(subcommand, "help") == 0) {
logAvailableSubcommands(program, INFO);
return 0;
} else if (strcmp(subcommand, "build") == 0) {
if (argc < 1) {
nob_log(INFO, "To compile for a different target, use: %s build <target> [options]", program);
}
if (!mkdir_if_not_exists("./build")) return 1;
// Set default target
#ifdef _WIN32
Target target = TARGET_WIN32_MINGW;
#else
Target target = TARGET_LINUX;
#endif // _WIN32
if (argc >= 1) {
const char* value = shift(argv, argc);
if (!parseTarget(value, &target)) return 1;
}
bool debugMode = false;
while (argc >= 1) {
const char* arg = shift(argv, argc);
if (strcmp(arg, "--debug") == 0) {
debugMode = true;
} else {
nob_log(ERROR, "Usage: %s build %s [options]", program, targetNames[target]);
nob_log(ERROR, "Available options:");
nob_log(ERROR, " --debug Extra debug visuals and logs");
return 1;
}
}
if (!buildRaylib(target)) return 1;
if (!buildMain(target, debugMode)) return 1;
} else {
logAvailableSubcommands(program, ERROR);
return 1;
}
return 0;
}