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
|
#define B_IMPLEMENTATION
#include "src/b.h"
int debug_level = 1;
void
debug_or_release(B_Cmd* cmd)
{
if (debug_level == 0)
b_cmd_append(cmd, "-O3", "-s");
else if (debug_level == 1)
b_cmd_append(cmd, "-O2", "-ggdb", "-DVKDEBUG");
else if (debug_level == 2)
b_cmd_append(cmd, "-O1", "-ggdb", "-DVKDEBUG");
else
b_cmd_append(cmd, "-O0", "-ggdb", "-DVKDEBUG");
}
void
inlcude_dirs(B_Cmd* cmd)
{
b_cmd_append(cmd, "-I./src/");
b_cmd_append(cmd, "-I./lib");
b_cmd_append(cmd, "-I./lib/imgui-1.90.7");
}
void
cflags(B_Cmd* cmd)
{
b_cmd_append(cmd, "-Wall", "-Wextra");
debug_or_release(cmd);
b_cmd_append(cmd, "-march=native");
b_cmd_append(cmd, "-fno-math-errno", "-funroll-loops");
b_cmd_append(cmd, "-flto", "-pthread");
inlcude_dirs(cmd);
}
void cxxflags(B_Cmd *cmd)
{
b_cmd_append(cmd, "-Wall", "-Wextra");
b_cmd_append(cmd, "-Wno-string-plus-int", "-Wno-nullability-completeness", "-Wno-unused-function", "-Wno-missing-field-initializers", "-Wno-unused-parameter", "-Wno-unused-variable");
debug_or_release(cmd);
b_cmd_append(cmd, "-march=native");
b_cmd_append(cmd, "-fno-math-errno", "-funroll-loops");
b_cmd_append(cmd, "-flto", "-pthread");
inlcude_dirs(cmd);
//b_cmd_append(cmd, "-O3");
}
void cxx(B_Cmd *cmd)
{
b_cmd_append(cmd, "clang");
cxxflags(cmd);
}
void cc(B_Cmd *cmd)
{
b_cmd_append(cmd, "clang");
cflags(cmd);
}
void libs(B_Cmd *cmd)
{
b_cmd_append(cmd, "-lSDL2", "-lm", "-lvulkan", "-lshaderc_shared", "-lstdc++");
}
bool
build_c(bool force,
B_Cmd* cmd,
const char** input_paths,
size_t input_paths_len,
const char** dep_paths,
size_t dep_paths_len,
const char* output_path)
{
int rebuild_is_needed =
b_needs_rebuild(output_path, input_paths, input_paths_len);
int dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
if (rebuild_is_needed < 0 || dep_rebuild < 0) return false;
if (force || rebuild_is_needed || dep_rebuild) {
cmd->count = 0;
cc(cmd);
b_cmd_append(cmd, "-o", output_path);
b_da_append_many(cmd, input_paths, input_paths_len);
libs(cmd);
return b_cmd_run_sync(*cmd);
}
b_log(B_INFO, "%s is up-to-date", output_path);
return true;
}
bool
compile_cxx(bool force,
B_Cmd* cmd,
const char** input_paths,
size_t input_paths_len,
const char** dep_paths,
size_t dep_paths_len,
const char* output_path)
{
int rebuild_is_needed = b_needs_rebuild(output_path, input_paths, input_paths_len);
int dep_rebuild = b_needs_rebuild(output_path, dep_paths, dep_paths_len);
if (rebuild_is_needed < 0 || dep_rebuild < 0) return false;
if (force || rebuild_is_needed || dep_rebuild) {
cmd->count = 0;
cxx(cmd);
b_cmd_append(cmd, "-c");
//b_cmd_append(cmd, "-o", output_path);
b_da_append_many(cmd, input_paths, input_paths_len);
//libs(cmd);
return b_cmd_run_sync(*cmd);
}
b_log(B_INFO, "%s is up-to-date", output_path);
return true;
}
int
main(int argc, char *argv[])
{
B_GO_REBUILD_URSELF(argc, argv);
const char *program_name = b_shift_args(&argc, &argv);
bool force = false;
while (argc > 0) {
const char *flag = b_shift_args(&argc, &argv);
if (strcmp(flag, "-f") == 0) {
force = true;
} else {
b_log(B_ERROR, "Unknown flag `%s`", flag);
return 1;
}
}
const char *cxx_dep_paths[] = {
"src/vksetup.h",
};
const char* cxx_input_paths[] = {
"src/cplusplus.cpp",
"lib/imgui-1.90.7/imgui.cpp",
"lib/imgui-1.90.7/imgui_demo.cpp",
"lib/imgui-1.90.7/imgui_draw.cpp",
"lib/imgui-1.90.7/imgui_tables.cpp",
"lib/imgui-1.90.7/imgui_widgets.cpp",
"lib/imgui-1.90.7/backends/imgui_impl_sdl2.cpp",
"lib/imgui-1.90.7/backends/imgui_impl_vulkan.cpp",
};
const char *c_dep_paths[] = {
"src/state.h",
"src/vksetup.h",
};
const char* c_input_paths[] = {
"src/render.c",
"cplusplus.o",
"imgui.o",
"imgui_demo.o",
"imgui_draw.o",
"imgui_tables.o",
"imgui_widgets.o",
"imgui_impl_sdl2.o",
"imgui_impl_vulkan.o",
};
B_Cmd cmd = {0};
if (!compile_cxx(force, &cmd, cxx_input_paths, B_ARRAY_LEN(cxx_input_paths), cxx_dep_paths, B_ARRAY_LEN(c_dep_paths),"cplusplus.o")) return 1;
if (!build_c(force, &cmd, c_input_paths, B_ARRAY_LEN(c_input_paths), c_dep_paths, B_ARRAY_LEN(c_dep_paths),"render")) return 1;
return 0;
}
/*
1. Build list of <object>.cpp (with <dep>s) -> into build/cxx/<object>/<dep>.o
2. Build list of <object>.c (with <dep>s) -> into build/c/<object>/<dep>.o
3. Build <target>.c linking with the objects from 1 & 2 -> into <target>
Check for rebuilds:
1. <object.cpp> and <dep>s
2. <object.c> and <dep>s
3. <target.c> and all links {cxx,c}/ * / *.o
*/
|