Skip to content

Commit 60cd11b

Browse files
committed
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.
1 parent 8591731 commit 60cd11b

26 files changed

+170
-166
lines changed

lib/std/build.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ pub const RunStep = struct {
20002000
}
20012001

20022002
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";
20042004
const env_map = self.getEnvMap();
20052005
const prev_path = env_map.get(PATH) orelse {
20062006
env_map.set(PATH, search_path) catch unreachable;

lib/std/c/darwin.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const mach_hdr = if (@sizeOf(usize) == 8) mach_header_64 else mach_header;
3636
/// export a weak symbol here, to be overridden by the real one.
3737
pub extern "c" var _mh_execute_header: mach_hdr = undefined;
3838
comptime {
39-
if (std.os.darwin.is_the_target) {
39+
if (std.Target.current.isDarwin()) {
4040
@export("_mh_execute_header", _mh_execute_header, .Weak);
4141
}
4242
}

lib/std/child_process.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const TailQueue = std.TailQueue;
1717
const maxInt = std.math.maxInt;
1818

1919
pub const ChildProcess = struct {
20-
pid: if (os.windows.is_the_target) void else i32,
21-
handle: if (os.windows.is_the_target) windows.HANDLE else void,
22-
thread_handle: if (os.windows.is_the_target) windows.HANDLE else void,
20+
pid: if (builtin.os == .windows) void else i32,
21+
handle: if (builtin.os == .windows) windows.HANDLE else void,
22+
thread_handle: if (builtin.os == .windows) windows.HANDLE else void,
2323

2424
allocator: *mem.Allocator,
2525

@@ -39,16 +39,16 @@ pub const ChildProcess = struct {
3939
stderr_behavior: StdIo,
4040

4141
/// 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

4444
/// 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

4747
/// Set to change the current working directory when spawning the child process.
4848
cwd: ?[]const u8,
4949

50-
err_pipe: if (os.windows.is_the_target) void else [2]os.fd_t,
51-
llnode: if (os.windows.is_the_target) void else TailQueue(*ChildProcess).Node,
50+
err_pipe: if (builtin.os == .windows) void else [2]os.fd_t,
51+
llnode: if (builtin.os == .windows) void else TailQueue(*ChildProcess).Node,
5252

5353
pub const SpawnError = error{OutOfMemory} || os.ExecveError || os.SetIdError ||
5454
os.ChangeCurDirError || windows.CreateProcessError;
@@ -82,8 +82,8 @@ pub const ChildProcess = struct {
8282
.term = null,
8383
.env_map = null,
8484
.cwd = null,
85-
.uid = if (os.windows.is_the_target) {} else null,
86-
.gid = if (os.windows.is_the_target) {} else null,
85+
.uid = if (builtin.os == .windows) {} else null,
86+
.gid = if (builtin.os == .windows) {} else null,
8787
.stdin = null,
8888
.stdout = null,
8989
.stderr = null,
@@ -103,7 +103,7 @@ pub const ChildProcess = struct {
103103

104104
/// On success must call `kill` or `wait`.
105105
pub fn spawn(self: *ChildProcess) !void {
106-
if (os.windows.is_the_target) {
106+
if (builtin.os == .windows) {
107107
return self.spawnWindows();
108108
} else {
109109
return self.spawnPosix();
@@ -117,7 +117,7 @@ pub const ChildProcess = struct {
117117

118118
/// Forcibly terminates child process and then cleans up all resources.
119119
pub fn kill(self: *ChildProcess) !Term {
120-
if (os.windows.is_the_target) {
120+
if (builtin.os == .windows) {
121121
return self.killWindows(1);
122122
} else {
123123
return self.killPosix();
@@ -147,7 +147,7 @@ pub const ChildProcess = struct {
147147

148148
/// Blocks until child process terminates and then cleans up all resources.
149149
pub fn wait(self: *ChildProcess) !Term {
150-
if (os.windows.is_the_target) {
150+
if (builtin.os == .windows) {
151151
return self.waitWindows();
152152
} else {
153153
return self.waitPosix();

lib/std/debug.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
133133
/// chopping off the irrelevant frames and shifting so that the returned addresses pointer
134134
/// equals the passed in addresses pointer.
135135
pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void {
136-
if (windows.is_the_target) {
136+
if (builtin.os == .windows) {
137137
const addrs = stack_trace.instruction_addresses;
138138
const u32_addrs_len = @intCast(u32, addrs.len);
139139
const first_addr = first_address orelse {
@@ -310,7 +310,7 @@ pub const StackIterator = struct {
310310
};
311311

312312
pub 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) {
314314
return writeCurrentStackTraceWindows(out_stream, debug_info, tty_color, start_addr);
315315
}
316316
var it = StackIterator.init(start_addr);
@@ -342,10 +342,10 @@ pub fn writeCurrentStackTraceWindows(
342342
/// TODO once https://github.com/ziglang/zig/issues/3157 is fully implemented,
343343
/// make this `noasync fn` and remove the individual noasync calls.
344344
pub 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) {
346346
return noasync printSourceAtAddressWindows(debug_info, out_stream, address, tty_color);
347347
}
348-
if (os.darwin.is_the_target) {
348+
if (comptime std.Target.current.isDarwin()) {
349349
return noasync printSourceAtAddressMacOs(debug_info, out_stream, address, tty_color);
350350
}
351351
return noasync printSourceAtAddressPosix(debug_info, out_stream, address, tty_color);
@@ -832,10 +832,10 @@ pub const OpenSelfDebugInfoError = error{
832832
pub fn openSelfDebugInfo(allocator: *mem.Allocator) !DebugInfo {
833833
if (builtin.strip_debug_info)
834834
return error.MissingDebugInfo;
835-
if (windows.is_the_target) {
835+
if (builtin.os == .windows) {
836836
return noasync openSelfDebugInfoWindows(allocator);
837837
}
838-
if (os.darwin.is_the_target) {
838+
if (comptime std.Target.current.isDarwin()) {
839839
return noasync openSelfDebugInfoMacOs(allocator);
840840
}
841841
return noasync openSelfDebugInfoPosix(allocator);
@@ -2364,7 +2364,7 @@ pub fn attachSegfaultHandler() void {
23642364
if (!have_segfault_handling_support) {
23652365
@compileError("segfault handler not supported for this target");
23662366
}
2367-
if (windows.is_the_target) {
2367+
if (builtin.os == .windows) {
23682368
windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
23692369
return;
23702370
}
@@ -2378,7 +2378,7 @@ pub fn attachSegfaultHandler() void {
23782378
}
23792379

23802380
fn resetSegfaultHandler() void {
2381-
if (windows.is_the_target) {
2381+
if (builtin.os == .windows) {
23822382
if (windows_segfault_handle) |handle| {
23832383
assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
23842384
windows_segfault_handle = null;

lib/std/event/channel.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ test "std.event.Channel" {
307307
// https://github.com/ziglang/zig/issues/1908
308308
if (builtin.single_threaded) return error.SkipZigTest;
309309
// 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

312312
var loop: Loop = undefined;
313313
// TODO make a multi threaded test

lib/std/event/fs.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ fn hashString(s: []const u16) u32 {
909909
// var close_op_consumed = false;
910910
// defer if (!close_op_consumed) close_op.finish();
911911
//
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;
913913
// const mode = 0;
914914
// const fd = try await (async openPosix(self.channel.loop, resolved_path, flags, mode) catch unreachable);
915915
// close_op.setHandle(fd);

lib/std/event/future.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test "std.event.Future" {
8686
// https://github.com/ziglang/zig/issues/1908
8787
if (builtin.single_threaded) return error.SkipZigTest;
8888
// 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

9191
const allocator = std.heap.direct_allocator;
9292

lib/std/event/lock.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ test "std.event.Lock" {
119119
// TODO https://github.com/ziglang/zig/issues/1908
120120
if (builtin.single_threaded) return error.SkipZigTest;
121121
// 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

124124
const allocator = std.heap.direct_allocator;
125125

lib/std/fs.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub const AtomicFile = struct {
255255
assert(!self.finished);
256256
self.file.close();
257257
self.finished = true;
258-
if (os.windows.is_the_target) {
258+
if (builtin.os == .windows) {
259259
const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path);
260260
const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);
261261
return os.renameW(&tmp_path_w, &dest_path_w);
@@ -659,7 +659,7 @@ pub const Dir = struct {
659659
/// Closing the returned `Dir` is checked illegal behavior.
660660
/// On POSIX targets, this function is comptime-callable.
661661
pub fn cwd() Dir {
662-
if (os.windows.is_the_target) {
662+
if (builtin.os == .windows) {
663663
return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
664664
} else {
665665
return Dir{ .fd = os.AT_FDCWD };
@@ -711,7 +711,7 @@ pub const Dir = struct {
711711

712712
/// Call `close` on the result when done.
713713
pub fn openDir(self: Dir, sub_path: []const u8) OpenError!Dir {
714-
if (os.windows.is_the_target) {
714+
if (builtin.os == .windows) {
715715
const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
716716
return self.openDirW(&sub_path_w);
717717
}
@@ -722,7 +722,7 @@ pub const Dir = struct {
722722

723723
/// Same as `openDir` except the parameter is null-terminated.
724724
pub fn openDirC(self: Dir, sub_path_c: [*]const u8) OpenError!Dir {
725-
if (os.windows.is_the_target) {
725+
if (builtin.os == .windows) {
726726
const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
727727
return self.openDirW(&sub_path_w);
728728
}
@@ -829,7 +829,7 @@ pub const Dir = struct {
829829
/// Returns `error.DirNotEmpty` if the directory is not empty.
830830
/// To delete a directory recursively, see `deleteTree`.
831831
pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
832-
if (os.windows.is_the_target) {
832+
if (builtin.os == .windows) {
833833
const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
834834
return self.deleteDirW(&sub_path_w);
835835
}
@@ -1146,10 +1146,10 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
11461146
pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;
11471147

11481148
pub fn openSelfExe() OpenSelfExeError!File {
1149-
if (os.linux.is_the_target) {
1149+
if (builtin.os == .linux) {
11501150
return File.openReadC(c"/proc/self/exe");
11511151
}
1152-
if (os.windows.is_the_target) {
1152+
if (builtin.os == .windows) {
11531153
var buf: [os.windows.PATH_MAX_WIDE]u16 = undefined;
11541154
const wide_slice = try selfExePathW(&buf);
11551155
return File.openReadW(wide_slice.ptr);
@@ -1180,7 +1180,7 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
11801180
/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
11811181
/// TODO make the return type of this a null terminated pointer
11821182
pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1183-
if (os.darwin.is_the_target) {
1183+
if (comptime std.Target.current.isDarwin()) {
11841184
var u32_len: u32 = out_buffer.len;
11851185
const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
11861186
if (rc != 0) return error.NameTooLong;
@@ -1228,7 +1228,7 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
12281228
/// Get the directory path that contains the current executable.
12291229
/// Returned value is a slice of out_buffer.
12301230
pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {
1231-
if (os.linux.is_the_target) {
1231+
if (builtin.os == .linux) {
12321232
// If the currently executing binary has been deleted,
12331233
// the file path looks something like `/a/b/c/exe (deleted)`
12341234
// This path cannot be opened, but it's valid for determining the directory

lib/std/fs/file.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub const File = struct {
2727

2828
/// Call close to clean up.
2929
pub fn openRead(path: []const u8) OpenError!File {
30-
if (windows.is_the_target) {
30+
if (builtin.os == .windows) {
3131
const path_w = try windows.sliceToPrefixedFileW(path);
3232
return openReadW(&path_w);
3333
}
@@ -37,7 +37,7 @@ pub const File = struct {
3737

3838
/// `openRead` except with a null terminated path
3939
pub fn openReadC(path: [*]const u8) OpenError!File {
40-
if (windows.is_the_target) {
40+
if (builtin.os == .windows) {
4141
const path_w = try windows.cStrToPrefixedFileW(path);
4242
return openReadW(&path_w);
4343
}
@@ -69,7 +69,7 @@ pub const File = struct {
6969
/// If a file already exists in the destination it will be truncated.
7070
/// Call close to clean up.
7171
pub fn openWriteMode(path: []const u8, file_mode: Mode) OpenError!File {
72-
if (windows.is_the_target) {
72+
if (builtin.os == .windows) {
7373
const path_w = try windows.sliceToPrefixedFileW(path);
7474
return openWriteModeW(&path_w, file_mode);
7575
}
@@ -79,7 +79,7 @@ pub const File = struct {
7979

8080
/// Same as `openWriteMode` except `path` is null-terminated.
8181
pub fn openWriteModeC(path: [*]const u8, file_mode: Mode) OpenError!File {
82-
if (windows.is_the_target) {
82+
if (builtin.os == .windows) {
8383
const path_w = try windows.cStrToPrefixedFileW(path);
8484
return openWriteModeW(&path_w, file_mode);
8585
}
@@ -106,7 +106,7 @@ pub const File = struct {
106106
/// If a file already exists in the destination this returns OpenError.PathAlreadyExists
107107
/// Call close to clean up.
108108
pub fn openWriteNoClobber(path: []const u8, file_mode: Mode) OpenError!File {
109-
if (windows.is_the_target) {
109+
if (builtin.os == .windows) {
110110
const path_w = try windows.sliceToPrefixedFileW(path);
111111
return openWriteNoClobberW(&path_w, file_mode);
112112
}
@@ -115,7 +115,7 @@ pub const File = struct {
115115
}
116116

117117
pub fn openWriteNoClobberC(path: [*]const u8, file_mode: Mode) OpenError!File {
118-
if (windows.is_the_target) {
118+
if (builtin.os == .windows) {
119119
const path_w = try windows.cStrToPrefixedFileW(path);
120120
return openWriteNoClobberW(&path_w, file_mode);
121121
}
@@ -174,7 +174,7 @@ pub const File = struct {
174174

175175
/// Test whether ANSI escape codes will be treated as such.
176176
pub fn supportsAnsiEscapeCodes(self: File) bool {
177-
if (windows.is_the_target) {
177+
if (builtin.os == .windows) {
178178
return os.isCygwinPty(self.handle);
179179
}
180180
if (self.isTty()) {
@@ -214,7 +214,7 @@ pub const File = struct {
214214
}
215215

216216
pub fn getEndPos(self: File) GetPosError!u64 {
217-
if (windows.is_the_target) {
217+
if (builtin.os == .windows) {
218218
return windows.GetFileSizeEx(self.handle);
219219
}
220220
return (try self.stat()).size;
@@ -223,7 +223,7 @@ pub const File = struct {
223223
pub const ModeError = os.FStatError;
224224

225225
pub fn mode(self: File) ModeError!Mode {
226-
if (windows.is_the_target) {
226+
if (builtin.os == .windows) {
227227
return {};
228228
}
229229
return (try self.stat()).mode;
@@ -246,7 +246,7 @@ pub const File = struct {
246246
pub const StatError = os.FStatError;
247247

248248
pub fn stat(self: File) StatError!Stat {
249-
if (windows.is_the_target) {
249+
if (builtin.os == .windows) {
250250
var io_status_block: windows.IO_STATUS_BLOCK = undefined;
251251
var info: windows.FILE_ALL_INFORMATION = undefined;
252252
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 {
291291
/// last modification timestamp in nanoseconds
292292
mtime: i64,
293293
) UpdateTimesError!void {
294-
if (windows.is_the_target) {
294+
if (builtin.os == .windows) {
295295
const atime_ft = windows.nanoSecondsToFileTime(atime);
296296
const mtime_ft = windows.nanoSecondsToFileTime(mtime);
297297
return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);

0 commit comments

Comments
 (0)