From 988613be67133ce6f2de3ac8b3f2e63f610c4470 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Mon, 6 Jul 2026 19:29:17 +0200 Subject: [PATCH] std.Build.Configuration: improve storage of preopens in run steps --- lib/compiler/Maker/Step/Run.zig | 14 +++++++------- lib/std/Build/Configuration.zig | 11 ++++++++--- lib/std/Build/Serialize.zig | 11 +++++++++-- lib/std/Build/Step/Run.zig | 5 +++-- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/compiler/Maker/Step/Run.zig b/lib/compiler/Maker/Step/Run.zig index b6fc911f01f8d25a27cf2f3829118accfe2f58ea..0ac652096de65af81a50d25fb94532c805d0205c 100644 --- a/lib/compiler/Maker/Step/Run.zig +++ b/lib/compiler/Maker/Step/Run.zig @@ -64,9 +64,9 @@ pub fn make( } } - for (conf_run.preopen_names.slice, conf_run.preopen_paths.slice) |name, path| { - man.hash.addBytesZ(name.slice(conf)); - const cwd_path = try maker.resolveLazyPathIndex(arena, path, run_index); + for (conf_run.preopens.slice) |preopen| { + man.hash.addBytesZ(preopen.name.slice(conf)); + const cwd_path = try maker.resolveLazyPathIndex(arena, preopen.path, run_index); man.hash.addBytes(try cwd_path.toString(arena)); } @@ -1917,14 +1917,14 @@ fn runCommand( }, .wasmtime => |bin_name| { if (graph.enable_wasmtime) { - try interp_argv.ensureUnusedCapacity(arena, 3 + argv.len + conf_run.preopen_names.slice.len); + try interp_argv.ensureUnusedCapacity(arena, 3 + argv.len + conf_run.preopens.slice.len); interp_argv.appendAssumeCapacity(bin_name); interp_argv.appendAssumeCapacity("--dir=."); - for (conf_run.preopen_names.slice, conf_run.preopen_paths.slice) |name, lazy_path| { - const path = try maker.resolveLazyPath(arena, lazy_path.get(conf), run_index); + for (conf_run.preopens.slice) |preopen| { + const path = try maker.resolveLazyPath(arena, preopen.path.get(conf), run_index); path.root_dir.handle.createDirPath(io, path.subPathOrDot()) catch |e| return step.fail(maker, "failed creating directory {f}: {t}", .{ path, e }); - interp_argv.appendAssumeCapacity(try arena.print("--dir={f}::{s}", .{ path, name.slice(conf) })); + interp_argv.appendAssumeCapacity(try arena.print("--dir={f}::{s}", .{ path, preopen.name.slice(conf) })); } // Wasmtime doeesn't inherit environment variables from the parent process // by default. '-S inherit-env' was added in Wasmtime version 20. diff --git a/lib/std/Build/Configuration.zig b/lib/std/Build/Configuration.zig index b04a7c592460b1db60c80b412edcbf973c5d29e2..ccdb74499c7b41ed30b6f8b62953710e207d459b 100644 --- a/lib/std/Build/Configuration.zig +++ b/lib/std/Build/Configuration.zig @@ -569,8 +569,7 @@ pub const Step = extern struct { flags2: Flags2, args: Storage.LengthPrefixedList(Arg.Index), cwd: Storage.FlagOptional(.flags, .cwd, LazyPath.Index), - preopen_names: Storage.LengthPrefixedList(String), - preopen_paths: Storage.LengthPrefixedList(LazyPath.Index), + preopens: Storage.FlagLengthPrefixedList(.flags, .preopens, Preopen), captured_stdout: Storage.FlagOptional(.flags, .captured_stdout, CapturedStream), captured_stderr: Storage.FlagOptional(.flags, .captured_stderr, CapturedStream), file_inputs: Storage.LengthPrefixedList(LazyPath.Index), @@ -646,6 +645,11 @@ pub const Step = extern struct { manual, }; + pub const Preopen = extern struct { + name: String, + path: LazyPath.Index, + }; + pub const StdIn = union(@This().Tag) { none: void, bytes: Bytes, @@ -676,7 +680,8 @@ pub const Step = extern struct { captured_stdout: bool, captured_stderr: bool, environ_map: bool, - _: u4 = 0, + preopens: bool, + _: u3 = 0, }; pub const Flags2 = packed struct(u32) { diff --git a/lib/std/Build/Serialize.zig b/lib/std/Build/Serialize.zig index 8849f7aaeec31bf6093f3ee60ec48ba2376f026d..72355264d978c15b712d92242df14f0c92eb4b6f 100644 --- a/lib/std/Build/Serialize.zig +++ b/lib/std/Build/Serialize.zig @@ -454,6 +454,13 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi }, else => {}, } + const preopens = try arena.alloc( + Configuration.Step.Run.Preopen, + run.preopens.count(), + ); + for (preopens, run.preopens.keys(), run.preopens.values()) |*dest, name, path| { + dest.* = .{ .name = name, .path = try s.addLazyPath(path) }; + } break :e try wc.addExtraErased(Configuration.Step.Run, .{ .flags = .{ @@ -482,6 +489,7 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi .captured_stdout = run.captured_stdout != null, .captured_stderr = run.captured_stderr != null, .environ_map = run.environ_map != null, + .preopens = run.preopens.count() > 0, }, .flags2 = .{ .expect_stderr_exact = expect_stderr_exact != null, @@ -496,8 +504,7 @@ pub fn write(b: *std.Build, wc: *Configuration.Wip, writer: *std.Io.Writer) !voi .file_inputs = .{ .slice = try s.initLazyPathList(run.file_inputs.items) }, .args = .{ .slice = try s.initArgsList(run.argv.items) }, .cwd = .{ .value = try s.addOptionalLazyPath(run.cwd) }, - .preopen_names = .{ .slice = try s.initStringList(run.preopens.keys()) }, - .preopen_paths = .{ .slice = try s.initLazyPathList(run.preopens.values()) }, + .preopens = .{ .slice = preopens }, .captured_stdout = .{ .value = if (run.captured_stdout) |cs| .{ .basename = try wc.addString(cs.basename), .generated_file = cs.generated_file, diff --git a/lib/std/Build/Step/Run.zig b/lib/std/Build/Step/Run.zig index a0fc19f5dc8dc33d5f2ee870bd6210c0172d1bd4..9e1f3ca7047585a13fcae9b77de0afb6c45f9eee 100644 --- a/lib/std/Build/Step/Run.zig +++ b/lib/std/Build/Step/Run.zig @@ -28,7 +28,7 @@ environ_map: ?*EnvMap, /// Named files that will be provided to the parent process. /// See `std.process.Preopens`. -preopens: std.array_hash_map.String(Build.LazyPath), +preopens: std.array_hash_map.Auto(Configuration.String, Build.LazyPath), /// Controls the `NO_COLOR` and `CLICOLOR_FORCE` environment variables. color: Color = .auto, @@ -624,11 +624,12 @@ pub fn removeEnvironmentVariable(run: *Run, key: []const u8) void { pub fn setPreopen(run: *Run, name: []const u8, resource: Build.LazyPath) void { const graph = run.step.owner.graph; + const wc = &graph.wip_configuration; const arena = graph.arena; resource.addStepDependencies(&run.step); run.preopens.put( arena, - graph.dupeString(name), + wc.addString(name) catch @panic("OOM"), resource.dupe(graph), ) catch @panic("OOM"); } -- 2.54.0