authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-24 01:06:03-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-10-24 01:14:52-04:00
log60cd11bd4b48e4dfdf11d1f25cb1ee842a49ee1d
tree457ea0157f0f42dccf905593426154be8682812b
parent8591731f2b938b2b55b05181d5ec0e27f79c86fa
signaturelock-open Commit is signed but in an unrecognized format.

get rid of std.os.foo.is_the_target

It had the downside of running all the comptime blocks and resolving all the usingnamespaces of each system, when just trying to discover if the current system is a particular one. For Darwin, where it's nice to use `std.Target.current.isDarwin()`, this demonstrates the utility that #425 would provide.

26 files changed, 170 insertions(+), 166 deletions(-)

lib/std/build.zig+1-1
...@@ -2000,7 +2000,7 @@ pub const RunStep = struct {...@@ -2000,7 +2000,7 @@ pub const RunStep = struct {
2000 }2000 }
20012001
2002 pub fn addPathDir(self: *RunStep, search_path: []const u8) void {2002 pub fn addPathDir(self: *RunStep, search_path: []const u8) void {
2003 const PATH = if (std.os.windows.is_the_target) "Path" else "PATH";2003 const PATH = if (builtin.os == .windows) "Path" else "PATH";
2004 const env_map = self.getEnvMap();2004 const env_map = self.getEnvMap();
2005 const prev_path = env_map.get(PATH) orelse {2005 const prev_path = env_map.get(PATH) orelse {
2006 env_map.set(PATH, search_path) catch unreachable;2006 env_map.set(PATH, search_path) catch unreachable;
lib/std/c/darwin.zig+1-1
...@@ -36,7 +36,7 @@ const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header;...@@ -36,7 +36,7 @@ const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header;
36/// export a weak symbol here, to be overridden by the real one.36/// export a weak symbol here, to be overridden by the real one.
37pub extern "c" var _mh_execute_header: mach_hdr = undefined;37pub extern "c" var _mh_execute_header: mach_hdr = undefined;
38comptime {38comptime {
39 if (std.os.darwin.is_the_target) {39 if (std.Target.current.isDarwin()) {
40 @export("_mh_execute_header", _mh_execute_header, .Weak);40 @export("_mh_execute_header", _mh_execute_header, .Weak);
41 }41 }
42}42}
lib/std/child_process.zig+12-12
...@@ -17,9 +17,9 @@ const TailQueue = std.TailQueue;...@@ -17,9 +17,9 @@ const TailQueue = std.TailQueue;
17const maxInt = std.math.maxInt;17const maxInt = std.math.maxInt;
1818
19pub const ChildProcess = struct {19pub const ChildProcess = struct {
20 pid: if (os.windows.is_the_target) void else i32,20 pid: if (builtin.os == .windows) void else i32,
21 handle: if (os.windows.is_the_target) windows.HANDLE else void,21 handle: if (builtin.os == .windows) windows.HANDLE else void,
22 thread_handle: if (os.windows.is_the_target) windows.HANDLE else void,22 thread_handle: if (builtin.os == .windows) windows.HANDLE else void,
2323
24 allocator: *mem.Allocator,24 allocator: *mem.Allocator,
2525
...@@ -39,16 +39,16 @@ pub const ChildProcess = struct {...@@ -39,16 +39,16 @@ pub const ChildProcess = struct {
39 stderr_behavior: StdIo,39 stderr_behavior: StdIo,
4040
41 /// Set to change the user id when spawning the child process.41 /// Set to change the user id when spawning the child process.
42 uid: if (os.windows.is_the_target) void else ?u32,42 uid: if (builtin.os == .windows) void else ?u32,
4343
44 /// Set to change the group id when spawning the child process.44 /// Set to change the group id when spawning the child process.
45 gid: if (os.windows.is_the_target) void else ?u32,45 gid: if (builtin.os == .windows) void else ?u32,
4646
47 /// Set to change the current working directory when spawning the child process.47 /// Set to change the current working directory when spawning the child process.
48 cwd: ?[]const u8,48 cwd: ?[]const u8,
4949
50 err_pipe: if (os.windows.is_the_target) void else [2]os.fd_t,50 err_pipe: if (builtin.os == .windows) void else [2]os.fd_t,
51 llnode: if (os.windows.is_the_target) void else TailQueue(*ChildProcess).Node,51 llnode: if (builtin.os == .windows) void else TailQueue(*ChildProcess).Node,
5252
53 pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||53 pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||
54 os.ChangeCurDirError || windows.CreateProcessError;54 os.ChangeCurDirError || windows.CreateProcessError;
...@@ -82,8 +82,8 @@ pub const ChildProcess = struct {...@@ -82,8 +82,8 @@ pub const ChildProcess = struct {
82 .term = null,82 .term = null,
83 .env_map = null,83 .env_map = null,
84 .cwd = null,84 .cwd = null,
85 .uid = if (os.windows.is_the_target) {} else null,85 .uid = if (builtin.os == .windows) {} else null,
86 .gid = if (os.windows.is_the_target) {} else null,86 .gid = if (builtin.os == .windows) {} else null,
87 .stdin = null,87 .stdin = null,
88 .stdout = null,88 .stdout = null,
89 .stderr = null,89 .stderr = null,
...@@ -103,7 +103,7 @@ pub const ChildProcess = struct {...@@ -103,7 +103,7 @@ pub const ChildProcess = struct {
103103
104 /// On success must call `kill` or `wait`.104 /// On success must call `kill` or `wait`.
105 pub fn spawn(self: *ChildProcess) !void {105 pub fn spawn(self: *ChildProcess) !void {
106 if (os.windows.is_the_target) {106 if (builtin.os == .windows) {
107 return self.spawnWindows();107 return self.spawnWindows();
108 } else {108 } else {
109 return self.spawnPosix();109 return self.spawnPosix();
...@@ -117,7 +117,7 @@ pub const ChildProcess = struct {...@@ -117,7 +117,7 @@ pub const ChildProcess = struct {
117117
118 /// Forcibly terminates child process and then cleans up all resources.118 /// Forcibly terminates child process and then cleans up all resources.
119 pub fn kill(self: *ChildProcess) !Term {119 pub fn kill(self: *ChildProcess) !Term {
120 if (os.windows.is_the_target) {120 if (builtin.os == .windows) {
121 return self.killWindows(1);121 return self.killWindows(1);
122 } else {122 } else {
123 return self.killPosix();123 return self.killPosix();
...@@ -147,7 +147,7 @@ pub const ChildProcess = struct {...@@ -147,7 +147,7 @@ pub const ChildProcess = struct {
147147
148 /// Blocks until child process terminates and then cleans up all resources.148 /// Blocks until child process terminates and then cleans up all resources.
149 pub fn wait(self: *ChildProcess) !Term {149 pub fn wait(self: *ChildProcess) !Term {
150 if (os.windows.is_the_target) {150 if (builtin.os == .windows) {
151 return self.waitWindows();151 return self.waitWindows();
152 } else {152 } else {
153 return self.waitPosix();153 return self.waitPosix();
lib/std/debug.zig+8-8
...@@ -133,7 +133,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {...@@ -133,7 +133,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
133/// chopping off the irrelevant frames and shifting so that the returned addresses pointer133/// chopping off the irrelevant frames and shifting so that the returned addresses pointer
134/// equals the passed in addresses pointer.134/// equals the passed in addresses pointer.
135pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void {135pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void {
136 if (windows.is_the_target) {136 if (builtin.os == .windows) {
137 const addrs = stack_trace.instruction_addresses;137 const addrs = stack_trace.instruction_addresses;
138 const u32_addrs_len = @intCast(u32, addrs.len);138 const u32_addrs_len = @intCast(u32, addrs.len);
139 const first_addr = first_address orelse {139 const first_addr = first_address orelse {
...@@ -310,7 +310,7 @@ pub const StackIterator = struct {...@@ -310,7 +310,7 @@ pub const StackIterator = struct {
310};310};
311311
312pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void {312pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void {
313 if (windows.is_the_target) {313 if (builtin.os == .windows) {
314 return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr);314 return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr);
315 }315 }
316 var it = StackIterator.init(start_addr);316 var it = StackIterator.init(start_addr);
...@@ -342,10 +342,10 @@ pub fn writeCurrentStackTraceWindows(...@@ -342,10 +342,10 @@ pub fn writeCurrentStackTraceWindows(
342/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,342/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,
343/// make this `noasync fn` and remove the individual noasync calls.343/// make this `noasync fn` and remove the individual noasync calls.
344pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {344pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {
345 if (windows.is_the_target) {345 if (builtin.os == .windows) {
346 return noasync printSourceAtAddressWindows(debug_info, out_stream, address, tty_color);346 return noasync printSourceAtAddressWindows(debug_info, out_stream, address, tty_color);
347 }347 }
348 if (os.darwin.is_the_target) {348 if (comptime std.Target.current.isDarwin()) {
349 return noasync printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color);349 return noasync printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color);
350 }350 }
351 return noasync printSourceAtAddressPosix(debug_info, out_stream, address, tty_color);351 return noasync printSourceAtAddressPosix(debug_info, out_stream, address, tty_color);
...@@ -832,10 +832,10 @@ pub const OpenSelfDebugInfoError = error{...@@ -832,10 +832,10 @@ pub const OpenSelfDebugInfoError = error{
832pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {832pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
833 if (builtin.strip_debug_info)833 if (builtin.strip_debug_info)
834 return error.MissingDebugInfo;834 return error.MissingDebugInfo;
835 if (windows.is_the_target) {835 if (builtin.os == .windows) {
836 return noasync openSelfDebugInfoWindows(allocator);836 return noasync openSelfDebugInfoWindows(allocator);
837 }837 }
838 if (os.darwin.is_the_target) {838 if (comptime std.Target.current.isDarwin()) {
839 return noasync openSelfDebugInfoMacOs(allocator);839 return noasync openSelfDebugInfoMacOs(allocator);
840 }840 }
841 return noasync openSelfDebugInfoPosix(allocator);841 return noasync openSelfDebugInfoPosix(allocator);
...@@ -2364,7 +2364,7 @@ pub fn attachSegfaultHandler() void {...@@ -2364,7 +2364,7 @@ pub fn attachSegfaultHandler() void {
2364 if (!have_segfault_handling_support) {2364 if (!have_segfault_handling_support) {
2365 @compileError("segfault handler not supported for this target");2365 @compileError("segfault handler not supported for this target");
2366 }2366 }
2367 if (windows.is_the_target) {2367 if (builtin.os == .windows) {
2368 windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);2368 windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
2369 return;2369 return;
2370 }2370 }
...@@ -2378,7 +2378,7 @@ pub fn attachSegfaultHandler() void {...@@ -2378,7 +2378,7 @@ pub fn attachSegfaultHandler() void {
2378}2378}
23792379
2380fn resetSegfaultHandler() void {2380fn resetSegfaultHandler() void {
2381 if (windows.is_the_target) {2381 if (builtin.os == .windows) {
2382 if (windows_segfault_handle) |handle| {2382 if (windows_segfault_handle) |handle| {
2383 assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);2383 assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
2384 windows_segfault_handle = null;2384 windows_segfault_handle = null;
lib/std/event/channel.zig+1-1
...@@ -307,7 +307,7 @@ test "std.event.Channel" {...@@ -307,7 +307,7 @@ test "std.event.Channel" {
307 // https://github.com/ziglang/zig/issues/1908307 // https://github.com/ziglang/zig/issues/1908
308 if (builtin.single_threaded) return error.SkipZigTest;308 if (builtin.single_threaded) return error.SkipZigTest;
309 // https://github.com/ziglang/zig/issues/3251309 // https://github.com/ziglang/zig/issues/3251
310 if (std.os.freebsd.is_the_target) return error.SkipZigTest;310 if (builtin.os == .freebsd) return error.SkipZigTest;
311311
312 var loop: Loop = undefined;312 var loop: Loop = undefined;
313 // TODO make a multi threaded test313 // TODO make a multi threaded test
lib/std/event/fs.zig+1-1
...@@ -909,7 +909,7 @@ fn hashString(s: []const u16) u32 {...@@ -909,7 +909,7 @@ fn hashString(s: []const u16) u32 {
909// var close_op_consumed = false;909// var close_op_consumed = false;
910// defer if (!close_op_consumed) close_op.finish();910// defer if (!close_op_consumed) close_op.finish();
911//911//
912// const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0;912// const flags = if (comptime std.Target.current.isDarwin()) os.O_SYMLINK | os.O_EVTONLY else 0;
913// const mode = 0;913// const mode = 0;
914// const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable);914// const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable);
915// close_op.setHandle(fd);915// close_op.setHandle(fd);
lib/std/event/future.zig+1-1
...@@ -86,7 +86,7 @@ test "std.event.Future" {...@@ -86,7 +86,7 @@ test "std.event.Future" {
86 // https://github.com/ziglang/zig/issues/190886 // https://github.com/ziglang/zig/issues/1908
87 if (builtin.single_threaded) return error.SkipZigTest;87 if (builtin.single_threaded) return error.SkipZigTest;
88 // https://github.com/ziglang/zig/issues/325188 // https://github.com/ziglang/zig/issues/3251
89 if (std.os.freebsd.is_the_target) return error.SkipZigTest;89 if (builtin.os == .freebsd) return error.SkipZigTest;
9090
91 const allocator = std.heap.direct_allocator;91 const allocator = std.heap.direct_allocator;
9292
lib/std/event/lock.zig+1-1
...@@ -119,7 +119,7 @@ test "std.event.Lock" {...@@ -119,7 +119,7 @@ test "std.event.Lock" {
119 // TODO https://github.com/ziglang/zig/issues/1908119 // TODO https://github.com/ziglang/zig/issues/1908
120 if (builtin.single_threaded) return error.SkipZigTest;120 if (builtin.single_threaded) return error.SkipZigTest;
121 // TODO https://github.com/ziglang/zig/issues/3251121 // TODO https://github.com/ziglang/zig/issues/3251
122 if (std.os.freebsd.is_the_target) return error.SkipZigTest;122 if (builtin.os == .freebsd) return error.SkipZigTest;
123123
124 const allocator = std.heap.direct_allocator;124 const allocator = std.heap.direct_allocator;
125125
lib/std/fs.zig+9-9
...@@ -255,7 +255,7 @@ pub const AtomicFile = struct {...@@ -255,7 +255,7 @@ pub const AtomicFile = struct {
255 assert(!self.finished);255 assert(!self.finished);
256 self.file.close();256 self.file.close();
257 self.finished = true;257 self.finished = true;
258 if (os.windows.is_the_target) {258 if (builtin.os == .windows) {
259 const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path);259 const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path);
260 const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);260 const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);
261 return os.renameW(&tmp_path_w, &dest_path_w);261 return os.renameW(&tmp_path_w, &dest_path_w);
...@@ -659,7 +659,7 @@ pub const Dir = struct {...@@ -659,7 +659,7 @@ pub const Dir = struct {
659 /// Closing the returned `Dir` is checked illegal behavior.659 /// Closing the returned `Dir` is checked illegal behavior.
660 /// On POSIX targets, this function is comptime-callable.660 /// On POSIX targets, this function is comptime-callable.
661 pub fn cwd() Dir {661 pub fn cwd() Dir {
662 if (os.windows.is_the_target) {662 if (builtin.os == .windows) {
663 return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };663 return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
664 } else {664 } else {
665 return Dir{ .fd = os.AT_FDCWD };665 return Dir{ .fd = os.AT_FDCWD };
...@@ -711,7 +711,7 @@ pub const Dir = struct {...@@ -711,7 +711,7 @@ pub const Dir = struct {
711711
712 /// Call `close` on the result when done.712 /// Call `close` on the result when done.
713 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {713 pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
714 if (os.windows.is_the_target) {714 if (builtin.os == .windows) {
715 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);715 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
716 return self.openDirW(&sub_path_w);716 return self.openDirW(&sub_path_w);
717 }717 }
...@@ -722,7 +722,7 @@ pub const Dir = struct {...@@ -722,7 +722,7 @@ pub const Dir = struct {
722722
723 /// Same as `openDir` except the parameter is null-terminated.723 /// Same as `openDir` except the parameter is null-terminated.
724 pub fn openDirC(self: Dir, sub_path_c: [*]const u8) OpenError!Dir {724 pub fn openDirC(self: Dir, sub_path_c: [*]const u8) OpenError!Dir {
725 if (os.windows.is_the_target) {725 if (builtin.os == .windows) {
726 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);726 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
727 return self.openDirW(&sub_path_w);727 return self.openDirW(&sub_path_w);
728 }728 }
...@@ -829,7 +829,7 @@ pub const Dir = struct {...@@ -829,7 +829,7 @@ pub const Dir = struct {
829 /// Returns `error.DirNotEmpty` if the directory is not empty.829 /// Returns `error.DirNotEmpty` if the directory is not empty.
830 /// To delete a directory recursively, see `deleteTree`.830 /// To delete a directory recursively, see `deleteTree`.
831 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {831 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
832 if (os.windows.is_the_target) {832 if (builtin.os == .windows) {
833 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);833 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
834 return self.deleteDirW(&sub_path_w);834 return self.deleteDirW(&sub_path_w);
835 }835 }
...@@ -1146,10 +1146,10 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {...@@ -1146,10 +1146,10 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
1146pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;1146pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;
11471147
1148pub fn openSelfExe() OpenSelfExeError!File {1148pub fn openSelfExe() OpenSelfExeError!File {
1149 if (os.linux.is_the_target) {1149 if (builtin.os == .linux) {
1150 return File.openReadC(c"/proc/self/exe");1150 return File.openReadC(c"/proc/self/exe");
1151 }1151 }
1152 if (os.windows.is_the_target) {1152 if (builtin.os == .windows) {
1153 var buf: [os.windows.PATH_MAX_WIDE]u16 = undefined;1153 var buf: [os.windows.PATH_MAX_WIDE]u16 = undefined;
1154 const wide_slice = try selfExePathW(&buf);1154 const wide_slice = try selfExePathW(&buf);
1155 return File.openReadW(wide_slice.ptr);1155 return File.openReadW(wide_slice.ptr);
...@@ -1180,7 +1180,7 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;...@@ -1180,7 +1180,7 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
1180/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.1180/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
1181/// TODO make the return type of this a null terminated pointer1181/// TODO make the return type of this a null terminated pointer
1182pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {1182pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1183 if (os.darwin.is_the_target) {1183 if (comptime std.Target.current.isDarwin()) {
1184 var u32_len: u32 = out_buffer.len;1184 var u32_len: u32 = out_buffer.len;
1185 const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);1185 const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
1186 if (rc != 0) return error.NameTooLong;1186 if (rc != 0) return error.NameTooLong;
...@@ -1228,7 +1228,7 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {...@@ -1228,7 +1228,7 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
1228/// Get the directory path that contains the current executable.1228/// Get the directory path that contains the current executable.
1229/// Returned value is a slice of out_buffer.1229/// Returned value is a slice of out_buffer.
1230pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {1230pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {
1231 if (os.linux.is_the_target) {1231 if (builtin.os == .linux) {
1232 // If the currently executing binary has been deleted,1232 // If the currently executing binary has been deleted,
1233 // the file path looks something like `/a/b/c/exe (deleted)`1233 // the file path looks something like `/a/b/c/exe (deleted)`
1234 // This path cannot be opened, but it's valid for determining the directory1234 // This path cannot be opened, but it's valid for determining the directory
lib/std/fs/file.zig+11-11
...@@ -27,7 +27,7 @@ pub const File = struct {...@@ -27,7 +27,7 @@ pub const File = struct {
2727
28 /// Call close to clean up.28 /// Call close to clean up.
29 pub fn openRead(path: []const u8) OpenError!File {29 pub fn openRead(path: []const u8) OpenError!File {
30 if (windows.is_the_target) {30 if (builtin.os == .windows) {
31 const path_w = try windows.sliceToPrefixedFileW(path);31 const path_w = try windows.sliceToPrefixedFileW(path);
32 return openReadW(&path_w);32 return openReadW(&path_w);
33 }33 }
...@@ -37,7 +37,7 @@ pub const File = struct {...@@ -37,7 +37,7 @@ pub const File = struct {
3737
38 /// `openRead` except with a null terminated path38 /// `openRead` except with a null terminated path
39 pub fn openReadC(path: [*]const u8) OpenError!File {39 pub fn openReadC(path: [*]const u8) OpenError!File {
40 if (windows.is_the_target) {40 if (builtin.os == .windows) {
41 const path_w = try windows.cStrToPrefixedFileW(path);41 const path_w = try windows.cStrToPrefixedFileW(path);
42 return openReadW(&path_w);42 return openReadW(&path_w);
43 }43 }
...@@ -69,7 +69,7 @@ pub const File = struct {...@@ -69,7 +69,7 @@ pub const File = struct {
69 /// If a file already exists in the destination it will be truncated.69 /// If a file already exists in the destination it will be truncated.
70 /// Call close to clean up.70 /// Call close to clean up.
71 pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {71 pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {
72 if (windows.is_the_target) {72 if (builtin.os == .windows) {
73 const path_w = try windows.sliceToPrefixedFileW(path);73 const path_w = try windows.sliceToPrefixedFileW(path);
74 return openWriteModeW(&path_w, file_mode);74 return openWriteModeW(&path_w, file_mode);
75 }75 }
...@@ -79,7 +79,7 @@ pub const File = struct {...@@ -79,7 +79,7 @@ pub const File = struct {
7979
80 /// Same as `openWriteMode` except `path` is null-terminated.80 /// Same as `openWriteMode` except `path` is null-terminated.
81 pub fn openWriteModeC(path: [*]const u8, file_mode: Mode) OpenError!File {81 pub fn openWriteModeC(path: [*]const u8, file_mode: Mode) OpenError!File {
82 if (windows.is_the_target) {82 if (builtin.os == .windows) {
83 const path_w = try windows.cStrToPrefixedFileW(path);83 const path_w = try windows.cStrToPrefixedFileW(path);
84 return openWriteModeW(&path_w, file_mode);84 return openWriteModeW(&path_w, file_mode);
85 }85 }
...@@ -106,7 +106,7 @@ pub const File = struct {...@@ -106,7 +106,7 @@ pub const File = struct {
106 /// If a file already exists in the destination this returns OpenError.PathAlreadyExists106 /// If a file already exists in the destination this returns OpenError.PathAlreadyExists
107 /// Call close to clean up.107 /// Call close to clean up.
108 pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {108 pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
109 if (windows.is_the_target) {109 if (builtin.os == .windows) {
110 const path_w = try windows.sliceToPrefixedFileW(path);110 const path_w = try windows.sliceToPrefixedFileW(path);
111 return openWriteNoClobberW(&path_w, file_mode);111 return openWriteNoClobberW(&path_w, file_mode);
112 }112 }
...@@ -115,7 +115,7 @@ pub const File = struct {...@@ -115,7 +115,7 @@ pub const File = struct {
115 }115 }
116116
117 pub fn openWriteNoClobberC(path: [*]const u8, file_mode: Mode) OpenError!File {117 pub fn openWriteNoClobberC(path: [*]const u8, file_mode: Mode) OpenError!File {
118 if (windows.is_the_target) {118 if (builtin.os == .windows) {
119 const path_w = try windows.cStrToPrefixedFileW(path);119 const path_w = try windows.cStrToPrefixedFileW(path);
120 return openWriteNoClobberW(&path_w, file_mode);120 return openWriteNoClobberW(&path_w, file_mode);
121 }121 }
...@@ -174,7 +174,7 @@ pub const File = struct {...@@ -174,7 +174,7 @@ pub const File = struct {
174174
175 /// Test whether ANSI escape codes will be treated as such.175 /// Test whether ANSI escape codes will be treated as such.
176 pub fn supportsAnsiEscapeCodes(self: File) bool {176 pub fn supportsAnsiEscapeCodes(self: File) bool {
177 if (windows.is_the_target) {177 if (builtin.os == .windows) {
178 return os.isCygwinPty(self.handle);178 return os.isCygwinPty(self.handle);
179 }179 }
180 if (self.isTty()) {180 if (self.isTty()) {
...@@ -214,7 +214,7 @@ pub const File = struct {...@@ -214,7 +214,7 @@ pub const File = struct {
214 }214 }
215215
216 pub fn getEndPos(self: File) GetPosError!u64 {216 pub fn getEndPos(self: File) GetPosError!u64 {
217 if (windows.is_the_target) {217 if (builtin.os == .windows) {
218 return windows.GetFileSizeEx(self.handle);218 return windows.GetFileSizeEx(self.handle);
219 }219 }
220 return (try self.stat()).size;220 return (try self.stat()).size;
...@@ -223,7 +223,7 @@ pub const File = struct {...@@ -223,7 +223,7 @@ pub const File = struct {
223 pub const ModeError = os.FStatError;223 pub const ModeError = os.FStatError;
224224
225 pub fn mode(self: File) ModeError!Mode {225 pub fn mode(self: File) ModeError!Mode {
226 if (windows.is_the_target) {226 if (builtin.os == .windows) {
227 return {};227 return {};
228 }228 }
229 return (try self.stat()).mode;229 return (try self.stat()).mode;
...@@ -246,7 +246,7 @@ pub const File = struct {...@@ -246,7 +246,7 @@ pub const File = struct {
246 pub const StatError = os.FStatError;246 pub const StatError = os.FStatError;
247247
248 pub fn stat(self: File) StatError!Stat {248 pub fn stat(self: File) StatError!Stat {
249 if (windows.is_the_target) {249 if (builtin.os == .windows) {
250 var io_status_block: windows.IO_STATUS_BLOCK = undefined;250 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
251 var info: windows.FILE_ALL_INFORMATION = undefined;251 var info: windows.FILE_ALL_INFORMATION = undefined;
252 const rc = windows.ntdll.NtQueryInformationFile(self.handle, &io_status_block, &info, @sizeOf(windows.FILE_ALL_INFORMATION), .FileAllInformation);252 const rc = windows.ntdll.NtQueryInformationFile(self.handle, &io_status_block, &info, @sizeOf(windows.FILE_ALL_INFORMATION), .FileAllInformation);
...@@ -291,7 +291,7 @@ pub const File = struct {...@@ -291,7 +291,7 @@ pub const File = struct {
291 /// last modification timestamp in nanoseconds291 /// last modification timestamp in nanoseconds
292 mtime: i64,292 mtime: i64,
293 ) UpdateTimesError!void {293 ) UpdateTimesError!void {
294 if (windows.is_the_target) {294 if (builtin.os == .windows) {
295 const atime_ft = windows.nanoSecondsToFileTime(atime);295 const atime_ft = windows.nanoSecondsToFileTime(atime);
296 const mtime_ft = windows.nanoSecondsToFileTime(mtime);296 const mtime_ft = windows.nanoSecondsToFileTime(mtime);
297 return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);297 return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);
lib/std/fs/path.zig+17-17
...@@ -13,16 +13,16 @@ const process = std.process;...@@ -13,16 +13,16 @@ const process = std.process;
1313
14pub const sep_windows = '\\';14pub const sep_windows = '\\';
15pub const sep_posix = '/';15pub const sep_posix = '/';
16pub const sep = if (windows.is_the_target) sep_windows else sep_posix;16pub const sep = if (builtin.os == .windows) sep_windows else sep_posix;
1717
18pub const sep_str = [1]u8{sep};18pub const sep_str = [1]u8{sep};
1919
20pub const delimiter_windows = ';';20pub const delimiter_windows = ';';
21pub const delimiter_posix = ':';21pub const delimiter_posix = ':';
22pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix;22pub const delimiter = if (builtin.os == .windows) delimiter_windows else delimiter_posix;
2323
24pub fn isSep(byte: u8) bool {24pub fn isSep(byte: u8) bool {
25 if (windows.is_the_target) {25 if (builtin.os == .windows) {
26 return byte == '/' or byte == '\\';26 return byte == '/' or byte == '\\';
27 } else {27 } else {
28 return byte == '/';28 return byte == '/';
...@@ -72,7 +72,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u...@@ -72,7 +72,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u
72 return buf;72 return buf;
73}73}
7474
75pub const join = if (windows.is_the_target) joinWindows else joinPosix;75pub const join = if (builtin.os == .windows) joinWindows else joinPosix;
7676
77/// Naively combines a series of paths with the native path seperator.77/// Naively combines a series of paths with the native path seperator.
78/// Allocates memory for the result, which must be freed by the caller.78/// Allocates memory for the result, which must be freed by the caller.
...@@ -129,7 +129,7 @@ test "join" {...@@ -129,7 +129,7 @@ test "join" {
129}129}
130130
131pub fn isAbsolute(path: []const u8) bool {131pub fn isAbsolute(path: []const u8) bool {
132 if (windows.is_the_target) {132 if (builtin.os == .windows) {
133 return isAbsoluteWindows(path);133 return isAbsoluteWindows(path);
134 } else {134 } else {
135 return isAbsolutePosix(path);135 return isAbsolutePosix(path);
...@@ -327,7 +327,7 @@ test "windowsParsePath" {...@@ -327,7 +327,7 @@ test "windowsParsePath" {
327}327}
328328
329pub fn diskDesignator(path: []const u8) []const u8 {329pub fn diskDesignator(path: []const u8) []const u8 {
330 if (windows.is_the_target) {330 if (builtin.os == .windows) {
331 return diskDesignatorWindows(path);331 return diskDesignatorWindows(path);
332 } else {332 } else {
333 return "";333 return "";
...@@ -392,7 +392,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {...@@ -392,7 +392,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
392392
393/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.393/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
394pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {394pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
395 if (windows.is_the_target) {395 if (builtin.os == .windows) {
396 return resolveWindows(allocator, paths);396 return resolveWindows(allocator, paths);
397 } else {397 } else {
398 return resolvePosix(allocator, paths);398 return resolvePosix(allocator, paths);
...@@ -409,7 +409,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -409,7 +409,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
409/// Without performing actual syscalls, resolving `..` could be incorrect.409/// Without performing actual syscalls, resolving `..` could be incorrect.
410pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {410pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
411 if (paths.len == 0) {411 if (paths.len == 0) {
412 assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd412 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
413 return process.getCwdAlloc(allocator);413 return process.getCwdAlloc(allocator);
414 }414 }
415415
...@@ -504,7 +504,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -504,7 +504,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
504 result_disk_designator = result[0..result_index];504 result_disk_designator = result[0..result_index];
505 },505 },
506 WindowsPath.Kind.None => {506 WindowsPath.Kind.None => {
507 assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd507 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
508 const cwd = try process.getCwdAlloc(allocator);508 const cwd = try process.getCwdAlloc(allocator);
509 defer allocator.free(cwd);509 defer allocator.free(cwd);
510 const parsed_cwd = windowsParsePath(cwd);510 const parsed_cwd = windowsParsePath(cwd);
...@@ -519,7 +519,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -519,7 +519,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
519 },519 },
520 }520 }
521 } else {521 } else {
522 assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd522 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
523 // TODO call get cwd for the result_disk_designator instead of the global one523 // TODO call get cwd for the result_disk_designator instead of the global one
524 const cwd = try process.getCwdAlloc(allocator);524 const cwd = try process.getCwdAlloc(allocator);
525 defer allocator.free(cwd);525 defer allocator.free(cwd);
...@@ -590,7 +590,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -590,7 +590,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
590/// Without performing actual syscalls, resolving `..` could be incorrect.590/// Without performing actual syscalls, resolving `..` could be incorrect.
591pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {591pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
592 if (paths.len == 0) {592 if (paths.len == 0) {
593 assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd593 assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
594 return process.getCwdAlloc(allocator);594 return process.getCwdAlloc(allocator);
595 }595 }
596596
...@@ -612,7 +612,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -612,7 +612,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
612 if (have_abs) {612 if (have_abs) {
613 result = try allocator.alloc(u8, max_size);613 result = try allocator.alloc(u8, max_size);
614 } else {614 } else {
615 assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd615 assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
616 const cwd = try process.getCwdAlloc(allocator);616 const cwd = try process.getCwdAlloc(allocator);
617 defer allocator.free(cwd);617 defer allocator.free(cwd);
618 result = try allocator.alloc(u8, max_size + cwd.len + 1);618 result = try allocator.alloc(u8, max_size + cwd.len + 1);
...@@ -653,7 +653,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {...@@ -653,7 +653,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
653653
654test "resolve" {654test "resolve" {
655 const cwd = try process.getCwdAlloc(debug.global_allocator);655 const cwd = try process.getCwdAlloc(debug.global_allocator);
656 if (windows.is_the_target) {656 if (builtin.os == .windows) {
657 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {657 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
658 cwd[0] = asciiUpper(cwd[0]);658 cwd[0] = asciiUpper(cwd[0]);
659 }659 }
...@@ -669,7 +669,7 @@ test "resolveWindows" {...@@ -669,7 +669,7 @@ test "resolveWindows" {
669 // TODO https://github.com/ziglang/zig/issues/3288669 // TODO https://github.com/ziglang/zig/issues/3288
670 return error.SkipZigTest;670 return error.SkipZigTest;
671 }671 }
672 if (windows.is_the_target) {672 if (builtin.os == .windows) {
673 const cwd = try process.getCwdAlloc(debug.global_allocator);673 const cwd = try process.getCwdAlloc(debug.global_allocator);
674 const parsed_cwd = windowsParsePath(cwd);674 const parsed_cwd = windowsParsePath(cwd);
675 {675 {
...@@ -735,7 +735,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 {...@@ -735,7 +735,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
735/// If the path is a file in the current directory (no directory component)735/// If the path is a file in the current directory (no directory component)
736/// then returns null736/// then returns null
737pub fn dirname(path: []const u8) ?[]const u8 {737pub fn dirname(path: []const u8) ?[]const u8 {
738 if (windows.is_the_target) {738 if (builtin.os == .windows) {
739 return dirnameWindows(path);739 return dirnameWindows(path);
740 } else {740 } else {
741 return dirnamePosix(path);741 return dirnamePosix(path);
...@@ -867,7 +867,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void {...@@ -867,7 +867,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void {
867}867}
868868
869pub fn basename(path: []const u8) []const u8 {869pub fn basename(path: []const u8) []const u8 {
870 if (windows.is_the_target) {870 if (builtin.os == .windows) {
871 return basenameWindows(path);871 return basenameWindows(path);
872 } else {872 } else {
873 return basenamePosix(path);873 return basenamePosix(path);
...@@ -983,7 +983,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {...@@ -983,7 +983,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
983/// string is returned.983/// string is returned.
984/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.984/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
985pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {985pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
986 if (windows.is_the_target) {986 if (builtin.os == .windows) {
987 return relativeWindows(allocator, from, to);987 return relativeWindows(allocator, from, to);
988 } else {988 } else {
989 return relativePosix(allocator, from, to);989 return relativePosix(allocator, from, to);
lib/std/heap.zig+3-3
...@@ -44,7 +44,7 @@ const DirectAllocator = struct {...@@ -44,7 +44,7 @@ const DirectAllocator = struct {
44 if (n == 0)44 if (n == 0)
45 return (([*]u8)(undefined))[0..0];45 return (([*]u8)(undefined))[0..0];
4646
47 if (os.windows.is_the_target) {47 if (builtin.os == .windows) {
48 const w = os.windows;48 const w = os.windows;
4949
50 // Although officially it's at least aligned to page boundary,50 // Although officially it's at least aligned to page boundary,
...@@ -130,7 +130,7 @@ const DirectAllocator = struct {...@@ -130,7 +130,7 @@ const DirectAllocator = struct {
130130
131 fn shrink(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {131 fn shrink(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
132 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);132 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
133 if (os.windows.is_the_target) {133 if (builtin.os == .windows) {
134 const w = os.windows;134 const w = os.windows;
135 if (new_size == 0) {135 if (new_size == 0) {
136 // From the docs:136 // From the docs:
...@@ -170,7 +170,7 @@ const DirectAllocator = struct {...@@ -170,7 +170,7 @@ const DirectAllocator = struct {
170170
171 fn realloc(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {171 fn realloc(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
172 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);172 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
173 if (os.windows.is_the_target) {173 if (builtin.os == .windows) {
174 if (old_mem.len == 0) {174 if (old_mem.len == 0) {
175 return alloc(allocator, new_size, new_align);175 return alloc(allocator, new_size, new_align);
176 }176 }
lib/std/io.zig+3-3
...@@ -37,7 +37,7 @@ pub const is_async = mode != .blocking;...@@ -37,7 +37,7 @@ pub const is_async = mode != .blocking;
37pub const GetStdIoError = os.windows.GetStdHandleError;37pub const GetStdIoError = os.windows.GetStdHandleError;
3838
39pub fn getStdOut() GetStdIoError!File {39pub fn getStdOut() GetStdIoError!File {
40 if (os.windows.is_the_target) {40 if (builtin.os == .windows) {
41 const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE);41 const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE);
42 return File.openHandle(handle);42 return File.openHandle(handle);
43 }43 }
...@@ -45,7 +45,7 @@ pub fn getStdOut() GetStdIoError!File {...@@ -45,7 +45,7 @@ pub fn getStdOut() GetStdIoError!File {
45}45}
4646
47pub fn getStdErr() GetStdIoError!File {47pub fn getStdErr() GetStdIoError!File {
48 if (os.windows.is_the_target) {48 if (builtin.os == .windows) {
49 const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE);49 const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE);
50 return File.openHandle(handle);50 return File.openHandle(handle);
51 }51 }
...@@ -53,7 +53,7 @@ pub fn getStdErr() GetStdIoError!File {...@@ -53,7 +53,7 @@ pub fn getStdErr() GetStdIoError!File {
53}53}
5454
55pub fn getStdIn() GetStdIoError!File {55pub fn getStdIn() GetStdIoError!File {
56 if (os.windows.is_the_target) {56 if (builtin.os == .windows) {
57 const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE);57 const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE);
58 return File.openHandle(handle);58 return File.openHandle(handle);
59 }59 }
lib/std/os.zig+64-61
...@@ -85,13 +85,13 @@ pub const errno = system.getErrno;...@@ -85,13 +85,13 @@ pub const errno = system.getErrno;
85/// must call `fsync` before `close`.85/// must call `fsync` before `close`.
86/// Note: The Zig standard library does not support POSIX thread cancellation.86/// Note: The Zig standard library does not support POSIX thread cancellation.
87pub fn close(fd: fd_t) void {87pub fn close(fd: fd_t) void {
88 if (windows.is_the_target) {88 if (builtin.os == .windows) {
89 return windows.CloseHandle(fd);89 return windows.CloseHandle(fd);
90 }90 }
91 if (wasi.is_the_target) {91 if (builtin.os == .wasi) {
92 _ = wasi.fd_close(fd);92 _ = wasi.fd_close(fd);
93 }93 }
94 if (darwin.is_the_target) {94 if (comptime std.Target.current.isDarwin()) {
95 // This avoids the EINTR problem.95 // This avoids the EINTR problem.
96 switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {96 switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {
97 EBADF => unreachable, // Always a race condition.97 EBADF => unreachable, // Always a race condition.
...@@ -113,12 +113,12 @@ pub const GetRandomError = OpenError;...@@ -113,12 +113,12 @@ pub const GetRandomError = OpenError;
113/// appropriate OS-specific library call. Otherwise it uses the zig standard113/// appropriate OS-specific library call. Otherwise it uses the zig standard
114/// library implementation.114/// library implementation.
115pub fn getrandom(buffer: []u8) GetRandomError!void {115pub fn getrandom(buffer: []u8) GetRandomError!void {
116 if (windows.is_the_target) {116 if (builtin.os == .windows) {
117 return windows.RtlGenRandom(buffer);117 return windows.RtlGenRandom(buffer);
118 }118 }
119 if (linux.is_the_target or freebsd.is_the_target) {119 if (builtin.os == .linux or builtin.os == .freebsd) {
120 var buf = buffer;120 var buf = buffer;
121 const use_c = !linux.is_the_target or121 const use_c = builtin.os != .linux or
122 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;122 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
123123
124 while (buf.len != 0) {124 while (buf.len != 0) {
...@@ -145,7 +145,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {...@@ -145,7 +145,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
145 }145 }
146 return;146 return;
147 }147 }
148 if (wasi.is_the_target) {148 if (builtin.os == .wasi) {
149 switch (wasi.random_get(buffer.ptr, buffer.len)) {149 switch (wasi.random_get(buffer.ptr, buffer.len)) {
150 0 => return,150 0 => return,
151 else => |err| return unexpectedErrno(err),151 else => |err| return unexpectedErrno(err),
...@@ -175,7 +175,7 @@ pub fn abort() noreturn {...@@ -175,7 +175,7 @@ pub fn abort() noreturn {
175 // MSVCRT abort() sometimes opens a popup window which is undesirable, so175 // MSVCRT abort() sometimes opens a popup window which is undesirable, so
176 // even when linking libc on Windows we use our own abort implementation.176 // even when linking libc on Windows we use our own abort implementation.
177 // See https://github.com/ziglang/zig/issues/2071 for more details.177 // See https://github.com/ziglang/zig/issues/2071 for more details.
178 if (windows.is_the_target) {178 if (builtin.os == .windows) {
179 if (builtin.mode == .Debug) {179 if (builtin.mode == .Debug) {
180 @breakpoint();180 @breakpoint();
181 }181 }
...@@ -206,14 +206,14 @@ pub fn raise(sig: u8) RaiseError!void {...@@ -206,14 +206,14 @@ pub fn raise(sig: u8) RaiseError!void {
206 }206 }
207 }207 }
208208
209 if (wasi.is_the_target) {209 if (builtin.os == .wasi) {
210 switch (wasi.proc_raise(SIGABRT)) {210 switch (wasi.proc_raise(SIGABRT)) {
211 0 => return,211 0 => return,
212 else => |err| return unexpectedErrno(err),212 else => |err| return unexpectedErrno(err),
213 }213 }
214 }214 }
215215
216 if (linux.is_the_target) {216 if (builtin.os == .linux) {
217 var set: linux.sigset_t = undefined;217 var set: linux.sigset_t = undefined;
218 linux.blockAppSignals(&set);218 linux.blockAppSignals(&set);
219 const tid = linux.syscall0(linux.SYS_gettid);219 const tid = linux.syscall0(linux.SYS_gettid);
...@@ -245,16 +245,16 @@ pub fn exit(status: u8) noreturn {...@@ -245,16 +245,16 @@ pub fn exit(status: u8) noreturn {
245 if (builtin.link_libc) {245 if (builtin.link_libc) {
246 system.exit(status);246 system.exit(status);
247 }247 }
248 if (windows.is_the_target) {248 if (builtin.os == .windows) {
249 windows.kernel32.ExitProcess(status);249 windows.kernel32.ExitProcess(status);
250 }250 }
251 if (wasi.is_the_target) {251 if (builtin.os == .wasi) {
252 wasi.proc_exit(status);252 wasi.proc_exit(status);
253 }253 }
254 if (linux.is_the_target and !builtin.single_threaded) {254 if (builtin.os == .linux and !builtin.single_threaded) {
255 linux.exit_group(status);255 linux.exit_group(status);
256 }256 }
257 if (uefi.is_the_target) {257 if (builtin.os == .uefi) {
258 // exit() is only avaliable if exitBootServices() has not been called yet.258 // exit() is only avaliable if exitBootServices() has not been called yet.
259 // This call to exit should not fail, so we don't care about its return value.259 // This call to exit should not fail, so we don't care about its return value.
260 if (uefi.system_table.boot_services) |bs| {260 if (uefi.system_table.boot_services) |bs| {
...@@ -283,11 +283,11 @@ pub const ReadError = error{...@@ -283,11 +283,11 @@ pub const ReadError = error{
283/// If the application has a global event loop enabled, EAGAIN is handled283/// If the application has a global event loop enabled, EAGAIN is handled
284/// via the event loop. Otherwise EAGAIN results in error.WouldBlock.284/// via the event loop. Otherwise EAGAIN results in error.WouldBlock.
285pub fn read(fd: fd_t, buf: []u8) ReadError!usize {285pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
286 if (windows.is_the_target) {286 if (builtin.os == .windows) {
287 return windows.ReadFile(fd, buf);287 return windows.ReadFile(fd, buf);
288 }288 }
289289
290 if (wasi.is_the_target and !builtin.link_libc) {290 if (builtin.os == .wasi and !builtin.link_libc) {
291 const iovs = [1]iovec{iovec{291 const iovs = [1]iovec{iovec{
292 .iov_base = buf.ptr,292 .iov_base = buf.ptr,
293 .iov_len = buf.len,293 .iov_len = buf.len,
...@@ -327,7 +327,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -327,7 +327,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
327/// Number of bytes read is returned. Upon reading end-of-file, zero is returned.327/// Number of bytes read is returned. Upon reading end-of-file, zero is returned.
328/// This function is for blocking file descriptors only.328/// This function is for blocking file descriptors only.
329pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize {329pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize {
330 if (darwin.is_the_target) {330 if (comptime std.Target.current.isDarwin()) {
331 // Darwin does not have preadv but it does have pread.331 // Darwin does not have preadv but it does have pread.
332 var off: usize = 0;332 var off: usize = 0;
333 var iov_i: usize = 0;333 var iov_i: usize = 0;
...@@ -398,11 +398,11 @@ pub const WriteError = error{...@@ -398,11 +398,11 @@ pub const WriteError = error{
398/// Write to a file descriptor. Keeps trying if it gets interrupted.398/// Write to a file descriptor. Keeps trying if it gets interrupted.
399/// This function is for blocking file descriptors only.399/// This function is for blocking file descriptors only.
400pub fn write(fd: fd_t, bytes: []const u8) WriteError!void {400pub fn write(fd: fd_t, bytes: []const u8) WriteError!void {
401 if (windows.is_the_target) {401 if (builtin.os == .windows) {
402 return windows.WriteFile(fd, bytes);402 return windows.WriteFile(fd, bytes);
403 }403 }
404404
405 if (wasi.is_the_target and !builtin.link_libc) {405 if (builtin.os == .wasi and !builtin.link_libc) {
406 const ciovs = [1]iovec_const{iovec_const{406 const ciovs = [1]iovec_const{iovec_const{
407 .iov_base = bytes.ptr,407 .iov_base = bytes.ptr,
408 .iov_len = bytes.len,408 .iov_len = bytes.len,
...@@ -477,7 +477,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!void {...@@ -477,7 +477,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!void {
477/// This function is for blocking file descriptors only. For non-blocking, see477/// This function is for blocking file descriptors only. For non-blocking, see
478/// `pwritevAsync`.478/// `pwritevAsync`.
479pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void {479pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void {
480 if (darwin.is_the_target) {480 if (comptime std.Target.current.isDarwin()) {
481 // Darwin does not have pwritev but it does have pwrite.481 // Darwin does not have pwritev but it does have pwrite.
482 var off: usize = 0;482 var off: usize = 0;
483 var iov_i: usize = 0;483 var iov_i: usize = 0;
...@@ -841,7 +841,7 @@ pub const GetCwdError = error{...@@ -841,7 +841,7 @@ pub const GetCwdError = error{
841841
842/// The result is a slice of out_buffer, indexed from 0.842/// The result is a slice of out_buffer, indexed from 0.
843pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {843pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
844 if (windows.is_the_target) {844 if (builtin.os == .windows) {
845 return windows.GetCurrentDirectory(out_buffer);845 return windows.GetCurrentDirectory(out_buffer);
846 }846 }
847847
...@@ -882,7 +882,7 @@ pub const SymLinkError = error{...@@ -882,7 +882,7 @@ pub const SymLinkError = error{
882/// If `sym_link_path` exists, it will not be overwritten.882/// If `sym_link_path` exists, it will not be overwritten.
883/// See also `symlinkC` and `symlinkW`.883/// See also `symlinkC` and `symlinkW`.
884pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {884pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
885 if (windows.is_the_target) {885 if (builtin.os == .windows) {
886 const target_path_w = try windows.sliceToPrefixedFileW(target_path);886 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
887 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);887 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
888 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);888 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
...@@ -896,7 +896,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!...@@ -896,7 +896,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!
896/// This is the same as `symlink` except the parameters are null-terminated pointers.896/// This is the same as `symlink` except the parameters are null-terminated pointers.
897/// See also `symlink`.897/// See also `symlink`.
898pub fn symlinkC(target_path: [*]const u8, sym_link_path: [*]const u8) SymLinkError!void {898pub fn symlinkC(target_path: [*]const u8, sym_link_path: [*]const u8) SymLinkError!void {
899 if (windows.is_the_target) {899 if (builtin.os == .windows) {
900 const target_path_w = try windows.cStrToPrefixedFileW(target_path);900 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
901 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);901 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
902 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);902 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
...@@ -971,7 +971,7 @@ pub const UnlinkError = error{...@@ -971,7 +971,7 @@ pub const UnlinkError = error{
971/// Delete a name and possibly the file it refers to.971/// Delete a name and possibly the file it refers to.
972/// See also `unlinkC`.972/// See also `unlinkC`.
973pub fn unlink(file_path: []const u8) UnlinkError!void {973pub fn unlink(file_path: []const u8) UnlinkError!void {
974 if (windows.is_the_target) {974 if (builtin.os == .windows) {
975 const file_path_w = try windows.sliceToPrefixedFileW(file_path);975 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
976 return windows.DeleteFileW(&file_path_w);976 return windows.DeleteFileW(&file_path_w);
977 } else {977 } else {
...@@ -982,7 +982,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {...@@ -982,7 +982,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {
982982
983/// Same as `unlink` except the parameter is a null terminated UTF8-encoded string.983/// Same as `unlink` except the parameter is a null terminated UTF8-encoded string.
984pub fn unlinkC(file_path: [*]const u8) UnlinkError!void {984pub fn unlinkC(file_path: [*]const u8) UnlinkError!void {
985 if (windows.is_the_target) {985 if (builtin.os == .windows) {
986 const file_path_w = try windows.cStrToPrefixedFileW(file_path);986 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
987 return windows.DeleteFileW(&file_path_w);987 return windows.DeleteFileW(&file_path_w);
988 }988 }
...@@ -1012,7 +1012,7 @@ pub const UnlinkatError = UnlinkError || error{...@@ -1012,7 +1012,7 @@ pub const UnlinkatError = UnlinkError || error{
10121012
1013/// Delete a file name and possibly the file it refers to, based on an open directory handle.1013/// Delete a file name and possibly the file it refers to, based on an open directory handle.
1014pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {1014pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
1015 if (windows.is_the_target) {1015 if (builtin.os == .windows) {
1016 const file_path_w = try windows.sliceToPrefixedFileW(file_path);1016 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1017 return unlinkatW(dirfd, &file_path_w, flags);1017 return unlinkatW(dirfd, &file_path_w, flags);
1018 }1018 }
...@@ -1022,7 +1022,7 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo...@@ -1022,7 +1022,7 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
10221022
1023/// Same as `unlinkat` but `file_path` is a null-terminated string.1023/// Same as `unlinkat` but `file_path` is a null-terminated string.
1024pub fn unlinkatC(dirfd: fd_t, file_path_c: [*]const u8, flags: u32) UnlinkatError!void {1024pub fn unlinkatC(dirfd: fd_t, file_path_c: [*]const u8, flags: u32) UnlinkatError!void {
1025 if (windows.is_the_target) {1025 if (builtin.os == .windows) {
1026 const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);1026 const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);
1027 return unlinkatW(dirfd, &file_path_w, flags);1027 return unlinkatW(dirfd, &file_path_w, flags);
1028 }1028 }
...@@ -1133,7 +1133,7 @@ const RenameError = error{...@@ -1133,7 +1133,7 @@ const RenameError = error{
11331133
1134/// Change the name or location of a file.1134/// Change the name or location of a file.
1135pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {1135pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
1136 if (windows.is_the_target) {1136 if (builtin.os == .windows) {
1137 const old_path_w = try windows.sliceToPrefixedFileW(old_path);1137 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
1138 const new_path_w = try windows.sliceToPrefixedFileW(new_path);1138 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
1139 return renameW(&old_path_w, &new_path_w);1139 return renameW(&old_path_w, &new_path_w);
...@@ -1146,7 +1146,7 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {...@@ -1146,7 +1146,7 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
11461146
1147/// Same as `rename` except the parameters are null-terminated byte arrays.1147/// Same as `rename` except the parameters are null-terminated byte arrays.
1148pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void {1148pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void {
1149 if (windows.is_the_target) {1149 if (builtin.os == .windows) {
1150 const old_path_w = try windows.cStrToPrefixedFileW(old_path);1150 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
1151 const new_path_w = try windows.cStrToPrefixedFileW(new_path);1151 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
1152 return renameW(&old_path_w, &new_path_w);1152 return renameW(&old_path_w, &new_path_w);
...@@ -1201,7 +1201,7 @@ pub const MakeDirError = error{...@@ -1201,7 +1201,7 @@ pub const MakeDirError = error{
1201/// Create a directory.1201/// Create a directory.
1202/// `mode` is ignored on Windows.1202/// `mode` is ignored on Windows.
1203pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {1203pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
1204 if (windows.is_the_target) {1204 if (builtin.os == .windows) {
1205 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);1205 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
1206 return windows.CreateDirectoryW(&dir_path_w, null);1206 return windows.CreateDirectoryW(&dir_path_w, null);
1207 } else {1207 } else {
...@@ -1212,7 +1212,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {...@@ -1212,7 +1212,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
12121212
1213/// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string.1213/// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string.
1214pub fn mkdirC(dir_path: [*]const u8, mode: u32) MakeDirError!void {1214pub fn mkdirC(dir_path: [*]const u8, mode: u32) MakeDirError!void {
1215 if (windows.is_the_target) {1215 if (builtin.os == .windows) {
1216 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);1216 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
1217 return windows.CreateDirectoryW(&dir_path_w, null);1217 return windows.CreateDirectoryW(&dir_path_w, null);
1218 }1218 }
...@@ -1251,7 +1251,7 @@ pub const DeleteDirError = error{...@@ -1251,7 +1251,7 @@ pub const DeleteDirError = error{
12511251
1252/// Deletes an empty directory.1252/// Deletes an empty directory.
1253pub fn rmdir(dir_path: []const u8) DeleteDirError!void {1253pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
1254 if (windows.is_the_target) {1254 if (builtin.os == .windows) {
1255 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);1255 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
1256 return windows.RemoveDirectoryW(&dir_path_w);1256 return windows.RemoveDirectoryW(&dir_path_w);
1257 } else {1257 } else {
...@@ -1262,7 +1262,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {...@@ -1262,7 +1262,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
12621262
1263/// Same as `rmdir` except the parameter is null-terminated.1263/// Same as `rmdir` except the parameter is null-terminated.
1264pub fn rmdirC(dir_path: [*]const u8) DeleteDirError!void {1264pub fn rmdirC(dir_path: [*]const u8) DeleteDirError!void {
1265 if (windows.is_the_target) {1265 if (builtin.os == .windows) {
1266 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);1266 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
1267 return windows.RemoveDirectoryW(&dir_path_w);1267 return windows.RemoveDirectoryW(&dir_path_w);
1268 }1268 }
...@@ -1298,7 +1298,7 @@ pub const ChangeCurDirError = error{...@@ -1298,7 +1298,7 @@ pub const ChangeCurDirError = error{
1298/// Changes the current working directory of the calling process.1298/// Changes the current working directory of the calling process.
1299/// `dir_path` is recommended to be a UTF-8 encoded string.1299/// `dir_path` is recommended to be a UTF-8 encoded string.
1300pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {1300pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
1301 if (windows.is_the_target) {1301 if (builtin.os == .windows) {
1302 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);1302 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
1303 @compileError("TODO implement chdir for Windows");1303 @compileError("TODO implement chdir for Windows");
1304 } else {1304 } else {
...@@ -1309,7 +1309,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {...@@ -1309,7 +1309,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
13091309
1310/// Same as `chdir` except the parameter is null-terminated.1310/// Same as `chdir` except the parameter is null-terminated.
1311pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void {1311pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void {
1312 if (windows.is_the_target) {1312 if (builtin.os == .windows) {
1313 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);1313 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
1314 @compileError("TODO implement chdir for Windows");1314 @compileError("TODO implement chdir for Windows");
1315 }1315 }
...@@ -1340,7 +1340,7 @@ pub const ReadLinkError = error{...@@ -1340,7 +1340,7 @@ pub const ReadLinkError = error{
1340/// Read value of a symbolic link.1340/// Read value of a symbolic link.
1341/// The return value is a slice of `out_buffer` from index 0.1341/// The return value is a slice of `out_buffer` from index 0.
1342pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {1342pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
1343 if (windows.is_the_target) {1343 if (builtin.os == .windows) {
1344 const file_path_w = try windows.sliceToPrefixedFileW(file_path);1344 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1345 @compileError("TODO implement readlink for Windows");1345 @compileError("TODO implement readlink for Windows");
1346 } else {1346 } else {
...@@ -1351,7 +1351,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {...@@ -1351,7 +1351,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
13511351
1352/// Same as `readlink` except `file_path` is null-terminated.1352/// Same as `readlink` except `file_path` is null-terminated.
1353pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {1353pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
1354 if (windows.is_the_target) {1354 if (builtin.os == .windows) {
1355 const file_path_w = try windows.cStrToPrefixedFileW(file_path);1355 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
1356 @compileError("TODO implement readlink for Windows");1356 @compileError("TODO implement readlink for Windows");
1357 }1357 }
...@@ -1372,7 +1372,7 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {...@@ -1372,7 +1372,7 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
1372}1372}
13731373
1374pub fn readlinkatC(dirfd: fd_t, file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {1374pub fn readlinkatC(dirfd: fd_t, file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
1375 if (windows.is_the_target) {1375 if (builtin.os == .windows) {
1376 const file_path_w = try windows.cStrToPrefixedFileW(file_path);1376 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
1377 @compileError("TODO implement readlink for Windows");1377 @compileError("TODO implement readlink for Windows");
1378 }1378 }
...@@ -1440,7 +1440,7 @@ pub fn setregid(rgid: u32, egid: u32) SetIdError!void {...@@ -1440,7 +1440,7 @@ pub fn setregid(rgid: u32, egid: u32) SetIdError!void {
14401440
1441/// Test whether a file descriptor refers to a terminal.1441/// Test whether a file descriptor refers to a terminal.
1442pub fn isatty(handle: fd_t) bool {1442pub fn isatty(handle: fd_t) bool {
1443 if (windows.is_the_target) {1443 if (builtin.os == .windows) {
1444 if (isCygwinPty(handle))1444 if (isCygwinPty(handle))
1445 return true;1445 return true;
14461446
...@@ -1450,10 +1450,10 @@ pub fn isatty(handle: fd_t) bool {...@@ -1450,10 +1450,10 @@ pub fn isatty(handle: fd_t) bool {
1450 if (builtin.link_libc) {1450 if (builtin.link_libc) {
1451 return system.isatty(handle) != 0;1451 return system.isatty(handle) != 0;
1452 }1452 }
1453 if (wasi.is_the_target) {1453 if (builtin.os == .wasi) {
1454 @compileError("TODO implement std.os.isatty for WASI");1454 @compileError("TODO implement std.os.isatty for WASI");
1455 }1455 }
1456 if (linux.is_the_target) {1456 if (builtin.os == .linux) {
1457 var wsz: linux.winsize = undefined;1457 var wsz: linux.winsize = undefined;
1458 return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;1458 return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
1459 }1459 }
...@@ -1461,7 +1461,7 @@ pub fn isatty(handle: fd_t) bool {...@@ -1461,7 +1461,7 @@ pub fn isatty(handle: fd_t) bool {
1461}1461}
14621462
1463pub fn isCygwinPty(handle: fd_t) bool {1463pub fn isCygwinPty(handle: fd_t) bool {
1464 if (!windows.is_the_target) return false;1464 if (builtin.os != .windows) return false;
14651465
1466 const size = @sizeOf(windows.FILE_NAME_INFO);1466 const size = @sizeOf(windows.FILE_NAME_INFO);
1467 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (size + windows.MAX_PATH);1467 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (size + windows.MAX_PATH);
...@@ -1961,7 +1961,7 @@ pub const FStatError = error{SystemResources} || UnexpectedError;...@@ -1961,7 +1961,7 @@ pub const FStatError = error{SystemResources} || UnexpectedError;
19611961
1962pub fn fstat(fd: fd_t) FStatError!Stat {1962pub fn fstat(fd: fd_t) FStatError!Stat {
1963 var stat: Stat = undefined;1963 var stat: Stat = undefined;
1964 if (darwin.is_the_target) {1964 if (comptime std.Target.current.isDarwin()) {
1965 switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) {1965 switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) {
1966 0 => return stat,1966 0 => return stat,
1967 EINVAL => unreachable,1967 EINVAL => unreachable,
...@@ -2227,7 +2227,7 @@ pub const AccessError = error{...@@ -2227,7 +2227,7 @@ pub const AccessError = error{
2227/// check user's permissions for a file2227/// check user's permissions for a file
2228/// TODO currently this assumes `mode` is `F_OK` on Windows.2228/// TODO currently this assumes `mode` is `F_OK` on Windows.
2229pub fn access(path: []const u8, mode: u32) AccessError!void {2229pub fn access(path: []const u8, mode: u32) AccessError!void {
2230 if (windows.is_the_target) {2230 if (builtin.os == .windows) {
2231 const path_w = try windows.sliceToPrefixedFileW(path);2231 const path_w = try windows.sliceToPrefixedFileW(path);
2232 _ = try windows.GetFileAttributesW(&path_w);2232 _ = try windows.GetFileAttributesW(&path_w);
2233 return;2233 return;
...@@ -2238,7 +2238,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {...@@ -2238,7 +2238,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {
22382238
2239/// Same as `access` except `path` is null-terminated.2239/// Same as `access` except `path` is null-terminated.
2240pub fn accessC(path: [*]const u8, mode: u32) AccessError!void {2240pub fn accessC(path: [*]const u8, mode: u32) AccessError!void {
2241 if (windows.is_the_target) {2241 if (builtin.os == .windows) {
2242 const path_w = try windows.cStrToPrefixedFileW(path);2242 const path_w = try windows.cStrToPrefixedFileW(path);
2243 _ = try windows.GetFileAttributesW(&path_w);2243 _ = try windows.GetFileAttributesW(&path_w);
2244 return;2244 return;
...@@ -2358,7 +2358,7 @@ pub const SeekError = error{Unseekable} || UnexpectedError;...@@ -2358,7 +2358,7 @@ pub const SeekError = error{Unseekable} || UnexpectedError;
23582358
2359/// Repositions read/write file offset relative to the beginning.2359/// Repositions read/write file offset relative to the beginning.
2360pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {2360pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
2361 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2361 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2362 var result: u64 = undefined;2362 var result: u64 = undefined;
2363 switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) {2363 switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) {
2364 0 => return,2364 0 => return,
...@@ -2370,7 +2370,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {...@@ -2370,7 +2370,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
2370 else => |err| return unexpectedErrno(err),2370 else => |err| return unexpectedErrno(err),
2371 }2371 }
2372 }2372 }
2373 if (windows.is_the_target) {2373 if (builtin.os == .windows) {
2374 return windows.SetFilePointerEx_BEGIN(fd, offset);2374 return windows.SetFilePointerEx_BEGIN(fd, offset);
2375 }2375 }
2376 const ipos = @bitCast(i64, offset); // the OS treats this as unsigned2376 const ipos = @bitCast(i64, offset); // the OS treats this as unsigned
...@@ -2387,7 +2387,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {...@@ -2387,7 +2387,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
23872387
2388/// Repositions read/write file offset relative to the current offset.2388/// Repositions read/write file offset relative to the current offset.
2389pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {2389pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
2390 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2390 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2391 var result: u64 = undefined;2391 var result: u64 = undefined;
2392 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) {2392 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) {
2393 0 => return,2393 0 => return,
...@@ -2399,7 +2399,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {...@@ -2399,7 +2399,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
2399 else => |err| return unexpectedErrno(err),2399 else => |err| return unexpectedErrno(err),
2400 }2400 }
2401 }2401 }
2402 if (windows.is_the_target) {2402 if (builtin.os == .windows) {
2403 return windows.SetFilePointerEx_CURRENT(fd, offset);2403 return windows.SetFilePointerEx_CURRENT(fd, offset);
2404 }2404 }
2405 switch (errno(system.lseek(fd, offset, SEEK_CUR))) {2405 switch (errno(system.lseek(fd, offset, SEEK_CUR))) {
...@@ -2415,7 +2415,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {...@@ -2415,7 +2415,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
24152415
2416/// Repositions read/write file offset relative to the end.2416/// Repositions read/write file offset relative to the end.
2417pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {2417pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
2418 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2418 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2419 var result: u64 = undefined;2419 var result: u64 = undefined;
2420 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) {2420 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) {
2421 0 => return,2421 0 => return,
...@@ -2427,7 +2427,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {...@@ -2427,7 +2427,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
2427 else => |err| return unexpectedErrno(err),2427 else => |err| return unexpectedErrno(err),
2428 }2428 }
2429 }2429 }
2430 if (windows.is_the_target) {2430 if (builtin.os == .windows) {
2431 return windows.SetFilePointerEx_END(fd, offset);2431 return windows.SetFilePointerEx_END(fd, offset);
2432 }2432 }
2433 switch (errno(system.lseek(fd, offset, SEEK_END))) {2433 switch (errno(system.lseek(fd, offset, SEEK_END))) {
...@@ -2443,7 +2443,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {...@@ -2443,7 +2443,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
24432443
2444/// Returns the read/write file offset relative to the beginning.2444/// Returns the read/write file offset relative to the beginning.
2445pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {2445pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
2446 if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {2446 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2447 var result: u64 = undefined;2447 var result: u64 = undefined;
2448 switch (errno(system.llseek(fd, 0, &result, SEEK_CUR))) {2448 switch (errno(system.llseek(fd, 0, &result, SEEK_CUR))) {
2449 0 => return result,2449 0 => return result,
...@@ -2455,7 +2455,7 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {...@@ -2455,7 +2455,7 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
2455 else => |err| return unexpectedErrno(err),2455 else => |err| return unexpectedErrno(err),
2456 }2456 }
2457 }2457 }
2458 if (windows.is_the_target) {2458 if (builtin.os == .windows) {
2459 return windows.SetFilePointerEx_CURRENT_get(fd);2459 return windows.SetFilePointerEx_CURRENT_get(fd);
2460 }2460 }
2461 const rc = system.lseek(fd, 0, SEEK_CUR);2461 const rc = system.lseek(fd, 0, SEEK_CUR);
...@@ -2504,7 +2504,7 @@ pub const RealPathError = error{...@@ -2504,7 +2504,7 @@ pub const RealPathError = error{
2504/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.2504/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.
2505/// See also `realpathC` and `realpathW`.2505/// See also `realpathC` and `realpathW`.
2506pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {2506pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
2507 if (windows.is_the_target) {2507 if (builtin.os == .windows) {
2508 const pathname_w = try windows.sliceToPrefixedFileW(pathname);2508 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
2509 return realpathW(&pathname_w, out_buffer);2509 return realpathW(&pathname_w, out_buffer);
2510 }2510 }
...@@ -2514,11 +2514,11 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE...@@ -2514,11 +2514,11 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
25142514
2515/// Same as `realpath` except `pathname` is null-terminated.2515/// Same as `realpath` except `pathname` is null-terminated.
2516pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {2516pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
2517 if (windows.is_the_target) {2517 if (builtin.os == .windows) {
2518 const pathname_w = try windows.cStrToPrefixedFileW(pathname);2518 const pathname_w = try windows.cStrToPrefixedFileW(pathname);
2519 return realpathW(&pathname_w, out_buffer);2519 return realpathW(&pathname_w, out_buffer);
2520 }2520 }
2521 if (linux.is_the_target and !builtin.link_libc) {2521 if (builtin.os == .linux and !builtin.link_libc) {
2522 const fd = try openC(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0);2522 const fd = try openC(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0);
2523 defer close(fd);2523 defer close(fd);
25242524
...@@ -2596,9 +2596,12 @@ pub fn nanosleep(seconds: u64, nanoseconds: u64) void {...@@ -2596,9 +2596,12 @@ pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
2596 }2596 }
2597}2597}
25982598
2599pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {2599pub fn dl_iterate_phdr(
2600 // This is implemented only for systems using ELF executables2600 comptime T: type,
2601 if (windows.is_the_target or builtin.os == .uefi or wasi.is_the_target or darwin.is_the_target)2601 callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32,
2602 data: ?*T,
2603) isize {
2604 if (builtin.object_format != .elf)
2602 @compileError("dl_iterate_phdr is not available for this target");2605 @compileError("dl_iterate_phdr is not available for this target");
26032606
2604 if (builtin.link_libc) {2607 if (builtin.link_libc) {
...@@ -2737,7 +2740,7 @@ pub const SigaltstackError = error{...@@ -2737,7 +2740,7 @@ pub const SigaltstackError = error{
2737} || UnexpectedError;2740} || UnexpectedError;
27382741
2739pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {2742pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
2740 if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target)2743 if (builtin.os == .windows or builtin.os == .uefi or builtin.os == .wasi)
2741 @compileError("std.os.sigaltstack not available for this target");2744 @compileError("std.os.sigaltstack not available for this target");
27422745
2743 switch (errno(system.sigaltstack(ss, old_ss))) {2746 switch (errno(system.sigaltstack(ss, old_ss))) {
...@@ -2809,7 +2812,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {...@@ -2809,7 +2812,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
2809 else => |err| return unexpectedErrno(err),2812 else => |err| return unexpectedErrno(err),
2810 }2813 }
2811 }2814 }
2812 if (linux.is_the_target) {2815 if (builtin.os == .linux) {
2813 var uts: utsname = undefined;2816 var uts: utsname = undefined;
2814 switch (errno(system.uname(&uts))) {2817 switch (errno(system.uname(&uts))) {
2815 0 => {2818 0 => {
lib/std/os/darwin.zig-4
...@@ -1,8 +1,4 @@...@@ -1,8 +1,4 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const std = @import("../std.zig");2const std = @import("../std.zig");
3pub const is_the_target = switch (builtin.os) {
4 .macosx, .tvos, .watchos, .ios => true,
5 else => false,
6};
7pub usingnamespace std.c;3pub usingnamespace std.c;
8pub usingnamespace @import("bits.zig");4pub usingnamespace @import("bits.zig");
lib/std/os/freebsd.zig-2
...@@ -1,5 +1,3 @@...@@ -1,5 +1,3 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const builtin = @import("builtin");
3pub const is_the_target = builtin.os == .freebsd;
4pub usingnamespace std.c;2pub usingnamespace std.c;
5pub usingnamespace @import("bits.zig");3pub usingnamespace @import("bits.zig");
lib/std/os/linux.zig+1-2
...@@ -13,7 +13,6 @@ const elf = std.elf;...@@ -13,7 +13,6 @@ const elf = std.elf;
13const vdso = @import("linux/vdso.zig");13const vdso = @import("linux/vdso.zig");
14const dl = @import("../dynamic_library.zig");14const dl = @import("../dynamic_library.zig");
1515
16pub const is_the_target = builtin.os == .linux;
17pub usingnamespace switch (builtin.arch) {16pub usingnamespace switch (builtin.arch) {
18 .x86_64 => @import("linux/x86_64.zig"),17 .x86_64 => @import("linux/x86_64.zig"),
19 .aarch64 => @import("linux/arm64.zig"),18 .aarch64 => @import("linux/arm64.zig"),
...@@ -1079,7 +1078,7 @@ pub fn io_uring_register(fd: i32, opcode: u32, arg: ?*const c_void, nr_args: u32...@@ -1079,7 +1078,7 @@ pub fn io_uring_register(fd: i32, opcode: u32, arg: ?*const c_void, nr_args: u32
1079}1078}
10801079
1081test "" {1080test "" {
1082 if (is_the_target) {1081 if (builtin.os == .linux) {
1083 _ = @import("linux/test.zig");1082 _ = @import("linux/test.zig");
1084 }1083 }
1085}1084}
lib/std/os/netbsd.zig-2
...@@ -1,5 +1,3 @@...@@ -1,5 +1,3 @@
1const builtin = @import("builtin");
2const std = @import("../std.zig");1const std = @import("../std.zig");
3pub const is_the_target = builtin.os == .netbsd;
4pub usingnamespace std.c;2pub usingnamespace std.c;
5pub usingnamespace @import("bits.zig");3pub usingnamespace @import("bits.zig");
lib/std/os/test.zig+3-3
...@@ -53,7 +53,7 @@ test "std.Thread.getCurrentId" {...@@ -53,7 +53,7 @@ test "std.Thread.getCurrentId" {
53 thread.wait();53 thread.wait();
54 if (Thread.use_pthreads) {54 if (Thread.use_pthreads) {
55 expect(thread_current_id == thread_id);55 expect(thread_current_id == thread_id);
56 } else if (os.windows.is_the_target) {56 } else if (builtin.os == .windows) {
57 expect(Thread.getCurrentId() != thread_current_id);57 expect(Thread.getCurrentId() != thread_current_id);
58 } else {58 } else {
59 // If the thread completes very quickly, then thread_id can be 0. See the59 // If the thread completes very quickly, then thread_id can be 0. See the
...@@ -212,7 +212,7 @@ test "dl_iterate_phdr" {...@@ -212,7 +212,7 @@ test "dl_iterate_phdr" {
212}212}
213213
214test "gethostname" {214test "gethostname" {
215 if (os.windows.is_the_target)215 if (builtin.os == .windows)
216 return error.SkipZigTest;216 return error.SkipZigTest;
217217
218 var buf: [os.HOST_NAME_MAX]u8 = undefined;218 var buf: [os.HOST_NAME_MAX]u8 = undefined;
...@@ -221,7 +221,7 @@ test "gethostname" {...@@ -221,7 +221,7 @@ test "gethostname" {
221}221}
222222
223test "pipe" {223test "pipe" {
224 if (os.windows.is_the_target)224 if (builtin.os == .windows)
225 return error.SkipZigTest;225 return error.SkipZigTest;
226226
227 var fds = try os.pipe();227 var fds = try os.pipe();
lib/std/os/uefi.zig+13-3
...@@ -1,16 +1,15 @@...@@ -1,16 +1,15 @@
1/// A protocol is an interface identified by a GUID.1/// A protocol is an interface identified by a GUID.
2pub const protocols = @import("uefi/protocols.zig");2pub const protocols = @import("uefi/protocols.zig");
3
3/// Status codes returned by EFI interfaces4/// Status codes returned by EFI interfaces
4pub const status = @import("uefi/status.zig");5pub const status = @import("uefi/status.zig");
5pub const tables = @import("uefi/tables.zig");6pub const tables = @import("uefi/tables.zig");
67
7const fmt = @import("std").fmt;8const fmt = @import("std").fmt;
89
9const builtin = @import("builtin");
10pub const is_the_target = builtin.os == .uefi;
11
12/// The EFI image's handle that is passed to its entry point.10/// The EFI image's handle that is passed to its entry point.
13pub var handle: Handle = undefined;11pub var handle: Handle = undefined;
12
14/// A pointer to the EFI System Table that is passed to the EFI image's entry point.13/// A pointer to the EFI System Table that is passed to the EFI image's entry point.
15pub var system_table: *tables.SystemTable = undefined;14pub var system_table: *tables.SystemTable = undefined;
1615
...@@ -50,26 +49,35 @@ pub const Handle = *@OpaqueType();...@@ -50,26 +49,35 @@ pub const Handle = *@OpaqueType();
50pub const Time = extern struct {49pub const Time = extern struct {
51 /// 1900 - 999950 /// 1900 - 9999
52 year: u16,51 year: u16,
52
53 /// 1 - 1253 /// 1 - 12
54 month: u8,54 month: u8,
55
55 /// 1 - 3156 /// 1 - 31
56 day: u8,57 day: u8,
58
57 /// 0 - 2359 /// 0 - 23
58 hour: u8,60 hour: u8,
61
59 /// 0 - 5962 /// 0 - 59
60 minute: u8,63 minute: u8,
64
61 /// 0 - 5965 /// 0 - 59
62 second: u8,66 second: u8,
63 _pad1: u8,67 _pad1: u8,
68
64 /// 0 - 99999999969 /// 0 - 999999999
65 nanosecond: u32,70 nanosecond: u32,
71
66 /// The time's offset in minutes from UTC.72 /// The time's offset in minutes from UTC.
67 /// Allowed values are -1440 to 1440 or unspecified_timezone73 /// Allowed values are -1440 to 1440 or unspecified_timezone
68 timezone: i16,74 timezone: i16,
69 daylight: packed struct {75 daylight: packed struct {
70 _pad1: u6,76 _pad1: u6,
77
71 /// If true, the time has been adjusted for daylight savings time.78 /// If true, the time has been adjusted for daylight savings time.
72 in_daylight: bool,79 in_daylight: bool,
80
73 /// If true, the time is affected by daylight savings time.81 /// If true, the time is affected by daylight savings time.
74 adjust_daylight: bool,82 adjust_daylight: bool,
75 },83 },
...@@ -83,8 +91,10 @@ pub const Time = extern struct {...@@ -83,8 +91,10 @@ pub const Time = extern struct {
83pub const TimeCapabilities = extern struct {91pub const TimeCapabilities = extern struct {
84 /// Resolution in Hz92 /// Resolution in Hz
85 resolution: u32,93 resolution: u32,
94
86 /// Accuracy in an error rate of 1e-6 parts per million.95 /// Accuracy in an error rate of 1e-6 parts per million.
87 accuracy: u32,96 accuracy: u32,
97
88 /// If true, a time set operation clears the device's time below the resolution level.98 /// If true, a time set operation clears the device's time below the resolution level.
89 sets_to_zero: bool,99 sets_to_zero: bool,
90};100};
lib/std/os/wasi.zig-2
...@@ -1,10 +1,8 @@...@@ -1,10 +1,8 @@
1// Based on https://github.com/CraneStation/wasi-sysroot/blob/wasi/libc-bottom-half/headers/public/wasi/core.h1// Based on https://github.com/CraneStation/wasi-sysroot/blob/wasi/libc-bottom-half/headers/public/wasi/core.h
2// and https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md2// and https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md
3const builtin = @import("builtin");
4const std = @import("std");3const std = @import("std");
5const assert = std.debug.assert;4const assert = std.debug.assert;
65
7pub const is_the_target = builtin.os == .wasi;
8pub usingnamespace @import("bits.zig");6pub usingnamespace @import("bits.zig");
97
10comptime {8comptime {
lib/std/os/windows.zig-1
...@@ -11,7 +11,6 @@ const assert = std.debug.assert;...@@ -11,7 +11,6 @@ const assert = std.debug.assert;
11const math = std.math;11const math = std.math;
12const maxInt = std.math.maxInt;12const maxInt = std.math.maxInt;
1313
14pub const is_the_target = builtin.os == .windows;
15pub const advapi32 = @import("windows/advapi32.zig");14pub const advapi32 = @import("windows/advapi32.zig");
16pub const kernel32 = @import("windows/kernel32.zig");15pub const kernel32 = @import("windows/kernel32.zig");
17pub const ntdll = @import("windows/ntdll.zig");16pub const ntdll = @import("windows/ntdll.zig");
lib/std/process.zig+2-2
...@@ -39,7 +39,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {...@@ -39,7 +39,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
39 var result = BufMap.init(allocator);39 var result = BufMap.init(allocator);
40 errdefer result.deinit();40 errdefer result.deinit();
4141
42 if (os.windows.is_the_target) {42 if (builtin.os == .windows) {
43 const ptr = try os.windows.GetEnvironmentStringsW();43 const ptr = try os.windows.GetEnvironmentStringsW();
44 defer os.windows.FreeEnvironmentStringsW(ptr);44 defer os.windows.FreeEnvironmentStringsW(ptr);
4545
...@@ -129,7 +129,7 @@ pub const GetEnvVarOwnedError = error{...@@ -129,7 +129,7 @@ pub const GetEnvVarOwnedError = error{
129/// Caller must free returned memory.129/// Caller must free returned memory.
130/// TODO make this go through libc when we have it130/// TODO make this go through libc when we have it
131pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {131pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
132 if (os.windows.is_the_target) {132 if (builtin.os == .windows) {
133 const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key);133 const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key);
134 defer allocator.free(key_with_null);134 defer allocator.free(key_with_null);
135135
lib/std/target.zig+3
...@@ -1,6 +1,9 @@...@@ -1,6 +1,9 @@
1const std = @import("std.zig");1const std = @import("std.zig");
2const builtin = std.builtin;2const builtin = std.builtin;
33
4/// TODO Nearly all the functions in this namespace would be
5/// better off if https://github.com/ziglang/zig/issues/425
6/// was solved.
4pub const Target = union(enum) {7pub const Target = union(enum) {
5 Native: void,8 Native: void,
6 Cross: Cross,9 Cross: Cross,
lib/std/thread.zig+5-5
...@@ -9,7 +9,7 @@ const assert = std.debug.assert;...@@ -9,7 +9,7 @@ const assert = std.debug.assert;
9pub const Thread = struct {9pub const Thread = struct {
10 data: Data,10 data: Data,
1111
12 pub const use_pthreads = !windows.is_the_target and builtin.link_libc;12 pub const use_pthreads = builtin.os != .windows and builtin.link_libc;
1313
14 /// Represents a kernel thread handle.14 /// Represents a kernel thread handle.
15 /// May be an integer or a pointer depending on the platform.15 /// May be an integer or a pointer depending on the platform.
...@@ -309,7 +309,7 @@ pub const Thread = struct {...@@ -309,7 +309,7 @@ pub const Thread = struct {
309 os.EINVAL => unreachable,309 os.EINVAL => unreachable,
310 else => return os.unexpectedErrno(@intCast(usize, err)),310 else => return os.unexpectedErrno(@intCast(usize, err)),
311 }311 }
312 } else if (os.linux.is_the_target) {312 } else if (builtin.os == .linux) {
313 var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |313 var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
314 os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |314 os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
315 os.CLONE_DETACHED;315 os.CLONE_DETACHED;
...@@ -342,18 +342,18 @@ pub const Thread = struct {...@@ -342,18 +342,18 @@ pub const Thread = struct {
342 };342 };
343343
344 pub fn cpuCount() CpuCountError!usize {344 pub fn cpuCount() CpuCountError!usize {
345 if (os.linux.is_the_target) {345 if (builtin.os == .linux) {
346 const cpu_set = try os.sched_getaffinity(0);346 const cpu_set = try os.sched_getaffinity(0);
347 return usize(os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast347 return usize(os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
348 }348 }
349 if (os.windows.is_the_target) {349 if (builtin.os == .windows) {
350 var system_info: windows.SYSTEM_INFO = undefined;350 var system_info: windows.SYSTEM_INFO = undefined;
351 windows.kernel32.GetSystemInfo(&system_info);351 windows.kernel32.GetSystemInfo(&system_info);
352 return @intCast(usize, system_info.dwNumberOfProcessors);352 return @intCast(usize, system_info.dwNumberOfProcessors);
353 }353 }
354 var count: c_int = undefined;354 var count: c_int = undefined;
355 var count_len: usize = @sizeOf(c_int);355 var count_len: usize = @sizeOf(c_int);
356 const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu";356 const name = if (comptime std.Target.current.isDarwin()) c"hw.logicalcpu" else c"hw.ncpu";
357 os.sysctlbynameC(name, &count, &count_len, null, 0) catch |err| switch (err) {357 os.sysctlbynameC(name, &count, &count_len, null, 0) catch |err| switch (err) {
358 error.NameTooLong => unreachable,358 error.NameTooLong => unreachable,
359 else => |e| return e,359 else => |e| return e,
lib/std/time.zig+10-10
...@@ -9,7 +9,7 @@ pub const epoch = @import("time/epoch.zig");...@@ -9,7 +9,7 @@ pub const epoch = @import("time/epoch.zig");
99
10/// Spurious wakeups are possible and no precision of timing is guaranteed.10/// Spurious wakeups are possible and no precision of timing is guaranteed.
11pub fn sleep(nanoseconds: u64) void {11pub fn sleep(nanoseconds: u64) void {
12 if (os.windows.is_the_target) {12 if (builtin.os == .windows) {
13 const ns_per_ms = ns_per_s / ms_per_s;13 const ns_per_ms = ns_per_s / ms_per_s;
14 const big_ms_from_ns = nanoseconds / ns_per_ms;14 const big_ms_from_ns = nanoseconds / ns_per_ms;
15 const ms = math.cast(os.windows.DWORD, big_ms_from_ns) catch math.maxInt(os.windows.DWORD);15 const ms = math.cast(os.windows.DWORD, big_ms_from_ns) catch math.maxInt(os.windows.DWORD);
...@@ -30,7 +30,7 @@ pub fn timestamp() u64 {...@@ -30,7 +30,7 @@ pub fn timestamp() u64 {
30/// Get the posix timestamp, UTC, in milliseconds30/// Get the posix timestamp, UTC, in milliseconds
31/// TODO audit this function. is it possible to return an error?31/// TODO audit this function. is it possible to return an error?
32pub fn milliTimestamp() u64 {32pub fn milliTimestamp() u64 {
33 if (os.windows.is_the_target) {33 if (builtin.os == .windows) {
34 //FileTime has a granularity of 100 nanoseconds34 //FileTime has a granularity of 100 nanoseconds
35 // and uses the NTFS/Windows epoch35 // and uses the NTFS/Windows epoch
36 var ft: os.windows.FILETIME = undefined;36 var ft: os.windows.FILETIME = undefined;
...@@ -41,7 +41,7 @@ pub fn milliTimestamp() u64 {...@@ -41,7 +41,7 @@ pub fn milliTimestamp() u64 {
41 const ft64 = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;41 const ft64 = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
42 return @divFloor(ft64, hns_per_ms) - -epoch_adj;42 return @divFloor(ft64, hns_per_ms) - -epoch_adj;
43 }43 }
44 if (os.wasi.is_the_target and !builtin.link_libc) {44 if (builtin.os == .wasi and !builtin.link_libc) {
45 var ns: os.wasi.timestamp_t = undefined;45 var ns: os.wasi.timestamp_t = undefined;
4646
47 // TODO: Verify that precision is ignored47 // TODO: Verify that precision is ignored
...@@ -51,7 +51,7 @@ pub fn milliTimestamp() u64 {...@@ -51,7 +51,7 @@ pub fn milliTimestamp() u64 {
51 const ns_per_ms = 1000;51 const ns_per_ms = 1000;
52 return @divFloor(ns, ns_per_ms);52 return @divFloor(ns, ns_per_ms);
53 }53 }
54 if (os.darwin.is_the_target) {54 if (comptime std.Target.current.isDarwin()) {
55 var tv: os.darwin.timeval = undefined;55 var tv: os.darwin.timeval = undefined;
56 var err = os.darwin.gettimeofday(&tv, null);56 var err = os.darwin.gettimeofday(&tv, null);
57 assert(err == 0);57 assert(err == 0);
...@@ -126,11 +126,11 @@ pub const Timer = struct {...@@ -126,11 +126,11 @@ pub const Timer = struct {
126 pub fn start() Error!Timer {126 pub fn start() Error!Timer {
127 var self: Timer = undefined;127 var self: Timer = undefined;
128128
129 if (os.windows.is_the_target) {129 if (builtin.os == .windows) {
130 self.frequency = os.windows.QueryPerformanceFrequency();130 self.frequency = os.windows.QueryPerformanceFrequency();
131 self.resolution = @divFloor(ns_per_s, self.frequency);131 self.resolution = @divFloor(ns_per_s, self.frequency);
132 self.start_time = os.windows.QueryPerformanceCounter();132 self.start_time = os.windows.QueryPerformanceCounter();
133 } else if (os.darwin.is_the_target) {133 } else if (comptime std.Target.current.isDarwin()) {
134 os.darwin.mach_timebase_info(&self.frequency);134 os.darwin.mach_timebase_info(&self.frequency);
135 self.resolution = @divFloor(self.frequency.numer, self.frequency.denom);135 self.resolution = @divFloor(self.frequency.numer, self.frequency.denom);
136 self.start_time = os.darwin.mach_absolute_time();136 self.start_time = os.darwin.mach_absolute_time();
...@@ -154,10 +154,10 @@ pub const Timer = struct {...@@ -154,10 +154,10 @@ pub const Timer = struct {
154 /// Reads the timer value since start or the last reset in nanoseconds154 /// Reads the timer value since start or the last reset in nanoseconds
155 pub fn read(self: *Timer) u64 {155 pub fn read(self: *Timer) u64 {
156 var clock = clockNative() - self.start_time;156 var clock = clockNative() - self.start_time;
157 if (os.windows.is_the_target) {157 if (builtin.os == .windows) {
158 return @divFloor(clock * ns_per_s, self.frequency);158 return @divFloor(clock * ns_per_s, self.frequency);
159 }159 }
160 if (os.darwin.is_the_target) {160 if (comptime std.Target.current.isDarwin()) {
161 return @divFloor(clock * self.frequency.numer, self.frequency.denom);161 return @divFloor(clock * self.frequency.numer, self.frequency.denom);
162 }162 }
163 return clock;163 return clock;
...@@ -177,10 +177,10 @@ pub const Timer = struct {...@@ -177,10 +177,10 @@ pub const Timer = struct {
177 }177 }
178178
179 fn clockNative() u64 {179 fn clockNative() u64 {
180 if (os.windows.is_the_target) {180 if (builtin.os == .windows) {
181 return os.windows.QueryPerformanceCounter();181 return os.windows.QueryPerformanceCounter();
182 }182 }
183 if (os.darwin.is_the_target) {183 if (comptime std.Target.current.isDarwin()) {
184 return os.darwin.mach_absolute_time();184 return os.darwin.mach_absolute_time();
185 }185 }
186 var ts: os.timespec = undefined;186 var ts: os.timespec = undefined;