From c294b849d2a3b968cd5daf5f435220d69fbbfcaa Mon Sep 17 00:00:00 2001 From: InKryption Date: Thu, 16 Feb 2023 02:56:15 +0100 Subject: [PATCH] make build.zig friendly to the zig package manager --- .github/workflows/build.yml | 2 +- build.zig | 31 ++++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 3778975..bb2f5a4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -37,7 +37,7 @@ jobs: - name: Build with latest zig & vk.xml run: | - zig build -Dvulkan-registry=./vk.xml + zig build -Dexample-registry=./vk.xml - name: Archive vk.xml uses: actions/upload-artifact@v2 diff --git a/build.zig b/build.zig index de6a0cb..fdf5a2e 100644 --- a/build.zig +++ b/build.zig @@ -5,15 +5,36 @@ const Step = std.build.Step; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const vk_xml_path: ?[]const u8 = b.option([]const u8, "registry", "Override the path to the Vulkan registry"); + // using the package manager, this artifact can be obtained by the user + // through `b.dependency(, .{}).artifact("generator")`. + // with that, the user need only `.addArg("path/to/vk.xml")`, and then obtain + // a file source to the generated code with `.addOutputArg("vk.zig")` const generator_exe = b.addExecutable(.{ - .name = "vulkan-zig-generator", + .name = "generator", .root_source_file = .{ .path = "generator/main.zig" }, .target = target, .optimize = optimize, }); generator_exe.install(); + // or they can skip all that, and just make sure to pass `.registry = "path/to/vk.xml"` to `b.dependency`, + // and then obtain the module directly via `.module("vulkan-zig")`. + if (vk_xml_path) |path| { + const generate_cmd = b.addRunArtifact(generator_exe); + + if (!std.fs.path.isAbsolute(path)) @panic("Make sure to assign an absolute path to the `registry` option (see: std.Build.pathFromRoot).\n"); + generate_cmd.addArg(path); + + b.addModule(.{ + .name = "vulkan-zig", + .source_file = generate_cmd.addOutputFileArg("vk.zig"), + }); + } + + // remaindure of the script is for local testing + const triangle_exe = b.addExecutable(.{ .name = "triangle", .root_source_file = .{ .path = "examples/triangle.zig" }, @@ -24,9 +45,8 @@ pub fn build(b: *std.Build) void { triangle_exe.linkLibC(); triangle_exe.linkSystemLibrary("glfw"); - 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.create(b, vk_xml_path, "vk.zig"); + const example_registry = b.option([]const u8, "example-registry", "Override the path to the Vulkan registry") orelse "examples/vk.xml"; + const gen = vkgen.VkGenerateStep.create(b, example_registry, "vk.zig"); triangle_exe.addModule("vulkan", gen.getModule()); const shaders = vkgen.ShaderCompileStep.create( @@ -39,8 +59,9 @@ pub fn build(b: *std.Build) void { triangle_exe.addModule("shaders", shaders.getModule()); const triangle_run_cmd = b.addRunArtifact(triangle_exe); - triangle_run_cmd.condition = .always; triangle_run_cmd.step.dependOn(b.getInstallStep()); + triangle_run_cmd.condition = .always; + const triangle_run_step = b.step("run-triangle", "Run the triangle example"); triangle_run_step.dependOn(&triangle_run_cmd.step);