authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-22 16:10:43-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-12-23 22:15:12-08:00
log7c1236e267e536379f8b91148117fb0b8e965334
tree3ab9d70317961171ff2978c61cbdc903948a4240
parent9aee45be97020e65a3eaca67f7a343823a3439f5

std: different way of doing some options

to avoid dependency loops

6 files changed, 17 insertions(+), 17 deletions(-)

lib/std/Io/File.zig+1-1
...@@ -389,7 +389,7 @@ pub fn setOwner(file: File, io: Io, owner: ?Uid, group: ?Gid) SetOwnerError!void...@@ -389,7 +389,7 @@ pub fn setOwner(file: File, io: Io, owner: ?Uid, group: ?Gid) SetOwnerError!void
389/// Cross-platform representation of permissions on a file.389/// Cross-platform representation of permissions on a file.
390///390///
391/// On POSIX systems this corresponds to "mode" and on Windows this corresponds to "attributes".391/// On POSIX systems this corresponds to "mode" and on Windows this corresponds to "attributes".
392pub const Permissions = if (is_windows) enum(std.os.windows.DWORD) {392pub const Permissions = std.Options.FilePermissions orelse if (is_windows) enum(std.os.windows.DWORD) {
393 default_file = 0,393 default_file = 0,
394 _,394 _,
395395
lib/std/Thread.zig+1-1
...@@ -322,7 +322,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co...@@ -322,7 +322,7 @@ pub fn getName(self: Thread, buffer_ptr: *[max_name_len:0]u8) GetNameError!?[]co
322 var buf: [32]u8 = undefined;322 var buf: [32]u8 = undefined;
323 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});323 const path = try std.fmt.bufPrint(&buf, "/proc/self/task/{d}/comm", .{self.getHandle()});
324324
325 const io = std.options.debug_io;325 const io = std.Options.debug_io;
326326
327 const file = try Io.Dir.cwd().openFile(io, path, .{});327 const file = try Io.Dir.cwd().openFile(io, path, .{});
328 defer file.close(io);328 defer file.close(io);
lib/std/debug.zig+5-5
...@@ -280,7 +280,7 @@ pub const sys_can_stack_trace = switch (builtin.cpu.arch) {...@@ -280,7 +280,7 @@ pub const sys_can_stack_trace = switch (builtin.cpu.arch) {
280/// Alternatively, use the higher-level `Io.lockStderr` to integrate with the280/// Alternatively, use the higher-level `Io.lockStderr` to integrate with the
281/// application's chosen `Io` implementation.281/// application's chosen `Io` implementation.
282pub fn lockStderr(buffer: []u8) Io.LockedStderr {282pub fn lockStderr(buffer: []u8) Io.LockedStderr {
283 const io = std.options.debug_io;283 const io = std.Options.debug_io;
284 const prev = io.swapCancelProtection(.blocked);284 const prev = io.swapCancelProtection(.blocked);
285 defer _ = io.swapCancelProtection(prev);285 defer _ = io.swapCancelProtection(prev);
286 return io.lockStderr(buffer, null) catch |err| switch (err) {286 return io.lockStderr(buffer, null) catch |err| switch (err) {
...@@ -289,7 +289,7 @@ pub fn lockStderr(buffer: []u8) Io.LockedStderr {...@@ -289,7 +289,7 @@ pub fn lockStderr(buffer: []u8) Io.LockedStderr {
289}289}
290290
291pub fn unlockStderr() void {291pub fn unlockStderr() void {
292 const io = std.options.debug_io;292 const io = std.Options.debug_io;
293 io.unlockStderr();293 io.unlockStderr();
294}294}
295295
...@@ -623,7 +623,7 @@ pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf:...@@ -623,7 +623,7 @@ pub noinline fn captureCurrentStackTrace(options: StackUnwindOptions, addr_buf:
623 defer it.deinit();623 defer it.deinit();
624 if (!it.stratOk(options.allow_unsafe_unwind)) return empty_trace;624 if (!it.stratOk(options.allow_unsafe_unwind)) return empty_trace;
625625
626 const io = std.options.debug_io;626 const io = std.Options.debug_io;
627627
628 var total_frames: usize = 0;628 var total_frames: usize = 0;
629 var index: usize = 0;629 var index: usize = 0;
...@@ -685,7 +685,7 @@ pub noinline fn writeCurrentStackTrace(options: StackUnwindOptions, t: Io.Termin...@@ -685,7 +685,7 @@ pub noinline fn writeCurrentStackTrace(options: StackUnwindOptions, t: Io.Termin
685 var total_frames: usize = 0;685 var total_frames: usize = 0;
686 var wait_for = options.first_address;686 var wait_for = options.first_address;
687 var printed_any_frame = false;687 var printed_any_frame = false;
688 const io = std.options.debug_io;688 const io = std.Options.debug_io;
689 while (true) switch (it.next(io)) {689 while (true) switch (it.next(io)) {
690 .switch_to_fp => |unwind_error| {690 .switch_to_fp => |unwind_error| {
691 switch (StackIterator.fp_usability) {691 switch (StackIterator.fp_usability) {
...@@ -793,7 +793,7 @@ pub fn writeStackTrace(st: *const StackTrace, t: Io.Terminal) Writer.Error!void...@@ -793,7 +793,7 @@ pub fn writeStackTrace(st: *const StackTrace, t: Io.Terminal) Writer.Error!void
793 return;793 return;
794 },794 },
795 };795 };
796 const io = std.options.debug_io;796 const io = std.Options.debug_io;
797 const captured_frames = @min(n_frames, st.instruction_addresses.len);797 const captured_frames = @min(n_frames, st.instruction_addresses.len);
798 for (st.instruction_addresses[0..captured_frames]) |ret_addr| {798 for (st.instruction_addresses[0..captured_frames]) |ret_addr| {
799 // `ret_addr` is the return address, which is *after* the function call.799 // `ret_addr` is the return address, which is *after* the function call.
lib/std/dynamic_library.zig+1-1
...@@ -222,7 +222,7 @@ pub const ElfDynLib = struct {...@@ -222,7 +222,7 @@ pub const ElfDynLib = struct {
222222
223 /// Trusts the file. Malicious file will be able to execute arbitrary code.223 /// Trusts the file. Malicious file will be able to execute arbitrary code.
224 pub fn open(path: []const u8) Error!ElfDynLib {224 pub fn open(path: []const u8) Error!ElfDynLib {
225 const io = std.options.debug_io;225 const io = std.Options.debug_io;
226226
227 const fd = try resolveFromName(io, path);227 const fd = try resolveFromName(io, path);
228 defer posix.close(fd);228 defer posix.close(fd);
lib/std/start.zig+2-2
...@@ -669,7 +669,7 @@ inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {...@@ -669,7 +669,7 @@ inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
669 std.os.argv = argv[0..argc];669 std.os.argv = argv[0..argc];
670 std.os.environ = envp;670 std.os.environ = envp;
671671
672 if (std.io_options.debug_threaded_io) |t| {672 if (std.Options.debug_threaded_io) |t| {
673 if (@sizeOf(std.Io.Threaded.Argv0) != 0) t.argv0.value = argv[0];673 if (@sizeOf(std.Io.Threaded.Argv0) != 0) t.argv0.value = argv[0];
674 t.environ = .{ .block = envp };674 t.environ = .{ .block = envp };
675 }675 }
...@@ -698,7 +698,7 @@ fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.c) c_int {...@@ -698,7 +698,7 @@ fn mainWithoutEnv(c_argc: c_int, c_argv: [*][*:0]c_char) callconv(.c) c_int {
698 std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@intCast(c_argc)];698 std.os.argv = @as([*][*:0]u8, @ptrCast(c_argv))[0..@intCast(c_argc)];
699699
700 if (@sizeOf(std.Io.Threaded.Argv0) != 0) {700 if (@sizeOf(std.Io.Threaded.Argv0) != 0) {
701 if (std.io_options.debug_threaded_io) |t| t.argv0.value = std.os.argv[0];701 if (std.Options.debug_threaded_io) |t| t.argv0.value = std.os.argv[0];
702 }702 }
703703
704 return callMain();704 return callMain();
lib/std/std.zig+7-7
...@@ -110,9 +110,6 @@ const root = @import("root");...@@ -110,9 +110,6 @@ const root = @import("root");
110110
111/// Compile-time known settings overridable by the root source file.111/// Compile-time known settings overridable by the root source file.
112pub const options: Options = if (@hasDecl(root, "std_options")) root.std_options else .{};112pub const options: Options = if (@hasDecl(root, "std_options")) root.std_options else .{};
113/// Minimal set of `options` moved here to avoid dependency loop compilation
114/// errors.
115pub const io_options: IoOptions = if (@hasDecl(root, "std_io_options")) root.std_io_options else .{};
116113
117pub const Options = struct {114pub const Options = struct {
118 enable_segfault_handler: bool = debug.default_enable_segfault_handler,115 enable_segfault_handler: bool = debug.default_enable_segfault_handler,
...@@ -177,6 +174,10 @@ pub const Options = struct {...@@ -177,6 +174,10 @@ pub const Options = struct {
177 /// stack traces will just print an error to the relevant `Io.Writer` and return.174 /// stack traces will just print an error to the relevant `Io.Writer` and return.
178 allow_stack_tracing: bool = !@import("builtin").strip_debug_info,175 allow_stack_tracing: bool = !@import("builtin").strip_debug_info,
179176
177 pub const debug_threaded_io: ?*Io.Threaded = if (@hasDecl(root, "std_options_debug_threaded_io"))
178 root.std_options_debug_threaded_io
179 else
180 Io.Threaded.global_single_threaded;
180 /// The `Io` instance that `std.debug` uses for `std.debug.print`,181 /// The `Io` instance that `std.debug` uses for `std.debug.print`,
181 /// capturing stack traces, loading debug info, finding the executable's182 /// capturing stack traces, loading debug info, finding the executable's
182 /// own path, and environment variables that affect terminal mode183 /// own path, and environment variables that affect terminal mode
...@@ -186,11 +187,10 @@ pub const Options = struct {...@@ -186,11 +187,10 @@ pub const Options = struct {
186 /// implementation based on coroutines, one likely wants `std.debug.print`187 /// implementation based on coroutines, one likely wants `std.debug.print`
187 /// to directly write to stderr without trying to interact with the code188 /// to directly write to stderr without trying to interact with the code
188 /// being debugged.189 /// being debugged.
189 debug_io: Io = io_options.debug_threaded_io.?.ioBasic(),190 pub const debug_io: Io = if (@hasDecl(root, "std_options_debug_io")) root.std_options_debug_io else debug_threaded_io.?.ioBasic();
190};
191191
192pub const IoOptions = struct {192 /// Overrides `std.Io.File.Permissions`.
193 debug_threaded_io: ?*Io.Threaded = Io.Threaded.global_single_threaded,193 pub const FilePermissions: ?type = if (@hasDecl(root, "std_options_FilePermissions")) root.std_options_FilePermissions else null;
194};194};
195195
196// This forces the start.zig file to be imported, and the comptime logic inside that196// This forces the start.zig file to be imported, and the comptime logic inside that