-
Notifications
You must be signed in to change notification settings - Fork 2
/
nob.c
128 lines (102 loc) · 3.72 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
#define NOB_IMPLEMENTATION
#include "./nob.h"
static const char *raylibModules[] = {
"raudio",
"rcore",
"rglfw",
"rmodels",
"rshapes",
"rtext",
"rtextures",
"utils",
};
bool buildRaylib() {
bool result = true;
Nob_Cmd cmd = {0};
Nob_File_Paths objectFiles = {0};
Nob_Procs procs = {0};
if (!nob_mkdir_if_not_exists("./build/raylib")) nob_return_defer(false);
const char *buildPath = "./build/raylib/gcc";
if (!nob_mkdir_if_not_exists(buildPath)) nob_return_defer(false);
for (size_t i = 0; i < NOB_ARRAY_LEN(raylibModules); i++) {
const char *inputPath = nob_temp_sprintf("./raylib/raylib-5.0/src/%s.c", raylibModules[i]);
const char *outputPath = nob_temp_sprintf("%s/%s.o", buildPath, raylibModules[i]);
nob_da_append(&objectFiles, outputPath);
if (nob_needs_rebuild(outputPath, &inputPath, 1)) {
cmd.count = 0;
nob_cmd_append(&cmd, "gcc");
nob_cmd_append(&cmd, "-DPLATFORM_DESKTOP", "-fPIC");
nob_cmd_append(&cmd, "-I./raylib/raylib-5.0/src/external/glfw/include");
nob_cmd_append(&cmd, "-c", inputPath);
nob_cmd_append(&cmd, "-o", outputPath);
Nob_Proc proc = nob_cmd_run_async(cmd);
nob_da_append(&procs, proc);
}
}
if (!nob_procs_wait(procs)) nob_return_defer(false);
cmd.count = 0;
const char *libraylibPath = nob_temp_sprintf("%s/libraylib.a", buildPath);
if (nob_needs_rebuild(libraylibPath, objectFiles.items, objectFiles.count)) {
nob_cmd_append(&cmd, "ar", "-crs", libraylibPath);
for (size_t i = 0; i < NOB_ARRAY_LEN(raylibModules); ++i) {
const char *inputPath = nob_temp_sprintf("%s/%s.o", buildPath, raylibModules[i]);
nob_cmd_append(&cmd, inputPath);
}
if (!nob_cmd_run_sync(cmd)) nob_return_defer(false);
}
defer:
nob_cmd_free(cmd);
nob_da_free(objectFiles);
nob_da_free(procs);
return result;
}
bool buildPovBrainIsWeird(bool platformWindows) {
bool result = true;
Nob_Cmd cmd = {0};
nob_cmd_append(&cmd, "gcc");
if (platformWindows) nob_cmd_append(&cmd, "-mwindows");
nob_cmd_append(&cmd, "-Wall", "-Wextra");
nob_cmd_append(&cmd, "-I./build/");
nob_cmd_append(&cmd, "-I./raylib/raylib-5.0/src/");
nob_cmd_append(&cmd, "-o", "./build/pov-brain-is-weird");
nob_cmd_append(&cmd, "./pov-brain-is-weird.c");
nob_cmd_append(&cmd, "-L./build/raylib/gcc");
nob_cmd_append(&cmd, "-l:libraylib.a");
nob_cmd_append(&cmd, "-lm"); // needed on Linux, doesn't cause issues on Windows
if (platformWindows) {
nob_cmd_append(&cmd, "-lwinmm", "-lgdi32");
nob_cmd_append(&cmd, "-static");
}
if (!nob_cmd_run_sync(cmd)) nob_return_defer(false);
defer:
nob_cmd_free(cmd);
return result;
}
void print_usage(void) {
nob_log(NOB_INFO, "usage: [./]nob [-platform] [platform]");
nob_log(NOB_INFO, "platforms supported: windows, linux");
}
int main(int argc, char **argv) {
NOB_GO_REBUILD_URSELF(argc, argv);
bool platformWindows = true;
if (argc != 1) {
if (strcmp(argv[1], "-platform") == 0) {
if (strcmp(argv[2], "linux") == 0) {
platformWindows = false;
} else if (strcmp(argv[2], "windows") == 0) {
platformWindows = true;
} else {
nob_log(NOB_ERROR, "unsupported platform\n");
print_usage();
return 1;
}
} else {
print_usage();
return 1;
}
}
if (!nob_mkdir_if_not_exists("build")) return 1;
if (!buildRaylib()) return 1;
if (!buildPovBrainIsWeird(platformWindows)) return 1;
return 0;
}