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 {
451451 return result;
452452 }
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
454472 fn initLazyPathList(s: *Serialize, list: []const std.Build.LazyPath) ![]const Configuration.LazyPath.Index {
455473 const result = try s.arena.alloc(Configuration.LazyPath.Index, list.len);
456474 for (result, list) |*dest, src| dest.* = try addLazyPath(s, src);
......@@ -486,18 +504,6 @@ const Serialize = struct {
486504 const wc = s.wc;
487505 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
501507 const rpaths = try arena.alloc(Configuration.Module.RPath, m.rpaths.items.len);
502508 for (rpaths, m.rpaths.items) |*dest, src| dest.* = switch (src) {
503509 .lazy_path => |lp| .{ .lazy_path = try addLazyPath(s, lp) },
......@@ -542,7 +548,7 @@ const Serialize = struct {
542548 .fuzz = .init(m.fuzz),
543549 .code_model = m.code_model,
544550 .c_macros = c_macros.len != 0,
545 .include_dirs = include_dirs.len != 0,
551 .include_dirs = m.include_dirs.items.len != 0,
546552 .lib_paths = lib_paths.len != 0,
547553 .rpaths = rpaths.len != 0,
548554 .frameworks = frameworks.len != 0,
......@@ -566,7 +572,7 @@ const Serialize = struct {
566572 .c_macros = .{ .slice = c_macros },
567573 .lib_paths = .{ .slice = lib_paths },
568574 .export_symbol_names = .{ .slice = export_symbol_names },
569 .include_dirs = .init(include_dirs),
575 .include_dirs = .init(try s.initIncludeDirList(m.include_dirs.items)),
570576 .rpaths = .init(rpaths),
571577 .link_objects = .init(link_objects),
572578 .frameworks = .{ .slice = frameworks },
......@@ -910,7 +916,28 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
910916 .exclude_paths = .{ .slice = try s.initLazyPathList(sf.exclude_paths) },
911917 })));
912918 },
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 },
914941 .write_file => e: {
915942 const wf: *Step.WriteFile = @fieldParentPtr("step", step);
916943
lib/std/Build/Configuration.zig+12-1
......@@ -1321,10 +1321,21 @@ pub const Step = extern struct {
13211321
13221322 pub const TranslateC = struct {
13231323 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
13251331 pub const Flags = packed struct(u32) {
13261332 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,
13281339 };
13291340 };
13301341
lib/std/Build/Step/TranslateC.zig+10-9
......@@ -10,9 +10,9 @@ const Configuration = std.Build.Configuration;
1010
1111step: Step,
1212source: std.Build.LazyPath,
13include_dirs: std.ArrayList(std.Build.Module.IncludeDir),
14system_libs: std.ArrayList(std.Build.Module.SystemLib),
15c_macros: std.ArrayList([]const u8),
13include_dirs: std.ArrayList(std.Build.Module.IncludeDir) = .empty,
14system_libs: std.ArrayList(std.Build.Module.SystemLib) = .empty,
15c_macros: std.ArrayList(Configuration.String) = .empty,
1616target: std.Build.ResolvedTarget,
1717optimize: std.builtin.OptimizeMode,
1818output_file: Configuration.GeneratedFileIndex,
......@@ -38,13 +38,10 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
3838 .owner = owner,
3939 }),
4040 .source = source,
41 .include_dirs = .empty,
42 .c_macros = .empty,
4341 .target = options.target,
4442 .optimize = options.optimize,
4543 .output_file = graph.addGeneratedFile(&translate_c.step),
4644 .link_libc = options.link_libc,
47 .system_libs = .empty,
4845 };
4946 source.addStepDependencies(&translate_c.step);
5047 return translate_c;
......@@ -160,15 +157,19 @@ pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const
160157pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {
161158 const graph = translate_c.step.owner.graph;
162159 const arena = graph.arena;
160 const wc = &graph.wip_configuration;
163161 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");
165164}
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].
168167pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {
169168 const graph = translate_c.step.owner.graph;
170169 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");
172173}
173174
174175pub fn linkSystemLibrary(