authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-08 13:18:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:35-07:00
logd7eab060db6aff326a957e4dbffcd09263f7ffbd
tree6c09212927ff8b7558f9f5ca5b79d81b30a018c0
parentf9f00c2dee151231361df7aa45a71ca33f90dcd1

configurer: serialize Step.UpdateSourceFiles


5 files changed, 63 insertions(+), 65 deletions(-)

lib/compiler/Maker/Step/UpdateSourceFiles.zig+4-4
......@@ -35,7 +35,7 @@ pub fn make(
3535 for (conf_usf.embeds.slice) |*embed| {
3636 const dest_path: Path = .{
3737 .root_dir = build_root,
38 .sub_path = embed.dest_path.slice(conf),
38 .sub_path = embed.sub_path.slice(conf),
3939 };
4040 if (Io.Dir.path.dirname(dest_path.sub_path)) |dirname| {
4141 const dirname_path: Path = .{
......@@ -47,7 +47,7 @@ pub fn make(
4747 }
4848 dest_path.root_dir.handle.writeFile(io, .{
4949 .sub_path = dest_path.sub_path,
50 .data = embed.bytes.slice(conf),
50 .data = embed.contents.slice(conf),
5151 }) catch |err| return step.fail(maker, "failed to write file {f}: {t}", .{ dest_path, err });
5252 any_miss = true;
5353 progress_node.completeOne();
......@@ -56,7 +56,7 @@ pub fn make(
5656 for (conf_usf.copies.slice) |*copy| {
5757 const dest_path: Path = .{
5858 .root_dir = build_root,
59 .sub_path = copy.dest_path.slice(conf),
59 .sub_path = copy.sub_path.slice(conf),
6060 };
6161 if (Io.Dir.path.dirname(dest_path.sub_path)) |dirname| {
6262 const dirname_path: Path = .{
......@@ -66,7 +66,7 @@ pub fn make(
6666 dirname_path.root_dir.handle.createDirPath(io, dirname_path.sub_path) catch |err|
6767 return step.fail(maker, "failed to create path {f}: {t}", .{ dirname_path, err });
6868 }
69 const src_lazy_path = copy.src_path.get(conf);
69 const src_lazy_path = copy.src_file.get(conf);
7070 const source_path = try maker.resolveLazyPath(arena, src_lazy_path, step_index);
7171 if (!step.inputs.populated()) try step.addWatchInput(maker, arena, src_lazy_path);
7272
lib/compiler/configurer.zig+22-9
......@@ -464,6 +464,15 @@ const Serialize = struct {
464464 return result;
465465 }
466466
467 fn initCopyList(s: *Serialize, list: []const Step.WriteFile.Copy) ![]const Configuration.Step.WriteFile.Copy {
468 const result = try s.arena.alloc(Configuration.Step.WriteFile.Copy, list.len);
469 for (result, list) |*dest, src| dest.* = .{
470 .sub_path = src.sub_path,
471 .src_file = try s.addLazyPath(src.src_file),
472 };
473 return result;
474 }
475
467476 fn initOptionalStringList(s: *Serialize, list: []const ?[]const u8) ![]const Configuration.OptionalString {
468477 const wc = s.wc;
469478 const result = try s.arena.alloc(Configuration.OptionalString, list.len);
......@@ -905,12 +914,6 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
905914 .write_file => e: {
906915 const wf: *Step.WriteFile = @fieldParentPtr("step", step);
907916
908 const copies = try arena.alloc(Configuration.Step.WriteFile.Copy, wf.copies.items.len);
909 for (copies, wf.copies.items) |*dest, src| dest.* = .{
910 .sub_path = src.sub_path,
911 .src_file = try s.addLazyPath(src.src_file),
912 };
913
914917 const directories = try arena.alloc(
915918 Configuration.Step.WriteFile.Directory,
916919 wf.directories.items.len,
......@@ -925,7 +928,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
925928 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.WriteFile, .{
926929 .flags = .{
927930 .embeds = wf.embeds.items.len != 0,
928 .copies = copies.len != 0,
931 .copies = wf.copies.items.len != 0,
929932 .directories = directories.len != 0,
930933 .mode = switch (wf.mode) {
931934 .whole_cached => .whole_cached,
......@@ -935,7 +938,7 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
935938 },
936939 .generated_directory = wf.generated_directory,
937940 .embeds = .{ .slice = wf.embeds.items },
938 .copies = .{ .slice = copies },
941 .copies = .{ .slice = try s.initCopyList(wf.copies.items) },
939942 .directories = .{ .slice = directories },
940943 .mutate_path = .{ .value = switch (wf.mode) {
941944 .mutate => |lp| try s.addLazyPath(lp),
......@@ -943,7 +946,17 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
943946 } },
944947 })));
945948 },
946 .update_source_files => @panic("TODO"),
949 .update_source_files => e: {
950 const usf: *Step.UpdateSourceFiles = @fieldParentPtr("step", step);
951 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.UpdateSourceFiles, .{
952 .flags = .{
953 .embeds = usf.embeds.items.len != 0,
954 .copies = usf.copies.items.len != 0,
955 },
956 .embeds = .{ .slice = usf.embeds.items },
957 .copies = .{ .slice = try s.initCopyList(usf.copies.items) },
958 })));
959 },
947960 .run => e: {
948961 const run: *Step.Run = @fieldParentPtr("step", step);
949962 var expect_stderr_exact: ?Configuration.Bytes = null;
lib/std/Build/Configuration.zig+2-11
......@@ -1229,17 +1229,8 @@ pub const Step = extern struct {
12291229 embeds: Storage.FlagLengthPrefixedList(.flags, .embeds, Embed),
12301230 copies: Storage.FlagLengthPrefixedList(.flags, .copies, Copy),
12311231
1232 pub const Embed = extern struct {
1233 /// Relative to build root.
1234 dest_path: String,
1235 bytes: Bytes,
1236 };
1237
1238 pub const Copy = extern struct {
1239 /// Relative to build root.
1240 dest_path: String,
1241 src_path: LazyPath.Index,
1242 };
1232 pub const Embed = WriteFile.Embed;
1233 pub const Copy = WriteFile.Copy;
12431234
12441235 pub const Flags = packed struct(u32) {
12451236 tag: Tag = .update_source_files,
lib/std/Build/Step/UpdateSourceFiles.zig+35-36
......@@ -6,64 +6,63 @@
66const UpdateSourceFiles = @This();
77
88const std = @import("std");
9const Io = std.Io;
109const Step = std.Build.Step;
11const fs = std.fs;
12const ArrayList = std.ArrayList;
10const Configuration = std.Build.Configuration;
1311
1412step: Step,
15output_source_files: std.ArrayList(OutputSourceFile),
13embeds: std.ArrayList(Embed) = .empty,
14copies: std.ArrayList(Copy) = .empty,
1615
1716pub const base_tag: Step.Tag = .update_source_files;
1817
19pub const OutputSourceFile = struct {
20 contents: Contents,
21 sub_path: []const u8,
22};
23
24pub const Contents = union(enum) {
25 bytes: []const u8,
26 copy: std.Build.LazyPath,
27};
18pub const Embed = Step.WriteFile.Embed;
19pub const Copy = Step.WriteFile.Copy;
2820
2921pub fn create(owner: *std.Build) *UpdateSourceFiles {
30 const usf = owner.allocator.create(UpdateSourceFiles) catch @panic("OOM");
22 const graph = owner.graph;
23 const usf = graph.create(UpdateSourceFiles);
3124 usf.* = .{
3225 .step = .init(.{
3326 .tag = base_tag,
3427 .name = "UpdateSourceFiles",
3528 .owner = owner,
3629 }),
37 .output_source_files = .empty,
3830 };
3931 return usf;
4032}
4133
42/// A path relative to the package root.
34/// Overwrites a path relative to the build root with the contents of another file.
4335///
44/// Be careful with this because it updates source files. This should not be
45/// used as part of the normal build process, but as a utility occasionally
46/// run by a developer with intent to modify source files and then commit
47/// those changes to version control.
48pub fn addCopyFileToSource(usf: *UpdateSourceFiles, source: std.Build.LazyPath, sub_path: []const u8) void {
49 const b = usf.step.owner;
50 usf.output_source_files.append(b.allocator, .{
51 .contents = .{ .copy = source },
52 .sub_path = sub_path,
36/// Because it updates source files, this should not be used as part of the
37/// normal build process, but as a utility occasionally run by a developer with
38/// intent to modify source files and then commit those changes to version
39/// control.
40pub fn addCopyFileToSource(usf: *UpdateSourceFiles, src_file: std.Build.LazyPath, sub_path: []const u8) void {
41 const graph = usf.step.owner.graph;
42 const wc = &graph.wip_configuration;
43 const arena = graph.arena;
44
45 usf.copies.append(arena, .{
46 .sub_path = wc.addString(sub_path) catch @panic("OOM"),
47 .src_file = src_file.dupe(graph),
5348 }) catch @panic("OOM");
54 source.addStepDependencies(&usf.step);
49
50 src_file.addStepDependencies(&usf.step);
5551}
5652
57/// A path relative to the package root.
53/// Overwrites a path relative to the package root with the provided bytes.
5854///
59/// Be careful with this because it updates source files. This should not be
60/// used as part of the normal build process, but as a utility occasionally
61/// run by a developer with intent to modify source files and then commit
62/// those changes to version control.
63pub fn addBytesToSource(usf: *UpdateSourceFiles, bytes: []const u8, sub_path: []const u8) void {
64 const b = usf.step.owner;
65 usf.output_source_files.append(b.allocator, .{
66 .contents = .{ .bytes = bytes },
67 .sub_path = sub_path,
55/// Because it updates source files, this should not be used as part of the
56/// normal build process, but as a utility occasionally run by a developer with
57/// intent to modify source files and then commit those changes to version
58/// control.
59pub fn addBytesToSource(usf: *UpdateSourceFiles, contents: []const u8, sub_path: []const u8) void {
60 const graph = usf.step.owner.graph;
61 const wc = &graph.wip_configuration;
62 const arena = graph.arena;
63
64 usf.embeds.append(arena, .{
65 .sub_path = wc.addString(sub_path) catch @panic("OOM"),
66 .contents = wc.addBytes(contents) catch @panic("OOM"),
6867 }) catch @panic("OOM");
6968}
lib/std/Build/Step/WriteFile.zig-5
......@@ -4,15 +4,10 @@
44const WriteFile = @This();
55
66const std = @import("std");
7const Io = std.Io;
8const Dir = std.Io.Dir;
97const Step = std.Build.Step;
10const ArrayList = std.ArrayList;
11const assert = std.debug.assert;
128const Configuration = std.Build.Configuration;
139
1410step: Step,
15
1611embeds: std.ArrayList(Embed) = .empty,
1712copies: std.ArrayList(Copy) = .empty,
1813directories: std.ArrayList(Directory) = .empty,