authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-08 17:11:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:35-07:00
log42be6c0088ece7f3df2a30cfbaee0d77151f762a
treefc008bace76baade6b3bf0910e1fcc108fa71542
parent315d6ee59b42c07ac0a4e31924ce229d7c5afc32

configurer: serialize Step.TranslateC


3 files changed, 64 insertions(+), 25 deletions(-)

lib/compiler/configurer.zig+42-15
...@@ -451,6 +451,24 @@ const Serialize = struct {...@@ -451,6 +451,24 @@ const Serialize = struct {
451 return result;451 return result;
452 }452 }
453453
454 fn initIncludeDirList(
455 s: *Serialize,
456 list: []const std.Build.Module.IncludeDir,
457 ) ![]const Configuration.Module.IncludeDir {
458 const result = try s.arena.alloc(Configuration.Module.IncludeDir, list.len);
459 for (result, list) |*dest, src| dest.* = switch (src) {
460 .path => |lp| .{ .path = try addLazyPath(s, lp) },
461 .path_system => |lp| .{ .path_system = try addLazyPath(s, lp) },
462 .path_after => |lp| .{ .path_after = try addLazyPath(s, lp) },
463 .framework_path => |lp| .{ .framework_path = try addLazyPath(s, lp) },
464 .framework_path_system => |lp| .{ .framework_path_system = try addLazyPath(s, lp) },
465 .embed_path => |lp| .{ .embed_path = try addLazyPath(s, lp) },
466 .other_step => |cs| .{ .other_step = stepIndex(s, &cs.step) },
467 .config_header_step => |chs| .{ .config_header_step = stepIndex(s, &chs.step) },
468 };
469 return result;
470 }
471
454 fn initLazyPathList(s: *Serialize, list: []const std.Build.LazyPath) ![]const Configuration.LazyPath.Index {472 fn initLazyPathList(s: *Serialize, list: []const std.Build.LazyPath) ![]const Configuration.LazyPath.Index {
455 const result = try s.arena.alloc(Configuration.LazyPath.Index, list.len);473 const result = try s.arena.alloc(Configuration.LazyPath.Index, list.len);
456 for (result, list) |*dest, src| dest.* = try addLazyPath(s, src);474 for (result, list) |*dest, src| dest.* = try addLazyPath(s, src);
...@@ -486,18 +504,6 @@ const Serialize = struct {...@@ -486,18 +504,6 @@ const Serialize = struct {
486 const wc = s.wc;504 const wc = s.wc;
487 const arena = s.arena;505 const arena = s.arena;
488506
489 const include_dirs = try arena.alloc(Configuration.Module.IncludeDir, m.include_dirs.items.len);
490 for (include_dirs, m.include_dirs.items) |*dest, src| dest.* = switch (src) {
491 .path => |lp| .{ .path = try addLazyPath(s, lp) },
492 .path_system => |lp| .{ .path_system = try addLazyPath(s, lp) },
493 .path_after => |lp| .{ .path_after = try addLazyPath(s, lp) },
494 .framework_path => |lp| .{ .framework_path = try addLazyPath(s, lp) },
495 .framework_path_system => |lp| .{ .framework_path_system = try addLazyPath(s, lp) },
496 .embed_path => |lp| .{ .embed_path = try addLazyPath(s, lp) },
497 .other_step => |cs| .{ .other_step = stepIndex(s, &cs.step) },
498 .config_header_step => |chs| .{ .config_header_step = stepIndex(s, &chs.step) },
499 };
500
501 const rpaths = try arena.alloc(Configuration.Module.RPath, m.rpaths.items.len);507 const rpaths = try arena.alloc(Configuration.Module.RPath, m.rpaths.items.len);
502 for (rpaths, m.rpaths.items) |*dest, src| dest.* = switch (src) {508 for (rpaths, m.rpaths.items) |*dest, src| dest.* = switch (src) {
503 .lazy_path => |lp| .{ .lazy_path = try addLazyPath(s, lp) },509 .lazy_path => |lp| .{ .lazy_path = try addLazyPath(s, lp) },
...@@ -542,7 +548,7 @@ const Serialize = struct {...@@ -542,7 +548,7 @@ const Serialize = struct {
542 .fuzz = .init(m.fuzz),548 .fuzz = .init(m.fuzz),
543 .code_model = m.code_model,549 .code_model = m.code_model,
544 .c_macros = c_macros.len != 0,550 .c_macros = c_macros.len != 0,
545 .include_dirs = include_dirs.len != 0,551 .include_dirs = m.include_dirs.items.len != 0,
546 .lib_paths = lib_paths.len != 0,552 .lib_paths = lib_paths.len != 0,
547 .rpaths = rpaths.len != 0,553 .rpaths = rpaths.len != 0,
548 .frameworks = frameworks.len != 0,554 .frameworks = frameworks.len != 0,
...@@ -566,7 +572,7 @@ const Serialize = struct {...@@ -566,7 +572,7 @@ const Serialize = struct {
566 .c_macros = .{ .slice = c_macros },572 .c_macros = .{ .slice = c_macros },
567 .lib_paths = .{ .slice = lib_paths },573 .lib_paths = .{ .slice = lib_paths },
568 .export_symbol_names = .{ .slice = export_symbol_names },574 .export_symbol_names = .{ .slice = export_symbol_names },
569 .include_dirs = .init(include_dirs),575 .include_dirs = .init(try s.initIncludeDirList(m.include_dirs.items)),
570 .rpaths = .init(rpaths),576 .rpaths = .init(rpaths),
571 .link_objects = .init(link_objects),577 .link_objects = .init(link_objects),
572 .frameworks = .{ .slice = frameworks },578 .frameworks = .{ .slice = frameworks },
...@@ -910,7 +916,28 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {...@@ -910,7 +916,28 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
910 .exclude_paths = .{ .slice = try s.initLazyPathList(sf.exclude_paths) },916 .exclude_paths = .{ .slice = try s.initLazyPathList(sf.exclude_paths) },
911 })));917 })));
912 },918 },
913 .translate_c => @panic("TODO"),919 .translate_c => e: {
920 const tc: *Step.TranslateC = @fieldParentPtr("step", step);
921
922 const system_libs = try arena.alloc(Configuration.SystemLib.Index, tc.system_libs.items.len);
923 for (system_libs, tc.system_libs.items) |*dest, *src| dest.* = try s.addSystemLib(src);
924
925 break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.TranslateC, .{
926 .flags = .{
927 .include_dirs = tc.include_dirs.items.len != 0,
928 .system_libs = system_libs.len != 0,
929 .c_macros = tc.c_macros.items.len != 0,
930 .link_libc = tc.link_libc,
931 .optimize = .init(tc.optimize),
932 },
933 .src_path = try s.addLazyPath(tc.source),
934 .output_file = tc.output_file,
935 .include_dirs = .init(try s.initIncludeDirList(tc.include_dirs.items)),
936 .system_libs = .{ .slice = system_libs },
937 .c_macros = .{ .slice = tc.c_macros.items },
938 .target = try addOptionalResolvedTarget(wc, tc.target),
939 })));
940 },
914 .write_file => e: {941 .write_file => e: {
915 const wf: *Step.WriteFile = @fieldParentPtr("step", step);942 const wf: *Step.WriteFile = @fieldParentPtr("step", step);
916943
lib/std/Build/Configuration.zig+12-1
...@@ -1321,10 +1321,21 @@ pub const Step = extern struct {...@@ -1321,10 +1321,21 @@ pub const Step = extern struct {
13211321
1322 pub const TranslateC = struct {1322 pub const TranslateC = struct {
1323 flags: @This().Flags,1323 flags: @This().Flags,
1324 src_path: LazyPath.Index,
1325 output_file: GeneratedFileIndex,
1326 include_dirs: Storage.UnionList(.flags, .include_dirs, Module.IncludeDir),
1327 system_libs: Storage.FlagLengthPrefixedList(.flags, .system_libs, SystemLib.Index),
1328 c_macros: Storage.FlagLengthPrefixedList(.flags, .c_macros, String),
1329 target: ResolvedTarget.OptionalIndex,
13241330
1325 pub const Flags = packed struct(u32) {1331 pub const Flags = packed struct(u32) {
1326 tag: Tag = .translate_c,1332 tag: Tag = .translate_c,
1327 _: u27 = 0,1333 include_dirs: bool,
1334 system_libs: bool,
1335 c_macros: bool,
1336 link_libc: bool,
1337 optimize: Module.Optimize,
1338 _: u20 = 0,
1328 };1339 };
1329 };1340 };
13301341
lib/std/Build/Step/TranslateC.zig+10-9
...@@ -10,9 +10,9 @@ const Configuration = std.Build.Configuration;...@@ -10,9 +10,9 @@ const Configuration = std.Build.Configuration;
1010
11step: Step,11step: Step,
12source: std.Build.LazyPath,12source: std.Build.LazyPath,
13include_dirs: std.ArrayList(std.Build.Module.IncludeDir),13include_dirs: std.ArrayList(std.Build.Module.IncludeDir) = .empty,
14system_libs: std.ArrayList(std.Build.Module.SystemLib),14system_libs: std.ArrayList(std.Build.Module.SystemLib) = .empty,
15c_macros: std.ArrayList([]const u8),15c_macros: std.ArrayList(Configuration.String) = .empty,
16target: std.Build.ResolvedTarget,16target: std.Build.ResolvedTarget,
17optimize: std.builtin.OptimizeMode,17optimize: std.builtin.OptimizeMode,
18output_file: Configuration.GeneratedFileIndex,18output_file: Configuration.GeneratedFileIndex,
...@@ -38,13 +38,10 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {...@@ -38,13 +38,10 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
38 .owner = owner,38 .owner = owner,
39 }),39 }),
40 .source = source,40 .source = source,
41 .include_dirs = .empty,
42 .c_macros = .empty,
43 .target = options.target,41 .target = options.target,
44 .optimize = options.optimize,42 .optimize = options.optimize,
45 .output_file = graph.addGeneratedFile(&translate_c.step),43 .output_file = graph.addGeneratedFile(&translate_c.step),
46 .link_libc = options.link_libc,44 .link_libc = options.link_libc,
47 .system_libs = .empty,
48 };45 };
49 source.addStepDependencies(&translate_c.step);46 source.addStepDependencies(&translate_c.step);
50 return translate_c;47 return translate_c;
...@@ -160,15 +157,19 @@ pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const...@@ -160,15 +157,19 @@ pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const
160pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {157pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {
161 const graph = translate_c.step.owner.graph;158 const graph = translate_c.step.owner.graph;
162 const arena = graph.arena;159 const arena = graph.arena;
160 const wc = &graph.wip_configuration;
163 const macro = allocPrint(arena, "{s}={s}", .{ name, value orelse "1" }) catch @panic("OOM");161 const macro = allocPrint(arena, "{s}={s}", .{ name, value orelse "1" }) catch @panic("OOM");
164 translate_c.c_macros.append(arena, macro) catch @panic("OOM");162 const macro_string = wc.addString(macro) catch @panic("OOM");
163 translate_c.c_macros.append(arena, macro_string) catch @panic("OOM");
165}164}
166165
167/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.166/// name_and_value looks like [name]=[value].
168pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {167pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {
169 const graph = translate_c.step.owner.graph;168 const graph = translate_c.step.owner.graph;
170 const arena = graph.arena;169 const arena = graph.arena;
171 translate_c.c_macros.append(arena, translate_c.step.owner.dupe(name_and_value)) catch @panic("OOM");170 const wc = &graph.wip_configuration;
171 const macro_string = wc.addString(name_and_value) catch @panic("OOM");
172 translate_c.c_macros.append(arena, macro_string) catch @panic("OOM");
172}173}
173174
174pub fn linkSystemLibrary(175pub fn linkSystemLibrary(