Update to new build API

This commit is contained in:
Robin Voetter
2021-06-12 13:44:48 +02:00
parent 4b4ef38c93
commit 6f965fead0
3 changed files with 22 additions and 13 deletions

View File

@@ -8,6 +8,7 @@ pub const ResourceGenStep = struct {
shader_step: *vkgen.ShaderCompileStep, shader_step: *vkgen.ShaderCompileStep,
builder: *Builder, builder: *Builder,
package: std.build.Pkg, package: std.build.Pkg,
output_file: std.build.GeneratedFile,
resources: std.ArrayList(u8), resources: std.ArrayList(u8),
pub fn init(builder: *Builder, out: []const u8) *ResourceGenStep { pub fn init(builder: *Builder, out: []const u8) *ResourceGenStep {
@@ -19,14 +20,18 @@ pub const ResourceGenStep = struct {
}) catch unreachable; }) catch unreachable;
self.* = .{ self.* = .{
.step = Step.init(.Custom, "resources", builder.allocator, make), .step = Step.init(.custom, "resources", builder.allocator, make),
.shader_step = vkgen.ShaderCompileStep.init(builder, &[_][]const u8{"glslc", "--target-env=vulkan1.2"}), .shader_step = vkgen.ShaderCompileStep.init(builder, &[_][]const u8{"glslc", "--target-env=vulkan1.2"}),
.builder = builder, .builder = builder,
.package = .{ .package = .{
.name = "resources", .name = "resources",
.path = full_out_path, .path = .{.generated = &self.output_file},
.dependencies = null, .dependencies = null,
}, },
.output_file = .{
.step = &self.step,
.path = full_out_path,
},
.resources = std.ArrayList(u8).init(builder.allocator), .resources = std.ArrayList(u8).init(builder.allocator),
}; };
@@ -63,9 +68,9 @@ pub const ResourceGenStep = struct {
const self = @fieldParentPtr(ResourceGenStep, "step", step); const self = @fieldParentPtr(ResourceGenStep, "step", step);
const cwd = std.fs.cwd(); const cwd = std.fs.cwd();
const dir = std.fs.path.dirname(self.package.path).?; const dir = std.fs.path.dirname(self.output_file.path.?).?;
try cwd.makePath(dir); try cwd.makePath(dir);
try cwd.writeFile(self.package.path, self.resources.items); try cwd.writeFile(self.output_file.path.?, self.resources.items);
} }
}; };
@@ -88,16 +93,14 @@ pub fn build(b: *Builder) void {
triangle_exe.linkLibC(); triangle_exe.linkLibC();
triangle_exe.linkSystemLibrary("glfw"); triangle_exe.linkSystemLibrary("glfw");
const vk_xml_path = b.option([]const u8, "vulkan-registry", "Override the to the Vulkan registry") orelse "examples/vk.xml"; const vk_xml_path = b.option([]const u8, "vulkan-registry", "Override the path to the Vulkan registry") orelse "examples/vk.xml";
const gen = vkgen.VkGenerateStep.init(b, vk_xml_path, "vk.zig"); const gen = vkgen.VkGenerateStep.init(b, vk_xml_path, "vk.zig");
triangle_exe.step.dependOn(&gen.step);
triangle_exe.addPackage(gen.package); triangle_exe.addPackage(gen.package);
const res = ResourceGenStep.init(b, "resources.zig"); const res = ResourceGenStep.init(b, "resources.zig");
res.addShader("triangle_vert", "examples/shaders/triangle.vert"); res.addShader("triangle_vert", "examples/shaders/triangle.vert");
res.addShader("triangle_frag", "examples/shaders/triangle.frag"); res.addShader("triangle_frag", "examples/shaders/triangle.frag");
triangle_exe.step.dependOn(&res.step);
triangle_exe.addPackage(res.package); triangle_exe.addPackage(res.package);
const triangle_run_cmd = triangle_exe.run(); const triangle_run_cmd = triangle_exe.run();

View File

@@ -30,7 +30,7 @@ pub const ShaderCompileStep = struct {
pub fn init(builder: *Builder, glslc_cmd: []const []const u8) *ShaderCompileStep { pub fn init(builder: *Builder, glslc_cmd: []const []const u8) *ShaderCompileStep {
const self = builder.allocator.create(ShaderCompileStep) catch unreachable; const self = builder.allocator.create(ShaderCompileStep) catch unreachable;
self.* = .{ self.* = .{
.step = Step.init(.Custom, "shader-compile", builder.allocator, make), .step = Step.init(.custom, "shader-compile", builder.allocator, make),
.builder = builder, .builder = builder,
.glslc_cmd = glslc_cmd, .glslc_cmd = glslc_cmd,
.shaders = std.ArrayList(Shader).init(builder.allocator), .shaders = std.ArrayList(Shader).init(builder.allocator),

View File

@@ -21,6 +21,8 @@ pub const GenerateStep = struct {
/// name `vulkan`. /// name `vulkan`.
package: std.build.Pkg, package: std.build.Pkg,
output_file: std.build.GeneratedFile,
/// Initialize a Vulkan generation step, for `builder`. `spec_path` is the path to /// Initialize a Vulkan generation step, for `builder`. `spec_path` is the path to
/// vk.xml, relative to the project root. The generated bindings will be placed at /// vk.xml, relative to the project root. The generated bindings will be placed at
/// `out_path`, which is relative to the zig-cache directory. /// `out_path`, which is relative to the zig-cache directory.
@@ -33,14 +35,18 @@ pub const GenerateStep = struct {
}) catch unreachable; }) catch unreachable;
self.* = .{ self.* = .{
.step = Step.init(.Custom, "vulkan-generate", builder.allocator, make), .step = Step.init(.custom, "vulkan-generate", builder.allocator, make),
.builder = builder, .builder = builder,
.spec_path = spec_path, .spec_path = spec_path,
.package = .{ .package = .{
.name = "vulkan", .name = "vulkan",
.path = full_out_path, .path = .{.generated = &self.output_file},
.dependencies = null, .dependencies = null,
} },
.output_file = .{
.step = &self.step,
.path = full_out_path,
},
}; };
return self; return self;
} }
@@ -75,8 +81,8 @@ pub const GenerateStep = struct {
var formatted = try tree.render(self.builder.allocator); var formatted = try tree.render(self.builder.allocator);
const dir = path.dirname(self.package.path).?; const dir = path.dirname(self.output_file.path.?).?;
try cwd.makePath(dir); try cwd.makePath(dir);
try cwd.writeFile(self.package.path, formatted); try cwd.writeFile(self.output_file.path.?, formatted);
} }
}; };