-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnob.c
More file actions
256 lines (223 loc) · 10.1 KB
/
nob.c
File metadata and controls
256 lines (223 loc) · 10.1 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
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
#define NOB_IMPLEMENTATION
#define NOB_EXPERIMENTAL_DELETE_OLD
#include "nob.h"
static bool nob_read_entire_dir_recursively(const char* parent, Nob_File_Paths* file_paths, int idx)
{
if (!nob_read_entire_dir(parent, file_paths)) {
return false;
}
int count = file_paths->count;
for (int i = idx; i < count; i++) {
if (strcmp(file_paths->items[i], ".") == 0) continue;
if (strcmp(file_paths->items[i], "..") == 0) continue;
// NOTE: Here I'm leaking the allocation for each file name on the nob_read_entire_dir call
// but I'm don't care that much because that alloc is done on the temp static arena
file_paths->items[i] = nob_temp_sprintf("%s/%s", parent, file_paths->items[i]);
Nob_File_Type file_type = nob_get_file_type(file_paths->items[i]);
if (file_type == NOB_FILE_DIRECTORY) {
if (!nob_read_entire_dir_recursively(file_paths->items[i], file_paths, file_paths->count)) {
return false;
}
}
}
return true;
}
static bool clear_folder(const char* parent, Nob_File_Paths* file_paths)
{
if (!nob_read_entire_dir_recursively(parent, file_paths, 0)) {
return false;
}
for (int i = 0; i < file_paths->count; i++) {
Nob_File_Type file_type = nob_get_file_type(file_paths->items[i]);
if (file_type == NOB_FILE_REGULAR) {
if (!nob_delete_file(file_paths->items[i])) {
return false;
}
}
}
file_paths->count = 0;
return true;
}
int main(int argc, char** argv)
{
NOB_GO_REBUILD_URSELF(argc, argv);
if (!nob_mkdir_if_not_exists("build")) return 1;
if (!nob_mkdir_if_not_exists("external/cimgui/build")) return 1;
Nob_Cmd cmd = {0};
Nob_File_Paths file_paths = {0};
nob_log(NOB_INFO, "Compiling and linking...");
int build_release = 0;
int build_msvc = 0;
if (argc > 1) {
// Shift the first argument (the executable path) so
// that the next arguments are the actual command-line arguments
nob_shift_args(&argc, &argv);
while (argc > 0) {
const char* arg = nob_shift_args(&argc, &argv);
if (arg) {
if (strcmp(arg, "--build") == 0 || strcmp(arg, "-b") == 0) {
if (argc == 0) {
nob_log(NOB_ERROR, "missing build mode after %s", arg);
return 1;
}
const char* build_mode_value = nob_shift_args(&argc, &argv);
if (!build_mode_value) {
nob_log(NOB_ERROR, "missing build mode after %s", arg);
return 1;
}
if (strcasecmp(build_mode_value, "debug") == 0) {
build_release = 0;
} else if (strcasecmp(build_mode_value, "release") == 0) {
build_release = 1;
} else {
nob_log(NOB_ERROR, "unknown build mode: %s. Valid options are 'debug' or 'release'.",
build_mode_value);
return 1;
}
} else if (strcmp(arg, "--compiler") == 0 || strcmp(arg, "-cc") == 0) {
if (argc == 0) {
nob_log(NOB_ERROR, "missing compiler after %s", arg);
return 1;
}
const char* compiler_value = nob_shift_args(&argc, &argv);
if (!compiler_value) {
nob_log(NOB_ERROR, "missing compiler after %s", arg);
return 1;
}
if (strcasecmp(compiler_value, "gcc") == 0) {
build_msvc = 0;
} else if (strcasecmp(compiler_value, "msvc") == 0) {
build_msvc = 1;
} else {
nob_log(NOB_ERROR, "unknown compiler: %s. Valid options are 'gcc' or 'msvc'.", compiler_value);
return 1;
}
} else {
nob_log(NOB_WARNING, "ignoring unknown argument: %s", arg);
}
}
}
}
const char* cimgui_src_files[] = {
"external/cimgui/cimgui.cpp",
"external/cimgui/cimgui_impl.cpp",
"external/cimgui/imgui/imgui.cpp",
"external/cimgui/imgui/imgui_demo.cpp",
"external/cimgui/imgui/imgui_draw.cpp",
"external/cimgui/imgui/imgui_tables.cpp",
"external/cimgui/imgui/imgui_widgets.cpp",
"external/cimgui/imgui/backends/imgui_impl_sdl2.cpp",
"external/cimgui/imgui/backends/imgui_impl_opengl3.cpp",
};
int rebuild_cimgui = !nob_file_exists("external/cimgui/build/cimgui.dll");
if (rebuild_cimgui || build_release) {
if (!clear_folder("external/cimgui/build", &file_paths)) {
return 1;
}
nob_log(NOB_INFO, "Compiling cimgui.dll...");
if (build_msvc) {
nob_cmd_append(&cmd, "cl.exe", "/LD");
if (build_release) {
nob_cmd_append(&cmd, "/O2");
} else {
nob_cmd_append(&cmd, "/Od", "/Zi");
}
nob_cmd_append(&cmd, "/Iexternal/SDL2/include");
nob_cmd_append(&cmd, "/Iexternal/SDL2/include/SDL2");
nob_cmd_append(&cmd, "/Iexternal/cimgui/imgui");
nob_cmd_append(&cmd, "/Iexternal/cimgui/imgui/backends");
nob_cmd_append(&cmd, "/Iexternal/cimgui");
nob_cmd_append(&cmd, "/DCIMGUI_USE_SDL2", "/DCIMGUI_USE_OPENGL3");
nob_cmd_append(&cmd, "/DIMGUI_IMPL_API=extern \"C\" __declspec(dllexport)");
for (size_t i = 0; i < NOB_ARRAY_LEN(cimgui_src_files); ++i) {
nob_cmd_append(&cmd, cimgui_src_files[i]);
}
nob_cmd_append(&cmd, "/link");
nob_cmd_append(&cmd, "/LIBPATH:external/SDL2/lib/msvc/x64", "SDL2.lib", "opengl32.lib");
nob_cmd_append(&cmd, "/OUT:external/cimgui/build/cimgui.dll", "/IMPLIB:external/cimgui/build/cimgui.lib");
} else {
nob_cmd_append(&cmd, "g++", "-shared");
if (build_release) {
nob_cmd_append(&cmd, "-O2");
} else {
nob_cmd_append(&cmd, "-O0", "-g");
}
nob_cmd_append(&cmd, "-Iexternal/SDL2/include");
nob_cmd_append(&cmd, "-Iexternal/SDL2/include/SDL2");
nob_cmd_append(&cmd, "-Iexternal/cimgui/imgui");
nob_cmd_append(&cmd, "-Iexternal/cimgui/imgui/backends");
nob_cmd_append(&cmd, "-Iexternal/cimgui");
nob_cmd_append(&cmd, "-DCIMGUI_USE_SDL2", "-DCIMGUI_USE_OPENGL3");
nob_cmd_append(&cmd, "-DIMGUI_IMPL_API=extern \"C\" __declspec(dllexport)");
for (size_t i = 0; i < NOB_ARRAY_LEN(cimgui_src_files); ++i) {
nob_cmd_append(&cmd, cimgui_src_files[i]);
}
nob_cmd_append(&cmd, "-Lexternal/SDL2/lib/mingw32", "-lSDL2", "-lopengl32");
nob_cmd_append(&cmd, "-o", "external/cimgui/build/cimgui.dll",
"-Wl,--out-implib,external/cimgui/build/libcimgui.dll.a");
}
if (!nob_cmd_run_sync(cmd)) {
return 1;
}
}
cmd.count = 0;
if (!clear_folder("build", &file_paths)) {
return 1;
}
nob_log(NOB_INFO, "Compiling nes.exe...");
if (build_msvc) {
nob_cmd_append(&cmd, "cl.exe");
if (build_release) {
nob_cmd_append(&cmd, "/O2");
} else {
nob_cmd_append(&cmd, "/Od", "/Zi");
}
nob_cmd_append(&cmd, "/W3");
nob_cmd_append(&cmd, "/Iexternal/SDL2/include");
nob_cmd_append(&cmd, "/Iexternal/SDL2/include/SDL2");
nob_cmd_append(&cmd, "/Iexternal/cimgui/imgui");
nob_cmd_append(&cmd, "/Iexternal/cimgui/imgui/backends");
nob_cmd_append(&cmd, "/Iexternal/cimgui");
nob_cmd_append(&cmd, "/DCIMGUI_USE_SDL2", "/DCIMGUI_USE_OPENGL3");
nob_cmd_append(&cmd, "/DIMGUI_IMPL_API=extern __declspec(dllimport)", "/DCIMGUI_NO_EXPORT");
nob_cmd_append(&cmd, "src/main.c");
nob_cmd_append(&cmd, "/link");
nob_cmd_append(&cmd, "/LIBPATH:external/cimgui/build", "cimgui.lib");
nob_cmd_append(&cmd, "/LIBPATH:external/SDL2/lib/msvc/x64", "SDL2.lib", "SDL2main.lib", "opengl32.lib",
"Shell32.lib");
nob_cmd_append(&cmd, "/OUT:build/nes.exe");
if (build_release) {
nob_cmd_append(&cmd, "/SUBSYSTEM:WINDOWS");
} else {
nob_cmd_append(&cmd, "/SUBSYSTEM:CONSOLE");
}
} else {
nob_cmd_append(&cmd, "gcc");
if (build_release) {
nob_cmd_append(&cmd, "-O2");
} else {
nob_cmd_append(&cmd, "-O0", "-g");
}
nob_cmd_append(&cmd, "-Wall", "-Wno-narrowing", "-Wno-missing-braces", "--pedantic");
nob_cmd_append(&cmd, "-Iexternal/SDL2/include");
nob_cmd_append(&cmd, "-Iexternal/SDL2/include/SDL2");
nob_cmd_append(&cmd, "-Iexternal/cimgui/imgui");
nob_cmd_append(&cmd, "-Iexternal/cimgui/imgui/backends");
nob_cmd_append(&cmd, "-Iexternal/cimgui");
nob_cmd_append(&cmd, "-DCIMGUI_USE_SDL2", "-DCIMGUI_USE_OPENGL3");
nob_cmd_append(&cmd, "-DIMGUI_IMPL_API=extern __declspec(dllimport)", "-DCIMGUI_NO_EXPORT");
nob_cmd_append(&cmd, "src/main.c");
nob_cmd_append(&cmd, "-Lexternal/cimgui/build", "-lcimgui");
nob_cmd_append(&cmd, "-Lexternal/SDL2/lib/mingw32", "-lSDL2");
nob_cmd_append(&cmd, "-lopengl32");
nob_cmd_append(&cmd, "-o", "build/nes.exe");
}
if (!nob_cmd_run_sync(cmd) || !nob_copy_directory_recursively("fonts", "build/fonts") ||
(build_msvc ? !nob_copy_file("external/SDL2/lib/msvc/x64/SDL2.dll", "build/SDL2.dll")
: !nob_copy_file("external/SDL2/lib/mingw32/SDL2.dll", "build/SDL2.dll")) ||
!nob_copy_file("external/cimgui/build/cimgui.dll", "build/cimgui.dll")) {
return 1;
}
nob_log(NOB_INFO, "Built build/nes.exe");
return 0;
}