-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
238 lines (185 loc) · 7.45 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
const std = @import("std");
const fs = std.fs;
const os = std.os;
const LibExeObjStep = std.build.LibExeObjStep;
const Builder = std.build.Builder;
const Step = std.build.Step;
const ArrayList = std.ArrayList;
const vkgen = @import("deps/vulkan-zig/generator/index.zig");
const glfw = @import("deps/mach-glfw/build.zig");
const stbi = @import("deps/stb_image/build.zig");
// TODO: this file could use a refactor pass or atleast some comments to make it more readable
const MoveDirError = error {
NotFound
};
/// Holds the path to a file where parent directory and file name is separated
const SplitPath = struct {
dir: []const u8,
file_name: []const u8,
};
inline fn pathAndFile(file_path: []const u8) !SplitPath {
var i = file_path.len - 1;
while (i > 0) : (i -= 1) {
if (file_path[i] == '/') {
return SplitPath{
.dir = file_path[0..i],
.file_name = file_path[i+1..file_path.len],
};
}
}
return MoveDirError.NotFound;
}
const ShaderMoveStep = struct {
step: Step,
builder: *Builder,
abs_from: [20]?[]const u8 = [_]?[]const u8{null} ** 20,
abs_len: usize = 0,
fn init(b: *Builder, shader_step: *vkgen.ShaderCompileStep) !*ShaderMoveStep {
var step = Step.init(.custom, "shader_resource", b.allocator, make);
step.dependOn(&shader_step.step);
const self = try b.allocator.create(ShaderMoveStep);
self.* = .{
.step = step,
.builder = b,
};
return self;
}
fn add_abs_resource(self: *ShaderMoveStep, new_abs: []const u8) !void {
if (self.abs_len >= self.abs_from.len) {
return error.MaxResources;
}
defer self.abs_len += 1;
self.abs_from[self.abs_len] = new_abs;
}
fn make(step: *Step) anyerror!void {
const self: *ShaderMoveStep = @fieldParentPtr(ShaderMoveStep, "step", step);
try createFolder(self.builder.install_prefix);
for (self.abs_from) |from| {
if (from) |some| {
try self.moveShaderToOut(some);
} else {
break;
}
}
}
/// moves a given resource to a given path relative to the output binary
fn moveShaderToOut(self: *ShaderMoveStep, abs_from: []const u8) anyerror!void {
const old_path = try pathAndFile(abs_from);
var old_dir = try fs.openDirAbsolute(old_path.dir, .{});
defer old_dir.close();
var new_dir = try fs.openDirAbsolute(self.builder.install_prefix, .{});
defer new_dir.close();
const join_arr = [_][]const u8 {old_path.file_name, "spv" };
const new_file_name = try std.mem.join(self.*.builder.allocator, ".", join_arr[0..join_arr.len]);
self.*.builder.allocator.destroy(new_file_name.ptr);
try fs.rename(old_dir, old_path.file_name, new_dir, new_file_name);
}
};
const AssetMoveStep = struct {
step: Step,
builder: *Builder,
fn init(b: *Builder) !*AssetMoveStep {
var step = Step.init(.custom, "assets", b.allocator, make);
const self = try b.allocator.create(AssetMoveStep);
self.* = .{
.step = step,
.builder = b,
};
return self;
}
fn make(step: *Step) anyerror!void {
const self: *AssetMoveStep = @fieldParentPtr(AssetMoveStep, "step", step);
try createFolder(self.builder.install_prefix);
const dst_asset_path = blk: {
const dst_asset_path_arr = [_][]const u8{self.builder.install_prefix, "assets"};
break :blk try std.fs.path.join(self.builder.allocator, dst_asset_path_arr[0..]);
};
try createFolder(dst_asset_path);
var dst_assets_dir = try fs.openDirAbsolute(dst_asset_path, .{});
defer dst_assets_dir.close();
var src_assets_dir = try fs.cwd().openDir("assets/", .{ .iterate = true, });
defer src_assets_dir.close();
copyDir(src_assets_dir, dst_assets_dir);
}
};
// TODO: HACK: catch unreachable to avoid error hell from recursion
fn copyDir(src_dir: fs.Dir, dst_parent_dir: fs.Dir) void {
const Kind = fs.File.Kind;
var iter = src_dir.iterate();
while(iter.next() catch unreachable) |asset| {
switch (asset.kind) {
Kind.Directory => {
if (std.mem.eql(u8, asset.name, "shaders")) {
continue; // skip shader folder which will be compiled by glslc before being moved
}
var src_child_dir = src_dir.openDir(asset.name, .{ .iterate = true, }) catch unreachable;
defer src_child_dir.close();
dst_parent_dir.makeDir(asset.name) catch |err| switch(err) {
std.os.MakeDirError.PathAlreadyExists => {}, // ok
else => unreachable,
};
var dst_child_dir = dst_parent_dir.openDir(asset.name, .{}) catch unreachable;
defer dst_child_dir.close();
copyDir(src_child_dir, dst_child_dir);
},
Kind.File => std.fs.Dir.copyFile(src_dir, asset.name, dst_parent_dir, asset.name, .{}) catch unreachable,
else => {}, // don't care
}
}
}
inline fn createFolder(path: []const u8) !void {
if (fs.makeDirAbsolute(path)) |_| {
// ok
} else |err| switch(err) {
std.os.MakeDirError.PathAlreadyExists => {
// ok
},
else => |e| return e,
}
}
pub fn build(b: *Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("zig_vulkan", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.linkLibC();
// compile and link with glfw statically
glfw.link(b, exe, .{});
exe.addPackagePath("glfw", "deps/mach-glfw/src/main.zig");
exe.addPackagePath("ecs", "deps/zig-ecs/src/ecs.zig");
exe.addPackagePath("zlm", "deps/zlm/zlm.zig");
stbi.linkStep(b, exe);
const vk_xml_path = b.option([]const u8, "vulkan-registry", "Override to the Vulkan registry") orelse "deps/vk.xml";
const gen = vkgen.VkGenerateStep.init(b, vk_xml_path, "vk.zig");
exe.step.dependOn(&gen.step);
exe.addPackage(gen.package);
const shader_comp = vkgen.ShaderCompileStep.init(
b,
// TODO: -O (optimize), -I (includes)
&[_][]const u8{"glslc", "--target-env=vulkan1.2"},
);
const shader_move_step = ShaderMoveStep.init(b, shader_comp) catch unreachable;
const vert = shader_comp.add("assets/shaders/pass.vert");
shader_move_step.add_abs_resource(vert) catch unreachable;
const frag = shader_comp.add("assets/shaders/pass.frag");
shader_move_step.add_abs_resource(frag) catch unreachable;
const comp = shader_comp.add("assets/shaders/comp.comp");
shader_move_step.add_abs_resource(comp) catch unreachable;
exe.step.dependOn(&shader_comp.step);
exe.step.dependOn(&shader_move_step.step);
const asset_move = AssetMoveStep.init(b) catch unreachable;
exe.step.dependOn(&asset_move.step);
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);
var tests = b.addTest("src/test.zig");
tests.setBuildMode(mode);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests.step);
}