Promote using std.build.Pkg to canonicalize package name

This commit is contained in:
Robin Voetter
2020-08-10 01:22:32 +02:00
parent e7d2668cdd
commit c16d70f210
2 changed files with 36 additions and 22 deletions

View File

@@ -8,20 +8,26 @@ pub const ResourceGenStep = struct {
step: Step, step: Step,
shader_step: *vkgen.ShaderCompileStep, shader_step: *vkgen.ShaderCompileStep,
builder: *Builder, builder: *Builder,
full_out_path: []const u8, package: std.build.Pkg,
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 {
const self = builder.allocator.create(ResourceGenStep) catch unreachable; const self = builder.allocator.create(ResourceGenStep) catch unreachable;
const full_out_path = path.join(builder.allocator, &[_][]const u8{
builder.build_root,
builder.cache_root,
out,
}) 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,
.full_out_path = path.join(builder.allocator, &[_][]const u8{ .package = .{
self.builder.build_root, .name = "resources",
builder.cache_root, .path = full_out_path,
out, .dependencies = null,
}) catch unreachable, },
.resources = std.ArrayList(u8).init(builder.allocator), .resources = std.ArrayList(u8).init(builder.allocator),
}; };
@@ -40,9 +46,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 = path.dirname(self.full_out_path).?; const dir = path.dirname(self.package.path).?;
try cwd.makePath(dir); try cwd.makePath(dir);
try cwd.writeFile(self.full_out_path, self.resources.items); try cwd.writeFile(self.package.path, self.resources.items);
} }
}; };
@@ -61,13 +67,13 @@ pub fn build(b: *Builder) void {
const gen = vkgen.VkGenerateStep.init(b, "examples/vk.xml", "vk.zig"); const gen = vkgen.VkGenerateStep.init(b, "examples/vk.xml", "vk.zig");
triangle_exe.step.dependOn(&gen.step); triangle_exe.step.dependOn(&gen.step);
triangle_exe.addPackagePath("vulkan", gen.full_out_path); 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.step.dependOn(&res.step);
triangle_exe.addPackagePath("resources", res.full_out_path); triangle_exe.addPackage(res.package);
const triangle_run_cmd = triangle_exe.run(); const triangle_run_cmd = triangle_exe.run();
triangle_run_cmd.step.dependOn(b.getInstallStep()); triangle_run_cmd.step.dependOn(b.getInstallStep());

View File

@@ -6,8 +6,8 @@ const Step = std.build.Step;
/// build.zig integration for Vulkan binding generation. This step can be used to generate /// build.zig integration for Vulkan binding generation. This step can be used to generate
/// Vulkan bindings at compiletime from vk.xml, by providing the path to vk.xml and the output /// Vulkan bindings at compiletime from vk.xml, by providing the path to vk.xml and the output
/// path relative to zig-cache. The final file can then be added to the project using /// path relative to zig-cache. The final package can then be obtained by `package()`, the result
/// `std.build.Builder.addPackagePath`. /// of which can be added to the project using `std.build.Builder.addPackage`.
pub const GenerateStep = struct { pub const GenerateStep = struct {
step: Step, step: Step,
builder: *Builder, builder: *Builder,
@@ -15,24 +15,32 @@ pub const GenerateStep = struct {
/// The path to vk.xml /// The path to vk.xml
spec_path: []const u8, spec_path: []const u8,
/// The full path where the final generated binding will be placed. When using this step, /// The package representing the generated bindings. The generated bindings will be placed
/// this path should be passed to addPackagePath. /// in `package.path`. When using this step, this member should be passed to
full_out_path: []const u8, /// `std.build.Builder.addPackage`, which causes the bindings to become available under the
/// name `vulkan`.
package: std.build.Pkg,
/// 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.
pub fn init(builder: *Builder, spec_path: []const u8, out_path: []const u8) *GenerateStep { pub fn init(builder: *Builder, spec_path: []const u8, out_path: []const u8) *GenerateStep {
const self = builder.allocator.create(GenerateStep) catch unreachable; const self = builder.allocator.create(GenerateStep) catch unreachable;
const full_out_path = path.join(builder.allocator, &[_][]const u8{
builder.build_root,
builder.cache_root,
out_path,
}) 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,
.full_out_path = path.join(builder.allocator, &[_][]const u8{ .package = .{
self.builder.build_root, .name = "vulkan",
builder.cache_root, .path = full_out_path,
out_path, .dependencies = null,
}) catch unreachable, }
}; };
return self; return self;
} }
@@ -50,9 +58,9 @@ pub const GenerateStep = struct {
const tree = try std.zig.parse(self.builder.allocator, out_buffer.items); const tree = try std.zig.parse(self.builder.allocator, out_buffer.items);
const dir = path.dirname(self.full_out_path).?; const dir = path.dirname(self.package.path).?;
try cwd.makePath(dir); try cwd.makePath(dir);
const output_file = cwd.createFile(self.full_out_path, .{}) catch unreachable; const output_file = cwd.createFile(self.package.path, .{}) catch unreachable;
defer output_file.close(); defer output_file.close();
_ = try std.zig.render(self.builder.allocator, output_file.outStream(), tree); _ = try std.zig.render(self.builder.allocator, output_file.outStream(), tree);
} }