-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnob.c
More file actions
248 lines (193 loc) · 7.32 KB
/
nob.c
File metadata and controls
248 lines (193 loc) · 7.32 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
#include <linux/limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NOB_IMPLEMENTATION
#include "external/nob.h"
#define CHECK(call) do { int ret = call; if (ret != 0) return ret; } while (0)
#define BUILD_DIR "build/"
#define OUT_DIR "out/"
#define OUT OUT_DIR"eclairbot.elf"
// helper
bool read_entire_dir_recursive(const char *parent, Nob_File_Paths *children, size_t base_len) {
Nob_File_Paths entries = {0};
if (!nob_read_entire_dir(parent, &entries)) return false;
for (size_t i = 0; i < entries.count; i++) {
const char* name = entries.items[i];
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", parent, name);
Nob_File_Type type = nob_get_file_type(path);
if (type == NOB_FILE_DIRECTORY) {
read_entire_dir_recursive(path, children, base_len);
} else {
size_t len = strlen(path);
if (len > 2 && strcmp(path + len - 2, ".c") == 0) {
const char *rel_path = path + base_len; // skip "src/"
if (*rel_path == '/') rel_path++; // skip leading '/'
nob_da_append(children, nob_temp_strdup(rel_path));
}
}
}
nob_da_free(entries);
return true;
}
typedef struct Flags {
const char** items;
size_t count;
size_t capacity;
} Flags;
int chdir_to_project_root() {
return chdir(getenv("__ECLAIRBOTC_PROJECT_ROOT"));
}
int build_sqlite3(Flags* additional_compile_flags, Flags* additional_link_flags) {
chdir("external/sqlite");
if (nob_get_file_type("./build_output") == NOB_FILE_DIRECTORY) {
goto add_flags;
}
char configure_script[PATH_MAX];
getcwd(configure_script, sizeof configure_script);
size_t cwd_len = strlen(configure_script);
memcpy(configure_script + cwd_len, "/configure", sizeof(configure_script) - cwd_len);
Nob_Cmd configure = {0};
nob_cmd_append(&configure, configure_script);
nob_cmd_append(&configure, "--enable-static", "--disable-shared");
nob_cmd_append(&configure, "--prefix=./build_output");
if (!nob_cmd_run(&configure, .async = false)) {
nob_log(NOB_ERROR, "Failed to build sqlite3: ./configure failed");
nob_cmd_free(configure);
goto error;
}
nob_cmd_free(configure);
Nob_Cmd make = {0};
nob_cmd_append(&make, "make");
if (!nob_cmd_run(&make, .async = false)) {
nob_log(NOB_ERROR, "Failed to build sqlite3: Makefile failed");
nob_cmd_free(make);
goto error;
}
nob_cmd_free(make);
Nob_Cmd make_install = {0};
nob_cmd_append(&make_install, "make", "install");
if (!nob_cmd_run(&make_install, .async = false)) {
nob_log(NOB_ERROR, "Failed to build sqlite3: Makefile failed");
nob_cmd_free(make_install);
goto error;
}
nob_cmd_free(make_install);
add_flags:
nob_da_append(additional_compile_flags, "-Iexternal/sqlite/build_output/include");
nob_da_append(additional_link_flags, "-Lexternal/sqlite/build_output/lib");
success:
chdir_to_project_root();
nob_log(NOB_INFO, "SQLite3 compiled successfully");
return 0;
error:
chdir_to_project_root();
return 1;
}
int build_concord(Flags* additional_compile_flags, Flags* additional_link_flags) {
chdir("external/concord");
if (nob_get_file_type("./include/") == NOB_FILE_DIRECTORY && nob_get_file_type("./lib/") == NOB_FILE_DIRECTORY) {
goto add_flags;
}
Nob_Cmd make = {0};
nob_cmd_append(&make, "make");
if (!nob_cmd_run(&make, .async = false)) {
nob_log(NOB_ERROR, "Failed to build concord: Makefile failed");
goto error;
}
nob_cmd_free(make);
add_flags:
nob_da_append(additional_compile_flags, "-Iexternal/concord/include");
nob_da_append(additional_compile_flags, "-Iexternal/concord/core");
nob_da_append(additional_compile_flags, "-Iexternal/concord/gencodecs");
nob_da_append(additional_link_flags, "-Lexternal/concord/lib");
nob_da_append(additional_link_flags, "-ldiscord");
nob_da_append(additional_link_flags, "-lcurl");
success:
chdir_to_project_root();
nob_log(NOB_INFO, "Concord compiled successfully");
return 0;
error:
chdir_to_project_root();
return 1;
}
int build_external_libs(Flags* additional_compile_flags, Flags* additional_link_flags) {
CHECK(build_concord(additional_compile_flags, additional_link_flags));
CHECK(build_sqlite3(additional_compile_flags, additional_link_flags));
return 0;
}
bool needs_rebuild(const char *src, const char *obj) {
struct stat src_stat, obj_stat;
if (stat(src, &src_stat) != 0) return true; // just in case
if (stat(obj, &obj_stat) != 0) return true; // rebuild if object file does not exists
return src_stat.st_mtime > obj_stat.st_mtime;
}
int build(int argc, char** argv) {
chdir_to_project_root();
nob_mkdir_if_not_exists(BUILD_DIR);
nob_mkdir_if_not_exists(OUT_DIR);
Flags compile_flags = {0}, link_flags = {0};
build_external_libs(&compile_flags, &link_flags);
nob_da_append(&compile_flags, "-Iinclude");
chdir_to_project_root();
Nob_File_Paths sources = {0};
read_entire_dir_recursive("src", &sources, strlen("src"));
Nob_File_Paths objects = {0};
for (size_t i = 0; i < sources.count; ++i) {
char source_path[PATH_MAX];
snprintf(source_path, sizeof source_path, "./src/%s", sources.items[i]);
char path_normalized[PATH_MAX];
strncpy(path_normalized, sources.items[i], sizeof path_normalized);
path_normalized[sizeof(path_normalized) - 1] = '\0';
int len = strlen(path_normalized);
if (len > 2 && strcmp(path_normalized + len - 2, ".c") == 0) {
path_normalized[len - 2] = '\0'; // remove .c
len -= 2;
}
for (int i = 0; i < len; ++i) {
if (path_normalized[i] == '/') path_normalized[i] = '_';
}
char* out_obj_path = nob_temp_sprintf("./build/%s.o", path_normalized);
if (!needs_rebuild(source_path, out_obj_path)) {
nob_log(NOB_INFO, "Skipping %s.c (up to date)", path_normalized);
nob_da_append(&objects, out_obj_path);
continue;
}
Nob_Cmd cc = {0};
nob_cc(&cc);
nob_cmd_append(&cc, source_path, "-o", out_obj_path, "-c");
nob_cc_flags(&cc);
nob_cmd_extend(&cc, &compile_flags);
if (!nob_cmd_run(&cc, .async = false)) {
nob_log(NOB_ERROR, "Failed to compile %s.c", path_normalized);
nob_cmd_free(cc);
return 1;
}
nob_cmd_free(cc);
nob_da_append(&objects, out_obj_path);
}
nob_da_free(compile_flags);
Nob_Cmd link = {0};
nob_cc(&link);
nob_cmd_extend(&link, &objects);
nob_cmd_extend(&link, &link_flags);
nob_cmd_append(&link, "-o", OUT);
nob_da_free(link_flags);
if (!nob_cmd_run_sync_and_reset(&link)) {
nob_log(NOB_ERROR, "Failed to link executable %s.", OUT);
nob_cmd_free(link);
return 1;
}
nob_cmd_free(link);
nob_log(NOB_INFO, "EclairBot-C compiled and linked successfully");
return 0;
}
int main(int argc, char** argv) {
// NOB_GO_REBUILD_URSELF(argc, argv);
if (argc == 1) {
CHECK(build(argc, argv));
}
}