forked from Honeybunch/sdltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
450 lines (381 loc) · 15.7 KB
/
build.zig
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
const std = @import("std");
const Builder = std.build.Builder;
const vma_build = @import("Zig-VMA/vma_build.zig");
pub fn build(b: *Builder) !void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
// Option to override whether shaders are built in debug mode. Defaults to
// (mode == .Debug).
const debug_shaders = b.option(bool, "debug-shaders", "Set whether shaders are compiled in debug mode")
orelse (mode == .Debug);
const fractal = addShaderSteps(b, "fractal", debug_shaders);
const mesh = addShaderSteps(b, "mesh", debug_shaders);
const exe = b.addExecutable("sdltest", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.step.dependOn(fractal);
exe.step.dependOn(mesh);
try linkSDL(exe, target, mode);
// Link platform object files
if (target.isWindows()) {
exe.addObjectFile("lib/win_x64/vulkan-1.lib");
} else {
exe.linkSystemLibrary("vulkan");
}
// Link VMA source directly
const vk_root = "include/vulkan_core.zig";
const vk: std.build.Pkg = .{
.name = "vk",
.source = .{ .path = vk_root },
};
const sdl: std.build.Pkg = .{
.name = "sdl",
.source = .{ .path = "include/sdl.zig" },
.dependencies = &.{ vk },
};
exe.addPackage(vk);
exe.addPackage(sdl);
vma_build.link(exe, vk_root, mode, target);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
fn addShaderSteps(b: *Builder, name: []const u8, debug: bool) *std.build.Step {
const hlsl = b.fmt("shaders/{s}.hlsl", .{name});
const vert_out = b.fmt("src/shader_data/{s}_vert.spv", .{name});
const frag_out = b.fmt("src/shader_data/{s}_frag.spv", .{name});
const vert = DxcCompileShaderStep.createVert(b, hlsl, vert_out);
const frag = DxcCompileShaderStep.createFrag(b, hlsl, frag_out);
if (debug) {
vert.optimization_level = 0;
vert.enable_debug_info = true;
vert.embed_debug_info = true;
frag.optimization_level = 0;
frag.enable_debug_info = true;
frag.embed_debug_info = true;
}
const step = b.step(name, b.fmt("Build shaders from {s}", .{hlsl}));
step.dependOn(&vert.step);
step.dependOn(&frag.step);
return step;
}
fn linkSDL(step: *std.build.LibExeObjStep, target: std.zig.CrossTarget, mode: std.builtin.Mode) !void {
const b = step.builder;
step.addIncludeDir("sdl/include");
step.addIncludeDir("sdl/src/video/khronos");
var c_args = std.ArrayList([]const u8).init(b.allocator);
c_args.append("-DHAVE_LIBC") catch unreachable;
step.linkLibC();
if (mode == .ReleaseFast) {
c_args.append("-DNDEBUG") catch unreachable;
} else {
c_args.append("-D_DEBUG") catch unreachable;
}
if (target.isWindows()) {
c_args.append("-D_WINDOWS") catch unreachable;
}
// TODO: There's probably a much better way to do this, building a static
// data structure and pulling dirs out of it.
var source_files = std.ArrayList([]const u8).init(b.allocator);
var source_dirs = std.ArrayList([]const u8).init(b.allocator);
source_dirs.appendSlice(&.{
"sdl/src",
"sdl/src/atomic",
"sdl/src/audio",
"sdl/src/cpuinfo",
"sdl/src/dynapi",
"sdl/src/events",
"sdl/src/file",
"sdl/src/haptic",
"sdl/src/libm",
"sdl/src/locale",
"sdl/src/misc",
"sdl/src/power",
"sdl/src/render",
"sdl/src/sensor",
"sdl/src/stdlib",
"sdl/src/thread",
"sdl/src/timer",
"sdl/src/video",
"sdl/src/video/yuv2rgb",
}) catch unreachable;
source_dirs.appendSlice(try collectChildDirectories(b, "sdl/src/render")) catch unreachable;
// TODO: Detect android, can't do this from target alone
const android = false;
const have_audio = true;
const have_filesystem = true;
const have_haptic = true;
const have_hidapi = true;
const have_joystick = true;
const have_loadso = true;
const have_misc = true;
const have_power = true;
const have_locale = true;
const have_timers = true;
const have_sensor = true;
const have_threads = true;
const have_video = true;
if (have_joystick) {
source_dirs.append("sdl/src/joystick") catch unreachable;
const have_virtual_joystick = true;
if (have_virtual_joystick) {
source_dirs.append("sdl/src/joystick/virtual") catch unreachable;
}
if (have_hidapi) {
source_dirs.append("sdl/src/joystick/hidapi") catch unreachable;
}
}
if (have_audio) {
const dummy_audio = true;
const disk_audio = true;
if (dummy_audio) {
source_dirs.append("sdl/src/audio/dummy") catch unreachable;
}
if (disk_audio) {
source_dirs.append("sdl/src/audio/disk") catch unreachable;
}
}
if (have_video) {
const dummy_video = true;
const offscreen_video = false;
if (dummy_video) {
source_dirs.append("sdl/src/video/dummy") catch unreachable;
}
if (offscreen_video) {
source_dirs.append("sdl/src/video/offscreen") catch unreachable;
}
}
if (android) {
// TODO: CMakeLists.txt line 964 include ndk sources also.
source_dirs.append("sdl/src/core/android") catch unreachable;
source_dirs.append("sdl/src/misc/android") catch unreachable;
if (have_audio) source_dirs.append("sdl/src/audio/android") catch unreachable;
if (have_filesystem) source_dirs.append("sdl/src/filesystem/android") catch unreachable;
if (have_haptic) source_dirs.append("sdl/src/haptic/android") catch unreachable;
if (have_joystick) source_dirs.append("sdl/src/joystick/android") catch unreachable;
if (have_loadso) source_dirs.append("sdl/src/loadso/dlopen") catch unreachable;
if (have_power) source_dirs.append("sdl/src/power/android") catch unreachable;
if (have_locale) source_dirs.append("sdl/src/locale/android") catch unreachable;
if (have_timers) source_dirs.append("sdl/src/timer/unix") catch unreachable;
if (have_sensor) source_dirs.append("sdl/src/sensor/android") catch unreachable;
if (have_video) {
source_dirs.append("sdl/src/video/android") catch unreachable;
c_args.append("-DGL_GLEXT_PROTOTYPES") catch unreachable;
// TODO: more stuff here
}
}
// TODO: Linux
if (target.isWindows()) {
source_dirs.appendSlice(&.{
"sdl/src/core/windows",
"sdl/src/misc/windows",
}) catch unreachable;
// TODO: windows store
// TODO: directx
if (have_audio) {
const have_dsound = true;
const have_wasapi = true;
source_dirs.append("sdl/src/audio/winmm") catch unreachable;
if (have_dsound) {
source_dirs.append("sdl/src/audio/directsound") catch unreachable;
}
if (have_wasapi) {
source_dirs.append("sdl/src/audio/wasapi") catch unreachable;
}
}
if (have_video) {
if (!have_loadso) {
std.debug.print("{s}", .{"Error: SDL video requires loadso on windows.\n"});
std.os.exit(-1);
}
// TODO: Windows store
source_dirs.append("sdl/src/video/windows") catch unreachable;
// TODO: D3D
}
if (have_threads) {
source_files.appendSlice(&.{
"sdl/src/thread/windows/SDL_sysmutex.c",
"sdl/src/thread/windows/SDL_syssem.c",
"sdl/src/thread/windows/SDL_systhread.c",
"sdl/src/thread/windows/SDL_systls.c",
"sdl/src/thread/generic/SDL_syscond.c",
}) catch unreachable;
}
if (have_sensor) {
source_dirs.append("sdl/src/sensor/windows") catch unreachable;
}
if (have_power) {
// TODO: windows store
source_files.append("sdl/src/power/windows/SDL_syspower.c") catch unreachable;
}
if (have_locale) {
source_dirs.append("sdl/src/locale/windows") catch unreachable;
}
if (have_filesystem) {
// TODO: windows store
source_dirs.append("sdl/src/filesystem/windows") catch unreachable;
}
if (have_timers) {
source_dirs.append("sdl/src/timer/windows") catch unreachable;
}
if (have_loadso) {
source_dirs.append("sdl/src/loadso/windows") catch unreachable;
}
if (have_video) {
// TODO: This just sets vars in cmake that aren't used?
// Also it's redundant with the check above.
}
if (have_joystick) {
if (have_hidapi) {
source_files.append("sdl/src/hidapi/windows/hid.c") catch unreachable;
}
// TODO: lots of options here
source_dirs.append("sdl/src/joystick/windows") catch unreachable;
if (have_haptic) {
// TODO
source_dirs.append("sdl/src/haptic/windows") catch unreachable;
}
}
// Windows DLLs we need
// TODO: not needed for windows store
step.linkSystemLibrary("Advapi32");
step.linkSystemLibrary("Gdi32");
step.linkSystemLibrary("Imm32");
step.linkSystemLibrary("Ole32");
step.linkSystemLibrary("OleAut32");
step.linkSystemLibrary("SetupAPI");
step.linkSystemLibrary("Shell32");
step.linkSystemLibrary("User32");
step.linkSystemLibrary("Version"); // Api-ms-win-core-version-l1-1-0.dll
step.linkSystemLibrary("Winmm");
}
// TODO: apple
// TODO: haiku
// TODO: riscos
// dummy files
if (!have_joystick) source_dirs.append("sdl/src/joystick/dummy") catch unreachable;
if (!have_haptic) source_dirs.append("sdl/src/haptic/dummy") catch unreachable;
if (!have_sensor) source_dirs.append("sdl/src/sensor/dummy") catch unreachable;
if (!have_loadso) source_dirs.append("sdl/src/loadso/dummy") catch unreachable;
if (!have_filesystem) source_dirs.append("sdl/src/filesystem/dummy") catch unreachable;
if (!have_locale) source_dirs.append("sdl/src/locale/dummy") catch unreachable;
if (!have_misc) source_dirs.append("sdl/src/misc/dummy") catch unreachable;
if (!have_threads) source_dirs.append("sdl/src/thread/generic") catch unreachable;
if (!have_timers) source_dirs.append("sdl/src/timer/dummy") catch unreachable;
for (source_dirs.items) |dir| {
source_files.appendSlice(try collectSources(b, dir, ".c")) catch unreachable;
}
step.addCSourceFiles(source_files.items, c_args.items);
}
fn collectSources(b: *Builder, path: []const u8, extension: []const u8) ![]const []const u8 {
var sources = std.ArrayList([]const u8).init(b.allocator);
const dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
var it = dir.iterate();
while (try it.next()) |child| {
if (child.kind == .File) {
const ext = std.fs.path.extension(child.name);
if (std.ascii.eqlIgnoreCase(ext, extension)) {
const full_path = std.fs.path.join(b.allocator, &.{path, child.name}) catch unreachable;
sources.append(full_path) catch unreachable;
}
}
}
return sources.toOwnedSlice();
}
fn collectChildDirectories(b: *Builder, path: []const u8) ![]const []const u8 {
var dirs = std.ArrayList([]const u8).init(b.allocator);
const dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
var it = dir.iterate();
while (try it.next()) |child| {
if (child.kind == .Directory) {
const full_path = std.fs.path.join(b.allocator, &.{path, child.name}) catch unreachable;
dirs.append(full_path) catch unreachable;
}
}
return dirs.toOwnedSlice();
}
const DxcCompileShaderStep = struct {
step: std.build.Step,
builder: *Builder,
hlsl_file: []const u8,
out_file: []const u8,
entry_point: []const u8,
profile: Profile = .unspecified,
optimization_level: u2 = 3,
enable_debug_info: bool = false,
embed_debug_info: bool = false,
include_reflection: bool = true,
pub fn createVert(b: *Builder, hlsl_file: []const u8, out_file: []const u8) *DxcCompileShaderStep {
const self = b.allocator.create(@This()) catch unreachable;
self.* = .{
.step = std.build.Step.init(.custom, hlsl_file, b.allocator, make),
.builder = b,
.hlsl_file = b.dupePath(hlsl_file),
.out_file = b.dupePath(out_file),
.entry_point = "vert",
.profile = .vs_6_0,
};
return self;
}
pub fn createFrag(b: *Builder, hlsl_file: []const u8, out_file: []const u8) *DxcCompileShaderStep {
const self = b.allocator.create(@This()) catch unreachable;
self.* = .{
.step = std.build.Step.init(.custom, hlsl_file, b.allocator, make),
.builder = b,
.hlsl_file = b.dupePath(hlsl_file),
.out_file = b.dupePath(out_file),
.entry_point = "frag",
.profile = .ps_6_0,
};
return self;
}
pub fn make(step: *std.build.Step) anyerror!void {
const self = @fieldParentPtr(@This(), "step", step);
const b = self.builder;
// dxc doesn't do mkdirs so we need to.
if (std.fs.path.dirname(self.out_file)) |dir| {
try std.fs.cwd().makePath(dir);
}
var args = std.ArrayList([]const u8).init(b.allocator);
defer args.deinit();
args.append("dxc") catch unreachable;
if (self.profile != .none) {
args.append("-T") catch unreachable;
args.append(@tagName(self.profile)) catch unreachable;
}
args.append("-E") catch unreachable;
args.append(self.entry_point) catch unreachable;
args.append(b.fmt("-O{d}", .{self.optimization_level})) catch unreachable;
if (self.enable_debug_info) args.append("-Zi") catch unreachable;
if (self.embed_debug_info) args.append("-Qembed_debug") catch unreachable;
if (self.include_reflection) args.append("-fspv-reflect") catch unreachable;
args.append("-spirv") catch unreachable;
args.append(self.hlsl_file) catch unreachable;
args.append("-Fo") catch unreachable;
args.append(self.out_file) catch unreachable;
_ = try self.builder.execFromStep(args.items, &self.step);
}
pub const Profile = enum {
none,
ps_6_0, ps_6_1, ps_6_2, ps_6_3, ps_6_4, ps_6_5, ps_6_6,
vs_6_0, vs_6_1, vs_6_2, vs_6_3, vs_6_4, vs_6_5, vs_6_6,
gs_6_0, gs_6_1, gs_6_2, gs_6_3, gs_6_4, gs_6_5, gs_6_6,
hs_6_0, hs_6_1, hs_6_2, hs_6_3, hs_6_4, hs_6_5, hs_6_6,
ds_6_0, ds_6_1, ds_6_2, ds_6_3, ds_6_4, ds_6_5, ds_6_6,
cs_6_0, cs_6_1, cs_6_2, cs_6_3, cs_6_4, cs_6_5, cs_6_6,
lib_6_1, lib_6_2, lib_6_3, lib_6_4, lib_6_5, lib_6_6,
ms_6_5, ms_6_6,
as_6_5, as_6_6,
};
};