aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Build/Step/UpdateSourceFiles.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2024-07-09 21:47:26 -0700
committerAndrew Kelley <andrew@ziglang.org>2024-07-12 00:14:08 -0700
commitd1c14f2f52ddec476eca6d605b985a27f4d4fe28 (patch)
tree34368ed797390d5e92a2b2125da1a3c1920aaebb /lib/std/Build/Step/UpdateSourceFiles.zig
parent0cc492a272ef9c03a34b57a26bf570b242615ddf (diff)
downloadzig-d1c14f2f52ddec476eca6d605b985a27f4d4fe28.tar.gz
zig-d1c14f2f52ddec476eca6d605b985a27f4d4fe28.zip
std.Build.Step.WriteFile: extract UpdateSourceFiles
This has been planned for quite some time; this commit finally does it. Also implements file system watching integration in the make() implementation for UpdateSourceFiles and fixes the reporting of step caching for both. WriteFile does not yet have file system watching integration.
Diffstat (limited to 'lib/std/Build/Step/UpdateSourceFiles.zig')
-rw-r--r--lib/std/Build/Step/UpdateSourceFiles.zig114
1 files changed, 114 insertions, 0 deletions
diff --git a/lib/std/Build/Step/UpdateSourceFiles.zig b/lib/std/Build/Step/UpdateSourceFiles.zig
new file mode 100644
index 0000000000..9d1c8e20fe
--- /dev/null
+++ b/lib/std/Build/Step/UpdateSourceFiles.zig
@@ -0,0 +1,114 @@
+//! Writes data to paths relative to the package root, effectively mutating the
+//! package's source files. Be careful with the latter functionality; it should
+//! not be used during the normal build process, but as a utility run by a
+//! developer with intention to update source files, which will then be
+//! committed to version control.
+const std = @import("std");
+const Step = std.Build.Step;
+const fs = std.fs;
+const ArrayList = std.ArrayList;
+const UpdateSourceFiles = @This();
+
+step: Step,
+output_source_files: std.ArrayListUnmanaged(OutputSourceFile),
+
+pub const base_id: Step.Id = .update_source_files;
+
+pub const OutputSourceFile = struct {
+ contents: Contents,
+ sub_path: []const u8,
+};
+
+pub const Contents = union(enum) {
+ bytes: []const u8,
+ copy: std.Build.LazyPath,
+};
+
+pub fn create(owner: *std.Build) *UpdateSourceFiles {
+ const usf = owner.allocator.create(UpdateSourceFiles) catch @panic("OOM");
+ usf.* = .{
+ .step = Step.init(.{
+ .id = base_id,
+ .name = "UpdateSourceFiles",
+ .owner = owner,
+ .makeFn = make,
+ }),
+ .output_source_files = .{},
+ };
+ return usf;
+}
+
+/// A path relative to the package root.
+///
+/// Be careful with this because it updates source files. This should not be
+/// used as part of the normal build process, but as a utility occasionally
+/// run by a developer with intent to modify source files and then commit
+/// those changes to version control.
+pub fn addCopyFileToSource(usf: *UpdateSourceFiles, source: std.Build.LazyPath, sub_path: []const u8) void {
+ const b = usf.step.owner;
+ usf.output_source_files.append(b.allocator, .{
+ .contents = .{ .copy = source },
+ .sub_path = sub_path,
+ }) catch @panic("OOM");
+ source.addStepDependencies(&usf.step);
+}
+
+/// A path relative to the package root.
+///
+/// Be careful with this because it updates source files. This should not be
+/// used as part of the normal build process, but as a utility occasionally
+/// run by a developer with intent to modify source files and then commit
+/// those changes to version control.
+pub fn addBytesToSource(usf: *UpdateSourceFiles, bytes: []const u8, sub_path: []const u8) void {
+ const b = usf.step.owner;
+ usf.output_source_files.append(b.allocator, .{
+ .contents = .{ .bytes = bytes },
+ .sub_path = sub_path,
+ }) catch @panic("OOM");
+}
+
+fn make(step: *Step, prog_node: std.Progress.Node) !void {
+ _ = prog_node;
+ const b = step.owner;
+ const usf: *UpdateSourceFiles = @fieldParentPtr("step", step);
+
+ var any_miss = false;
+ for (usf.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(.{ .sub_path = output_source_file.sub_path, .data = 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| {
+ if (!step.inputs.populated()) try step.addWatchInput(file_source);
+
+ const source_path = file_source.getPath2(b, step);
+ 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;
+ },
+ }
+ }
+
+ step.result_cached = !any_miss;
+}