commit 42be6c0088ece7f3df2a30cfbaee0d77151f762a (tree)
parent 315d6ee59b42c07ac0a4e31924ce229d7c5afc32
Author: Andrew Kelley <andrew@ziglang.org>
Date: Fri, 8 May 2026 17:11:18 -0700
configurer: serialize Step.TranslateC
Diffstat:
3 files changed, 64 insertions(+), 25 deletions(-)
diff --git a/lib/compiler/configurer.zig b/lib/compiler/configurer.zig
@@ -451,6 +451,24 @@ const Serialize = struct {
return result;
}
+ fn initIncludeDirList(
+ s: *Serialize,
+ list: []const std.Build.Module.IncludeDir,
+ ) ![]const Configuration.Module.IncludeDir {
+ const result = try s.arena.alloc(Configuration.Module.IncludeDir, list.len);
+ for (result, list) |*dest, src| dest.* = switch (src) {
+ .path => |lp| .{ .path = try addLazyPath(s, lp) },
+ .path_system => |lp| .{ .path_system = try addLazyPath(s, lp) },
+ .path_after => |lp| .{ .path_after = try addLazyPath(s, lp) },
+ .framework_path => |lp| .{ .framework_path = try addLazyPath(s, lp) },
+ .framework_path_system => |lp| .{ .framework_path_system = try addLazyPath(s, lp) },
+ .embed_path => |lp| .{ .embed_path = try addLazyPath(s, lp) },
+ .other_step => |cs| .{ .other_step = stepIndex(s, &cs.step) },
+ .config_header_step => |chs| .{ .config_header_step = stepIndex(s, &chs.step) },
+ };
+ return result;
+ }
+
fn initLazyPathList(s: *Serialize, list: []const std.Build.LazyPath) ![]const Configuration.LazyPath.Index {
const result = try s.arena.alloc(Configuration.LazyPath.Index, list.len);
for (result, list) |*dest, src| dest.* = try addLazyPath(s, src);
@@ -486,18 +504,6 @@ const Serialize = struct {
const wc = s.wc;
const arena = s.arena;
- const include_dirs = try arena.alloc(Configuration.Module.IncludeDir, m.include_dirs.items.len);
- for (include_dirs, m.include_dirs.items) |*dest, src| dest.* = switch (src) {
- .path => |lp| .{ .path = try addLazyPath(s, lp) },
- .path_system => |lp| .{ .path_system = try addLazyPath(s, lp) },
- .path_after => |lp| .{ .path_after = try addLazyPath(s, lp) },
- .framework_path => |lp| .{ .framework_path = try addLazyPath(s, lp) },
- .framework_path_system => |lp| .{ .framework_path_system = try addLazyPath(s, lp) },
- .embed_path => |lp| .{ .embed_path = try addLazyPath(s, lp) },
- .other_step => |cs| .{ .other_step = stepIndex(s, &cs.step) },
- .config_header_step => |chs| .{ .config_header_step = stepIndex(s, &chs.step) },
- };
-
const rpaths = try arena.alloc(Configuration.Module.RPath, m.rpaths.items.len);
for (rpaths, m.rpaths.items) |*dest, src| dest.* = switch (src) {
.lazy_path => |lp| .{ .lazy_path = try addLazyPath(s, lp) },
@@ -542,7 +548,7 @@ const Serialize = struct {
.fuzz = .init(m.fuzz),
.code_model = m.code_model,
.c_macros = c_macros.len != 0,
- .include_dirs = include_dirs.len != 0,
+ .include_dirs = m.include_dirs.items.len != 0,
.lib_paths = lib_paths.len != 0,
.rpaths = rpaths.len != 0,
.frameworks = frameworks.len != 0,
@@ -566,7 +572,7 @@ const Serialize = struct {
.c_macros = .{ .slice = c_macros },
.lib_paths = .{ .slice = lib_paths },
.export_symbol_names = .{ .slice = export_symbol_names },
- .include_dirs = .init(include_dirs),
+ .include_dirs = .init(try s.initIncludeDirList(m.include_dirs.items)),
.rpaths = .init(rpaths),
.link_objects = .init(link_objects),
.frameworks = .{ .slice = frameworks },
@@ -910,7 +916,28 @@ fn serialize(b: *std.Build, wc: *Configuration.Wip, writer: *Io.Writer) !void {
.exclude_paths = .{ .slice = try s.initLazyPathList(sf.exclude_paths) },
})));
},
- .translate_c => @panic("TODO"),
+ .translate_c => e: {
+ const tc: *Step.TranslateC = @fieldParentPtr("step", step);
+
+ const system_libs = try arena.alloc(Configuration.SystemLib.Index, tc.system_libs.items.len);
+ for (system_libs, tc.system_libs.items) |*dest, *src| dest.* = try s.addSystemLib(src);
+
+ break :e @enumFromInt(try wc.addExtra(@as(Configuration.Step.TranslateC, .{
+ .flags = .{
+ .include_dirs = tc.include_dirs.items.len != 0,
+ .system_libs = system_libs.len != 0,
+ .c_macros = tc.c_macros.items.len != 0,
+ .link_libc = tc.link_libc,
+ .optimize = .init(tc.optimize),
+ },
+ .src_path = try s.addLazyPath(tc.source),
+ .output_file = tc.output_file,
+ .include_dirs = .init(try s.initIncludeDirList(tc.include_dirs.items)),
+ .system_libs = .{ .slice = system_libs },
+ .c_macros = .{ .slice = tc.c_macros.items },
+ .target = try addOptionalResolvedTarget(wc, tc.target),
+ })));
+ },
.write_file => e: {
const wf: *Step.WriteFile = @fieldParentPtr("step", step);
diff --git a/lib/std/Build/Configuration.zig b/lib/std/Build/Configuration.zig
@@ -1321,10 +1321,21 @@ pub const Step = extern struct {
pub const TranslateC = struct {
flags: @This().Flags,
+ src_path: LazyPath.Index,
+ output_file: GeneratedFileIndex,
+ include_dirs: Storage.UnionList(.flags, .include_dirs, Module.IncludeDir),
+ system_libs: Storage.FlagLengthPrefixedList(.flags, .system_libs, SystemLib.Index),
+ c_macros: Storage.FlagLengthPrefixedList(.flags, .c_macros, String),
+ target: ResolvedTarget.OptionalIndex,
pub const Flags = packed struct(u32) {
tag: Tag = .translate_c,
- _: u27 = 0,
+ include_dirs: bool,
+ system_libs: bool,
+ c_macros: bool,
+ link_libc: bool,
+ optimize: Module.Optimize,
+ _: u20 = 0,
};
};
diff --git a/lib/std/Build/Step/TranslateC.zig b/lib/std/Build/Step/TranslateC.zig
@@ -10,9 +10,9 @@ const Configuration = std.Build.Configuration;
step: Step,
source: std.Build.LazyPath,
-include_dirs: std.ArrayList(std.Build.Module.IncludeDir),
-system_libs: std.ArrayList(std.Build.Module.SystemLib),
-c_macros: std.ArrayList([]const u8),
+include_dirs: std.ArrayList(std.Build.Module.IncludeDir) = .empty,
+system_libs: std.ArrayList(std.Build.Module.SystemLib) = .empty,
+c_macros: std.ArrayList(Configuration.String) = .empty,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
output_file: Configuration.GeneratedFileIndex,
@@ -38,13 +38,10 @@ pub fn create(owner: *std.Build, options: Options) *TranslateC {
.owner = owner,
}),
.source = source,
- .include_dirs = .empty,
- .c_macros = .empty,
.target = options.target,
.optimize = options.optimize,
.output_file = graph.addGeneratedFile(&translate_c.step),
.link_libc = options.link_libc,
- .system_libs = .empty,
};
source.addStepDependencies(&translate_c.step);
return translate_c;
@@ -160,15 +157,19 @@ pub fn addCheckFile(translate_c: *TranslateC, expected_matches: []const []const
pub fn defineCMacro(translate_c: *TranslateC, name: []const u8, value: ?[]const u8) void {
const graph = translate_c.step.owner.graph;
const arena = graph.arena;
+ const wc = &graph.wip_configuration;
const macro = allocPrint(arena, "{s}={s}", .{ name, value orelse "1" }) catch @panic("OOM");
- translate_c.c_macros.append(arena, macro) catch @panic("OOM");
+ const macro_string = wc.addString(macro) catch @panic("OOM");
+ translate_c.c_macros.append(arena, macro_string) catch @panic("OOM");
}
-/// name_and_value looks like [name]=[value]. If the value is omitted, it is set to 1.
+/// name_and_value looks like [name]=[value].
pub fn defineCMacroRaw(translate_c: *TranslateC, name_and_value: []const u8) void {
const graph = translate_c.step.owner.graph;
const arena = graph.arena;
- translate_c.c_macros.append(arena, translate_c.step.owner.dupe(name_and_value)) catch @panic("OOM");
+ const wc = &graph.wip_configuration;
+ const macro_string = wc.addString(name_and_value) catch @panic("OOM");
+ translate_c.c_macros.append(arena, macro_string) catch @panic("OOM");
}
pub fn linkSystemLibrary(