diff --git a/doc/docgen.zig b/doc/docgen.zig index 3dd58a012d..a101b96be7 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -1708,7 +1708,7 @@ fn genHtml( } } -fn exec(allocator: Allocator, env_map: *std.BufMap, args: []const []const u8) !ChildProcess.ExecResult { +fn exec(allocator: Allocator, env_map: *process.EnvMap, args: []const []const u8) !ChildProcess.ExecResult { const result = try ChildProcess.exec(.{ .allocator = allocator, .argv = args, @@ -1732,7 +1732,7 @@ fn exec(allocator: Allocator, env_map: *std.BufMap, args: []const []const u8) !C return result; } -fn getBuiltinCode(allocator: Allocator, env_map: *std.BufMap, zig_exe: []const u8) ![]const u8 { +fn getBuiltinCode(allocator: Allocator, env_map: *process.EnvMap, zig_exe: []const u8) ![]const u8 { const result = try exec(allocator, env_map, &[_][]const u8{ zig_exe, "build-obj", "--show-builtin" }); return result.stdout; } diff --git a/lib/std/build.zig b/lib/std/build.zig index b0200a928b..0f196cc9e9 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -12,7 +12,7 @@ const StringHashMap = std.StringHashMap; const Allocator = mem.Allocator; const process = std.process; const BufSet = std.BufSet; -const BufMap = std.BufMap; +const EnvMap = std.process.EnvMap; const fmt_lib = std.fmt; const File = std.fs.File; const CrossTarget = std.zig.CrossTarget; @@ -48,7 +48,7 @@ pub const Builder = struct { invalid_user_input: bool, zig_exe: []const u8, default_step: *Step, - env_map: *BufMap, + env_map: *EnvMap, top_level_steps: ArrayList(*TopLevelStep), install_prefix: []const u8, dest_dir: ?[]const u8, @@ -167,7 +167,7 @@ pub const Builder = struct { cache_root: []const u8, global_cache_root: []const u8, ) !*Builder { - const env_map = try allocator.create(BufMap); + const env_map = try allocator.create(EnvMap); env_map.* = try process.getEnvMap(allocator); const host = try NativeTargetInfo.detect(allocator, .{}); @@ -963,7 +963,7 @@ pub const Builder = struct { warn("\n", .{}); } - pub fn spawnChildEnvMap(self: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) !void { + pub fn spawnChildEnvMap(self: *Builder, cwd: ?[]const u8, env_map: *const EnvMap, argv: []const []const u8) !void { if (self.verbose) { printCmd(cwd, argv); } diff --git a/lib/std/build/RunStep.zig b/lib/std/build/RunStep.zig index e00fe3deb6..bc6ccd7b69 100644 --- a/lib/std/build/RunStep.zig +++ b/lib/std/build/RunStep.zig @@ -9,7 +9,7 @@ const fs = std.fs; const mem = std.mem; const process = std.process; const ArrayList = std.ArrayList; -const BufMap = std.BufMap; +const EnvMap = process.EnvMap; const Allocator = mem.Allocator; const ExecError = build.Builder.ExecError; @@ -29,7 +29,7 @@ argv: ArrayList(Arg), cwd: ?[]const u8, /// Override this field to modify the environment, or use setEnvironmentVariable -env_map: ?*BufMap, +env_map: ?*EnvMap, stdout_action: StdIoAction = .inherit, stderr_action: StdIoAction = .inherit, @@ -91,8 +91,8 @@ pub fn addArgs(self: *RunStep, args: []const []const u8) void { } pub fn clearEnvironment(self: *RunStep) void { - const new_env_map = self.builder.allocator.create(BufMap) catch unreachable; - new_env_map.* = BufMap.init(self.builder.allocator); + const new_env_map = self.builder.allocator.create(EnvMap) catch unreachable; + new_env_map.* = EnvMap.init(self.builder.allocator); self.env_map = new_env_map; } @@ -100,18 +100,7 @@ pub fn addPathDir(self: *RunStep, search_path: []const u8) void { const env_map = self.getEnvMap(); var key: []const u8 = undefined; - var prev_path: ?[]const u8 = undefined; - if (builtin.os.tag == .windows) { - key = "Path"; - prev_path = env_map.get(key); - if (prev_path == null) { - key = "PATH"; - prev_path = env_map.get(key); - } - } else { - key = "PATH"; - prev_path = env_map.get(key); - } + var prev_path = env_map.get("PATH"); if (prev_path) |pp| { const new_path = self.builder.fmt("{s}" ++ [1]u8{fs.path.delimiter} ++ "{s}", .{ pp, search_path }); @@ -121,9 +110,9 @@ pub fn addPathDir(self: *RunStep, search_path: []const u8) void { } } -pub fn getEnvMap(self: *RunStep) *BufMap { +pub fn getEnvMap(self: *RunStep) *EnvMap { return self.env_map orelse { - const env_map = self.builder.allocator.create(BufMap) catch unreachable; + const env_map = self.builder.allocator.create(EnvMap) catch unreachable; env_map.* = process.getEnvMap(self.builder.allocator) catch unreachable; self.env_map = env_map; return env_map; diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 5f01ed01dd..48f0776465 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -12,7 +12,7 @@ const linux = os.linux; const mem = std.mem; const math = std.math; const debug = std.debug; -const BufMap = std.BufMap; +const EnvMap = process.EnvMap; const Os = std.builtin.Os; const TailQueue = std.TailQueue; const maxInt = std.math.maxInt; @@ -34,7 +34,7 @@ pub const ChildProcess = struct { argv: []const []const u8, /// Leave as null to use the current env map using the supplied allocator. - env_map: ?*const BufMap, + env_map: ?*const EnvMap, stdin_behavior: StdIo, stdout_behavior: StdIo, @@ -375,7 +375,7 @@ pub const ChildProcess = struct { argv: []const []const u8, cwd: ?[]const u8 = null, cwd_dir: ?fs.Dir = null, - env_map: ?*const BufMap = null, + env_map: ?*const EnvMap = null, max_output_bytes: usize = 50 * 1024, expand_arg0: Arg0Expand = .no_expand, }) !ExecResult { @@ -1237,7 +1237,7 @@ fn readIntFd(fd: i32) !ErrInt { } /// Caller must free result. -pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const BufMap) ![]u16 { +pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16 { // count bytes needed const max_chars_needed = x: { var max_chars_needed: usize = 4; // 4 for the final 4 null bytes @@ -1245,7 +1245,7 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const BufMap) ! while (it.next()) |pair| { // +1 for '=' // +1 for null byte - max_chars_needed += pair.key_ptr.len + pair.value_ptr.len + 2; + max_chars_needed += pair.name.len + pair.value.len + 2; } break :x max_chars_needed; }; @@ -1255,10 +1255,10 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const BufMap) ! var it = env_map.iterator(); var i: usize = 0; while (it.next()) |pair| { - i += try unicode.utf8ToUtf16Le(result[i..], pair.key_ptr.*); + i += try unicode.utf8ToUtf16Le(result[i..], pair.name); result[i] = '='; i += 1; - i += try unicode.utf8ToUtf16Le(result[i..], pair.value_ptr.*); + i += try unicode.utf8ToUtf16Le(result[i..], pair.value); result[i] = 0; i += 1; } @@ -1273,17 +1273,17 @@ pub fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const BufMap) ! return allocator.shrink(result, i); } -pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const std.BufMap) ![:null]?[*:0]u8 { +pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) ![:null]?[*:0]u8 { const envp_count = env_map.count(); const envp_buf = try arena.allocSentinel(?[*:0]u8, envp_count, null); { var it = env_map.iterator(); var i: usize = 0; while (it.next()) |pair| : (i += 1) { - const env_buf = try arena.allocSentinel(u8, pair.key_ptr.len + pair.value_ptr.len + 1, 0); - mem.copy(u8, env_buf, pair.key_ptr.*); - env_buf[pair.key_ptr.len] = '='; - mem.copy(u8, env_buf[pair.key_ptr.len + 1 ..], pair.value_ptr.*); + const env_buf = try arena.allocSentinel(u8, pair.name.len + pair.value.len + 1, 0); + mem.copy(u8, env_buf, pair.name); + env_buf[pair.name.len] = '='; + mem.copy(u8, env_buf[pair.name.len + 1 ..], pair.value); envp_buf[i] = env_buf.ptr; } assert(i == envp_count); @@ -1294,7 +1294,7 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const std.BufMa test "createNullDelimitedEnvMap" { const testing = std.testing; const allocator = testing.allocator; - var envmap = BufMap.init(allocator); + var envmap = EnvMap.init(allocator); defer envmap.deinit(); try envmap.put("HOME", "/home/ifreund"); diff --git a/lib/std/process.zig b/lib/std/process.zig index f5d14cf6da..66a82c4b1d 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -1364,7 +1364,7 @@ pub fn execv(allocator: mem.Allocator, argv: []const []const u8) ExecvError { pub fn execve( allocator: mem.Allocator, argv: []const []const u8, - env_map: ?*const std.BufMap, + env_map: ?*const EnvMap, ) ExecvError { if (!can_execv) @compileError("The target OS does not support execv");