standardize names for step creation and obtaining packages

stdlib style is to use `create` for step creation and `getPackage`/`getSource`
to provide generated sources as package/file.
This commit is contained in:
Robin Voetter
2022-12-30 00:43:29 +01:00
parent f7a4e4346e
commit 08dc9f508c
3 changed files with 23 additions and 24 deletions

View File

@@ -34,13 +34,13 @@ pub fn build(b: *Builder) void {
const exe = b.addExecutable("my-executable", "src/main.zig");
// Create a step that generates vk.zig (stored in zig-cache) from the provided vulkan registry.
const gen = vkgen.VkGenerateStep.init(b, "path/to/vk.xml", "vk.zig");
const gen = vkgen.VkGenerateStep.create(b, "path/to/vk.xml", "vk.zig");
// Add the generated file as package to the final executable
exe.addPackage(gen.package);
exe.addPackage(gen.getPackage("vulkan"));
}
```
This reads vk.xml, parses its contents, and renders the Vulkan bindings to "vk.zig", which is then formatted and placed in `zig-cache`. The resulting file can then be added to an executable by using `addPackage`, after which the bindings will be made available to the executable under the name `vulkan`.
This reads vk.xml, parses its contents, and renders the Vulkan bindings to "vk.zig", which is then formatted and placed in `zig-cache`. The resulting file can then be added to an executable by using `addPackage`, after which the bindings will be made available to the executable under the name passed to `getPackage`.
### Function & field renaming
Functions and fields are renamed to be more or less in line with [Zig's standard library style](https://ziglang.org/documentation/master/#Style-Guide):

View File

@@ -21,8 +21,8 @@ pub fn build(b: *Builder) void {
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");
triangle_exe.addPackage(gen.package);
const gen = vkgen.VkGenerateStep.create(b, vk_xml_path, "vk.zig");
triangle_exe.addPackage(gen.getPackage("vulkan"));
const shaders = vkgen.ShaderCompileStep.create(
b,
@@ -43,6 +43,6 @@ pub fn build(b: *Builder) void {
// This test needs to be an object so that vulkan-zig can import types from the root.
// It does not need to run anyway.
const ref_all_decls_test = b.addObject("ref-all-decls-test", "test/ref_all_decls.zig");
ref_all_decls_test.addPackage(gen.package);
ref_all_decls_test.addPackage(gen.getPackage("vulkan"));
test_step.dependOn(&ref_all_decls_test.step);
}

View File

@@ -15,18 +15,12 @@ pub const GenerateStep = struct {
/// The path to vk.xml
spec_path: []const u8,
/// The package representing the generated bindings. The generated bindings will be placed
/// in `package.path`. When using this step, this member should be passed to
/// `std.build.Builder.addPackage`, which causes the bindings to become available under the
/// name `vulkan`.
package: std.build.Pkg,
output_file: std.build.GeneratedFile,
generated_file: std.build.GeneratedFile,
/// 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
/// `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 create(builder: *Builder, spec_path: []const u8, out_path: []const u8) *GenerateStep {
const self = builder.allocator.create(GenerateStep) catch unreachable;
const full_out_path = path.join(builder.allocator, &[_][]const u8{
builder.build_root,
@@ -38,12 +32,7 @@ pub const GenerateStep = struct {
.step = Step.init(.custom, "vulkan-generate", builder.allocator, make),
.builder = builder,
.spec_path = spec_path,
.package = .{
.name = "vulkan",
.source = .{ .generated = &self.output_file },
.dependencies = null,
},
.output_file = .{
.generated_file = .{
.step = &self.step,
.path = full_out_path,
},
@@ -55,13 +44,23 @@ pub const GenerateStep = struct {
/// root. Typically, the location of the LunarG SDK root can be retrieved by querying for the VULKAN_SDK
/// environment variable, set by activating the environment setup script located in the SDK root.
/// `builder` and `out_path` are used in the same manner as `init`.
pub fn initFromSdk(builder: *Builder, sdk_path: []const u8, out_path: []const u8) *GenerateStep {
pub fn createFromSdk(builder: *Builder, sdk_path: []const u8, out_path: []const u8) *GenerateStep {
const spec_path = std.fs.path.join(
builder.allocator,
&[_][]const u8{ sdk_path, "share/vulkan/registry/vk.xml" },
) catch unreachable;
return init(builder, spec_path, out_path);
return create(builder, spec_path, out_path);
}
/// Returns the package with the generated budings, with name `package_name`.
pub fn getPackage(self: *GenerateStep, package_name: []const u8) std.build.Pkg {
return .{ .name = package_name, .source = self.getSource() };
}
/// Returns the file source for the generated bindings.
pub fn getSource(self: *GenerateStep) std.build.FileSource {
return .{ .generated = &self.generated_file };
}
/// Internal build function. This reads `vk.xml`, and passes it to `generate`, which then generates
@@ -102,8 +101,8 @@ pub const GenerateStep = struct {
var formatted = try tree.render(self.builder.allocator);
const dir = path.dirname(self.output_file.path.?).?;
const dir = path.dirname(self.generated_file.path.?).?;
try cwd.makePath(dir);
try cwd.writeFile(self.output_file.path.?, formatted);
try cwd.writeFile(self.generated_file.path.?, formatted);
}
};