authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-01 19:40:18-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-04 00:27:08-08:00
log960c512efd71ab4b658952fe8761453128cc8292
tree5a519f99451bb6ad896dcfa438d8ebed77f2c03e
parenta6f519c20f105a60c2ac51530354f9ac7c3e1fd9

compiler: update std lib API usage


14 files changed, 126 insertions(+), 119 deletions(-)

lib/compiler/std-docs.zig+36-33
...@@ -21,19 +21,12 @@ fn usage(io: Io) noreturn {...@@ -21,19 +21,12 @@ fn usage(io: Io) noreturn {
21 std.process.exit(1);21 std.process.exit(1);
22}22}
2323
24pub fn main() !void {24pub fn main(init: std.process.Init) !void {
25 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);25 const arena = init.arena.allocator();
26 defer arena_instance.deinit();26 const gpa = init.gpa;
27 const arena = arena_instance.allocator();27 const io = init.io;
28
29 var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
30 const gpa = general_purpose_allocator.allocator();
3128
32 var threaded: Io.Threaded = .init(gpa, .{});29 var argv = try init.minimal.args.iterateAllocator(arena);
33 defer threaded.deinit();
34 const io = threaded.io();
35
36 var argv = try std.process.argsWithAllocator(arena);
37 defer argv.deinit();30 defer argv.deinit();
38 assert(argv.skip());31 assert(argv.skip());
39 const zig_lib_directory = argv.next().?;32 const zig_lib_directory = argv.next().?;
...@@ -72,7 +65,7 @@ pub fn main() !void {...@@ -72,7 +65,7 @@ pub fn main() !void {
72 const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});65 const url_with_newline = try std.fmt.allocPrint(arena, "http://127.0.0.1:{d}/\n", .{port});
73 Io.File.stdout().writeStreamingAll(io, url_with_newline) catch {};66 Io.File.stdout().writeStreamingAll(io, url_with_newline) catch {};
74 if (should_open_browser) {67 if (should_open_browser) {
75 openBrowserTab(gpa, io, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {68 openBrowserTab(io, url_with_newline[0 .. url_with_newline.len - 1 :'\n']) catch |err| {
76 std.log.err("unable to open browser: {t}", .{err});69 std.log.err("unable to open browser: {t}", .{err});
77 };70 };
78 }71 }
...@@ -324,11 +317,12 @@ fn buildWasmBinary(...@@ -324,11 +317,12 @@ fn buildWasmBinary(
324 "--listen=-", //317 "--listen=-", //
325 });318 });
326319
327 var child = std.process.Child.init(argv.items, gpa);320 var child = try std.process.spawn(io, .{
328 child.stdin_behavior = .Pipe;321 .argv = argv.items,
329 child.stdout_behavior = .Pipe;322 .stdin = .pipe,
330 child.stderr_behavior = .Pipe;323 .stdout = .pipe,
331 try child.spawn(io);324 .stderr = .pipe,
325 });
332326
333 var poller = Io.poll(gpa, enum { stdout, stderr }, .{327 var poller = Io.poll(gpa, enum { stdout, stderr }, .{
334 .stdout = child.stdout.?,328 .stdout = child.stdout.?,
...@@ -388,19 +382,26 @@ fn buildWasmBinary(...@@ -388,19 +382,26 @@ fn buildWasmBinary(
388 child.stdin = null;382 child.stdin = null;
389383
390 switch (try child.wait(io)) {384 switch (try child.wait(io)) {
391 .Exited => |code| {385 .exited => |code| {
392 if (code != 0) {386 if (code != 0) {
393 std.log.err(387 std.log.err(
394 "the following command exited with error code {d}:\n{s}",388 "the following command exited with error code {d}:\n{s}",
395 .{ code, try std.Build.Step.allocPrintCmd(arena, null, argv.items) },389 .{ code, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
396 );390 );
397 return error.WasmCompilationFailed;391 return error.WasmCompilationFailed;
398 }392 }
399 },393 },
400 .Signal, .Stopped, .Unknown => {394 .signal => |sig| {
395 std.log.err(
396 "the following command terminated with signal {t}:\n{s}",
397 .{ sig, try std.Build.Step.allocPrintCmd(arena, null, null, argv.items) },
398 );
399 return error.WasmCompilationFailed;
400 },
401 .stopped, .unknown => {
401 std.log.err(402 std.log.err(
402 "the following command terminated unexpectedly:\n{s}",403 "the following command terminated unexpectedly:\n{s}",
403 .{try std.Build.Step.allocPrintCmd(arena, null, argv.items)},404 .{try std.Build.Step.allocPrintCmd(arena, null, null, argv.items)},
404 );405 );
405 return error.WasmCompilationFailed;406 return error.WasmCompilationFailed;
406 },407 },
...@@ -410,14 +411,14 @@ fn buildWasmBinary(...@@ -410,14 +411,14 @@ fn buildWasmBinary(
410 try result_error_bundle.renderToStderr(io, .{}, .auto);411 try result_error_bundle.renderToStderr(io, .{}, .auto);
411 std.log.err("the following command failed with {d} compilation errors:\n{s}", .{412 std.log.err("the following command failed with {d} compilation errors:\n{s}", .{
412 result_error_bundle.errorMessageCount(),413 result_error_bundle.errorMessageCount(),
413 try std.Build.Step.allocPrintCmd(arena, null, argv.items),414 try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
414 });415 });
415 return error.WasmCompilationFailed;416 return error.WasmCompilationFailed;
416 }417 }
417418
418 return result orelse {419 return result orelse {
419 std.log.err("child process failed to report result\n{s}", .{420 std.log.err("child process failed to report result\n{s}", .{
420 try std.Build.Step.allocPrintCmd(arena, null, argv.items),421 try std.Build.Step.allocPrintCmd(arena, null, null, argv.items),
421 });422 });
422 return error.WasmCompilationFailed;423 return error.WasmCompilationFailed;
423 };424 };
...@@ -434,22 +435,24 @@ fn sendMessage(io: Io, file: Io.File, tag: std.zig.Client.Message.Tag) !void {...@@ -434,22 +435,24 @@ fn sendMessage(io: Io, file: Io.File, tag: std.zig.Client.Message.Tag) !void {
434 };435 };
435}436}
436437
437fn openBrowserTab(gpa: Allocator, io: Io, url: []const u8) !void {438fn openBrowserTab(io: Io, url: []const u8) !void {
438 // Until https://github.com/ziglang/zig/issues/19205 is implemented, we439 // Until https://github.com/ziglang/zig/issues/19205 is implemented, we
439 // spawn a thread for this child process.440 // spawn and then leak a concurrent task for this child process.
440 _ = try std.Thread.spawn(.{}, openBrowserTabThread, .{ gpa, io, url });441 const future = try io.concurrent(openBrowserTabTask, .{ io, url });
442 _ = future; // leak it
441}443}
442444
443fn openBrowserTabThread(gpa: Allocator, io: Io, url: []const u8) !void {445fn openBrowserTabTask(io: Io, url: []const u8) !void {
444 const main_exe = switch (builtin.os.tag) {446 const main_exe = switch (builtin.os.tag) {
445 .windows => "explorer",447 .windows => "explorer",
446 .macos => "open",448 .macos => "open",
447 else => "xdg-open",449 else => "xdg-open",
448 };450 };
449 var child = std.process.Child.init(&.{ main_exe, url }, gpa);451 var child = try std.process.spawn(io, .{
450 child.stdin_behavior = .ignore;452 .argv = &.{ main_exe, url },
451 child.stdout_behavior = .ignore;453 .stdin = .ignore,
452 child.stderr_behavior = .ignore;454 .stdout = .ignore,
453 try child.spawn(io);455 .stderr = .ignore,
456 });
454 _ = try child.wait(io);457 _ = try child.wait(io);
455}458}
lib/compiler/translate-c/main.zig+4-13
...@@ -9,19 +9,10 @@ const Translator = @import("Translator.zig");...@@ -9,19 +9,10 @@ const Translator = @import("Translator.zig");
99
10const fast_exit = @import("builtin").mode != .Debug;10const fast_exit = @import("builtin").mode != .Debug;
1111
12var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;12pub fn main(init: std.process.Init) u8 {
1313 const gpa = init.gpa;
14pub fn main() u8 {14 const arena = init.arena.allocator();
15 const gpa = general_purpose_allocator.allocator();15 const io = init.io;
16 defer _ = general_purpose_allocator.deinit();
17
18 var arena_instance = std.heap.ArenaAllocator.init(gpa);
19 defer arena_instance.deinit();
20 const arena = arena_instance.allocator();
21
22 var threaded: std.Io.Threaded = .init(gpa, .{});
23 defer threaded.deinit();
24 const io = threaded.io();
2516
26 const args = process.argsAlloc(arena) catch {17 const args = process.argsAlloc(arena) catch {
27 std.debug.print("ran out of memory allocating arguments\n", .{});18 std.debug.print("ran out of memory allocating arguments\n", .{});
lib/std/zig.zig+6
...@@ -742,6 +742,7 @@ pub const EnvVar = enum {...@@ -742,6 +742,7 @@ pub const EnvVar = enum {
742 ZIG_IS_DETECTING_LIBC_PATHS,742 ZIG_IS_DETECTING_LIBC_PATHS,
743 ZIG_IS_TRYING_TO_NOT_CALL_ITSELF,743 ZIG_IS_TRYING_TO_NOT_CALL_ITSELF,
744744
745 // C toolchain integration
745 NIX_CFLAGS_COMPILE,746 NIX_CFLAGS_COMPILE,
746 NIX_CFLAGS_LINK,747 NIX_CFLAGS_LINK,
747 NIX_LDFLAGS,748 NIX_LDFLAGS,
...@@ -750,13 +751,18 @@ pub const EnvVar = enum {...@@ -750,13 +751,18 @@ pub const EnvVar = enum {
750 LIBRARY_PATH,751 LIBRARY_PATH,
751 CC,752 CC,
752753
754 // Terminal integration
753 NO_COLOR,755 NO_COLOR,
754 CLICOLOR_FORCE,756 CLICOLOR_FORCE,
755757
758 // Debug info integration
756 XDG_CACHE_HOME,759 XDG_CACHE_HOME,
757 LOCALAPPDATA,760 LOCALAPPDATA,
758 HOME,761 HOME,
759762
763 // Windows SDK integration
764 PROGRAMDATA,
765
760 pub fn isSet(ev: EnvVar, map: *const std.process.Environ.Map) bool {766 pub fn isSet(ev: EnvVar, map: *const std.process.Environ.Map) bool {
761 return map.contains(@tagName(ev));767 return map.contains(@tagName(ev));
762 }768 }
lib/std/zig/LibCInstallation.zig+5-4
...@@ -13,6 +13,7 @@ const fs = std.fs;...@@ -13,6 +13,7 @@ const fs = std.fs;
13const Allocator = std.mem.Allocator;13const Allocator = std.mem.Allocator;
14const Path = std.Build.Cache.Path;14const Path = std.Build.Cache.Path;
15const log = std.log.scoped(.libc_installation);15const log = std.log.scoped(.libc_installation);
16const Environ = std.process.Environ;
1617
17include_dir: ?[]const u8 = null,18include_dir: ?[]const u8 = null,
18sys_include_dir: ?[]const u8 = null,19sys_include_dir: ?[]const u8 = null,
...@@ -167,7 +168,7 @@ pub fn render(self: LibCInstallation, out: *std.Io.Writer) !void {...@@ -167,7 +168,7 @@ pub fn render(self: LibCInstallation, out: *std.Io.Writer) !void {
167168
168pub const FindNativeOptions = struct {169pub const FindNativeOptions = struct {
169 target: *const std.Target,170 target: *const std.Target,
170 env_map: *const std.process.Environ.Map,171 env_map: *const Environ.Map,
171172
172 /// If enabled, will print human-friendly errors to stderr.173 /// If enabled, will print human-friendly errors to stderr.
173 verbose: bool = false,174 verbose: bool = false,
...@@ -192,7 +193,7 @@ pub fn findNative(gpa: Allocator, io: Io, args: FindNativeOptions) FindError!Lib...@@ -192,7 +193,7 @@ pub fn findNative(gpa: Allocator, io: Io, args: FindNativeOptions) FindError!Lib
192 });193 });
193 return self;194 return self;
194 } else if (is_windows) {195 } else if (is_windows) {
195 const sdk = std.zig.WindowsSdk.find(gpa, io, args.target.cpu.arch) catch |err| switch (err) {196 const sdk = std.zig.WindowsSdk.find(gpa, io, args.target.cpu.arch, args.env_map) catch |err| switch (err) {
196 error.NotFound => return error.WindowsSdkNotFound,197 error.NotFound => return error.WindowsSdkNotFound,
197 error.PathTooLong => return error.WindowsSdkNotFound,198 error.PathTooLong => return error.WindowsSdkNotFound,
198 error.OutOfMemory => return error.OutOfMemory,199 error.OutOfMemory => return error.OutOfMemory,
...@@ -552,7 +553,7 @@ fn findNativeMsvcLibDir(...@@ -552,7 +553,7 @@ fn findNativeMsvcLibDir(
552}553}
553554
554pub const CCPrintFileNameOptions = struct {555pub const CCPrintFileNameOptions = struct {
555 env_map: *const std.process.Environ.Map,556 env_map: *const Environ.Map,
556 search_basename: []const u8,557 search_basename: []const u8,
557 want_dirname: enum { full_path, only_dir },558 want_dirname: enum { full_path, only_dir },
558 verbose: bool = false,559 verbose: bool = false,
...@@ -672,7 +673,7 @@ const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";...@@ -672,7 +673,7 @@ const inf_loop_env_key = "ZIG_IS_DETECTING_LIBC_PATHS";
672fn appendCcExe(673fn appendCcExe(
673 args: *std.array_list.Managed([]const u8),674 args: *std.array_list.Managed([]const u8),
674 skip_cc_env_var: bool,675 skip_cc_env_var: bool,
675 env_map: *const std.process.Environ.Map,676 env_map: *const Environ.Map,
676) !void {677) !void {
677 const default_cc_exe = if (is_windows) "cc.exe" else "cc";678 const default_cc_exe = if (is_windows) "cc.exe" else "cc";
678 try args.ensureUnusedCapacity(1);679 try args.ensureUnusedCapacity(1);
lib/std/zig/WindowsSdk.zig+41-23
...@@ -6,6 +6,7 @@ const Io = std.Io;...@@ -6,6 +6,7 @@ const Io = std.Io;
6const Dir = std.Io.Dir;6const Dir = std.Io.Dir;
7const Writer = std.Io.Writer;7const Writer = std.Io.Writer;
8const Allocator = std.mem.Allocator;8const Allocator = std.mem.Allocator;
9const Environ = std.process.Environ;
910
10windows10sdk: ?Installation,11windows10sdk: ?Installation,
11windows81sdk: ?Installation,12windows81sdk: ?Installation,
...@@ -24,7 +25,12 @@ const product_version_max_length = version_major_minor_max_length + ".65535".len...@@ -24,7 +25,12 @@ const product_version_max_length = version_major_minor_max_length + ".65535".len
24/// Find path and version of Windows 10 SDK and Windows 8.1 SDK, and find path to MSVC's `lib/` directory.25/// Find path and version of Windows 10 SDK and Windows 8.1 SDK, and find path to MSVC's `lib/` directory.
25/// Caller owns the result's fields.26/// Caller owns the result's fields.
26/// Returns memory allocated by `gpa`27/// Returns memory allocated by `gpa`
27pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, NotFound, PathTooLong }!WindowsSdk {28pub fn find(
29 gpa: Allocator,
30 io: Io,
31 arch: std.Target.Cpu.Arch,
32 env_map: *const Environ.Map,
33) error{ OutOfMemory, NotFound, PathTooLong }!WindowsSdk {
28 if (builtin.os.tag != .windows) return error.NotFound;34 if (builtin.os.tag != .windows) return error.NotFound;
2935
30 //note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed36 //note(dimenus): If this key doesn't exist, neither the Win 8 SDK nor the Win 10 SDK is installed
...@@ -49,7 +55,7 @@ pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemor...@@ -49,7 +55,7 @@ pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemor
49 };55 };
50 errdefer if (windows81sdk) |*w| w.free(gpa);56 errdefer if (windows81sdk) |*w| w.free(gpa);
5157
52 const msvc_lib_dir: ?[]const u8 = MsvcLibDir.find(gpa, io, arch) catch |err| switch (err) {58 const msvc_lib_dir: ?[]const u8 = MsvcLibDir.find(gpa, io, arch, env_map) catch |err| switch (err) {
53 error.MsvcLibDirNotFound => null,59 error.MsvcLibDirNotFound => null,
54 error.OutOfMemory => return error.OutOfMemory,60 error.OutOfMemory => return error.OutOfMemory,
55 };61 };
...@@ -671,7 +677,11 @@ const MsvcLibDir = struct {...@@ -671,7 +677,11 @@ const MsvcLibDir = struct {
671 return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch return error.PathNotFound;677 return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch return error.PathNotFound;
672 }678 }
673679
674 fn findInstancesDir(gpa: Allocator, io: Io) error{ OutOfMemory, PathNotFound }!Dir {680 fn findInstancesDir(
681 gpa: Allocator,
682 io: Io,
683 env_map: *const Environ.Map,
684 ) error{ OutOfMemory, PathNotFound }!Dir {
675 // First, try getting the packages cache path from the registry.685 // First, try getting the packages cache path from the registry.
676 // This only seems to exist when the path is different from the default.686 // This only seems to exist when the path is different from the default.
677 method1: {687 method1: {
...@@ -691,16 +701,13 @@ const MsvcLibDir = struct {...@@ -691,16 +701,13 @@ const MsvcLibDir = struct {
691 // If that can't be found, fall back to manually appending701 // If that can't be found, fall back to manually appending
692 // `Microsoft\VisualStudio\Packages\_Instances` to %PROGRAMDATA%702 // `Microsoft\VisualStudio\Packages\_Instances` to %PROGRAMDATA%
693 method3: {703 method3: {
694 const program_data = std.process.getEnvVarOwned(gpa, "PROGRAMDATA") catch |err| switch (err) {704 const program_data = std.zig.EnvVar.PROGRAMDATA.get(env_map) orelse break :method3;
695 error.OutOfMemory => |e| return e,
696 error.InvalidWtf8 => unreachable,
697 error.EnvironmentVariableNotFound => break :method3,
698 };
699 defer gpa.free(program_data);
700705
701 if (!Dir.path.isAbsolute(program_data)) break :method3;706 if (!Dir.path.isAbsolute(program_data)) break :method3;
702707
703 const instances_path = try Dir.path.join(gpa, &.{ program_data, "Microsoft", "VisualStudio", "Packages", "_Instances" });708 const instances_path = try Dir.path.join(gpa, &.{
709 program_data, "Microsoft", "VisualStudio", "Packages", "_Instances",
710 });
704 defer gpa.free(instances_path);711 defer gpa.free(instances_path);
705712
706 return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch break :method3;713 return Dir.openDirAbsolute(io, instances_path, .{ .iterate = true }) catch break :method3;
...@@ -754,12 +761,17 @@ const MsvcLibDir = struct {...@@ -754,12 +761,17 @@ const MsvcLibDir = struct {
754 ///761 ///
755 /// The logic in this function is intended to match what ISetupConfiguration does762 /// The logic in this function is intended to match what ISetupConfiguration does
756 /// under-the-hood, as verified using Procmon.763 /// under-the-hood, as verified using Procmon.
757 fn findViaCOM(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, PathNotFound }![]const u8 {764 fn findViaCOM(
765 gpa: Allocator,
766 io: Io,
767 arch: std.Target.Cpu.Arch,
768 env_map: *const Environ.Map,
769 ) error{ OutOfMemory, PathNotFound }![]const u8 {
758 // Typically `%PROGRAMDATA%\Microsoft\VisualStudio\Packages\_Instances`770 // Typically `%PROGRAMDATA%\Microsoft\VisualStudio\Packages\_Instances`
759 // This will contain directories with names of instance IDs like 80a758ca,771 // This will contain directories with names of instance IDs like 80a758ca,
760 // which will contain `state.json` files that have the version and772 // which will contain `state.json` files that have the version and
761 // installation directory.773 // installation directory.
762 var instances_dir = try findInstancesDir(gpa, io);774 var instances_dir = try findInstancesDir(gpa, io, env_map);
763 defer instances_dir.close(io);775 defer instances_dir.close(io);
764776
765 var state_subpath_buf: [Dir.max_name_bytes + 32]u8 = undefined;777 var state_subpath_buf: [Dir.max_name_bytes + 32]u8 = undefined;
...@@ -856,15 +868,16 @@ const MsvcLibDir = struct {...@@ -856,15 +868,16 @@ const MsvcLibDir = struct {
856 }868 }
857869
858 // https://learn.microsoft.com/en-us/visualstudio/install/tools-for-managing-visual-studio-instances?view=vs-2022#editing-the-registry-for-a-visual-studio-instance870 // https://learn.microsoft.com/en-us/visualstudio/install/tools-for-managing-visual-studio-instances?view=vs-2022#editing-the-registry-for-a-visual-studio-instance
859 fn findViaRegistry(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, PathNotFound }![]const u8 {871 fn findViaRegistry(
872 gpa: Allocator,
873 io: Io,
874 arch: std.Target.Cpu.Arch,
875 env_map: *const Environ.Map,
876 ) error{ OutOfMemory, PathNotFound }![]const u8 {
860877
861 // %localappdata%\Microsoft\VisualStudio\878 // %localappdata%\Microsoft\VisualStudio\
862 // %appdata%\Local\Microsoft\VisualStudio\879 // %appdata%\Local\Microsoft\VisualStudio\
863 const local_app_data_path = (std.zig.EnvVar.LOCALAPPDATA.get(gpa) catch |err| switch (err) {880 const local_app_data_path = std.zig.EnvVar.LOCALAPPDATA.get(env_map) orelse return error.PathNotFound;
864 error.OutOfMemory => |e| return e,
865 error.InvalidWtf8 => return error.PathNotFound,
866 }) orelse return error.PathNotFound;
867 defer gpa.free(local_app_data_path);
868 const visualstudio_folder_path = try Dir.path.join(gpa, &.{881 const visualstudio_folder_path = try Dir.path.join(gpa, &.{
869 local_app_data_path, "Microsoft\\VisualStudio\\",882 local_app_data_path, "Microsoft\\VisualStudio\\",
870 });883 });
...@@ -955,7 +968,7 @@ const MsvcLibDir = struct {...@@ -955,7 +968,7 @@ const MsvcLibDir = struct {
955 gpa: Allocator,968 gpa: Allocator,
956 io: Io,969 io: Io,
957 arch: std.Target.Cpu.Arch,970 arch: std.Target.Cpu.Arch,
958 env_map: *const std.process.Environ.Map,971 env_map: *const Environ.Map,
959 ) error{ OutOfMemory, PathNotFound }![]const u8 {972 ) error{ OutOfMemory, PathNotFound }![]const u8 {
960 var base_path: std.array_list.Managed(u8) = base_path: {973 var base_path: std.array_list.Managed(u8) = base_path: {
961 try_env: {974 try_env: {
...@@ -1029,12 +1042,17 @@ const MsvcLibDir = struct {...@@ -1029,12 +1042,17 @@ const MsvcLibDir = struct {
10291042
1030 /// Find path to MSVC's `lib/` directory.1043 /// Find path to MSVC's `lib/` directory.
1031 /// Caller owns the result.1044 /// Caller owns the result.
1032 pub fn find(gpa: Allocator, io: Io, arch: std.Target.Cpu.Arch) error{ OutOfMemory, MsvcLibDirNotFound }![]const u8 {1045 pub fn find(
1033 const full_path = MsvcLibDir.findViaCOM(gpa, io, arch) catch |err1| switch (err1) {1046 gpa: Allocator,
1047 io: Io,
1048 arch: std.Target.Cpu.Arch,
1049 env_map: *const Environ.Map,
1050 ) error{ OutOfMemory, MsvcLibDirNotFound }![]const u8 {
1051 const full_path = MsvcLibDir.findViaCOM(gpa, io, arch, env_map) catch |err1| switch (err1) {
1034 error.OutOfMemory => return error.OutOfMemory,1052 error.OutOfMemory => return error.OutOfMemory,
1035 error.PathNotFound => MsvcLibDir.findViaRegistry(gpa, io, arch) catch |err2| switch (err2) {1053 error.PathNotFound => MsvcLibDir.findViaRegistry(gpa, io, arch, env_map) catch |err2| switch (err2) {
1036 error.OutOfMemory => return error.OutOfMemory,1054 error.OutOfMemory => return error.OutOfMemory,
1037 error.PathNotFound => MsvcLibDir.findViaVs7Key(gpa, io, arch) catch |err3| switch (err3) {1055 error.PathNotFound => MsvcLibDir.findViaVs7Key(gpa, io, arch, env_map) catch |err3| switch (err3) {
1038 error.OutOfMemory => return error.OutOfMemory,1056 error.OutOfMemory => return error.OutOfMemory,
1039 error.PathNotFound => return error.MsvcLibDirNotFound,1057 error.PathNotFound => return error.MsvcLibDirNotFound,
1040 },1058 },
src/Compilation.zig+1
...@@ -2128,6 +2128,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,...@@ -2128,6 +2128,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
2128 .manifest_dir = options.dirs.local_cache.handle.createDirPathOpen(io, "h", .{}) catch |err| {2128 .manifest_dir = options.dirs.local_cache.handle.createDirPathOpen(io, "h", .{}) catch |err| {
2129 return diag.fail(.{ .create_cache_path = .{ .which = .local, .sub = "h", .err = err } });2129 return diag.fail(.{ .create_cache_path = .{ .which = .local, .sub = "h", .err = err } });
2130 },2130 },
2131 .cwd = options.dirs.cwd,
2131 };2132 };
2132 // These correspond to std.zig.Server.Message.PathPrefix.2133 // These correspond to std.zig.Server.Message.PathPrefix.
2133 cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });2134 cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });
src/Zcu/PerThread.zig+1-4
...@@ -2571,10 +2571,7 @@ fn newEmbedFile(...@@ -2571,10 +2571,7 @@ fn newEmbedFile(
2571 try whole.cache_manifest_mutex.lock(io);2571 try whole.cache_manifest_mutex.lock(io);
2572 defer whole.cache_manifest_mutex.unlock(io);2572 defer whole.cache_manifest_mutex.unlock(io);
25732573
2574 man.addFilePostContents(path_str, contents, new_file.stat) catch |err| switch (err) {2574 try man.addFilePostContents(path_str, contents, new_file.stat);
2575 error.Unexpected => unreachable,
2576 else => |e| return e,
2577 };
2578 }2575 }
25792576
2580 return new_file;2577 return new_file;
src/introspect.zig+8-7
...@@ -6,6 +6,7 @@ const Dir = std.Io.Dir;...@@ -6,6 +6,7 @@ const Dir = std.Io.Dir;
6const mem = std.mem;6const mem = std.mem;
7const Allocator = std.mem.Allocator;7const Allocator = std.mem.Allocator;
8const Cache = std.Build.Cache;8const Cache = std.Build.Cache;
9const assert = std.debug.assert;
910
10const build_options = @import("build_options");11const build_options = @import("build_options");
1112
...@@ -62,14 +63,14 @@ pub fn getResolvedCwd(gpa: Allocator) error{...@@ -62,14 +63,14 @@ pub fn getResolvedCwd(gpa: Allocator) error{
62 if (std.debug.runtime_safety) {63 if (std.debug.runtime_safety) {
63 const cwd = try std.process.getCwdAlloc(gpa);64 const cwd = try std.process.getCwdAlloc(gpa);
64 defer gpa.free(cwd);65 defer gpa.free(cwd);
65 std.debug.assert(mem.eql(u8, cwd, "."));66 assert(mem.eql(u8, cwd, "."));
66 }67 }
67 return "";68 return "";
68 }69 }
69 const cwd = try std.process.getCwdAlloc(gpa);70 const cwd = try std.process.getCwdAlloc(gpa);
70 defer gpa.free(cwd);71 defer gpa.free(cwd);
71 const resolved = try Dir.path.resolve(gpa, &.{cwd});72 const resolved = try Dir.path.resolve(gpa, &.{cwd});
72 std.debug.assert(Dir.path.isAbsolute(resolved));73 assert(Dir.path.isAbsolute(resolved));
73 return resolved;74 return resolved;
74}75}
7576
...@@ -140,7 +141,7 @@ pub fn resolvePath(...@@ -140,7 +141,7 @@ pub fn resolvePath(
140 paths: []const []const u8,141 paths: []const []const u8,
141) Allocator.Error![]u8 {142) Allocator.Error![]u8 {
142 if (builtin.target.os.tag == .wasi) {143 if (builtin.target.os.tag == .wasi) {
143 std.debug.assert(mem.eql(u8, cwd_resolved, ""));144 assert(mem.eql(u8, cwd_resolved, ""));
144 const res = try Dir.path.resolve(gpa, paths);145 const res = try Dir.path.resolve(gpa, paths);
145 if (mem.eql(u8, res, ".")) {146 if (mem.eql(u8, res, ".")) {
146 gpa.free(res);147 gpa.free(res);
...@@ -160,8 +161,8 @@ pub fn resolvePath(...@@ -160,8 +161,8 @@ pub fn resolvePath(
160 gpa.free(res);161 gpa.free(res);
161 return "";162 return "";
162 }163 }
163 std.debug.assert(!Dir.path.isAbsolute(res));164 assert(!Dir.path.isAbsolute(res));
164 std.debug.assert(!isUpDir(res));165 assert(!isUpDir(res));
165 return res;166 return res;
166 }167 }
167168
...@@ -180,8 +181,8 @@ pub fn resolvePath(...@@ -180,8 +181,8 @@ pub fn resolvePath(
180 };181 };
181 errdefer gpa.free(path_resolved);182 errdefer gpa.free(path_resolved);
182183
183 std.debug.assert(Dir.path.isAbsolute(path_resolved));184 assert(Dir.path.isAbsolute(path_resolved));
184 std.debug.assert(Dir.path.isAbsolute(cwd_resolved));185 assert(Dir.path.isAbsolute(cwd_resolved));
185186
186 if (!std.mem.startsWith(u8, path_resolved, cwd_resolved)) return path_resolved; // not in cwd187 if (!std.mem.startsWith(u8, path_resolved, cwd_resolved)) return path_resolved; // not in cwd
187 if (path_resolved.len == cwd_resolved.len) {188 if (path_resolved.len == cwd_resolved.len) {
src/libs/mingw.zig+1
...@@ -259,6 +259,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {...@@ -259,6 +259,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
259 .gpa = gpa,259 .gpa = gpa,
260 .io = io,260 .io = io,
261 .manifest_dir = try comp.dirs.global_cache.handle.createDirPathOpen(io, "h", .{}),261 .manifest_dir = try comp.dirs.global_cache.handle.createDirPathOpen(io, "h", .{}),
262 .cwd = comp.dirs.cwd,
262 };263 };
263 cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });264 cache.addPrefix(.{ .path = null, .handle = Io.Dir.cwd() });
264 cache.addPrefix(comp.dirs.zig_lib);265 cache.addPrefix(comp.dirs.zig_lib);
test/standalone/child_process/main.zig+7-4
...@@ -1,15 +1,15 @@...@@ -1,15 +1,15 @@
1const std = @import("std");1const std = @import("std");
2const Io = std.Io;2const Io = std.Io;
33
4pub fn main() !void {4pub fn main(init: std.process.Init.Minimal) !void {
5 // make sure safety checks are enabled even in release modes5 // make sure safety checks are enabled even in release modes
6 var gpa_state = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};6 var gpa_state: std.heap.GeneralPurposeAllocator(.{ .safety = true }) = .{};
7 defer if (gpa_state.deinit() != .ok) {7 defer if (gpa_state.deinit() != .ok) {
8 @panic("found memory leaks");8 @panic("found memory leaks");
9 };9 };
10 const gpa = gpa_state.allocator();10 const gpa = gpa_state.allocator();
1111
12 var it = try std.process.argsWithAllocator(gpa);12 var it = try init.iterateAllocator(gpa);
13 defer it.deinit();13 defer it.deinit();
14 _ = it.next() orelse unreachable; // skip binary name14 _ = it.next() orelse unreachable; // skip binary name
15 const child_path, const needs_free = child_path: {15 const child_path, const needs_free = child_path: {
...@@ -21,7 +21,10 @@ pub fn main() !void {...@@ -21,7 +21,10 @@ pub fn main() !void {
21 };21 };
22 defer if (needs_free) gpa.free(child_path);22 defer if (needs_free) gpa.free(child_path);
2323
24 var threaded: Io.Threaded = .init(gpa, .{});24 var threaded: Io.Threaded = .init(gpa, .{
25 .argv0 = .init(init.args),
26 .environ = init.environ,
27 });
25 defer threaded.deinit();28 defer threaded.deinit();
26 const io = threaded.io();29 const io = threaded.io();
2730
test/standalone/env_vars/main.zig+3-8
...@@ -2,16 +2,11 @@ const std = @import("std");...@@ -2,16 +2,11 @@ const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4// Note: the environment variables under test are set by the build.zig4// Note: the environment variables under test are set by the build.zig
5pub fn main() !void {5pub fn main(init: std.process.Init) !void {
6 @setEvalBranchQuota(10000);6 @setEvalBranchQuota(10000);
77
8 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;8 const allocator = init.gpa;
9 defer _ = gpa.deinit();9 const arena = init.arena.allocator();
10 const allocator = gpa.allocator();
11
12 var arena_state = std.heap.ArenaAllocator.init(allocator);
13 defer arena_state.deinit();
14 const arena = arena_state.allocator();
1510
16 // hasNonEmptyEnvVar11 // hasNonEmptyEnvVar
17 {12 {
test/standalone/run_output_paths/create_file.zig+3-3
...@@ -1,8 +1,8 @@...@@ -1,8 +1,8 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() !void {3pub fn main(init: std.process.Init) !void {
4 const io = std.Io.Threaded.global_single_threaded.ioBasic();4 const io = init.io;
5 var args = try std.process.argsWithAllocator(std.heap.page_allocator);5 var args = try init.args.iterateAllocator(init.arena.allocator());
6 _ = args.skip();6 _ = args.skip();
7 const dir_name = args.next().?;7 const dir_name = args.next().?;
8 const dir = try std.Io.Dir.cwd().openDir(io, if (std.mem.startsWith(u8, dir_name, "--dir="))8 const dir = try std.Io.Dir.cwd().openDir(io, if (std.mem.startsWith(u8, dir_name, "--dir="))
test/standalone/self_exe_symlink/create-symlink.zig+6-10
...@@ -1,21 +1,17 @@...@@ -1,21 +1,17 @@
1const std = @import("std");1const std = @import("std");
22
3pub fn main() anyerror!void {3pub fn main(init: std.process.Init) !void {
4 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;4 const io = init.io;
5 defer if (gpa.deinit() == .leak) @panic("found memory leaks");5 const gpa = init.gpa;
6 const allocator = gpa.allocator();6 var it = try init.args.iterateAllocator(gpa);
7
8 var it = try std.process.argsWithAllocator(allocator);
9 defer it.deinit();7 defer it.deinit();
10 _ = it.next() orelse unreachable; // skip binary name8 _ = it.next() orelse unreachable; // skip binary name
11 const exe_path = it.next() orelse unreachable;9 const exe_path = it.next() orelse unreachable;
12 const symlink_path = it.next() orelse unreachable;10 const symlink_path = it.next() orelse unreachable;
1311
14 // If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`.12 // If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`.
15 const exe_rel_path = try std.fs.path.relative(allocator, std.fs.path.dirname(symlink_path) orelse ".", exe_path);13 const exe_rel_path = try std.fs.path.relative(gpa, std.fs.path.dirname(symlink_path) orelse ".", exe_path);
16 defer allocator.free(exe_rel_path);14 defer gpa.free(exe_rel_path);
17
18 const io = std.Io.Threaded.global_single_threaded.ioBasic();
1915
20 try std.Io.Dir.cwd().symLink(io, exe_rel_path, symlink_path, .{});16 try std.Io.Dir.cwd().symLink(io, exe_rel_path, symlink_path, .{});
21}17}
test/standalone/simple/cat/main.zig+4-10
...@@ -4,16 +4,10 @@ const mem = std.mem;...@@ -4,16 +4,10 @@ const mem = std.mem;
4const warn = std.log.warn;4const warn = std.log.warn;
5const fatal = std.process.fatal;5const fatal = std.process.fatal;
66
7pub fn main() !void {7pub fn main(init: std.process.Init) !void {
8 var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);8 const arena = init.arena.allocator();
9 defer arena_instance.deinit();9 const io = init.io;
10 const arena = arena_instance.allocator();10 const args = try init.args.toSlice(arena);
11
12 var threaded: std.Io.Threaded = .init(arena, .{});
13 defer threaded.deinit();
14 const io = threaded.io();
15
16 const args = try std.process.argsAlloc(arena);
1711
18 const exe = args[0];12 const exe = args[0];
19 var catted_anything = false;13 var catted_anything = false;