From c2e755d93424aa6e0220862b6d1d316da99f5363 Mon Sep 17 00:00:00 2001 From: antlilja Date: Tue, 9 Jul 2024 12:30:18 +0200 Subject: [PATCH] Add ability to pass a LazyPath to ShaderCompileStep Allows for executables generated by the build system to be used as shader compilers. --- examples/build.zig | 3 ++- src/build_integration.zig | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/examples/build.zig b/examples/build.zig index ea92c67..bc9b454 100644 --- a/examples/build.zig +++ b/examples/build.zig @@ -35,7 +35,8 @@ pub fn build(b: *std.Build) void { const shaders = ShaderCompileStep.create( b, - &[_][]const u8{ "glslc", "--target-env=vulkan1.2" }, + .{ .real_path = "glslc" }, + &[_][]const u8{"--target-env=vulkan1.2"}, "-o", ); shaders.add("triangle_vert", "shaders/triangle.vert", .{}); diff --git a/src/build_integration.zig b/src/build_integration.zig index 35f5bb4..af07793 100644 --- a/src/build_integration.zig +++ b/src/build_integration.zig @@ -59,10 +59,23 @@ pub const ShaderCompileStep = struct { options: ShaderOptions, }; + const CompileCommand = union(enum) { + real_path: []const u8, + lazy_path: Build.LazyPath, + + pub fn getPath(self: CompileCommand, b: *Build) []const u8 { + return switch (self) { + .real_path => |path| path, + .lazy_path => |lazy_path| lazy_path.getPath(b), + }; + } + }; + step: Build.Step, - /// The command and optional arguments used to invoke the shader compiler. - compile_command: []const []const u8, + /// The command and args to invoke the shader compiler. + compile_command: CompileCommand, + compile_command_args: []const []const u8, /// The compiler flag used to specify the output path, `-o` most of the time output_flag: []u8, @@ -78,7 +91,12 @@ pub const ShaderCompileStep = struct { /// system, ` ` is invoked for each shader. /// For example, if one calls this with `create(b, "glslc", "-o")` and then /// `c.addShader("vertex", "vertex.glsl", .{})`, the command will be `glslc vertex.glsl -o ` - pub fn create(builder: *Build, compile_command: []const []const u8, output_flag: []const u8) *ShaderCompileStep { + pub fn create( + builder: *Build, + compile_command: CompileCommand, + compile_command_args: []const []const u8, + output_flag: []const u8, + ) *ShaderCompileStep { const self = builder.allocator.create(ShaderCompileStep) catch unreachable; self.* = .{ .step = Build.Step.init(.{ @@ -87,7 +105,8 @@ pub const ShaderCompileStep = struct { .owner = builder, .makeFn = make, }), - .compile_command = builder.dupeStrings(compile_command), + .compile_command = compile_command, + .compile_command_args = builder.dupeStrings(compile_command_args), .output_flag = builder.dupe(output_flag), .shaders = std.ArrayList(Shader).init(builder.allocator), .generated_file = undefined, @@ -148,7 +167,8 @@ pub const ShaderCompileStep = struct { // the compilation options must be the same as well! try shader.options.hash(b, &hasher); // And the compile command, too. - for (self.compile_command) |cmd| { + hasher.update(self.compile_command.getPath(b)); + for (self.compile_command_args) |cmd| { hasher.update(cmd); } @@ -172,7 +192,8 @@ pub const ShaderCompileStep = struct { const cwd = std.fs.cwd(); var cmd = std.ArrayList([]const u8).init(b.allocator); - try cmd.appendSlice(self.compile_command); + try cmd.append(self.compile_command.getPath(b)); + try cmd.appendSlice(self.compile_command_args); const base_cmd_len = cmd.items.len; var shaders_file_contents = std.ArrayList(u8).init(b.allocator);