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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
|
config = {
c2 = {
"app",
"c2",
"video",
"pipeline",
"asset",
"ui",
"maths",
"model"
},
qstd = {
"memory",
"plat",
"str",
"pack"
},
cfg = {
"cfgparse"
},
sc = {
"sc",
"includer"
},
packer = {
"packer"
},
convtexture = {
"convtexture"
},
convmodel = {
"convmodel"
},
convmaterial = {
"convmaterial"
},
shaders = {
"triangle",
"ui",
"surface",
"surface_depthonly",
"sky",
"mip_spec"
},
materials = {
"bricks",
"plastic",
},
models = {
"monkey",
},
textures = {
{ "22", "bmp", "bc1" },
{ "kita", "bmp", "bc1" },
{ "brick_albedo", "bmp", "bc1" },
{ "brick_ao", "bmp", "bc4" },
{ "brick_normal", "bmp", "bc5" },
{ "sky", "hdr", "rgba16f" }
}
}
local system = arg[1]
if system ~= "windows" and system ~= "unix" then
print("Invalid system, needs to be 'windows' or 'unix'.")
os.exit(1)
end
function build_unix()
local compiler_c = nil
local compiler_cpp = nil
local ar = nil
if os.execute("gcc --version") then
compiler_c = "gcc"
compiler_cpp = "g++"
elseif os.execute("clang --version") then
compiler_c = "clang"
compiler_cpp = "clang++"
end
if os.execute("ar --version") then
ar = "ar"
end
if not compiler_c or not compiler_cpp then
print("didn't find a valid compiler :(")
os.exit(2)
end
print("c compiler: " .. compiler_c)
print("c++ compiler: " .. compiler_cpp)
print("ar: " .. ar)
local outfile = io.open("Makefile", "w")
if not outfile then
print("Failed to open Makefile.")
exit(3)
end
local std_flag = {
c = "-std=gnu99",
cpp = "-std=c++20"
}
outfile:write("# generated by configure.lua\n\n")
outfile:write(
"cflags = -pedantic -Wall -Wextra " ..
"-Dplat_x11 -Dplat_x86 -Dplat_posix -Dallocation_default_alignment=8 " ..
"-Icfg -Iqstd -Isc/glslang " ..
"-MMD -MF $(basename $@).d " ..
"\n\n"
)
outfile:write(".PHONY: all clean\n")
outfile:write("all: c2\n\n")
outfile:write("ifndef config\n")
outfile:write(" config=debug\n")
outfile:write("endif\n")
outfile:write("ifeq ($(config),debug)\n")
outfile:write(" opt_com = -DDEBUG -g -O0\n")
outfile:write(" opt_lnk =\n")
outfile:write("endif\n")
outfile:write("ifeq ($(config),release)\n")
outfile:write(" opt_com = -DNDEBUG -O3\n")
outfile:write(" opt_lnk = -s\n")
outfile:write("endif\n\n")
local all_objs = {}
local all_deps = {}
local all_assets = {}
local all_shaders = {}
local function objs(com, ext, tab, dir)
local std = std_flag[ext]
for _, fname in ipairs(tab) do
outfile:write(string.format(
"%s%s.o: %s%s.%s\n\t%s %s $(opt_com) $(cflags) -c %s%s.%s -o %s%s.o\n",
dir,
fname,
dir,
fname,
ext,
com,
std,
dir,
fname,
ext,
dir,
fname
))
all_objs[#all_objs + 1] = dir .. fname .. '.o'
all_deps[#all_deps + 1] = dir .. fname .. '.d'
end
end
local function lib(com, tab, name, dir)
objs(com, 'c', tab, dir)
outfile:write(name .. ": ")
for _, fname in ipairs(tab) do
outfile:write(dir .. fname .. ".o ")
end
outfile:write("\n\t")
outfile:write(ar .. " -rcs " .. name .. " ")
for _, fname in ipairs(tab) do
outfile:write(dir .. fname .. ".o ")
end
outfile:write("\n\n")
end
local function prog(type, com, tab, name, dir, dep, lib)
objs(com, type, tab, dir)
outfile:write(name .. ": ")
for _, fname in ipairs(tab) do
outfile:write(dir .. fname .. ".o ")
end
for _, fname in ipairs(dep) do
outfile:write(fname .. " ")
end
outfile:write("\n\t")
outfile:write(com .. " $(opt_lnk) $(lflags) -o " .. name .. " ")
for _, fname in ipairs(tab) do
outfile:write(dir .. fname .. ".o ")
end
for _, fname in ipairs(dep) do
outfile:write(fname .. " ")
end
for _, fname in ipairs(lib) do
outfile:write(fname .. " ")
end
outfile:write("\n\n")
end
local function shaders(stype, dtype, tool, tab)
for _, fname in ipairs(tab) do
outfile:write(string.format(
"data/%s.%s: intermediate/%s.%s %s | data\n\t./%s intermediate/%s.%s data/%s.%s\n",
fname,
dtype,
fname,
stype,
tool,
tool,
fname,
stype,
fname,
dtype
))
all_shaders[#all_shaders + 1] = "data/" .. fname .. "." .. dtype
all_assets[#all_assets + 1] = fname .. "." .. dtype
end
outfile:write("\n")
end
local function models(tab)
for _, fname in ipairs(tab) do
outfile:write(string.format(
"data/%s.mdl: convmodel intermediate/%s.glb ",
fname, fname
))
for _, fname in ipairs(all_shaders) do
outfile:write(fname .. " ")
end
outfile:write("| data\n\t")
outfile:write(string.format(
"./convmodel data intermediate/%s.glb data/%s.mdl\n",
fname, fname
))
all_assets[#all_assets + 1] = fname .. ".mdl"
end
outfile:write("\n")
end
local function textures(tab)
for _, c in ipairs(tab) do
local fname = c[1]
local ext = c[2]
local fmt = c[3]
outfile:write(string.format(
"data/%s.tex: convtexture intermediate/%s.%s ",
fname, fname, ext
))
outfile:write("| data\n\t")
outfile:write(string.format(
"./convtexture intermediate/%s.%s data/%s.tex %s\n",
fname, ext, fname, fmt
))
all_assets[#all_assets + 1] = fname .. ".tex"
end
outfile:write("\n")
end
local function materials(tab)
for _, fname in ipairs(tab) do
outfile:write(string.format(
"data/%s.mat: convmaterial intermediate/%s.mat ",
fname, fname
))
outfile:write("| data\n\t")
outfile:write(string.format(
"./convmaterial intermediate/%s.mat data/%s.mat\n",
fname, fname
))
all_assets[#all_assets + 1] = fname .. ".mat"
end
outfile:write("\n")
end
local function pack()
outfile:write("pack: packer ")
for _, fname in ipairs(all_assets) do
outfile:write("data/" .. fname .. " ")
end
outfile:write("\n\t./packer pack data ")
for _, fname in ipairs(all_assets) do
outfile:write(fname .. " ")
end
outfile:write("\n\n")
end
lib(compiler_c, config.qstd, "libqstd.a", "qstd/")
lib(compiler_c, config.cfg, "libcfg.a", "cfg/")
prog(
"cpp",
compiler_cpp,
config.sc,
"shadercompiler",
"sc/",
{ "libqstd.a", "libcfg.a" },
{
"-Lsc/glslang/build/glslang",
"-Lsc/glslang/build/SPIRV",
"-Lsc/glslang/build/glslang/OSDependent/Unix",
"-Lsc/glslang/build/External/spirv-tools/source",
"-Lsc/glslang/build/External/spirv-tools/source/link",
"-Lsc/glslang/build/External/spirv-tools/source/opt",
"-lglslang",
"-lglslang-default-resource-limits",
"-lSPIRV",
"-lMachineIndependent",
"-lSPVRemapper",
"-lOSDependent",
"-lGenericCodeGen",
"-lSPIRV-Tools-link",
"-lSPIRV-Tools-opt",
"-lSPIRV-Tools"
}
)
prog(
"cpp",
compiler_cpp,
config.c2,
"c2",
"",
{ "libqstd.a" },
{ "-lX11", "-lm" }
)
outfile:write("c2: pack\n\n")
prog(
"c",
compiler_c,
config.convtexture,
"convtexture",
"",
{ "libqstd.a" },
{}
)
prog(
"c",
compiler_c,
config.convmodel,
"convmodel",
"",
{ "libqstd.a" },
{}
)
prog(
"c",
compiler_c,
config.convmaterial,
"convmaterial",
"",
{ "libqstd.a", "libcfg.a" },
{}
)
prog(
"c",
compiler_c,
config.packer,
"packer",
"",
{ "libqstd.a" },
{}
)
shaders("glsl", "csh", "shadercompiler", config.shaders)
models(config.models)
textures(config.textures)
materials(config.materials)
pack()
outfile:write("data:\n\tmkdir -p data\n\n")
outfile:write("-include ")
for _, fname in ipairs(all_deps) do
outfile:write(fname .. " ")
end
outfile:write("\n\n")
outfile:write("clean:\n\trm -f ")
for _, fname in ipairs(all_objs) do
outfile:write(fname .. " ")
end
for _, fname in ipairs(all_deps) do
outfile:write(fname .. " ")
end
for _, fname in ipairs(all_assets) do
outfile:write("data/" .. fname .. " ")
end
outfile:write("\n\trm -f shadercompiler\n")
outfile:write("\trmdir data\n")
outfile:write("\trm -f c2\n")
outfile:write("\trm -f convtexture\n")
outfile:write("\trm -f convmodel\n")
outfile:write("\trm -f convmaterial\n")
outfile:write("\trm -f libqstd.a\n")
outfile:write("\trm -f libcfg.a\n")
outfile:write("\n")
print("done.")
end
if system == "unix" then
build_unix()
end
if system == "windows" then
print("windows isn't done yet.")
end
|