summaryrefslogtreecommitdiff
path: root/configure.lua
blob: d9fc7eceb608cc72b8df720db26df1157ee30da7 (plain) (blame)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
config = {
	c2 = {
		"app",
		"asset",
		"c2",
		"camera",
		"debugdraw",
		"editor",
		"lighting",
		"maths",
		"model",
		"physics",
		"pipeline",
		"scene",
		"ui",
		"video",
		"world",
	},
	qstd = {
		"memory",
		"plat",
		"str",
		"pack"
	},
	cfg = {
		"cfgparse"
	},
	sc = {
		"sc",
		"includer"
	},
	packer = {
		"packer"
	},
	convtexture = {
		"convtexture"
	},
	convmodel = {
		"convmodel"
	},
	convmaterial = {
		"convmaterial"
	},
	shaders = {
		"debug",
		"mip_spec",
		"sky",
		"surface",
		"surface_depthonly",
		"tonemap",
		"triangle",
		"ui",
	},
	materials = {
		"bricks",
		"plastic",
		"greybox"
	},
	models = {
		"monkey",
		"cube",
		"scene"
	},
	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
	local configuration = arg[2]
	if system ~= "release" or system ~= "debug" then
		print("Invalid configuration, defaulting to release.")
	end

	local outfile = io.open("build.ninja", "w")
	if not outfile then
		print("Failed to open build.ninja.")
		exit(3)
	end
	outfile:write("# generated by configure.lua\n\n")
	local function staticlib(name, dir, tab, ext)
		for _, fname in ipairs(tab) do
			outfile:write(string.format("build %s/%s.obj: cpp %s/%s.%s\n", dir, fname, dir, fname, ext))
		end
		outfile:write("build " .. dir .. "/" .. name .. ".lib: lib ")
		for i = 1, #tab - 1 do
			local fname = tab[i]
			outfile:write(dir .. "/" .. fname .. ".obj ")
		end
		outfile:write(dir .. "/" .. tab[#tab] .. ".obj\n")
		outfile:write("\n")
	end
	local function tool(name, dir, tab, ext, libs, deps)
		for _, fname in ipairs(tab) do
			outfile:write(string.format("build %s/%s.obj: cpp %s/%s.%s\n", dir, fname, dir, fname, ext))
		end
		outfile:write("build " .. name .. ".exe: link ")
		for i = 1, #tab - 1 do
			local fname = tab[i]
			outfile:write(dir .. "/" .. fname .. ".obj ")
		end
		outfile:write(dir .. "/" .. tab[#tab] .. ".obj")
		if #libs > 0 then
			for _, l in ipairs(libs) do
				outfile:write(" " .. l)
			end
		end
		outfile:write("\n")
		if #deps > 0 then
			outfile:write("  libs = ")
			for _, d in ipairs(deps) do
				outfile:write(d .. " ")
			end
		end
		outfile:write("\n\n")
	end
	all_assets = {}
	local function assets(tab, tool, exts, extd, deps)
		local depstr = ""
		if deps then
			for _, dep in ipairs(deps) do
				depstr = depstr .. " " .. dep
			end
		end
		for _, fname in ipairs(tab) do
			outfile:write(string.format("build data/%s.%s: %s intermediate/%s.%s | %s.exe", fname, extd, tool, fname, exts, tool))
			all_assets[#all_assets + 1] = "data/" .. fname .. "." .. extd
			outfile:write(depstr .. "\n")
		end
		outfile:write("\n")
	end
	local function textures(tab, tool)
		for _, item in ipairs(tab) do
			local fname = item[1]
			local exts = item[2]
			local format = item[3]
			local extd = "tex"
			outfile:write(string.format("build data/%s.%s: %s intermediate/%s.%s | %s.exe\n", fname, extd, tool, fname, exts, tool))
			outfile:write("  format = " .. format .. "\n")
			all_assets[#all_assets + 1] = "data/" .. fname .. "." .. extd
		end
		outfile:write("\n")
	end
	local function pack()
		outfile:write("build pack: packer ")
		for _, asset in ipairs(all_assets) do
			outfile:write(asset .. " ")
		end
		outfile:write("| packer.exe\n")
	end
	local cflags = "cflags = /nologo /MD /std:c++20 /Isc\\glslang /Iqstd /Icfg /Dplat_win /Dplat_x86 /Dallocation_default_alignment=8"
	local lflags = ""
	if configuration == "debug" then
		cflags = cflags .. " /Z7 /DDEBUG"
		lflags = lflags .. "/DEBUG"
	else
		cflags = cflags .. " /O2 /DNDEBUG"
	end
	outfile:write(cflags)
	outfile:write("\n\n")
	outfile:write("rule cpp\n")
	outfile:write("  command = cl $cflags $in /c /Fo: \"$out\"\n")
	outfile:write("  deps = msvc\n\n")
	outfile:write("rule link\n")
	outfile:write("  command = cl /nologo $in /Fe: \"$out\" /link " .. lflags .." $libs\n\n")
	outfile:write("rule lib\n")
	outfile:write("  command = lib /nologo $in /out:\"$out\"\n\n")
	for _, fname in ipairs(config.c2) do
		outfile:write("build " .. fname .. ".obj: cpp " .. fname .. ".cpp\n")
	end
	outfile:write("build c2.exe: link qstd/qstd.lib ")
	for i = 1, #config.c2 - 1 do
		local fname = config.c2[i]
		outfile:write(fname .. ".obj ")
	end
	outfile:write(config.c2[#config.c2] .. ".obj\n")
	outfile:write("  libs = user32.lib\n")
	outfile:write("\n")
	staticlib("qstd", "qstd", config.qstd, "c")
	staticlib("cfg", "cfg", config.cfg, "c")
	tool(
		"shadercompiler",
		"sc",
		config.sc,
		"cpp",
		{ "qstd/qstd.lib", "cfg/cfg.lib" },
		{
			"/LIBPATH:sc/glslang/build/glslang/Release",
			"/LIBPATH:sc/glslang/build/SPIRV/Release",
			"/LIBPATH:sc/glslang/build/glslang/OSDependent/Windows/Release",
			"/LIBPATH:sc/glslang/build/External/spirv-tools/source/Release",
			"/LIBPATH:sc/glslang/build/External/spirv-tools/source/link/Release",
			"/LIBPATH:sc/glslang/build/External/spirv-tools/source/opt/Release",
			"glslang.lib",
			"glslang-default-resource-limits.lib",
			"SPIRV.lib",
			"MachineIndependent.lib",
			"SPVRemapper.lib",
			"OSDependent.lib",
			"GenericCodeGen.lib",
			"SPIRV-Tools-link.lib",
			"SPIRV-Tools-opt.lib",
			"SPIRV-Tools.lib"
		}
	)
	tool(
		"packer",
		".",
		config.packer,
		"c",
		{ "qstd/qstd.lib" },
		{}
	)
	tool(
		"convtexture",
		".",
		config.convtexture,
		"c",
		{ "qstd/qstd.lib" },
		{}
	)
	tool(
		"convmodel",
		".",
		config.convmodel,
		"c",
		{ "qstd/qstd.lib" },
		{}
	)
	tool(
		"convmaterial",
		".",
		config.convmaterial,
		"c",
		{ "qstd/qstd.lib", "cfg/cfg.lib" },
		{}
	)
	local function tool_rule(name)
		outfile:write(string.format("rule %s\n  command = %s.exe $in $out\n\n", name, name))
	end
	tool_rule("shadercompiler")
	tool_rule("convmaterial")
	outfile:write("rule convmodel\n  command = convmodel.exe data $in $out\n\n")
	outfile:write("rule convtexture\n  command = convtexture.exe $in $out $format\n\n")
	outfile:write("rule packer\n  command = packer.exe $out -data $in\n\n")
	assets(config.shaders, "shadercompiler", "glsl", "csh")
	assets(config.models, "convmodel", "glb", "mdl", all_assets)
	assets(config.materials, "convmaterial", "mat", "mat")
	textures(config.textures, "convtexture")
	pack()
	outfile:write("\n")
end