Proper depfile handling

This commit is contained in:
sergeypdev 2024-09-23 00:11:57 +04:00
parent 2373a47340
commit f0c9e04887
2 changed files with 36 additions and 5 deletions

View File

@ -213,8 +213,10 @@ fn buildAssets(b: *std.Build, install_assetc_step: *Step, step: *Step, assetc: *
run_assetc.addPathDir(b.pathFromRoot("libs/ispc_texcomp/lib"));
run_assetc.addArg("-d");
_ = run_assetc.addDepFileOutputArg("depfile.d");
if (std.mem.eql(u8, "prog", ext)) {
run_assetc.addArg("-d");
_ = run_assetc.addDepFileOutputArg("depfile.d");
}
// Absolute input file arg, this will add it to step deps, cache and all that good stuff
run_assetc.addFileArg(b.path(b.pathJoin(&.{ path, entry.path })));

View File

@ -571,15 +571,30 @@ fn processMesh(allocator: std.mem.Allocator, scene: *const c.aiScene, material_o
try buf_writer.flush();
}
const DEPFILE_MAX_BYTES = 1024 * 16;
fn readFileContents(allocator: std.mem.Allocator, path: []const u8) ![]u8 {
return std.fs.cwd().readFileAlloc(allocator, path, DEPFILE_MAX_BYTES) catch |err| switch (err) {
error.FileNotFound => {
return allocator.alloc(u8, 0);
},
else => |e| {
return e;
},
};
}
// Returns spirv binary source
// Caller owns memory
fn processShader(allocator: std.mem.Allocator, flags: []const []const u8, input: []const u8, dep_file: ?[]const u8) ![]u8 {
fn processShader(allocator: std.mem.Allocator, flags: []const []const u8, input: []const u8, maybe_dep_file: ?[]const u8) ![]u8 {
const old_depfile_contents = if (maybe_dep_file) |dep| try readFileContents(allocator, dep) else try allocator.alloc(u8, 0);
defer allocator.free(old_depfile_contents);
// TODO: make sure output is stdout
const result = try std.process.Child.run(.{
.allocator = allocator,
.argv = try std.mem.concat(allocator, []const u8, &.{
&.{ "glslc", "--target-env=vulkan1.3", "-std=460core", "-g", "-o", "-" },
if (dep_file) |dep| &.{ "-MD", "-MF", dep } else &.{},
if (maybe_dep_file) |dep| &.{ "-MD", "-MF", dep } else &.{},
flags,
&.{input},
}),
@ -590,7 +605,7 @@ fn processShader(allocator: std.mem.Allocator, flags: []const []const u8, input:
switch (result.term) {
.Exited => |status| {
if (status != 0) {
std.log.debug("Shader compilation failed with status {}:\n{s}\n", .{ result.term.Exited, result.stdout });
std.log.debug("Shader compilation failed with status {}:\n{s}\n", .{ result.term.Exited, result.stderr });
return error.ShaderCompileError;
}
},
@ -599,12 +614,26 @@ fn processShader(allocator: std.mem.Allocator, flags: []const []const u8, input:
},
}
// TODO: figure out a better way to handle depfile
if (maybe_dep_file) |dep_file| {
const file = try std.fs.cwd().openFile(dep_file, .{ .mode = .read_write });
defer file.close();
try file.seekFromEnd(0);
try file.writeAll(old_depfile_contents);
}
return result.stdout;
}
fn processShaderProgram(allocator: std.mem.Allocator, input: []const u8, output_dir: std.fs.Dir, dep_file: ?[]const u8, asset_list_writer: anytype) !void {
const input_dir = std.fs.path.dirname(input).?;
// Recreate file in case it's not empty
if (dep_file) |dep| {
const file = try std.fs.cwd().createFile(dep, .{});
defer file.close();
}
var file_contents: []u8 = undefined;
{
const input_file = try std.fs.cwd().openFile(input, .{});