-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnob.c
More file actions
269 lines (243 loc) · 9.18 KB
/
nob.c
File metadata and controls
269 lines (243 loc) · 9.18 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
257
258
259
260
261
262
263
264
265
266
267
268
269
#define NOBDEF static inline
#define NOB_IMPLEMENTATION
#define NOB_WARN_DEPRECATED
#define NOB_STRIP_PREFIX
#include "nob.h"
#define BUILD_FOLDER "build/"
#define SRC_FOLDER ""
#define INCLUDE_FOLDER "include/"
typedef enum {
COMPILER_GCC,
COMPILER_CLANG,
COMPILER_MSVC,
COMPILER_CLANG_CL,
} Compiler;
typedef struct {
Compiler compiler;
bool debug;
bool profiling;
bool native;
bool run;
} BuildConfig;
static void print_usage(const char *program) {
fprintf(stderr, "Usage: %s [options]\n", program);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -gcc Use gcc / MinGW (default)\n");
fprintf(stderr, " -clang Use clang\n");
fprintf(stderr, " -msvc Use cl (MSVC, requires Developer PowerShell)\n");
fprintf(stderr, " -clang-cl Use clang-cl (for profiling with samply)\n");
fprintf(stderr,
" -debug Enable debug mode (defines DEBUG, disables "
"optimizations)\n");
fprintf(stderr, " -profiling Enable profiling flags (implies -clang-cl)\n");
fprintf(stderr, " -native Enable -march=native (clang/gcc only)\n");
fprintf(stderr, " -run Run the executable after building\n");
fprintf(stderr, " -help Show this help message\n");
}
// clang (and clang-cl) on Windows needs _USE_MATH_DEFINES to expose M_PI etc.
// gcc exposes them by default via its own extensions — do not add the define.
// cl also gates M_PI behind _USE_MATH_DEFINES in its CRT headers.
static void apply_gnu_style_flags(Cmd *cmd, Compiler compiler, bool debug,
bool native) {
cc_flags(cmd);
if (debug) {
cmd_append(cmd, "-O0", "-g", "-DDEBUG");
} else {
cmd_append(cmd, "-O3");
if (native) cmd_append(cmd, "-march=native");
}
if (compiler == COMPILER_CLANG) cmd_append(cmd, "-D_USE_MATH_DEFINES");
cmd_append(cmd, "-fopenmp");
cmd_append(cmd, "-mavx2", "-mfma");
cmd_append(cmd, "-I" INCLUDE_FOLDER);
}
static void apply_msvc_style_flags(Cmd *cmd, Compiler compiler, bool debug,
bool profiling) {
nob_cmd_append(cmd, "/nologo");
// nob_cmd_append(cmd, "/W4", "/D_CRT_SECURE_NO_WARNINGS");
if (profiling) {
cmd_append(cmd, "/Zi", "/Oy-");
} else if (debug) {
cmd_append(cmd, "/Zi", "/Od", "/DDEBUG");
} else {
cmd_append(cmd, "/O2");
}
cmd_append(cmd, "/D_USE_MATH_DEFINES");
cmd_append(cmd, "/openmp");
cmd_append(cmd, "/arch:AVX2");
cmd_append(cmd, "/I" INCLUDE_FOLDER);
(void)compiler; // suppress unused var, may use in the future
}
// /Fe:<path> is the MSVC way to name the output executable.
// With multiple input files, cl.exe otherwise names the exe after the first .c file,
// which is fragile. /Fe: makes it explicit. Note: no space between /Fe: and the
// path.
static void cc_output_msvc(Cmd *cmd, const char *path) {
String_Builder sb = {0};
sb_append_cstr(&sb, "/Fe:");
sb_append_cstr(&sb, path);
sb_append_null(&sb);
cmd_append(cmd, sb.items);
// nob copies the string into its own storage, so sb can go out of scope safely.
}
// /Fo:<dir>\ tells cl where to put .obj files.
// The trailing backslash is required — without it cl treats it as a filename prefix.
static void cc_objdir_msvc(Cmd *cmd, const char *dir) {
String_Builder sb = {0};
sb_append_cstr(&sb, "/Fo:");
sb_append_cstr(&sb, dir);
// Ensure trailing backslash — MSVC requires it to treat this as a directory.
if (dir[strlen(dir) - 1] != '\\' && dir[strlen(dir) - 1] != '/') {
sb_append_cstr(&sb, "\\");
}
sb_append_null(&sb);
cmd_append(cmd, sb.items);
}
static void cc_extra_outputs_msvc(Cmd *cmd, const char *dir) {
String_Builder pdb = {0};
sb_append_cstr(&pdb, "/Fd:");
sb_append_cstr(&pdb, dir);
sb_append_cstr(&pdb, "main.pdb");
sb_append_null(&pdb);
cmd_append(cmd, pdb.items);
// cmd_append(cmd, "/link");
// String_Builder ilk = {0};
// sb_append_cstr(&ilk, "/ILK:");
// sb_append_cstr(&ilk, dir);
// sb_append_cstr(&ilk, "main.ilk");
// sb_append_null(&ilk);
// cmd_append(cmd, ilk.items);
}
// Check that a given executable is findable on PATH before we try to invoke it.
#ifdef _WIN32
static bool check_on_path(const char *exe) {
char buf[MAX_PATH];
return SearchPathA(NULL, exe, NULL, MAX_PATH, buf, NULL) != 0;
}
#else
static bool check_on_path(const char *exe) {
(void)exe;
return true; // not needed outside Windows for these compilers
}
#endif
int main(int argc, char **argv) {
GO_REBUILD_URSELF(argc, argv);
const char *program = nob_shift(argv, argc);
BuildConfig cfg = {
.compiler = COMPILER_GCC, // default
.debug = false,
.profiling = false,
.native = false,
.run = false,
};
while (argc > 0) {
const char *flag = nob_shift(argv, argc);
if (strcmp(flag, "-gcc") == 0) {
cfg.compiler = COMPILER_GCC;
} else if (strcmp(flag, "-clang") == 0) {
cfg.compiler = COMPILER_CLANG;
} else if (strcmp(flag, "-msvc") == 0) {
cfg.compiler = COMPILER_MSVC;
} else if (strcmp(flag, "-clang-cl") == 0) {
cfg.compiler = COMPILER_CLANG_CL;
} else if (strcmp(flag, "-debug") == 0) {
cfg.debug = true;
} else if (strcmp(flag, "-profiling") == 0) {
cfg.profiling = true;
cfg.compiler = COMPILER_CLANG_CL;
nob_log(NOB_INFO, "-profiling flag forces clang-cl compiler");
} else if (strcmp(flag, "-native") == 0) {
cfg.native = true;
if (cfg.compiler == COMPILER_CLANG_CL || cfg.compiler == COMPILER_MSVC)
nob_log(NOB_WARNING,
"-march=native flag not available on msvc style compilers");
} else if (strcmp(flag, "-run") == 0) {
cfg.run = true;
} else if (strcmp(flag, "-help") == 0 || strcmp(flag, "--help") == 0 ||
strcmp(flag, "-h") == 0) {
print_usage(program);
return 0;
} else {
nob_log(NOB_ERROR, "Unknown flag: %s", flag);
print_usage(program);
return 1;
}
}
const char *compiler_names[] = {
[COMPILER_GCC] = "gcc (MinGW)",
[COMPILER_CLANG] = "clang",
[COMPILER_MSVC] = "cl (MSVC)",
[COMPILER_CLANG_CL] = "clang-cl",
};
nob_log(NOB_INFO, "Compiler : %s", compiler_names[cfg.compiler]);
nob_log(NOB_INFO, "Debug : %s", cfg.debug ? "yes" : "no");
nob_log(NOB_INFO, "Profiling: %s", cfg.profiling ? "yes" : "no");
// nob_log(NOB_INFO, "Native : %s", cfg.native ? "yes" : "no");
// Verify MSVC-style compilers are actually on PATH and give a helpful message if
// not.
if (cfg.compiler == COMPILER_MSVC) {
if (!check_on_path("cl.exe")) {
nob_log(NOB_ERROR, "'cl.exe' not found on PATH.");
nob_log(NOB_ERROR,
"Open a Visual Studio Developer PowerShell or Developer Command "
"Prompt and try again.");
return 1;
}
} else if (cfg.compiler == COMPILER_CLANG_CL) {
if (!check_on_path("clang-cl.exe")) {
nob_log(NOB_ERROR, "'clang-cl.exe' not found on PATH.");
nob_log(
NOB_ERROR,
"Make sure LLVM is installed and its bin/ directory is on PATH.");
return 1;
}
}
if (!mkdir_if_not_exists(BUILD_FOLDER)) return 1;
Cmd cmd = {0};
bool msvc_style = false;
switch (cfg.compiler) {
case COMPILER_GCC:
cmd_append(&cmd, "gcc");
apply_gnu_style_flags(&cmd, COMPILER_GCC, cfg.debug, cfg.native);
break;
case COMPILER_CLANG:
cmd_append(&cmd, "clang");
apply_gnu_style_flags(&cmd, COMPILER_CLANG, cfg.debug, cfg.native);
break;
case COMPILER_MSVC:
cmd_append(&cmd, "cl");
apply_msvc_style_flags(&cmd, COMPILER_MSVC, cfg.debug, false);
msvc_style = true;
break;
case COMPILER_CLANG_CL:
cmd_append(&cmd, "clang-cl");
apply_msvc_style_flags(&cmd, COMPILER_CLANG_CL, cfg.debug, cfg.profiling);
msvc_style = true;
break;
}
// NOTE: targets
Cmd targets = {0};
cc_inputs(&targets, SRC_FOLDER "main.c", INCLUDE_FOLDER "pffft.c");
if (!compile_commands(&cmd, &targets, BUILD_FOLDER "compile_commands.json"))
return 1;
// NOTE: output
if (msvc_style) {
cc_output_msvc(&cmd, BUILD_FOLDER "main.exe");
cc_objdir_msvc(&cmd, BUILD_FOLDER);
cc_extra_outputs_msvc(&cmd, BUILD_FOLDER);
} else {
// cc_output is already smart enough to use the right flag based on the
// available compilers in the current env (for example -o vs /Fe),
// but given an env it will always pick the same flag.
// By defining a separate cc_output_msvc func we can be more intentional
cc_output(&cmd, BUILD_FOLDER "main.exe");
}
cmd_extend(&cmd, &targets);
if (!cmd_run(&cmd)) return 1;
if (cfg.run) {
Cmd run_cmd = {0};
cmd_append(&run_cmd, BUILD_FOLDER "main.exe");
if (!cmd_run(&run_cmd)) return 1;
}
return 0;
}