step: Step,
files: std.ArrayListUnmanaged(*File),
output_source_files: std.ArrayListUnmanaged(OutputSourceFile),
generated_directory: std.Build.GeneratedFile,
pub const base_id = .write_file;
pub const File = struct {
generated_file: std.Build.GeneratedFile,
sub_path: []const u8,
contents: Contents,
};
pub const OutputSourceFile = struct {
contents: Contents,
sub_path: []const u8,
};
pub const Contents = union(enum) {
bytes: []const u8,
copy: std.Build.FileSource,
};
pub fn create(owner: *std.Build) *WriteFileStep {
const wf = owner.allocator.create(WriteFileStep) catch @panic("OOM");
wf.* = .{
.step = Step.init(.{
.id = .write_file,
.name = "WriteFile",
.owner = owner,
.makeFn = make,
}),
.files = .{},
.output_source_files = .{},
.generated_directory = .{ .step = &wf.step },
};
return wf;
}
pub fn add(wf: *WriteFileStep, sub_path: []const u8, bytes: []const u8) void {
const b = wf.step.owner;
const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM");
file.* = .{
.generated_file = .{ .step = &wf.step },
.sub_path = b.dupePath(sub_path),
.contents = .{ .bytes = b.dupe(bytes) },
};
wf.files.append(gpa, file) catch @panic("OOM");
wf.maybeUpdateName();
}
pub fn addCopyFile(wf: *WriteFileStep, source: std.Build.FileSource, sub_path: []const u8) void {
const b = wf.step.owner;
const gpa = b.allocator;
const file = gpa.create(File) catch @panic("OOM");
file.* = .{
.generated_file = .{ .step = &wf.step },
.sub_path = b.dupePath(sub_path),
.contents = .{ .copy = source },
};
wf.files.append(gpa, file) catch @panic("OOM");
wf.maybeUpdateName();
source.addStepDependencies(&wf.step);
}
pub fn addCopyFileToSource(wf: *WriteFileStep, source: std.Build.FileSource, sub_path: []const u8) void {
const b = wf.step.owner;
wf.output_source_files.append(b.allocator, .{
.contents = .{ .copy = source },
.sub_path = sub_path,
}) catch @panic("OOM");
source.addStepDependencies(&wf.step);
}
pub fn addBytesToSource(wf: *WriteFileStep, bytes: []const u8, sub_path: []const u8) void {
const b = wf.step.owner;
wf.output_source_files.append(b.allocator, .{
.contents = .{ .bytes = bytes },
.sub_path = sub_path,
}) catch @panic("OOM");
}
pub fn getFileSource(wf: *WriteFileStep, sub_path: []const u8) ?std.Build.FileSource {
for (wf.files.items) |file| {
if (std.mem.eql(u8, file.sub_path, sub_path)) {
return .{ .generated = &file.generated_file };
}
}
return null;
}
pub fn getDirectorySource(wf: *WriteFileStep) std.Build.FileSource {
return .{ .generated = &wf.generated_directory };
}
fn maybeUpdateName(wf: *WriteFileStep) void {
if (wf.files.items.len == 1) {
if (std.mem.eql(u8, wf.step.name, "WriteFile")) {
wf.step.name = wf.step.owner.fmt("WriteFile {s}", .{wf.files.items[0].sub_path});
}
}
}
fn make(step: *Step, prog_node: *std.Progress.Node) !void {
_ = prog_node;
const b = step.owner;
const wf = @fieldParentPtr(WriteFileStep, "step", step);
var any_miss = false;
for (wf.output_source_files.items) |output_source_file| {
if (fs.path.dirname(output_source_file.sub_path)) |dirname| {
b.build_root.handle.makePath(dirname) catch |err| {
return step.fail("unable to make path '{}{s}': {s}", .{
b.build_root, dirname, @errorName(err),
});
};
}
switch (output_source_file.contents) {
.bytes => |bytes| {
b.build_root.handle.writeFile(output_source_file.sub_path, bytes) catch |err| {
return step.fail("unable to write file '{}{s}': {s}", .{
b.build_root, output_source_file.sub_path, @errorName(err),
});
};
any_miss = true;
},
.copy => |file_source| {
const source_path = file_source.getPath(b);
const prev_status = fs.Dir.updateFile(
fs.cwd(),
source_path,
b.build_root.handle,
output_source_file.sub_path,
.{},
) catch |err| {
return step.fail("unable to update file from '{s}' to '{}{s}': {s}", .{
source_path, b.build_root, output_source_file.sub_path, @errorName(err),
});
};
any_miss = any_miss or prev_status == .stale;
},
}
}
var man = b.cache.obtain();
defer man.deinit();
man.hash.add(@as(u32, 0xd767ee59));
for (wf.files.items) |file| {
man.hash.addBytes(file.sub_path);
switch (file.contents) {
.bytes => |bytes| {
man.hash.addBytes(bytes);
},
.copy => |file_source| {
_ = try man.addFile(file_source.getPath(b), null);
},
}
}
if (try step.cacheHit(&man)) {
const digest = man.final();
for (wf.files.items) |file| {
file.generated_file.path = try b.cache_root.join(b.allocator, &.{
"o", &digest, file.sub_path,
});
}
wf.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });
return;
}
const digest = man.final();
const cache_path = "o" ++ fs.path.sep_str ++ digest;
wf.generated_directory.path = try b.cache_root.join(b.allocator, &.{ "o", &digest });
var cache_dir = b.cache_root.handle.makeOpenPath(cache_path, .{}) catch |err| {
return step.fail("unable to make path '{}{s}': {s}", .{
b.cache_root, cache_path, @errorName(err),
});
};
defer cache_dir.close();
for (wf.files.items) |file| {
if (fs.path.dirname(file.sub_path)) |dirname| {
cache_dir.makePath(dirname) catch |err| {
return step.fail("unable to make path '{}{s}{c}{s}': {s}", .{
b.cache_root, cache_path, fs.path.sep, dirname, @errorName(err),
});
};
}
switch (file.contents) {
.bytes => |bytes| {
cache_dir.writeFile(file.sub_path, bytes) catch |err| {
return step.fail("unable to write file '{}{s}{c}{s}': {s}", .{
b.cache_root, cache_path, fs.path.sep, file.sub_path, @errorName(err),
});
};
},
.copy => |file_source| {
const source_path = file_source.getPath(b);
const prev_status = fs.Dir.updateFile(
fs.cwd(),
source_path,
cache_dir,
file.sub_path,
.{},
) catch |err| {
return step.fail("unable to update file from '{s}' to '{}{s}{c}{s}': {s}", .{
source_path,
b.cache_root,
cache_path,
fs.path.sep,
file.sub_path,
@errorName(err),
});
};
_ = prev_status;
},
}
file.generated_file.path = try b.cache_root.join(b.allocator, &.{
cache_path, file.sub_path,
});
}
try step.writeManifest(&man);
}
const std = @import("../std.zig");
const Step = std.Build.Step;
const fs = std.fs;
const ArrayList = std.ArrayList;
const WriteFileStep = @This();