Skip to content

Commit

Permalink
Merge pull request #150 from antlilja/lazy-path-compile-command
Browse files Browse the repository at this point in the history
Add ability to pass a LazyPath as compile command to the ShaderCompileStep
  • Loading branch information
Snektron authored Aug 8, 2024
2 parents 9f6e617 + c2e755d commit d7ad51c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
3 changes: 2 additions & 1 deletion examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand Down
33 changes: 27 additions & 6 deletions src/build_integration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -78,7 +91,12 @@ pub const ShaderCompileStep = struct {
/// system, `<compile_command...> <shader_source> <output_flag> <path>` 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 <path>`
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(.{
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down

0 comments on commit d7ad51c

Please sign in to comment.