efemra/build.zig

121 lines
4.4 KiB
Zig
Raw Normal View History

2022-06-30 06:04:08 +00:00
const std = @import("std");
2022-06-30 10:51:51 +00:00
const Step = std.build.Step;
const Builder = std.build.Builder;
2022-07-08 07:46:39 +00:00
const pkgs = @import("deps.zig").pkgs;
2022-06-30 06:04:08 +00:00
const build_pkgs = @import("deps.zig").build_pkgs;
2022-06-30 10:51:51 +00:00
const build_glfw = build_pkgs.build_glfw;
2022-07-08 07:46:39 +00:00
const build_vma = build_pkgs.build_vma;
2022-07-08 08:52:31 +00:00
const build_imgui = build_pkgs.build_imgui;
2022-06-30 10:51:51 +00:00
const vkgen = build_pkgs.build_vulkan;
pub const ResourceGenStep = struct {
step: Step,
shader_step: *vkgen.ShaderCompileStep,
builder: *Builder,
package: std.build.Pkg,
output_file: std.build.GeneratedFile,
resources: std.ArrayList(u8),
pub fn init(builder: *Builder, out: []const u8) *ResourceGenStep {
const self = builder.allocator.create(ResourceGenStep) catch unreachable;
const full_out_path = std.fs.path.join(builder.allocator, &[_][]const u8{
builder.build_root,
out,
}) catch unreachable;
self.* = .{
.step = Step.init(.custom, "resources", builder.allocator, make),
.shader_step = vkgen.ShaderCompileStep.init(builder, &[_][]const u8{ "glslc", "--target-env=vulkan1.1" }, ""),
2022-06-30 10:51:51 +00:00
.builder = builder,
.package = .{
.name = "resources",
.source = .{ .generated = &self.output_file },
.dependencies = null,
},
.output_file = .{
.step = &self.step,
.path = full_out_path,
},
.resources = std.ArrayList(u8).init(builder.allocator),
};
self.step.dependOn(&self.shader_step.step);
return self;
}
fn renderPath(path: []const u8, writer: anytype) void {
const separators = &[_]u8{ std.fs.path.sep_windows, std.fs.path.sep_posix };
var i: usize = 0;
while (std.mem.indexOfAnyPos(u8, path, i, separators)) |j| {
writer.writeAll(path[i..j]) catch unreachable;
switch (std.fs.path.sep) {
std.fs.path.sep_windows => writer.writeAll("\\\\") catch unreachable,
std.fs.path.sep_posix => writer.writeByte(std.fs.path.sep_posix) catch unreachable,
else => unreachable,
}
i = j + 1;
}
writer.writeAll(path[i..]) catch unreachable;
}
pub fn addShader(self: *ResourceGenStep, name: []const u8, source: []const u8, entry_point: []const u8, stage: vkgen.ShaderStage) void {
const shader_out_path = self.shader_step.add(source, .{
.entry_point = entry_point,
.stage = stage,
.output_filename = std.fmt.allocPrint(self.builder.allocator, "{s}.spv", .{name}) catch unreachable,
});
2022-06-30 10:51:51 +00:00
var writer = self.resources.writer();
writer.print("pub const {s} align(4) = @embedFile(\"", .{name}) catch unreachable;
2022-06-30 10:51:51 +00:00
renderPath(shader_out_path, writer);
writer.writeAll("\").*;\n") catch unreachable;
2022-06-30 10:51:51 +00:00
}
fn make(step: *Step) !void {
const self = @fieldParentPtr(ResourceGenStep, "step", step);
const cwd = std.fs.cwd();
const dir = std.fs.path.dirname(self.output_file.path.?).?;
try cwd.makePath(dir);
try cwd.writeFile(self.output_file.path.?, self.resources.items);
}
};
2022-06-30 06:04:08 +00:00
pub fn build(b: *std.build.Builder) void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("efemra", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
2022-07-16 06:07:31 +00:00
exe.install();
2022-06-30 06:04:08 +00:00
2022-06-30 10:51:51 +00:00
const gen = vkgen.VkGenerateStep.init(b, "etc/vk.xml", "vk.zig");
2022-07-16 06:07:31 +00:00
2022-06-30 06:04:08 +00:00
exe.addPackage(gen.package);
2022-07-23 09:59:28 +00:00
exe.addPackage(pkgs.uuid);
2022-07-16 06:07:31 +00:00
exe.addPackage(pkgs.glfw);
2022-06-30 10:51:51 +00:00
build_glfw.link(b, exe, .{});
2022-07-16 06:07:31 +00:00
build_vma.link(exe, gen.output_file.getPath(), mode, target);
build_imgui.link(exe);
2022-06-30 10:51:51 +00:00
const res = ResourceGenStep.init(b, "resources.zig");
res.addShader("DepthOnly_vert", "src/shaders/DepthOnly.hlsl", "VSMain", .vertex);
res.addShader("DepthOnly_frag", "src/shaders/DepthOnly.hlsl", "PSMain", .fragment);
res.addShader("brush_vert", "src/shaders/brush.hlsl", "VSMain", .vertex);
res.addShader("brush_frag", "src/shaders/brush.hlsl", "PSMain", .fragment);
2022-06-30 10:51:51 +00:00
exe.addPackage(res.package);
2022-06-30 06:04:08 +00:00
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);
}