authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-19 22:07:52-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:34-07:00
logb53e4e84bd5641d3e506f88481c92e1abafb1325
tree24b2c8850fd04a20e7181948182464d84674ec47
parent052aeee4150eba68051311982d883688670f6ca4

ScannedConfig: more general zon printing

it's almost all automated now

2 files changed, 77 insertions(+), 37 deletions(-)

lib/compiler/Maker/ScannedConfig.zig+75-37
......@@ -3,6 +3,7 @@ const ScannedConfig = @This();
33const std = @import("std");
44const Configuration = std.Build.Configuration;
55const Writer = std.Io.Writer;
6const Serializer = std.zon.Serializer;
67
78const Graph = @import("Graph.zig");
89
......@@ -10,8 +11,12 @@ configuration: Configuration,
1011top_level_steps: std.StringArrayHashMapUnmanaged(Configuration.Step.Index),
1112
1213pub fn print(sc: *const ScannedConfig, w: *Writer) Writer.Error!void {
14 std.log.err("TODO also print paths", .{});
15 std.log.err("TODO also print unlazy deps", .{});
16 std.log.err("TODO also print system integrations", .{});
17 std.log.err("TODO also print available options", .{});
1318 const c = &sc.configuration;
14 var serializer: std.zon.Serializer = .{ .writer = w };
19 var serializer: Serializer = .{ .writer = w };
1520 var s = try serializer.beginStruct(.{});
1621
1722 try s.field("default_step", @intFromEnum(c.default_step), .{});
......@@ -27,42 +32,7 @@ pub fn print(sc: *const ScannedConfig, w: *Writer) Writer.Error!void {
2732 var tf = try s.beginTupleField("steps", .{});
2833 for (c.steps) |*step| {
2934 var step_field = try tf.beginStructField(.{});
30 try step_field.field("name", step.name.slice(c), .{});
31 switch (step.owner) {
32 .root => try step_field.field("owner", .root, .{}),
33 _ => try step_field.field("owner", @intFromEnum(step.owner), .{}),
34 }
35 {
36 var deps_field = try step_field.beginTupleField("deps", .{});
37 for (step.deps.slice(c)) |dep| {
38 try deps_field.field(@intFromEnum(dep), .{});
39 }
40 try deps_field.end();
41 }
42 try step_field.field("max_rss", step.max_rss.toBytes(), .{});
43 switch (step.extended.get(c.extra)) {
44 .check_file => try step_field.field("check_file", .TODO, .{}),
45 .check_object => try step_field.field("check_object", .TODO, .{}),
46 .compile => try step_field.field("compile", .TODO, .{}),
47 .config_header => try step_field.field("config_header", .TODO, .{}),
48 .fail => try step_field.field("fail", .TODO, .{}),
49 .fmt => try step_field.field("fmt", .TODO, .{}),
50 .install_artifact => try step_field.field("install_artifact", .TODO, .{}),
51 .install_dir => try step_field.field("install_dir", .TODO, .{}),
52 .install_file => try step_field.field("install_file", .TODO, .{}),
53 .objcopy => try step_field.field("objcopy", .TODO, .{}),
54 .options => try step_field.field("options", .TODO, .{}),
55 .remove_dir => try step_field.field("remove_dir", .TODO, .{}),
56 .run => try step_field.field("run", .TODO, .{}),
57 .top_level => |top_level| {
58 var sf = try step_field.beginStructField("top_level", .{});
59 try sf.field("description", top_level.description.slice(c), .{});
60 try sf.end();
61 },
62 .translate_c => try step_field.field("translate_c", .TODO, .{}),
63 .update_source_files => try step_field.field("update_source_files", .TODO, .{}),
64 .write_file => try step_field.field("write_file", .TODO, .{}),
65 }
35 try printStruct(sc, &step_field, Configuration.Step, step);
6636 try step_field.end();
6737 }
6838 try tf.end();
......@@ -71,6 +41,74 @@ pub fn print(sc: *const ScannedConfig, w: *Writer) Writer.Error!void {
7141 try s.end();
7242}
7343
44fn printStruct(sc: *const ScannedConfig, s: *Serializer.Struct, comptime S: type, v: *const S) !void {
45 inline for (@typeInfo(S).@"struct".fields) |field| {
46 try s.fieldPrefix(field.name);
47 try printValue(sc, s.container.serializer, field.type, @field(v, field.name));
48 }
49}
50
51fn printValue(sc: *const ScannedConfig, s: *Serializer, comptime Field: type, field_value: Field) !void {
52 const c = &sc.configuration;
53 switch (Field) {
54 Configuration.String => {
55 try s.value(field_value.slice(c), .{});
56 },
57 Configuration.Deps => {
58 var deps_field = try s.beginTuple(.{});
59 for (field_value.slice(c)) |dep| {
60 try deps_field.field(@intFromEnum(dep), .{});
61 }
62 try deps_field.end();
63 },
64 Configuration.MaxRss => {
65 try s.value(field_value.toBytes(), .{});
66 },
67 else => switch (@typeInfo(Field)) {
68 .int => try s.int(field_value),
69 .@"enum" => {
70 if (@hasDecl(Field, "storage")) switch (Field.storage) {
71 .extended => {
72 var sub_struct = try s.beginStruct(.{});
73 try printTaggedUnion(sc, &sub_struct, field_value.get(c.extra));
74 try sub_struct.end();
75 },
76 .flag_optional => comptime unreachable,
77 } else if (std.enums.tagName(Field, field_value)) |name| {
78 try s.ident(name);
79 } else {
80 try s.int(@intFromEnum(field_value));
81 }
82 },
83 .@"struct" => |info| switch (info.layout) {
84 .@"packed" => {
85 try s.value(field_value, .{});
86 },
87 .auto => switch (Field.storage) {
88 .flag_optional => {
89 if (field_value.value) |some| {
90 try printValue(sc, s, Field.Value, some);
91 } else {
92 try s.value(null, .{});
93 }
94 },
95 .extended => @compileError("TODO"),
96 },
97 else => @compileError("not implemented: " ++ @typeName(Field)),
98 },
99 else => @compileError("not implemented: " ++ @typeName(Field)),
100 },
101 }
102}
103
104fn printTaggedUnion(sc: *const ScannedConfig, s: *Serializer.Struct, value: anytype) !void {
105 switch (value) {
106 inline else => |*u| {
107 try printStruct(sc, s, @TypeOf(u.*), u);
108 },
109 }
110}
111
74112pub fn printSteps(sc: *const ScannedConfig, graph: *Graph, w: *Writer) !void {
75113 const arena = graph.arena;
76114 const c = &sc.configuration;
lib/std/zig/Configuration.zig+2
......@@ -1663,6 +1663,8 @@ pub const Storage = enum {
16631663 return enum(u32) {
16641664 _,
16651665
1666 pub const storage: Storage = .extended;
1667
16661668 pub fn get(this: @This(), buffer: []const u32) U {
16671669 var i: usize = @intFromEnum(this);
16681670 const base_flags: BaseFlags = @bitCast(buffer[i]);