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.
master
Andrew Kelley 2019-10-24 01:06:03 -04:00
parent 8591731f2b
commit 60cd11bd4b
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
26 changed files with 170 additions and 166 deletions

View File

@ -2000,7 +2000,7 @@ pub const RunStep = struct {
}
pub fn addPathDir(self: *RunStep, search_path: []const u8) void {
const PATH = if (std.os.windows.is_the_target) "Path" else "PATH";
const PATH = if (builtin.os == .windows) "Path" else "PATH";
const env_map = self.getEnvMap();
const prev_path = env_map.get(PATH) orelse {
env_map.set(PATH, search_path) catch unreachable;

View File

@ -36,7 +36,7 @@ const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header;
/// export a weak symbol here, to be overridden by the real one.
pub extern "c" var _mh_execute_header: mach_hdr = undefined;
comptime {
if (std.os.darwin.is_the_target) {
if (std.Target.current.isDarwin()) {
@export("_mh_execute_header", _mh_execute_header, .Weak);
}
}

View File

@ -17,9 +17,9 @@ const TailQueue = std.TailQueue;
const maxInt = std.math.maxInt;
pub const ChildProcess = struct {
pid: if (os.windows.is_the_target) void else i32,
handle: if (os.windows.is_the_target) windows.HANDLE else void,
thread_handle: if (os.windows.is_the_target) windows.HANDLE else void,
pid: if (builtin.os == .windows) void else i32,
handle: if (builtin.os == .windows) windows.HANDLE else void,
thread_handle: if (builtin.os == .windows) windows.HANDLE else void,
allocator: *mem.Allocator,
@ -39,16 +39,16 @@ pub const ChildProcess = struct {
stderr_behavior: StdIo,
/// Set to change the user id when spawning the child process.
uid: if (os.windows.is_the_target) void else ?u32,
uid: if (builtin.os == .windows) void else ?u32,
/// Set to change the group id when spawning the child process.
gid: if (os.windows.is_the_target) void else ?u32,
gid: if (builtin.os == .windows) void else ?u32,
/// Set to change the current working directory when spawning the child process.
cwd: ?[]const u8,
err_pipe: if (os.windows.is_the_target) void else [2]os.fd_t,
llnode: if (os.windows.is_the_target) void else TailQueue(*ChildProcess).Node,
err_pipe: if (builtin.os == .windows) void else [2]os.fd_t,
llnode: if (builtin.os == .windows) void else TailQueue(*ChildProcess).Node,
pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||
os.ChangeCurDirError || windows.CreateProcessError;
@ -82,8 +82,8 @@ pub const ChildProcess = struct {
.term = null,
.env_map = null,
.cwd = null,
.uid = if (os.windows.is_the_target) {} else null,
.gid = if (os.windows.is_the_target) {} else null,
.uid = if (builtin.os == .windows) {} else null,
.gid = if (builtin.os == .windows) {} else null,
.stdin = null,
.stdout = null,
.stderr = null,
@ -103,7 +103,7 @@ pub const ChildProcess = struct {
/// On success must call `kill` or `wait`.
pub fn spawn(self: *ChildProcess) !void {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return self.spawnWindows();
} else {
return self.spawnPosix();
@ -117,7 +117,7 @@ pub const ChildProcess = struct {
/// Forcibly terminates child process and then cleans up all resources.
pub fn kill(self: *ChildProcess) !Term {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return self.killWindows(1);
} else {
return self.killPosix();
@ -147,7 +147,7 @@ pub const ChildProcess = struct {
/// Blocks until child process terminates and then cleans up all resources.
pub fn wait(self: *ChildProcess) !Term {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return self.waitWindows();
} else {
return self.waitPosix();

View File

@ -133,7 +133,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
/// chopping off the irrelevant frames and shifting so that the returned addresses pointer
/// equals the passed in addresses pointer.
pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const addrs = stack_trace.instruction_addresses;
const u32_addrs_len = @intCast(u32, addrs.len);
const first_addr = first_address orelse {
@ -310,7 +310,7 @@ pub const StackIterator = struct {
};
pub fn writeCurrentStackTrace(out_stream: var, debug_info: *DebugInfo, tty_color: bool, start_addr: ?usize) !void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr);
}
var it = StackIterator.init(start_addr);
@ -342,10 +342,10 @@ pub fn writeCurrentStackTraceWindows(
/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,
/// make this `noasync fn` and remove the individual noasync calls.
pub fn printSourceAtAddress(debug_info: *DebugInfo, out_stream: var, address: usize, tty_color: bool) !void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return noasync printSourceAtAddressWindows(debug_info, out_stream, address, tty_color);
}
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
return noasync printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color);
}
return noasync printSourceAtAddressPosix(debug_info, out_stream, address, tty_color);
@ -832,10 +832,10 @@ pub const OpenSelfDebugInfoError = error{
pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
if (builtin.strip_debug_info)
return error.MissingDebugInfo;
if (windows.is_the_target) {
if (builtin.os == .windows) {
return noasync openSelfDebugInfoWindows(allocator);
}
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
return noasync openSelfDebugInfoMacOs(allocator);
}
return noasync openSelfDebugInfoPosix(allocator);
@ -2364,7 +2364,7 @@ pub fn attachSegfaultHandler() void {
if (!have_segfault_handling_support) {
@compileError("segfault handler not supported for this target");
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
return;
}
@ -2378,7 +2378,7 @@ pub fn attachSegfaultHandler() void {
}
fn resetSegfaultHandler() void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
if (windows_segfault_handle) |handle| {
assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
windows_segfault_handle = null;

View File

@ -307,7 +307,7 @@ test "std.event.Channel" {
// https://github.com/ziglang/zig/issues/1908
if (builtin.single_threaded) return error.SkipZigTest;
// https://github.com/ziglang/zig/issues/3251
if (std.os.freebsd.is_the_target) return error.SkipZigTest;
if (builtin.os == .freebsd) return error.SkipZigTest;
var loop: Loop = undefined;
// TODO make a multi threaded test

View File

@ -909,7 +909,7 @@ fn hashString(s: []const u16) u32 {
// var close_op_consumed = false;
// defer if (!close_op_consumed) close_op.finish();
//
// const flags = if (os.darwin.is_the_target) os.O_SYMLINK | os.O_EVTONLY else 0;
// const flags = if (comptime std.Target.current.isDarwin()) os.O_SYMLINK | os.O_EVTONLY else 0;
// const mode = 0;
// const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable);
// close_op.setHandle(fd);

View File

@ -86,7 +86,7 @@ test "std.event.Future" {
// https://github.com/ziglang/zig/issues/1908
if (builtin.single_threaded) return error.SkipZigTest;
// https://github.com/ziglang/zig/issues/3251
if (std.os.freebsd.is_the_target) return error.SkipZigTest;
if (builtin.os == .freebsd) return error.SkipZigTest;
const allocator = std.heap.direct_allocator;

View File

@ -119,7 +119,7 @@ test "std.event.Lock" {
// TODO https://github.com/ziglang/zig/issues/1908
if (builtin.single_threaded) return error.SkipZigTest;
// TODO https://github.com/ziglang/zig/issues/3251
if (std.os.freebsd.is_the_target) return error.SkipZigTest;
if (builtin.os == .freebsd) return error.SkipZigTest;
const allocator = std.heap.direct_allocator;

View File

@ -255,7 +255,7 @@ pub const AtomicFile = struct {
assert(!self.finished);
self.file.close();
self.finished = true;
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path);
const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);
return os.renameW(&tmp_path_w, &dest_path_w);
@ -659,7 +659,7 @@ pub const Dir = struct {
/// Closing the returned `Dir` is checked illegal behavior.
/// On POSIX targets, this function is comptime-callable.
pub fn cwd() Dir {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
} else {
return Dir{ .fd = os.AT_FDCWD };
@ -711,7 +711,7 @@ pub const Dir = struct {
/// Call `close` on the result when done.
pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
return self.openDirW(&sub_path_w);
}
@ -722,7 +722,7 @@ pub const Dir = struct {
/// Same as `openDir` except the parameter is null-terminated.
pub fn openDirC(self: Dir, sub_path_c: [*]const u8) OpenError!Dir {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
return self.openDirW(&sub_path_w);
}
@ -829,7 +829,7 @@ pub const Dir = struct {
/// Returns `error.DirNotEmpty` if the directory is not empty.
/// To delete a directory recursively, see `deleteTree`.
pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
return self.deleteDirW(&sub_path_w);
}
@ -1146,10 +1146,10 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;
pub fn openSelfExe() OpenSelfExeError!File {
if (os.linux.is_the_target) {
if (builtin.os == .linux) {
return File.openReadC(c"/proc/self/exe");
}
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
var buf: [os.windows.PATH_MAX_WIDE]u16 = undefined;
const wide_slice = try selfExePathW(&buf);
return File.openReadW(wide_slice.ptr);
@ -1180,7 +1180,7 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
/// TODO make the return type of this a null terminated pointer
pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
var u32_len: u32 = out_buffer.len;
const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
if (rc != 0) return error.NameTooLong;
@ -1228,7 +1228,7 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
/// Get the directory path that contains the current executable.
/// Returned value is a slice of out_buffer.
pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {
if (os.linux.is_the_target) {
if (builtin.os == .linux) {
// If the currently executing binary has been deleted,
// the file path looks something like `/a/b/c/exe (deleted)`
// This path cannot be opened, but it's valid for determining the directory

View File

@ -27,7 +27,7 @@ pub const File = struct {
/// Call close to clean up.
pub fn openRead(path: []const u8) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.sliceToPrefixedFileW(path);
return openReadW(&path_w);
}
@ -37,7 +37,7 @@ pub const File = struct {
/// `openRead` except with a null terminated path
pub fn openReadC(path: [*]const u8) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.cStrToPrefixedFileW(path);
return openReadW(&path_w);
}
@ -69,7 +69,7 @@ pub const File = struct {
/// If a file already exists in the destination it will be truncated.
/// Call close to clean up.
pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.sliceToPrefixedFileW(path);
return openWriteModeW(&path_w, file_mode);
}
@ -79,7 +79,7 @@ pub const File = struct {
/// Same as `openWriteMode` except `path` is null-terminated.
pub fn openWriteModeC(path: [*]const u8, file_mode: Mode) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.cStrToPrefixedFileW(path);
return openWriteModeW(&path_w, file_mode);
}
@ -106,7 +106,7 @@ pub const File = struct {
/// If a file already exists in the destination this returns OpenError.PathAlreadyExists
/// Call close to clean up.
pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.sliceToPrefixedFileW(path);
return openWriteNoClobberW(&path_w, file_mode);
}
@ -115,7 +115,7 @@ pub const File = struct {
}
pub fn openWriteNoClobberC(path: [*]const u8, file_mode: Mode) OpenError!File {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.cStrToPrefixedFileW(path);
return openWriteNoClobberW(&path_w, file_mode);
}
@ -174,7 +174,7 @@ pub const File = struct {
/// Test whether ANSI escape codes will be treated as such.
pub fn supportsAnsiEscapeCodes(self: File) bool {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return os.isCygwinPty(self.handle);
}
if (self.isTty()) {
@ -214,7 +214,7 @@ pub const File = struct {
}
pub fn getEndPos(self: File) GetPosError!u64 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.GetFileSizeEx(self.handle);
}
return (try self.stat()).size;
@ -223,7 +223,7 @@ pub const File = struct {
pub const ModeError = os.FStatError;
pub fn mode(self: File) ModeError!Mode {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return {};
}
return (try self.stat()).mode;
@ -246,7 +246,7 @@ pub const File = struct {
pub const StatError = os.FStatError;
pub fn stat(self: File) StatError!Stat {
if (windows.is_the_target) {
if (builtin.os == .windows) {
var io_status_block: windows.IO_STATUS_BLOCK = undefined;
var info: windows.FILE_ALL_INFORMATION = undefined;
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 {
/// last modification timestamp in nanoseconds
mtime: i64,
) UpdateTimesError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const atime_ft = windows.nanoSecondsToFileTime(atime);
const mtime_ft = windows.nanoSecondsToFileTime(mtime);
return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);

View File

@ -13,16 +13,16 @@ const process = std.process;
pub const sep_windows = '\\';
pub const sep_posix = '/';
pub const sep = if (windows.is_the_target) sep_windows else sep_posix;
pub const sep = if (builtin.os == .windows) sep_windows else sep_posix;
pub const sep_str = [1]u8{sep};
pub const delimiter_windows = ';';
pub const delimiter_posix = ':';
pub const delimiter = if (windows.is_the_target) delimiter_windows else delimiter_posix;
pub const delimiter = if (builtin.os == .windows) delimiter_windows else delimiter_posix;
pub fn isSep(byte: u8) bool {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return byte == '/' or byte == '\\';
} else {
return byte == '/';
@ -72,7 +72,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u
return buf;
}
pub const join = if (windows.is_the_target) joinWindows else joinPosix;
pub const join = if (builtin.os == .windows) joinWindows else joinPosix;
/// Naively combines a series of paths with the native path seperator.
/// Allocates memory for the result, which must be freed by the caller.
@ -129,7 +129,7 @@ test "join" {
}
pub fn isAbsolute(path: []const u8) bool {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return isAbsoluteWindows(path);
} else {
return isAbsolutePosix(path);
@ -327,7 +327,7 @@ test "windowsParsePath" {
}
pub fn diskDesignator(path: []const u8) []const u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return diskDesignatorWindows(path);
} else {
return "";
@ -392,7 +392,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return resolveWindows(allocator, paths);
} else {
return resolvePosix(allocator, paths);
@ -409,7 +409,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
/// Without performing actual syscalls, resolving `..` could be incorrect.
pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
return process.getCwdAlloc(allocator);
}
@ -504,7 +504,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
result_disk_designator = result[0..result_index];
},
WindowsPath.Kind.None => {
assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
const cwd = try process.getCwdAlloc(allocator);
defer allocator.free(cwd);
const parsed_cwd = windowsParsePath(cwd);
@ -519,7 +519,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
},
}
} else {
assert(windows.is_the_target); // resolveWindows called on non windows can't use getCwd
assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
// TODO call get cwd for the result_disk_designator instead of the global one
const cwd = try process.getCwdAlloc(allocator);
defer allocator.free(cwd);
@ -590,7 +590,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
/// Without performing actual syscalls, resolving `..` could be incorrect.
pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (paths.len == 0) {
assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd
assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
return process.getCwdAlloc(allocator);
}
@ -612,7 +612,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
if (have_abs) {
result = try allocator.alloc(u8, max_size);
} else {
assert(!windows.is_the_target); // resolvePosix called on windows can't use getCwd
assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
const cwd = try process.getCwdAlloc(allocator);
defer allocator.free(cwd);
result = try allocator.alloc(u8, max_size + cwd.len + 1);
@ -653,7 +653,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
test "resolve" {
const cwd = try process.getCwdAlloc(debug.global_allocator);
if (windows.is_the_target) {
if (builtin.os == .windows) {
if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
cwd[0] = asciiUpper(cwd[0]);
}
@ -669,7 +669,7 @@ test "resolveWindows" {
// TODO https://github.com/ziglang/zig/issues/3288
return error.SkipZigTest;
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
const cwd = try process.getCwdAlloc(debug.global_allocator);
const parsed_cwd = windowsParsePath(cwd);
{
@ -735,7 +735,7 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
/// If the path is a file in the current directory (no directory component)
/// then returns null
pub fn dirname(path: []const u8) ?[]const u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return dirnameWindows(path);
} else {
return dirnamePosix(path);
@ -867,7 +867,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void {
}
pub fn basename(path: []const u8) []const u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return basenameWindows(path);
} else {
return basenamePosix(path);
@ -983,7 +983,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
/// string is returned.
/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return relativeWindows(allocator, from, to);
} else {
return relativePosix(allocator, from, to);

View File

@ -44,7 +44,7 @@ const DirectAllocator = struct {
if (n == 0)
return (([*]u8)(undefined))[0..0];
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const w = os.windows;
// Although officially it's at least aligned to page boundary,
@ -130,7 +130,7 @@ const DirectAllocator = struct {
fn shrink(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const w = os.windows;
if (new_size == 0) {
// From the docs:
@ -170,7 +170,7 @@ const DirectAllocator = struct {
fn realloc(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
if (old_mem.len == 0) {
return alloc(allocator, new_size, new_align);
}

View File

@ -37,7 +37,7 @@ pub const is_async = mode != .blocking;
pub const GetStdIoError = os.windows.GetStdHandleError;
pub fn getStdOut() GetStdIoError!File {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const handle = try os.windows.GetStdHandle(os.windows.STD_OUTPUT_HANDLE);
return File.openHandle(handle);
}
@ -45,7 +45,7 @@ pub fn getStdOut() GetStdIoError!File {
}
pub fn getStdErr() GetStdIoError!File {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const handle = try os.windows.GetStdHandle(os.windows.STD_ERROR_HANDLE);
return File.openHandle(handle);
}
@ -53,7 +53,7 @@ pub fn getStdErr() GetStdIoError!File {
}
pub fn getStdIn() GetStdIoError!File {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const handle = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE);
return File.openHandle(handle);
}

View File

@ -85,13 +85,13 @@ pub const errno = system.getErrno;
/// must call `fsync` before `close`.
/// Note: The Zig standard library does not support POSIX thread cancellation.
pub fn close(fd: fd_t) void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.CloseHandle(fd);
}
if (wasi.is_the_target) {
if (builtin.os == .wasi) {
_ = wasi.fd_close(fd);
}
if (darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
// This avoids the EINTR problem.
switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {
EBADF => unreachable, // Always a race condition.
@ -113,12 +113,12 @@ pub const GetRandomError = OpenError;
/// appropriate OS-specific library call. Otherwise it uses the zig standard
/// library implementation.
pub fn getrandom(buffer: []u8) GetRandomError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.RtlGenRandom(buffer);
}
if (linux.is_the_target or freebsd.is_the_target) {
if (builtin.os == .linux or builtin.os == .freebsd) {
var buf = buffer;
const use_c = !linux.is_the_target or
const use_c = builtin.os != .linux or
std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
while (buf.len != 0) {
@ -145,7 +145,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
}
return;
}
if (wasi.is_the_target) {
if (builtin.os == .wasi) {
switch (wasi.random_get(buffer.ptr, buffer.len)) {
0 => return,
else => |err| return unexpectedErrno(err),
@ -175,7 +175,7 @@ pub fn abort() noreturn {
// MSVCRT abort() sometimes opens a popup window which is undesirable, so
// even when linking libc on Windows we use our own abort implementation.
// See https://github.com/ziglang/zig/issues/2071 for more details.
if (windows.is_the_target) {
if (builtin.os == .windows) {
if (builtin.mode == .Debug) {
@breakpoint();
}
@ -206,14 +206,14 @@ pub fn raise(sig: u8) RaiseError!void {
}
}
if (wasi.is_the_target) {
if (builtin.os == .wasi) {
switch (wasi.proc_raise(SIGABRT)) {
0 => return,
else => |err| return unexpectedErrno(err),
}
}
if (linux.is_the_target) {
if (builtin.os == .linux) {
var set: linux.sigset_t = undefined;
linux.blockAppSignals(&set);
const tid = linux.syscall0(linux.SYS_gettid);
@ -245,16 +245,16 @@ pub fn exit(status: u8) noreturn {
if (builtin.link_libc) {
system.exit(status);
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
windows.kernel32.ExitProcess(status);
}
if (wasi.is_the_target) {
if (builtin.os == .wasi) {
wasi.proc_exit(status);
}
if (linux.is_the_target and !builtin.single_threaded) {
if (builtin.os == .linux and !builtin.single_threaded) {
linux.exit_group(status);
}
if (uefi.is_the_target) {
if (builtin.os == .uefi) {
// exit() is only avaliable if exitBootServices() has not been called yet.
// This call to exit should not fail, so we don't care about its return value.
if (uefi.system_table.boot_services) |bs| {
@ -283,11 +283,11 @@ pub const ReadError = error{
/// If the application has a global event loop enabled, EAGAIN is handled
/// via the event loop. Otherwise EAGAIN results in error.WouldBlock.
pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.ReadFile(fd, buf);
}
if (wasi.is_the_target and !builtin.link_libc) {
if (builtin.os == .wasi and !builtin.link_libc) {
const iovs = [1]iovec{iovec{
.iov_base = buf.ptr,
.iov_len = buf.len,
@ -327,7 +327,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
/// Number of bytes read is returned. Upon reading end-of-file, zero is returned.
/// This function is for blocking file descriptors only.
pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize {
if (darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
// Darwin does not have preadv but it does have pread.
var off: usize = 0;
var iov_i: usize = 0;
@ -398,11 +398,11 @@ pub const WriteError = error{
/// Write to a file descriptor. Keeps trying if it gets interrupted.
/// This function is for blocking file descriptors only.
pub fn write(fd: fd_t, bytes: []const u8) WriteError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.WriteFile(fd, bytes);
}
if (wasi.is_the_target and !builtin.link_libc) {
if (builtin.os == .wasi and !builtin.link_libc) {
const ciovs = [1]iovec_const{iovec_const{
.iov_base = bytes.ptr,
.iov_len = bytes.len,
@ -477,7 +477,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!void {
/// This function is for blocking file descriptors only. For non-blocking, see
/// `pwritevAsync`.
pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) WriteError!void {
if (darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
// Darwin does not have pwritev but it does have pwrite.
var off: usize = 0;
var iov_i: usize = 0;
@ -841,7 +841,7 @@ pub const GetCwdError = error{
/// The result is a slice of out_buffer, indexed from 0.
pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.GetCurrentDirectory(out_buffer);
}
@ -882,7 +882,7 @@ pub const SymLinkError = error{
/// If `sym_link_path` exists, it will not be overwritten.
/// See also `symlinkC` and `symlinkW`.
pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const target_path_w = try windows.sliceToPrefixedFileW(target_path);
const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
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!
/// This is the same as `symlink` except the parameters are null-terminated pointers.
/// See also `symlink`.
pub fn symlinkC(target_path: [*]const u8, sym_link_path: [*]const u8) SymLinkError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const target_path_w = try windows.cStrToPrefixedFileW(target_path);
const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
@ -971,7 +971,7 @@ pub const UnlinkError = error{
/// Delete a name and possibly the file it refers to.
/// See also `unlinkC`.
pub fn unlink(file_path: []const u8) UnlinkError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
return windows.DeleteFileW(&file_path_w);
} else {
@ -982,7 +982,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {
/// Same as `unlink` except the parameter is a null terminated UTF8-encoded string.
pub fn unlinkC(file_path: [*]const u8) UnlinkError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.cStrToPrefixedFileW(file_path);
return windows.DeleteFileW(&file_path_w);
}
@ -1012,7 +1012,7 @@ pub const UnlinkatError = UnlinkError || error{
/// Delete a file name and possibly the file it refers to, based on an open directory handle.
pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
return unlinkatW(dirfd, &file_path_w, flags);
}
@ -1022,7 +1022,7 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
/// Same as `unlinkat` but `file_path` is a null-terminated string.
pub fn unlinkatC(dirfd: fd_t, file_path_c: [*]const u8, flags: u32) UnlinkatError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);
return unlinkatW(dirfd, &file_path_w, flags);
}
@ -1133,7 +1133,7 @@ const RenameError = error{
/// Change the name or location of a file.
pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const old_path_w = try windows.sliceToPrefixedFileW(old_path);
const new_path_w = try windows.sliceToPrefixedFileW(new_path);
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 {
/// Same as `rename` except the parameters are null-terminated byte arrays.
pub fn renameC(old_path: [*]const u8, new_path: [*]const u8) RenameError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const old_path_w = try windows.cStrToPrefixedFileW(old_path);
const new_path_w = try windows.cStrToPrefixedFileW(new_path);
return renameW(&old_path_w, &new_path_w);
@ -1201,7 +1201,7 @@ pub const MakeDirError = error{
/// Create a directory.
/// `mode` is ignored on Windows.
pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
return windows.CreateDirectoryW(&dir_path_w, null);
} else {
@ -1212,7 +1212,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
/// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string.
pub fn mkdirC(dir_path: [*]const u8, mode: u32) MakeDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
return windows.CreateDirectoryW(&dir_path_w, null);
}
@ -1251,7 +1251,7 @@ pub const DeleteDirError = error{
/// Deletes an empty directory.
pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
return windows.RemoveDirectoryW(&dir_path_w);
} else {
@ -1262,7 +1262,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
/// Same as `rmdir` except the parameter is null-terminated.
pub fn rmdirC(dir_path: [*]const u8) DeleteDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
return windows.RemoveDirectoryW(&dir_path_w);
}
@ -1298,7 +1298,7 @@ pub const ChangeCurDirError = error{
/// Changes the current working directory of the calling process.
/// `dir_path` is recommended to be a UTF-8 encoded string.
pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
@compileError("TODO implement chdir for Windows");
} else {
@ -1309,7 +1309,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
/// Same as `chdir` except the parameter is null-terminated.
pub fn chdirC(dir_path: [*]const u8) ChangeCurDirError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
@compileError("TODO implement chdir for Windows");
}
@ -1340,7 +1340,7 @@ pub const ReadLinkError = error{
/// Read value of a symbolic link.
/// The return value is a slice of `out_buffer` from index 0.
pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.sliceToPrefixedFileW(file_path);
@compileError("TODO implement readlink for Windows");
} else {
@ -1351,7 +1351,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
/// Same as `readlink` except `file_path` is null-terminated.
pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.cStrToPrefixedFileW(file_path);
@compileError("TODO implement readlink for Windows");
}
@ -1372,7 +1372,7 @@ pub fn readlinkC(file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
}
pub fn readlinkatC(dirfd: fd_t, file_path: [*]const u8, out_buffer: []u8) ReadLinkError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const file_path_w = try windows.cStrToPrefixedFileW(file_path);
@compileError("TODO implement readlink for Windows");
}
@ -1440,7 +1440,7 @@ pub fn setregid(rgid: u32, egid: u32) SetIdError!void {
/// Test whether a file descriptor refers to a terminal.
pub fn isatty(handle: fd_t) bool {
if (windows.is_the_target) {
if (builtin.os == .windows) {
if (isCygwinPty(handle))
return true;
@ -1450,10 +1450,10 @@ pub fn isatty(handle: fd_t) bool {
if (builtin.link_libc) {
return system.isatty(handle) != 0;
}
if (wasi.is_the_target) {
if (builtin.os == .wasi) {
@compileError("TODO implement std.os.isatty for WASI");
}
if (linux.is_the_target) {
if (builtin.os == .linux) {
var wsz: linux.winsize = undefined;
return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, isize(handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
}
@ -1461,7 +1461,7 @@ pub fn isatty(handle: fd_t) bool {
}
pub fn isCygwinPty(handle: fd_t) bool {
if (!windows.is_the_target) return false;
if (builtin.os != .windows) return false;
const size = @sizeOf(windows.FILE_NAME_INFO);
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;
pub fn fstat(fd: fd_t) FStatError!Stat {
var stat: Stat = undefined;
if (darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
switch (darwin.getErrno(darwin.@"fstat$INODE64"(fd, &stat))) {
0 => return stat,
EINVAL => unreachable,
@ -2227,7 +2227,7 @@ pub const AccessError = error{
/// check user's permissions for a file
/// TODO currently this assumes `mode` is `F_OK` on Windows.
pub fn access(path: []const u8, mode: u32) AccessError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.sliceToPrefixedFileW(path);
_ = try windows.GetFileAttributesW(&path_w);
return;
@ -2238,7 +2238,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {
/// Same as `access` except `path` is null-terminated.
pub fn accessC(path: [*]const u8, mode: u32) AccessError!void {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const path_w = try windows.cStrToPrefixedFileW(path);
_ = try windows.GetFileAttributesW(&path_w);
return;
@ -2358,7 +2358,7 @@ pub const SeekError = error{Unseekable} || UnexpectedError;
/// Repositions read/write file offset relative to the beginning.
pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
var result: u64 = undefined;
switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) {
0 => return,
@ -2370,7 +2370,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
else => |err| return unexpectedErrno(err),
}
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.SetFilePointerEx_BEGIN(fd, offset);
}
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 {
/// Repositions read/write file offset relative to the current offset.
pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
var result: u64 = undefined;
switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) {
0 => return,
@ -2399,7 +2399,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
else => |err| return unexpectedErrno(err),
}
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.SetFilePointerEx_CURRENT(fd, offset);
}
switch (errno(system.lseek(fd, offset, SEEK_CUR))) {
@ -2415,7 +2415,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
/// Repositions read/write file offset relative to the end.
pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
var result: u64 = undefined;
switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) {
0 => return,
@ -2427,7 +2427,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
else => |err| return unexpectedErrno(err),
}
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.SetFilePointerEx_END(fd, offset);
}
switch (errno(system.lseek(fd, offset, SEEK_END))) {
@ -2443,7 +2443,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
/// Returns the read/write file offset relative to the beginning.
pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
if (linux.is_the_target and !builtin.link_libc and @sizeOf(usize) == 4) {
if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
var result: u64 = undefined;
switch (errno(system.llseek(fd, 0, &result, SEEK_CUR))) {
0 => return result,
@ -2455,7 +2455,7 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
else => |err| return unexpectedErrno(err),
}
}
if (windows.is_the_target) {
if (builtin.os == .windows) {
return windows.SetFilePointerEx_CURRENT_get(fd);
}
const rc = system.lseek(fd, 0, SEEK_CUR);
@ -2504,7 +2504,7 @@ pub const RealPathError = error{
/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.
/// See also `realpathC` and `realpathW`.
pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const pathname_w = try windows.sliceToPrefixedFileW(pathname);
return realpathW(&pathname_w, out_buffer);
}
@ -2514,11 +2514,11 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
/// Same as `realpath` except `pathname` is null-terminated.
pub fn realpathC(pathname: [*]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
if (windows.is_the_target) {
if (builtin.os == .windows) {
const pathname_w = try windows.cStrToPrefixedFileW(pathname);
return realpathW(&pathname_w, out_buffer);
}
if (linux.is_the_target and !builtin.link_libc) {
if (builtin.os == .linux and !builtin.link_libc) {
const fd = try openC(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0);
defer close(fd);
@ -2596,9 +2596,12 @@ pub fn nanosleep(seconds: u64, nanoseconds: u64) void {
}
}
pub fn dl_iterate_phdr(comptime T: type, callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32, data: ?*T) isize {
// This is implemented only for systems using ELF executables
if (windows.is_the_target or builtin.os == .uefi or wasi.is_the_target or darwin.is_the_target)
pub fn dl_iterate_phdr(
comptime T: type,
callback: extern fn (info: *dl_phdr_info, size: usize, data: ?*T) i32,
data: ?*T,
) isize {
if (builtin.object_format != .elf)
@compileError("dl_iterate_phdr is not available for this target");
if (builtin.link_libc) {
@ -2737,7 +2740,7 @@ pub const SigaltstackError = error{
} || UnexpectedError;
pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
if (windows.is_the_target or uefi.is_the_target or wasi.is_the_target)
if (builtin.os == .windows or builtin.os == .uefi or builtin.os == .wasi)
@compileError("std.os.sigaltstack not available for this target");
switch (errno(system.sigaltstack(ss, old_ss))) {
@ -2809,7 +2812,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
else => |err| return unexpectedErrno(err),
}
}
if (linux.is_the_target) {
if (builtin.os == .linux) {
var uts: utsname = undefined;
switch (errno(system.uname(&uts))) {
0 => {

View File

@ -1,8 +1,4 @@
const builtin = @import("builtin");
const std = @import("../std.zig");
pub const is_the_target = switch (builtin.os) {
.macosx, .tvos, .watchos, .ios => true,
else => false,
};
pub usingnamespace std.c;
pub usingnamespace @import("bits.zig");

View File

@ -1,5 +1,3 @@
const std = @import("../std.zig");
const builtin = @import("builtin");
pub const is_the_target = builtin.os == .freebsd;
pub usingnamespace std.c;
pub usingnamespace @import("bits.zig");

View File

@ -13,7 +13,6 @@ const elf = std.elf;
const vdso = @import("linux/vdso.zig");
const dl = @import("../dynamic_library.zig");
pub const is_the_target = builtin.os == .linux;
pub usingnamespace switch (builtin.arch) {
.x86_64 => @import("linux/x86_64.zig"),
.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
}
test "" {
if (is_the_target) {
if (builtin.os == .linux) {
_ = @import("linux/test.zig");
}
}

View File

@ -1,5 +1,3 @@
const builtin = @import("builtin");
const std = @import("../std.zig");
pub const is_the_target = builtin.os == .netbsd;
pub usingnamespace std.c;
pub usingnamespace @import("bits.zig");

View File

@ -53,7 +53,7 @@ test "std.Thread.getCurrentId" {
thread.wait();
if (Thread.use_pthreads) {
expect(thread_current_id == thread_id);
} else if (os.windows.is_the_target) {
} else if (builtin.os == .windows) {
expect(Thread.getCurrentId() != thread_current_id);
} else {
// If the thread completes very quickly, then thread_id can be 0. See the
@ -212,7 +212,7 @@ test "dl_iterate_phdr" {
}
test "gethostname" {
if (os.windows.is_the_target)
if (builtin.os == .windows)
return error.SkipZigTest;
var buf: [os.HOST_NAME_MAX]u8 = undefined;
@ -221,7 +221,7 @@ test "gethostname" {
}
test "pipe" {
if (os.windows.is_the_target)
if (builtin.os == .windows)
return error.SkipZigTest;
var fds = try os.pipe();

View File

@ -1,16 +1,15 @@
/// A protocol is an interface identified by a GUID.
pub const protocols = @import("uefi/protocols.zig");
/// Status codes returned by EFI interfaces
pub const status = @import("uefi/status.zig");
pub const tables = @import("uefi/tables.zig");
const fmt = @import("std").fmt;
const builtin = @import("builtin");
pub const is_the_target = builtin.os == .uefi;
/// The EFI image's handle that is passed to its entry point.
pub var handle: Handle = undefined;
/// A pointer to the EFI System Table that is passed to the EFI image's entry point.
pub var system_table: *tables.SystemTable = undefined;
@ -50,26 +49,35 @@ pub const Handle = *@OpaqueType();
pub const Time = extern struct {
/// 1900 - 9999
year: u16,
/// 1 - 12
month: u8,
/// 1 - 31
day: u8,
/// 0 - 23
hour: u8,
/// 0 - 59
minute: u8,
/// 0 - 59
second: u8,
_pad1: u8,
/// 0 - 999999999
nanosecond: u32,
/// The time's offset in minutes from UTC.
/// Allowed values are -1440 to 1440 or unspecified_timezone
timezone: i16,
daylight: packed struct {
_pad1: u6,
/// If true, the time has been adjusted for daylight savings time.
in_daylight: bool,
/// If true, the time is affected by daylight savings time.
adjust_daylight: bool,
},
@ -83,8 +91,10 @@ pub const Time = extern struct {
pub const TimeCapabilities = extern struct {
/// Resolution in Hz
resolution: u32,
/// Accuracy in an error rate of 1e-6 parts per million.
accuracy: u32,
/// If true, a time set operation clears the device's time below the resolution level.
sets_to_zero: bool,
};

View File

@ -1,10 +1,8 @@
// Based on https://github.com/CraneStation/wasi-sysroot/blob/wasi/libc-bottom-half/headers/public/wasi/core.h
// and https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md
const builtin = @import("builtin");
const std = @import("std");
const assert = std.debug.assert;
pub const is_the_target = builtin.os == .wasi;
pub usingnamespace @import("bits.zig");
comptime {

View File

@ -11,7 +11,6 @@ const assert = std.debug.assert;
const math = std.math;
const maxInt = std.math.maxInt;
pub const is_the_target = builtin.os == .windows;
pub const advapi32 = @import("windows/advapi32.zig");
pub const kernel32 = @import("windows/kernel32.zig");
pub const ntdll = @import("windows/ntdll.zig");

View File

@ -39,7 +39,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
var result = BufMap.init(allocator);
errdefer result.deinit();
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const ptr = try os.windows.GetEnvironmentStringsW();
defer os.windows.FreeEnvironmentStringsW(ptr);
@ -129,7 +129,7 @@ pub const GetEnvVarOwnedError = error{
/// Caller must free returned memory.
/// TODO make this go through libc when we have it
pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const key_with_null = try std.unicode.utf8ToUtf16LeWithNull(allocator, key);
defer allocator.free(key_with_null);

View File

@ -1,6 +1,9 @@
const std = @import("std.zig");
const builtin = std.builtin;
/// TODO Nearly all the functions in this namespace would be
/// better off if https://github.com/ziglang/zig/issues/425
/// was solved.
pub const Target = union(enum) {
Native: void,
Cross: Cross,

View File

@ -9,7 +9,7 @@ const assert = std.debug.assert;
pub const Thread = struct {
data: Data,
pub const use_pthreads = !windows.is_the_target and builtin.link_libc;
pub const use_pthreads = builtin.os != .windows and builtin.link_libc;
/// Represents a kernel thread handle.
/// May be an integer or a pointer depending on the platform.
@ -309,7 +309,7 @@ pub const Thread = struct {
os.EINVAL => unreachable,
else => return os.unexpectedErrno(@intCast(usize, err)),
}
} else if (os.linux.is_the_target) {
} else if (builtin.os == .linux) {
var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
os.CLONE_DETACHED;
@ -342,18 +342,18 @@ pub const Thread = struct {
};
pub fn cpuCount() CpuCountError!usize {
if (os.linux.is_the_target) {
if (builtin.os == .linux) {
const cpu_set = try os.sched_getaffinity(0);
return usize(os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
}
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
var system_info: windows.SYSTEM_INFO = undefined;
windows.kernel32.GetSystemInfo(&system_info);
return @intCast(usize, system_info.dwNumberOfProcessors);
}
var count: c_int = undefined;
var count_len: usize = @sizeOf(c_int);
const name = if (os.darwin.is_the_target) c"hw.logicalcpu" else c"hw.ncpu";
const name = if (comptime std.Target.current.isDarwin()) c"hw.logicalcpu" else c"hw.ncpu";
os.sysctlbynameC(name, &count, &count_len, null, 0) catch |err| switch (err) {
error.NameTooLong => unreachable,
else => |e| return e,

View File

@ -9,7 +9,7 @@ pub const epoch = @import("time/epoch.zig");
/// Spurious wakeups are possible and no precision of timing is guaranteed.
pub fn sleep(nanoseconds: u64) void {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
const ns_per_ms = ns_per_s / ms_per_s;
const big_ms_from_ns = nanoseconds / ns_per_ms;
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 {
/// Get the posix timestamp, UTC, in milliseconds
/// TODO audit this function. is it possible to return an error?
pub fn milliTimestamp() u64 {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
//FileTime has a granularity of 100 nanoseconds
// and uses the NTFS/Windows epoch
var ft: os.windows.FILETIME = undefined;
@ -41,7 +41,7 @@ pub fn milliTimestamp() u64 {
const ft64 = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
return @divFloor(ft64, hns_per_ms) - -epoch_adj;
}
if (os.wasi.is_the_target and !builtin.link_libc) {
if (builtin.os == .wasi and !builtin.link_libc) {
var ns: os.wasi.timestamp_t = undefined;
// TODO: Verify that precision is ignored
@ -51,7 +51,7 @@ pub fn milliTimestamp() u64 {
const ns_per_ms = 1000;
return @divFloor(ns, ns_per_ms);
}
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
var tv: os.darwin.timeval = undefined;
var err = os.darwin.gettimeofday(&tv, null);
assert(err == 0);
@ -126,11 +126,11 @@ pub const Timer = struct {
pub fn start() Error!Timer {
var self: Timer = undefined;
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
self.frequency = os.windows.QueryPerformanceFrequency();
self.resolution = @divFloor(ns_per_s, self.frequency);
self.start_time = os.windows.QueryPerformanceCounter();
} else if (os.darwin.is_the_target) {
} else if (comptime std.Target.current.isDarwin()) {
os.darwin.mach_timebase_info(&self.frequency);
self.resolution = @divFloor(self.frequency.numer, self.frequency.denom);
self.start_time = os.darwin.mach_absolute_time();
@ -154,10 +154,10 @@ pub const Timer = struct {
/// Reads the timer value since start or the last reset in nanoseconds
pub fn read(self: *Timer) u64 {
var clock = clockNative() - self.start_time;
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return @divFloor(clock * ns_per_s, self.frequency);
}
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
return @divFloor(clock * self.frequency.numer, self.frequency.denom);
}
return clock;
@ -177,10 +177,10 @@ pub const Timer = struct {
}
fn clockNative() u64 {
if (os.windows.is_the_target) {
if (builtin.os == .windows) {
return os.windows.QueryPerformanceCounter();
}
if (os.darwin.is_the_target) {
if (comptime std.Target.current.isDarwin()) {
return os.darwin.mach_absolute_time();
}
var ts: os.timespec = undefined;