-
Notifications
You must be signed in to change notification settings - Fork 0
/
nob.c
231 lines (175 loc) · 7.48 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
#include <stdio.h>
#include <string.h>
#define NOB_IMPLEMENTATION
#include "./nob.h/nob.h"
bool strendswith(const char* str, const char* suf) {
size_t str_len = strlen(str), suf_len = strlen(suf);
if (suf_len > str_len) return false;
return strcmp(str + str_len - suf_len, suf) == 0;
}
bool strstartswith(const char *str, const char* pref) {
size_t str_len = strlen(str), pref_len = strlen(pref);
if (str_len < pref_len) return false;
for (size_t i = 0; i < pref_len; i++)
if (str[i] != pref[i]) return false;
return true;
}
#define SOURCE_DIR "src"
#define BUILD_DIR "build"
#define EXPERIMENT_DIR "experiments"
#define CCPP "clang++"
#define COMPILER_FLAGS_STRING "-Wall --std=c++23 -Werror -pedantic -g"
#define COMPILER_FLAGS "-Wall", "--std=c++23", "-Werror", "-pedantic", "-g"
#define INCLUDE_DIRS_STRING "-I" SOURCE_DIR
#define INCLUDE_DIRS "-I", SOURCE_DIR
//#define LD_FLAGS_STRING "-lncurses"
//#define LD_FLAGS "-lncurses"
#define LD_FLAGS_STRING ""
#define LD_FLAGS ""
Nob_File_Paths headers = { 0 };
Nob_File_Paths sources = { 0 };
Nob_File_Paths objects = { 0 };
Nob_File_Paths experiments_srcs = { 0 };
Nob_File_Paths experiments_bins = { 0 };
bool generate_compile_commands(void) {
FILE *compile_commands = fopen("compile_commands.json", "w");
if (!compile_commands) return false;
nob_log(NOB_INFO, "update `compile_commands.json`.");
fprintf(compile_commands, "[\n");
for (size_t i = 0; i < sources.count; i++) {
fprintf(compile_commands, " {\n");
const char *object = objects.items[i];
const char *source = sources.items[i];
fprintf(compile_commands, " \"file\":\"%s\",\n", source);
fprintf(compile_commands, " \"directory\":\"%s\",\n", nob_get_current_dir_temp());
fprintf(compile_commands, " \"command\":\"%s %s %s %s -o %s %s\"\n",
CCPP, COMPILER_FLAGS_STRING, INCLUDE_DIRS_STRING, LD_FLAGS_STRING,
object, source);
fprintf(compile_commands, " },\n");
}
for (size_t i = 0; i < experiments_srcs.count; i++) {
fprintf(compile_commands, " {\n");
const char *source = experiments_srcs.items[i];
const char *object = experiments_bins.items[i];
fprintf(compile_commands, " \"file\":\"%s\",\n", source);
fprintf(compile_commands, " \"directory\":\"%s\",\n", nob_get_current_dir_temp());
fprintf(compile_commands, " \"command\":\"%s %s %s %s -o %s %s\"\n",
CCPP, COMPILER_FLAGS_STRING, INCLUDE_DIRS_STRING, LD_FLAGS_STRING,
object, source);
fprintf(compile_commands, " }%s", i == sources.count-1 ? "\n" : ",\n");
}
fprintf(compile_commands, "]\n");
fclose(compile_commands);
return true;
}
void register_files(void) {
Nob_File_Paths files = { 0 };
nob_read_entire_dir(SOURCE_DIR, &files);
for (size_t i = 0; i < files.count; i++) {
const char *curr = files.items[i];
if (strendswith(curr, ".hpp")) {
char *header = nob_temp_sprintf("%s/%s", SOURCE_DIR, curr);
nob_log(NOB_INFO, "register header: `%s`.", header);
nob_da_append(&headers, header);
}
if (strendswith(curr, ".cpp")) {
char *source = nob_temp_sprintf("%s/%s", SOURCE_DIR, curr);
char *object = nob_temp_sprintf("%s/__obj_%s.o", BUILD_DIR, curr);
nob_log(NOB_INFO, "register source: `%s` => `%s`.", source, object);
nob_da_append(&sources, source);
nob_da_append(&objects, object);
}
}
}
void register_experiments(void) {
Nob_File_Paths files = { 0 };
nob_read_entire_dir(EXPERIMENT_DIR, &files);
for (size_t i = 0; i < files.count; i++) {
const char *curr = files.items[i];
if (strendswith(curr, ".cpp")) {
char *source = nob_temp_sprintf("%s/%s", EXPERIMENT_DIR, curr);
char *binary = nob_temp_sprintf("%s/%s_exp", BUILD_DIR, curr);
nob_log(NOB_INFO, "register experiment: `%s` => `%s`.", source, binary);
nob_da_append(&experiments_srcs, source);
nob_da_append(&experiments_bins, binary);
}
}
}
bool build_experiments(Nob_Cmd *cmd) {
Nob_Procs procs = { 0 };
for (size_t i = 0; i < experiments_srcs.count; i++) {
Nob_File_Paths deps = { 0 };
nob_da_append(&deps, experiments_srcs.items[i]);
nob_da_append_many(&deps, objects.items, objects.count);
nob_da_append_many(&deps, headers.items, objects.count);
int ret = nob_needs_rebuild(experiments_bins.items[i], deps.items, deps.count);
if (ret < 0) return false;
if (!ret) continue;
nob_cmd_append(cmd, CCPP, COMPILER_FLAGS, INCLUDE_DIRS, LD_FLAGS, "-o", experiments_bins.items[i], experiments_srcs.items[i]);
nob_da_append_many(cmd, objects.items, objects.count);
Nob_Proc proc = nob_cmd_run_async_and_reset(cmd);
nob_da_append(&procs, proc);
}
if (!nob_procs_wait(procs)) return false;
return true;
}
int main(int argc, char **argv) {
int result = 0;
NOB_GO_REBUILD_URSELF(argc, argv);
if (!nob_mkdir_if_not_exists(BUILD_DIR)) return 1;
register_files();
register_experiments();
generate_compile_commands();
Nob_Cmd cmd = { 0 };
Nob_Procs procs = { 0 };
for (size_t i = 0; i < objects.count; i++) {
const char *object = objects.items[i];
const char *source = sources.items[i];
Nob_File_Paths deps = { 0 };
nob_da_append(&deps, nob_temp_strdup(source));
nob_da_append_many(&deps, headers.items, headers.count);
int ret = nob_needs_rebuild(object, deps.items, deps.count);
if (ret < 0) return 1;
if (!ret) continue;
nob_cmd_append(&cmd, CCPP, COMPILER_FLAGS, INCLUDE_DIRS, LD_FLAGS,
"-o", object, "-c", source);
Nob_Proc proc = nob_cmd_run_async_and_reset(&cmd);
nob_da_append(&procs, proc);
nob_da_free(deps);
}
if (!nob_procs_wait(procs)) nob_return_defer(1);
if (!build_experiments(&cmd)) nob_return_defer(1);
if (argc > 1) {
if (strcmp(argv[1], "run") == 0) {
if (argc < 3) {
nob_log(NOB_ERROR, "Expected target name to run!");
nob_return_defer(1);
}
for (size_t i = 0; i < experiments_bins.count; i++) {
if (strstartswith(experiments_bins.items[i],
nob_temp_sprintf("%s/%s", BUILD_DIR, argv[2]))) {
nob_log(NOB_INFO, "Run: `%s`.", experiments_bins.items[i]);
nob_cmd_append(&cmd, experiments_bins.items[i]);
if (!nob_cmd_run_sync_and_reset(&cmd)) nob_return_defer(1);
}
}
}
if (strcmp(argv[1], "list") == 0) {
nob_log(NOB_INFO, "");
nob_log(NOB_INFO, "");
nob_log(NOB_INFO, "");
nob_log(NOB_INFO, " ---===< LIST TARGETS >===---");
nob_log(NOB_INFO, "");
for (size_t i = 0; i < experiments_bins.count; i++) {
nob_log(NOB_INFO, " < target > `%s`", experiments_bins.items[i] + strlen(BUILD_DIR) + 1);
}
}
}
defer:
nob_da_free(sources);
nob_da_free(objects);
nob_da_free(headers);
nob_da_free(experiments_bins);
nob_da_free(experiments_srcs);
return result;
}