authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-25 01:52:27-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-28 14:51:53-05:00
log4616af0ca459358ffa09ba27f9daa8527a38fd35
tree2705847ded931e30dc924bd44acf24847743855c
parentfba39ff331a84f1a32d076ccbb8b87cd02ea7121
signaturelock-open Commit is signed but in an unrecognized format.

introduce operating system version ranges as part of the target

* re-introduce `std.build.Target` which is distinct from `std.Target`. `std.build.Target` wraps `std.Target` so that it can be annotated as "the native target" or an explicitly specified target. * `std.Target.Os` is moved to `std.Target.Os.Tag`. The former is now a struct which has the tag as well as version range information. * `std.elf` gains some more ELF header constants. * `std.Target.parse` gains the ability to parse operating system version ranges as well as glibc version. * Added `std.Target.isGnuLibC()`. * self-hosted dynamic linker detection and glibc version detection. This also adds the improved logic using `/usr/bin/env` rather than invoking the system C compiler to find the dynamic linker when zig is statically linked. Related: #2084 Note: this `/usr/bin/env` code is work-in-progress. * `-target-glibc` CLI option is removed in favor of the new `-target` syntax. Example: `-target x86_64-linux-gnu.2.27` closes #1907

59 files changed, 1275 insertions(+), 933 deletions(-)

lib/std/build.zig+33-7
......@@ -971,9 +971,9 @@ pub const Builder = struct {
971971};
972972
973973test "builder.findProgram compiles" {
974 // TODO: uncomment and fix the leak
975 // const builder = try Builder.create(std.testing.allocator, "zig", "zig-cache", "zig-cache");
976 const builder = try Builder.create(std.heap.page_allocator, "zig", "zig-cache", "zig-cache");
974 var buf: [1000]u8 = undefined;
975 var fba = std.heap.FixedBufferAllocator.init(&buf);
976 const builder = try Builder.create(&fba.allocator, "zig", "zig-cache", "zig-cache");
977977 defer builder.destroy();
978978 _ = builder.findProgram(&[_][]const u8{}, &[_][]const u8{}) catch null;
979979}
......@@ -981,11 +981,37 @@ test "builder.findProgram compiles" {
981981/// Deprecated. Use `builtin.Version`.
982982pub const Version = builtin.Version;
983983
984/// Deprecated. Use `std.Target.Cross`.
985pub const CrossTarget = std.Target.Cross;
986
987984/// Deprecated. Use `std.Target`.
988pub const Target = std.Target;
985pub const CrossTarget = std.Target;
986
987/// Wraps `std.Target` so that it can be annotated as "the native target" or an explicitly specified target.
988pub const Target = union(enum) {
989 Native,
990 Cross: std.Target,
991
992 pub fn getTarget(self: Target) std.Target {
993 return switch (self) {
994 .Native => std.Target.current,
995 .Cross => |t| t,
996 };
997 }
998
999 pub fn getOs(self: Target) std.Target.Os.Tag {
1000 return self.getTarget().os.tag;
1001 }
1002
1003 pub fn getCpu(self: Target) std.Target.Cpu {
1004 return self.getTarget().cpu;
1005 }
1006
1007 pub fn getAbi(self: Target) std.Target.Abi {
1008 return self.getTarget().abi;
1009 }
1010
1011 pub fn getArch(self: Target) std.Target.Cpu.Arch {
1012 return self.getCpu().arch;
1013 }
1014};
9891015
9901016pub const Pkg = struct {
9911017 name: []const u8,
lib/std/build/run.zig+1-1
......@@ -82,7 +82,7 @@ pub const RunStep = struct {
8282
8383 var key: []const u8 = undefined;
8484 var prev_path: ?[]const u8 = undefined;
85 if (builtin.os == .windows) {
85 if (builtin.os.tag == .windows) {
8686 key = "Path";
8787 prev_path = env_map.get(key);
8888 if (prev_path == null) {
lib/std/builtin.zig+2-2
......@@ -411,7 +411,7 @@ pub const Version = struct {
411411 }
412412 };
413413
414 pub fn order(lhs: Version, rhs: version) std.math.Order {
414 pub fn order(lhs: Version, rhs: Version) std.math.Order {
415415 if (lhs.major < rhs.major) return .lt;
416416 if (lhs.major > rhs.major) return .gt;
417417 if (lhs.minor < rhs.minor) return .lt;
......@@ -504,7 +504,7 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn
504504 root.os.panic(msg, error_return_trace);
505505 unreachable;
506506 }
507 switch (os) {
507 switch (os.tag) {
508508 .freestanding => {
509509 while (true) {
510510 @breakpoint();
lib/std/c.zig+12-13
......@@ -1,5 +1,5 @@
1const builtin = @import("builtin");
21const std = @import("std");
2const builtin = std.builtin;
33const page_size = std.mem.page_size;
44
55pub const tokenizer = @import("c/tokenizer.zig");
......@@ -10,7 +10,7 @@ pub const ast = @import("c/ast.zig");
1010
1111pub usingnamespace @import("os/bits.zig");
1212
13pub usingnamespace switch (builtin.os) {
13pub usingnamespace switch (std.Target.current.os.tag) {
1414 .linux => @import("c/linux.zig"),
1515 .windows => @import("c/windows.zig"),
1616 .macosx, .ios, .tvos, .watchos => @import("c/darwin.zig"),
......@@ -46,17 +46,16 @@ pub fn versionCheck(glibc_version: builtin.Version) type {
4646 return struct {
4747 pub const ok = blk: {
4848 if (!builtin.link_libc) break :blk false;
49 switch (builtin.abi) {
50 .musl, .musleabi, .musleabihf => break :blk true,
51 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => {
52 const ver = builtin.glibc_version orelse break :blk false;
53 if (ver.major < glibc_version.major) break :blk false;
54 if (ver.major > glibc_version.major) break :blk true;
55 if (ver.minor < glibc_version.minor) break :blk false;
56 if (ver.minor > glibc_version.minor) break :blk true;
57 break :blk ver.patch >= glibc_version.patch;
58 },
59 else => break :blk false,
49 if (std.Target.current.abi.isMusl()) break :blk true;
50 if (std.Target.current.isGnuLibC()) {
51 const ver = std.Target.current.os.version_range.linux.glibc;
52 const order = ver.order(glibc_version);
53 break :blk switch (order) {
54 .gt, .eq => true,
55 .lt => false,
56 };
57 } else {
58 break :blk false;
6059 }
6160 };
6261 };
lib/std/c/linux.zig+1-1
......@@ -94,7 +94,7 @@ pub const pthread_cond_t = extern struct {
9494 size: [__SIZEOF_PTHREAD_COND_T]u8 align(@alignOf(usize)) = [_]u8{0} ** __SIZEOF_PTHREAD_COND_T,
9595};
9696const __SIZEOF_PTHREAD_COND_T = 48;
97const __SIZEOF_PTHREAD_MUTEX_T = if (builtin.os == .fuchsia) 40 else switch (builtin.abi) {
97const __SIZEOF_PTHREAD_MUTEX_T = if (builtin.os.tag == .fuchsia) 40 else switch (builtin.abi) {
9898 .musl, .musleabi, .musleabihf => if (@sizeOf(usize) == 8) 40 else 24,
9999 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (builtin.arch) {
100100 .aarch64 => 48,
lib/std/child_process.zig+13-13
......@@ -17,9 +17,9 @@ const TailQueue = std.TailQueue;
1717const maxInt = std.math.maxInt;
1818
1919pub const ChildProcess = struct {
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,
20 pid: if (builtin.os.tag == .windows) void else i32,
21 handle: if (builtin.os.tag == .windows) windows.HANDLE else void,
22 thread_handle: if (builtin.os.tag == .windows) windows.HANDLE else void,
2323
2424 allocator: *mem.Allocator,
2525
......@@ -39,15 +39,15 @@ pub const ChildProcess = struct {
3939 stderr_behavior: StdIo,
4040
4141 /// Set to change the user id when spawning the child process.
42 uid: if (builtin.os == .windows) void else ?u32,
42 uid: if (builtin.os.tag == .windows) void else ?u32,
4343
4444 /// Set to change the group id when spawning the child process.
45 gid: if (builtin.os == .windows) void else ?u32,
45 gid: if (builtin.os.tag == .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 (builtin.os == .windows) void else [2]os.fd_t,
50 err_pipe: if (builtin.os.tag == .windows) void else [2]os.fd_t,
5151
5252 expand_arg0: Arg0Expand,
5353
......@@ -96,8 +96,8 @@ pub const ChildProcess = struct {
9696 .term = null,
9797 .env_map = null,
9898 .cwd = null,
99 .uid = if (builtin.os == .windows) {} else null,
100 .gid = if (builtin.os == .windows) {} else null,
99 .uid = if (builtin.os.tag == .windows) {} else null,
100 .gid = if (builtin.os.tag == .windows) {} else null,
101101 .stdin = null,
102102 .stdout = null,
103103 .stderr = null,
......@@ -118,7 +118,7 @@ pub const ChildProcess = struct {
118118
119119 /// On success must call `kill` or `wait`.
120120 pub fn spawn(self: *ChildProcess) SpawnError!void {
121 if (builtin.os == .windows) {
121 if (builtin.os.tag == .windows) {
122122 return self.spawnWindows();
123123 } else {
124124 return self.spawnPosix();
......@@ -132,7 +132,7 @@ pub const ChildProcess = struct {
132132
133133 /// Forcibly terminates child process and then cleans up all resources.
134134 pub fn kill(self: *ChildProcess) !Term {
135 if (builtin.os == .windows) {
135 if (builtin.os.tag == .windows) {
136136 return self.killWindows(1);
137137 } else {
138138 return self.killPosix();
......@@ -162,7 +162,7 @@ pub const ChildProcess = struct {
162162
163163 /// Blocks until child process terminates and then cleans up all resources.
164164 pub fn wait(self: *ChildProcess) !Term {
165 if (builtin.os == .windows) {
165 if (builtin.os.tag == .windows) {
166166 return self.waitWindows();
167167 } else {
168168 return self.waitPosix();
......@@ -307,7 +307,7 @@ pub const ChildProcess = struct {
307307 fn cleanupAfterWait(self: *ChildProcess, status: u32) !Term {
308308 defer destroyPipe(self.err_pipe);
309309
310 if (builtin.os == .linux) {
310 if (builtin.os.tag == .linux) {
311311 var fd = [1]std.os.pollfd{std.os.pollfd{
312312 .fd = self.err_pipe[0],
313313 .events = std.os.POLLIN,
......@@ -402,7 +402,7 @@ pub const ChildProcess = struct {
402402 // This pipe is used to communicate errors between the time of fork
403403 // and execve from the child process to the parent process.
404404 const err_pipe = blk: {
405 if (builtin.os == .linux) {
405 if (builtin.os.tag == .linux) {
406406 const fd = try os.eventfd(0, 0);
407407 // There's no distinction between the readable and the writeable
408408 // end with eventfd
lib/std/cstr.zig+2-2
......@@ -4,8 +4,8 @@ const debug = std.debug;
44const mem = std.mem;
55const testing = std.testing;
66
7pub const line_sep = switch (builtin.os) {
8 builtin.Os.windows => "\r\n",
7pub const line_sep = switch (builtin.os.tag) {
8 .windows => "\r\n",
99 else => "\n",
1010};
1111
lib/std/debug.zig+12-12
......@@ -1,4 +1,5 @@
11const std = @import("std.zig");
2const builtin = std.builtin;
23const math = std.math;
34const mem = std.mem;
45const io = std.io;
......@@ -11,7 +12,6 @@ const macho = std.macho;
1112const coff = std.coff;
1213const pdb = std.pdb;
1314const ArrayList = std.ArrayList;
14const builtin = @import("builtin");
1515const root = @import("root");
1616const maxInt = std.math.maxInt;
1717const File = std.fs.File;
......@@ -101,7 +101,7 @@ pub fn detectTTYConfig() TTY.Config {
101101 } else |_| {
102102 if (stderr_file.supportsAnsiEscapeCodes()) {
103103 return .escape_codes;
104 } else if (builtin.os == .windows and stderr_file.isTty()) {
104 } else if (builtin.os.tag == .windows and stderr_file.isTty()) {
105105 return .windows_api;
106106 } else {
107107 return .no_color;
......@@ -155,7 +155,7 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
155155/// chopping off the irrelevant frames and shifting so that the returned addresses pointer
156156/// equals the passed in addresses pointer.
157157pub fn captureStackTrace(first_address: ?usize, stack_trace: *builtin.StackTrace) void {
158 if (builtin.os == .windows) {
158 if (builtin.os.tag == .windows) {
159159 const addrs = stack_trace.instruction_addresses;
160160 const u32_addrs_len = @intCast(u32, addrs.len);
161161 const first_addr = first_address orelse {
......@@ -231,7 +231,7 @@ pub fn assert(ok: bool) void {
231231pub fn panic(comptime format: []const u8, args: var) noreturn {
232232 @setCold(true);
233233 // TODO: remove conditional once wasi / LLVM defines __builtin_return_address
234 const first_trace_addr = if (builtin.os == .wasi) null else @returnAddress();
234 const first_trace_addr = if (builtin.os.tag == .wasi) null else @returnAddress();
235235 panicExtra(null, first_trace_addr, format, args);
236236}
237237
......@@ -361,7 +361,7 @@ pub fn writeCurrentStackTrace(
361361 tty_config: TTY.Config,
362362 start_addr: ?usize,
363363) !void {
364 if (builtin.os == .windows) {
364 if (builtin.os.tag == .windows) {
365365 return writeCurrentStackTraceWindows(out_stream, debug_info, tty_config, start_addr);
366366 }
367367 var it = StackIterator.init(start_addr, null);
......@@ -418,7 +418,7 @@ pub const TTY = struct {
418418 .Dim => noasync out_stream.write(DIM) catch return,
419419 .Reset => noasync out_stream.write(RESET) catch return,
420420 },
421 .windows_api => if (builtin.os == .windows) {
421 .windows_api => if (builtin.os.tag == .windows) {
422422 const S = struct {
423423 var attrs: windows.WORD = undefined;
424424 var init_attrs = false;
......@@ -617,7 +617,7 @@ pub fn openSelfDebugInfo(allocator: *mem.Allocator) anyerror!DebugInfo {
617617 if (@hasDecl(root, "os") and @hasDecl(root.os, "debug") and @hasDecl(root.os.debug, "openSelfDebugInfo")) {
618618 return noasync root.os.debug.openSelfDebugInfo(allocator);
619619 }
620 switch (builtin.os) {
620 switch (builtin.os.tag) {
621621 .linux,
622622 .freebsd,
623623 .macosx,
......@@ -1019,7 +1019,7 @@ pub const DebugInfo = struct {
10191019 pub fn getModuleForAddress(self: *DebugInfo, address: usize) !*ModuleDebugInfo {
10201020 if (comptime std.Target.current.isDarwin())
10211021 return self.lookupModuleDyld(address)
1022 else if (builtin.os == .windows)
1022 else if (builtin.os.tag == .windows)
10231023 return self.lookupModuleWin32(address)
10241024 else
10251025 return self.lookupModuleDl(address);
......@@ -1242,7 +1242,7 @@ const SymbolInfo = struct {
12421242 }
12431243};
12441244
1245pub const ModuleDebugInfo = switch (builtin.os) {
1245pub const ModuleDebugInfo = switch (builtin.os.tag) {
12461246 .macosx, .ios, .watchos, .tvos => struct {
12471247 base_address: usize,
12481248 mapped_memory: []const u8,
......@@ -1602,7 +1602,7 @@ fn getDebugInfoAllocator() *mem.Allocator {
16021602}
16031603
16041604/// Whether or not the current target can print useful debug information when a segfault occurs.
1605pub const have_segfault_handling_support = builtin.os == .linux or builtin.os == .windows;
1605pub const have_segfault_handling_support = builtin.os.tag == .linux or builtin.os.tag == .windows;
16061606pub const enable_segfault_handler: bool = if (@hasDecl(root, "enable_segfault_handler"))
16071607 root.enable_segfault_handler
16081608else
......@@ -1621,7 +1621,7 @@ pub fn attachSegfaultHandler() void {
16211621 if (!have_segfault_handling_support) {
16221622 @compileError("segfault handler not supported for this target");
16231623 }
1624 if (builtin.os == .windows) {
1624 if (builtin.os.tag == .windows) {
16251625 windows_segfault_handle = windows.kernel32.AddVectoredExceptionHandler(0, handleSegfaultWindows);
16261626 return;
16271627 }
......@@ -1637,7 +1637,7 @@ pub fn attachSegfaultHandler() void {
16371637}
16381638
16391639fn resetSegfaultHandler() void {
1640 if (builtin.os == .windows) {
1640 if (builtin.os.tag == .windows) {
16411641 if (windows_segfault_handle) |handle| {
16421642 assert(windows.kernel32.RemoveVectoredExceptionHandler(handle) != 0);
16431643 windows_segfault_handle = null;
lib/std/dynamic_library.zig+2-2
......@@ -11,7 +11,7 @@ const system = std.os.system;
1111const maxInt = std.math.maxInt;
1212const max = std.math.max;
1313
14pub const DynLib = switch (builtin.os) {
14pub const DynLib = switch (builtin.os.tag) {
1515 .linux => if (builtin.link_libc) DlDynlib else ElfDynLib,
1616 .windows => WindowsDynLib,
1717 .macosx, .tvos, .watchos, .ios, .freebsd => DlDynlib,
......@@ -390,7 +390,7 @@ pub const DlDynlib = struct {
390390};
391391
392392test "dynamic_library" {
393 const libname = switch (builtin.os) {
393 const libname = switch (builtin.os.tag) {
394394 .linux, .freebsd => "invalid_so.so",
395395 .windows => "invalid_dll.dll",
396396 .macosx, .tvos, .watchos, .ios => "invalid_dylib.dylib",
lib/std/elf.zig+15-10
......@@ -349,16 +349,6 @@ pub const Elf = struct {
349349 program_headers: []ProgramHeader,
350350 allocator: *mem.Allocator,
351351
352 /// Call close when done.
353 pub fn openPath(allocator: *mem.Allocator, path: []const u8) !Elf {
354 @compileError("TODO implement");
355 }
356
357 /// Call close when done.
358 pub fn openFile(allocator: *mem.Allocator, file: File) !Elf {
359 @compileError("TODO implement");
360 }
361
362352 pub fn openStream(
363353 allocator: *mem.Allocator,
364354 seekable_stream: *io.SeekableStream(anyerror, anyerror),
......@@ -554,6 +544,21 @@ pub const Elf = struct {
554544};
555545
556546pub const EI_NIDENT = 16;
547
548pub const EI_CLASS = 4;
549pub const ELFCLASSNONE = 0;
550pub const ELFCLASS32 = 1;
551pub const ELFCLASS64 = 2;
552pub const ELFCLASSNUM = 3;
553
554pub const EI_DATA = 5;
555pub const ELFDATANONE = 0;
556pub const ELFDATA2LSB = 1;
557pub const ELFDATA2MSB = 2;
558pub const ELFDATANUM = 3;
559
560pub const EI_VERSION = 6;
561
557562pub const Elf32_Half = u16;
558563pub const Elf64_Half = u16;
559564pub const Elf32_Word = u32;
lib/std/event/channel.zig+1-1
......@@ -273,7 +273,7 @@ test "std.event.Channel" {
273273 if (builtin.single_threaded) return error.SkipZigTest;
274274
275275 // https://github.com/ziglang/zig/issues/3251
276 if (builtin.os == .freebsd) return error.SkipZigTest;
276 if (builtin.os.tag == .freebsd) return error.SkipZigTest;
277277
278278 var channel: Channel(i32) = undefined;
279279 channel.init(&[0]i32{});
lib/std/event/future.zig+1-1
......@@ -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 (builtin.os == .freebsd) return error.SkipZigTest;
89 if (builtin.os.tag == .freebsd) return error.SkipZigTest;
9090 // TODO provide a way to run tests in evented I/O mode
9191 if (!std.io.is_async) return error.SkipZigTest;
9292
lib/std/event/lock.zig+1-1
......@@ -123,7 +123,7 @@ test "std.event.Lock" {
123123 if (builtin.single_threaded) return error.SkipZigTest;
124124
125125 // TODO https://github.com/ziglang/zig/issues/3251
126 if (builtin.os == .freebsd) return error.SkipZigTest;
126 if (builtin.os.tag == .freebsd) return error.SkipZigTest;
127127
128128 var lock = Lock.init();
129129 defer lock.deinit();
lib/std/event/loop.zig+13-13
......@@ -34,7 +34,7 @@ pub const Loop = struct {
3434 handle: anyframe,
3535 overlapped: Overlapped,
3636
37 pub const overlapped_init = switch (builtin.os) {
37 pub const overlapped_init = switch (builtin.os.tag) {
3838 .windows => windows.OVERLAPPED{
3939 .Internal = 0,
4040 .InternalHigh = 0,
......@@ -52,7 +52,7 @@ pub const Loop = struct {
5252 EventFd,
5353 };
5454
55 pub const EventFd = switch (builtin.os) {
55 pub const EventFd = switch (builtin.os.tag) {
5656 .macosx, .freebsd, .netbsd, .dragonfly => KEventFd,
5757 .linux => struct {
5858 base: ResumeNode,
......@@ -71,7 +71,7 @@ pub const Loop = struct {
7171 kevent: os.Kevent,
7272 };
7373
74 pub const Basic = switch (builtin.os) {
74 pub const Basic = switch (builtin.os.tag) {
7575 .macosx, .freebsd, .netbsd, .dragonfly => KEventBasic,
7676 .linux => struct {
7777 base: ResumeNode,
......@@ -173,7 +173,7 @@ pub const Loop = struct {
173173 const wakeup_bytes = [_]u8{0x1} ** 8;
174174
175175 fn initOsData(self: *Loop, extra_thread_count: usize) InitOsDataError!void {
176 switch (builtin.os) {
176 switch (builtin.os.tag) {
177177 .linux => {
178178 self.os_data.fs_queue = std.atomic.Queue(Request).init();
179179 self.os_data.fs_queue_item = 0;
......@@ -404,7 +404,7 @@ pub const Loop = struct {
404404 }
405405
406406 fn deinitOsData(self: *Loop) void {
407 switch (builtin.os) {
407 switch (builtin.os.tag) {
408408 .linux => {
409409 noasync os.close(self.os_data.final_eventfd);
410410 while (self.available_eventfd_resume_nodes.pop()) |node| noasync os.close(node.data.eventfd);
......@@ -568,7 +568,7 @@ pub const Loop = struct {
568568 };
569569 const eventfd_node = &resume_stack_node.data;
570570 eventfd_node.base.handle = next_tick_node.data;
571 switch (builtin.os) {
571 switch (builtin.os.tag) {
572572 .macosx, .freebsd, .netbsd, .dragonfly => {
573573 const kevent_array = @as(*const [1]os.Kevent, &eventfd_node.kevent);
574574 const empty_kevs = &[0]os.Kevent{};
......@@ -628,7 +628,7 @@ pub const Loop = struct {
628628
629629 self.workerRun();
630630
631 switch (builtin.os) {
631 switch (builtin.os.tag) {
632632 .linux,
633633 .macosx,
634634 .freebsd,
......@@ -678,7 +678,7 @@ pub const Loop = struct {
678678 const prev = @atomicRmw(usize, &self.pending_event_count, AtomicRmwOp.Sub, 1, AtomicOrder.SeqCst);
679679 if (prev == 1) {
680680 // cause all the threads to stop
681 switch (builtin.os) {
681 switch (builtin.os.tag) {
682682 .linux => {
683683 self.posixFsRequest(&self.os_data.fs_end_request);
684684 // writing 8 bytes to an eventfd cannot fail
......@@ -902,7 +902,7 @@ pub const Loop = struct {
902902 self.finishOneEvent();
903903 }
904904
905 switch (builtin.os) {
905 switch (builtin.os.tag) {
906906 .linux => {
907907 // only process 1 event so we don't steal from other threads
908908 var events: [1]os.linux.epoll_event = undefined;
......@@ -989,7 +989,7 @@ pub const Loop = struct {
989989 fn posixFsRequest(self: *Loop, request_node: *Request.Node) void {
990990 self.beginOneEvent(); // finished in posixFsRun after processing the msg
991991 self.os_data.fs_queue.put(request_node);
992 switch (builtin.os) {
992 switch (builtin.os.tag) {
993993 .macosx, .freebsd, .netbsd, .dragonfly => {
994994 const fs_kevs = @as(*const [1]os.Kevent, &self.os_data.fs_kevent_wake);
995995 const empty_kevs = &[0]os.Kevent{};
......@@ -1018,7 +1018,7 @@ pub const Loop = struct {
10181018 // https://github.com/ziglang/zig/issues/3157
10191019 fn posixFsRun(self: *Loop) void {
10201020 while (true) {
1021 if (builtin.os == .linux) {
1021 if (builtin.os.tag == .linux) {
10221022 @atomicStore(i32, &self.os_data.fs_queue_item, 0, .SeqCst);
10231023 }
10241024 while (self.os_data.fs_queue.get()) |node| {
......@@ -1053,7 +1053,7 @@ pub const Loop = struct {
10531053 }
10541054 self.finishOneEvent();
10551055 }
1056 switch (builtin.os) {
1056 switch (builtin.os.tag) {
10571057 .linux => {
10581058 const rc = os.linux.futex_wait(&self.os_data.fs_queue_item, os.linux.FUTEX_WAIT, 0, null);
10591059 switch (os.linux.getErrno(rc)) {
......@@ -1071,7 +1071,7 @@ pub const Loop = struct {
10711071 }
10721072 }
10731073
1074 const OsData = switch (builtin.os) {
1074 const OsData = switch (builtin.os.tag) {
10751075 .linux => LinuxOsData,
10761076 .macosx, .freebsd, .netbsd, .dragonfly => KEventData,
10771077 .windows => struct {
lib/std/fs.zig+23-23
......@@ -29,7 +29,7 @@ pub const Watch = @import("fs/watch.zig").Watch;
2929/// All file system operations which return a path are guaranteed to
3030/// fit into a UTF-8 encoded array of this length.
3131/// The byte count includes room for a null sentinel byte.
32pub const MAX_PATH_BYTES = switch (builtin.os) {
32pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
3333 .linux, .macosx, .ios, .freebsd, .netbsd, .dragonfly => os.PATH_MAX,
3434 // Each UTF-16LE character may be expanded to 3 UTF-8 bytes.
3535 // If it would require 4 UTF-8 bytes, then there would be a surrogate
......@@ -47,7 +47,7 @@ pub const base64_encoder = base64.Base64Encoder.init(
4747
4848/// Whether or not async file system syscalls need a dedicated thread because the operating
4949/// system does not support non-blocking I/O on the file system.
50pub const need_async_thread = std.io.is_async and switch (builtin.os) {
50pub const need_async_thread = std.io.is_async and switch (builtin.os.tag) {
5151 .windows, .other => false,
5252 else => true,
5353};
......@@ -270,7 +270,7 @@ pub const AtomicFile = struct {
270270 assert(!self.finished);
271271 self.file.close();
272272 self.finished = true;
273 if (builtin.os == .windows) {
273 if (builtin.os.tag == .windows) {
274274 const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_path);
275275 const tmp_path_w = try os.windows.cStrToPrefixedFileW(@ptrCast([*:0]u8, &self.tmp_path_buf));
276276 return os.renameW(&tmp_path_w, &dest_path_w);
......@@ -394,7 +394,7 @@ pub const Dir = struct {
394394
395395 const IteratorError = error{AccessDenied} || os.UnexpectedError;
396396
397 pub const Iterator = switch (builtin.os) {
397 pub const Iterator = switch (builtin.os.tag) {
398398 .macosx, .ios, .freebsd, .netbsd, .dragonfly => struct {
399399 dir: Dir,
400400 seek: i64,
......@@ -409,7 +409,7 @@ pub const Dir = struct {
409409 /// Memory such as file names referenced in this returned entry becomes invalid
410410 /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
411411 pub fn next(self: *Self) Error!?Entry {
412 switch (builtin.os) {
412 switch (builtin.os.tag) {
413413 .macosx, .ios => return self.nextDarwin(),
414414 .freebsd, .netbsd, .dragonfly => return self.nextBsd(),
415415 else => @compileError("unimplemented"),
......@@ -644,7 +644,7 @@ pub const Dir = struct {
644644 };
645645
646646 pub fn iterate(self: Dir) Iterator {
647 switch (builtin.os) {
647 switch (builtin.os.tag) {
648648 .macosx, .ios, .freebsd, .netbsd, .dragonfly => return Iterator{
649649 .dir = self,
650650 .seek = 0,
......@@ -710,7 +710,7 @@ pub const Dir = struct {
710710 /// Asserts that the path parameter has no null bytes.
711711 pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
712712 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
713 if (builtin.os == .windows) {
713 if (builtin.os.tag == .windows) {
714714 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
715715 return self.openFileW(&path_w, flags);
716716 }
......@@ -720,7 +720,7 @@ pub const Dir = struct {
720720
721721 /// Same as `openFile` but the path parameter is null-terminated.
722722 pub fn openFileC(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) File.OpenError!File {
723 if (builtin.os == .windows) {
723 if (builtin.os.tag == .windows) {
724724 const path_w = try os.windows.cStrToPrefixedFileW(sub_path);
725725 return self.openFileW(&path_w, flags);
726726 }
......@@ -760,7 +760,7 @@ pub const Dir = struct {
760760 /// Asserts that the path parameter has no null bytes.
761761 pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
762762 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
763 if (builtin.os == .windows) {
763 if (builtin.os.tag == .windows) {
764764 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
765765 return self.createFileW(&path_w, flags);
766766 }
......@@ -770,7 +770,7 @@ pub const Dir = struct {
770770
771771 /// Same as `createFile` but the path parameter is null-terminated.
772772 pub fn createFileC(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags) File.OpenError!File {
773 if (builtin.os == .windows) {
773 if (builtin.os.tag == .windows) {
774774 const path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
775775 return self.createFileW(&path_w, flags);
776776 }
......@@ -901,7 +901,7 @@ pub const Dir = struct {
901901 /// Asserts that the path parameter has no null bytes.
902902 pub fn openDirTraverse(self: Dir, sub_path: []const u8) OpenError!Dir {
903903 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
904 if (builtin.os == .windows) {
904 if (builtin.os.tag == .windows) {
905905 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
906906 return self.openDirTraverseW(&sub_path_w);
907907 }
......@@ -919,7 +919,7 @@ pub const Dir = struct {
919919 /// Asserts that the path parameter has no null bytes.
920920 pub fn openDirList(self: Dir, sub_path: []const u8) OpenError!Dir {
921921 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
922 if (builtin.os == .windows) {
922 if (builtin.os.tag == .windows) {
923923 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
924924 return self.openDirListW(&sub_path_w);
925925 }
......@@ -930,7 +930,7 @@ pub const Dir = struct {
930930
931931 /// Same as `openDirTraverse` except the parameter is null-terminated.
932932 pub fn openDirTraverseC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
933 if (builtin.os == .windows) {
933 if (builtin.os.tag == .windows) {
934934 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
935935 return self.openDirTraverseW(&sub_path_w);
936936 } else {
......@@ -941,7 +941,7 @@ pub const Dir = struct {
941941
942942 /// Same as `openDirList` except the parameter is null-terminated.
943943 pub fn openDirListC(self: Dir, sub_path_c: [*:0]const u8) OpenError!Dir {
944 if (builtin.os == .windows) {
944 if (builtin.os.tag == .windows) {
945945 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path_c);
946946 return self.openDirListW(&sub_path_w);
947947 } else {
......@@ -1083,7 +1083,7 @@ pub const Dir = struct {
10831083 /// Asserts that the path parameter has no null bytes.
10841084 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
10851085 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
1086 if (builtin.os == .windows) {
1086 if (builtin.os.tag == .windows) {
10871087 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
10881088 return self.deleteDirW(&sub_path_w);
10891089 }
......@@ -1340,7 +1340,7 @@ pub const Dir = struct {
13401340 /// For example, instead of testing if a file exists and then opening it, just
13411341 /// open it and handle the error for file not found.
13421342 pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {
1343 if (builtin.os == .windows) {
1343 if (builtin.os.tag == .windows) {
13441344 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
13451345 return self.accessW(&sub_path_w, flags);
13461346 }
......@@ -1350,7 +1350,7 @@ pub const Dir = struct {
13501350
13511351 /// Same as `access` except the path parameter is null-terminated.
13521352 pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {
1353 if (builtin.os == .windows) {
1353 if (builtin.os.tag == .windows) {
13541354 const sub_path_w = try os.windows.cStrToPrefixedFileW(sub_path);
13551355 return self.accessW(&sub_path_w, flags);
13561356 }
......@@ -1381,7 +1381,7 @@ pub const Dir = struct {
13811381/// Closing the returned `Dir` is checked illegal behavior. Iterating over the result is illegal behavior.
13821382/// On POSIX targets, this function is comptime-callable.
13831383pub fn cwd() Dir {
1384 if (builtin.os == .windows) {
1384 if (builtin.os.tag == .windows) {
13851385 return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
13861386 } else {
13871387 return Dir{ .fd = os.AT_FDCWD };
......@@ -1560,10 +1560,10 @@ pub fn readLinkC(pathname_c: [*]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
15601560pub const OpenSelfExeError = os.OpenError || os.windows.CreateFileError || SelfExePathError;
15611561
15621562pub fn openSelfExe() OpenSelfExeError!File {
1563 if (builtin.os == .linux) {
1563 if (builtin.os.tag == .linux) {
15641564 return openFileAbsoluteC("/proc/self/exe", .{});
15651565 }
1566 if (builtin.os == .windows) {
1566 if (builtin.os.tag == .windows) {
15671567 const wide_slice = selfExePathW();
15681568 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
15691569 return cwd().openReadW(&prefixed_path_w);
......@@ -1575,7 +1575,7 @@ pub fn openSelfExe() OpenSelfExeError!File {
15751575}
15761576
15771577test "openSelfExe" {
1578 switch (builtin.os) {
1578 switch (builtin.os.tag) {
15791579 .linux, .macosx, .ios, .windows, .freebsd, .dragonfly => (try openSelfExe()).close(),
15801580 else => return error.SkipZigTest, // Unsupported OS.
15811581 }
......@@ -1600,7 +1600,7 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
16001600 if (rc != 0) return error.NameTooLong;
16011601 return mem.toSlice(u8, @ptrCast([*:0]u8, out_buffer));
16021602 }
1603 switch (builtin.os) {
1603 switch (builtin.os.tag) {
16041604 .linux => return os.readlinkC("/proc/self/exe", out_buffer),
16051605 .freebsd, .dragonfly => {
16061606 var mib = [4]c_int{ os.CTL_KERN, os.KERN_PROC, os.KERN_PROC_PATHNAME, -1 };
......@@ -1642,7 +1642,7 @@ pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
16421642/// Get the directory path that contains the current executable.
16431643/// Returned value is a slice of out_buffer.
16441644pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {
1645 if (builtin.os == .linux) {
1645 if (builtin.os.tag == .linux) {
16461646 // If the currently executing binary has been deleted,
16471647 // the file path looks something like `/a/b/c/exe (deleted)`
16481648 // This path cannot be opened, but it's valid for determining the directory
lib/std/fs/file.zig+6-6
......@@ -29,7 +29,7 @@ pub const File = struct {
2929
3030 pub const Mode = os.mode_t;
3131
32 pub const default_mode = switch (builtin.os) {
32 pub const default_mode = switch (builtin.os.tag) {
3333 .windows => 0,
3434 else => 0o666,
3535 };
......@@ -83,7 +83,7 @@ pub const File = struct {
8383
8484 /// Test whether ANSI escape codes will be treated as such.
8585 pub fn supportsAnsiEscapeCodes(self: File) bool {
86 if (builtin.os == .windows) {
86 if (builtin.os.tag == .windows) {
8787 return os.isCygwinPty(self.handle);
8888 }
8989 if (self.isTty()) {
......@@ -128,7 +128,7 @@ pub const File = struct {
128128
129129 /// TODO: integrate with async I/O
130130 pub fn getEndPos(self: File) GetPosError!u64 {
131 if (builtin.os == .windows) {
131 if (builtin.os.tag == .windows) {
132132 return windows.GetFileSizeEx(self.handle);
133133 }
134134 return (try self.stat()).size;
......@@ -138,7 +138,7 @@ pub const File = struct {
138138
139139 /// TODO: integrate with async I/O
140140 pub fn mode(self: File) ModeError!Mode {
141 if (builtin.os == .windows) {
141 if (builtin.os.tag == .windows) {
142142 return {};
143143 }
144144 return (try self.stat()).mode;
......@@ -162,7 +162,7 @@ pub const File = struct {
162162
163163 /// TODO: integrate with async I/O
164164 pub fn stat(self: File) StatError!Stat {
165 if (builtin.os == .windows) {
165 if (builtin.os.tag == .windows) {
166166 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
167167 var info: windows.FILE_ALL_INFORMATION = undefined;
168168 const rc = windows.ntdll.NtQueryInformationFile(self.handle, &io_status_block, &info, @sizeOf(windows.FILE_ALL_INFORMATION), .FileAllInformation);
......@@ -209,7 +209,7 @@ pub const File = struct {
209209 /// last modification timestamp in nanoseconds
210210 mtime: i64,
211211 ) UpdateTimesError!void {
212 if (builtin.os == .windows) {
212 if (builtin.os.tag == .windows) {
213213 const atime_ft = windows.nanoSecondsToFileTime(atime);
214214 const mtime_ft = windows.nanoSecondsToFileTime(mtime);
215215 return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);
lib/std/fs/get_app_data_dir.zig+1-1
......@@ -13,7 +13,7 @@ pub const GetAppDataDirError = error{
1313/// Caller owns returned memory.
1414/// TODO determine if we can remove the allocator requirement
1515pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataDirError![]u8 {
16 switch (builtin.os) {
16 switch (builtin.os.tag) {
1717 .windows => {
1818 var dir_path_ptr: [*:0]u16 = undefined;
1919 switch (os.windows.shell32.SHGetKnownFolderPath(
lib/std/fs/path.zig+19-19
......@@ -13,18 +13,18 @@ const process = std.process;
1313
1414pub const sep_windows = '\\';
1515pub const sep_posix = '/';
16pub const sep = if (builtin.os == .windows) sep_windows else sep_posix;
16pub const sep = if (builtin.os.tag == .windows) sep_windows else sep_posix;
1717
1818pub const sep_str_windows = "\\";
1919pub const sep_str_posix = "/";
20pub const sep_str = if (builtin.os == .windows) sep_str_windows else sep_str_posix;
20pub const sep_str = if (builtin.os.tag == .windows) sep_str_windows else sep_str_posix;
2121
2222pub const delimiter_windows = ';';
2323pub const delimiter_posix = ':';
24pub const delimiter = if (builtin.os == .windows) delimiter_windows else delimiter_posix;
24pub const delimiter = if (builtin.os.tag == .windows) delimiter_windows else delimiter_posix;
2525
2626pub fn isSep(byte: u8) bool {
27 if (builtin.os == .windows) {
27 if (builtin.os.tag == .windows) {
2828 return byte == '/' or byte == '\\';
2929 } else {
3030 return byte == '/';
......@@ -74,7 +74,7 @@ fn joinSep(allocator: *Allocator, separator: u8, paths: []const []const u8) ![]u
7474 return buf;
7575}
7676
77pub const join = if (builtin.os == .windows) joinWindows else joinPosix;
77pub const join = if (builtin.os.tag == .windows) joinWindows else joinPosix;
7878
7979/// Naively combines a series of paths with the native path seperator.
8080/// Allocates memory for the result, which must be freed by the caller.
......@@ -129,7 +129,7 @@ test "join" {
129129}
130130
131131pub fn isAbsoluteC(path_c: [*:0]const u8) bool {
132 if (builtin.os == .windows) {
132 if (builtin.os.tag == .windows) {
133133 return isAbsoluteWindowsC(path_c);
134134 } else {
135135 return isAbsolutePosixC(path_c);
......@@ -137,7 +137,7 @@ pub fn isAbsoluteC(path_c: [*:0]const u8) bool {
137137}
138138
139139pub fn isAbsolute(path: []const u8) bool {
140 if (builtin.os == .windows) {
140 if (builtin.os.tag == .windows) {
141141 return isAbsoluteWindows(path);
142142 } else {
143143 return isAbsolutePosix(path);
......@@ -318,7 +318,7 @@ test "windowsParsePath" {
318318}
319319
320320pub fn diskDesignator(path: []const u8) []const u8 {
321 if (builtin.os == .windows) {
321 if (builtin.os.tag == .windows) {
322322 return diskDesignatorWindows(path);
323323 } else {
324324 return "";
......@@ -383,7 +383,7 @@ fn asciiEqlIgnoreCase(s1: []const u8, s2: []const u8) bool {
383383
384384/// On Windows, this calls `resolveWindows` and on POSIX it calls `resolvePosix`.
385385pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
386 if (builtin.os == .windows) {
386 if (builtin.os.tag == .windows) {
387387 return resolveWindows(allocator, paths);
388388 } else {
389389 return resolvePosix(allocator, paths);
......@@ -400,7 +400,7 @@ pub fn resolve(allocator: *Allocator, paths: []const []const u8) ![]u8 {
400400/// Without performing actual syscalls, resolving `..` could be incorrect.
401401pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
402402 if (paths.len == 0) {
403 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
403 assert(builtin.os.tag == .windows); // resolveWindows called on non windows can't use getCwd
404404 return process.getCwdAlloc(allocator);
405405 }
406406
......@@ -495,7 +495,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
495495 result_disk_designator = result[0..result_index];
496496 },
497497 WindowsPath.Kind.None => {
498 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
498 assert(builtin.os.tag == .windows); // resolveWindows called on non windows can't use getCwd
499499 const cwd = try process.getCwdAlloc(allocator);
500500 defer allocator.free(cwd);
501501 const parsed_cwd = windowsParsePath(cwd);
......@@ -510,7 +510,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
510510 },
511511 }
512512 } else {
513 assert(builtin.os == .windows); // resolveWindows called on non windows can't use getCwd
513 assert(builtin.os.tag == .windows); // resolveWindows called on non windows can't use getCwd
514514 // TODO call get cwd for the result_disk_designator instead of the global one
515515 const cwd = try process.getCwdAlloc(allocator);
516516 defer allocator.free(cwd);
......@@ -581,7 +581,7 @@ pub fn resolveWindows(allocator: *Allocator, paths: []const []const u8) ![]u8 {
581581/// Without performing actual syscalls, resolving `..` could be incorrect.
582582pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
583583 if (paths.len == 0) {
584 assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
584 assert(builtin.os.tag != .windows); // resolvePosix called on windows can't use getCwd
585585 return process.getCwdAlloc(allocator);
586586 }
587587
......@@ -603,7 +603,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
603603 if (have_abs) {
604604 result = try allocator.alloc(u8, max_size);
605605 } else {
606 assert(builtin.os != .windows); // resolvePosix called on windows can't use getCwd
606 assert(builtin.os.tag != .windows); // resolvePosix called on windows can't use getCwd
607607 const cwd = try process.getCwdAlloc(allocator);
608608 defer allocator.free(cwd);
609609 result = try allocator.alloc(u8, max_size + cwd.len + 1);
......@@ -645,7 +645,7 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
645645test "resolve" {
646646 const cwd = try process.getCwdAlloc(testing.allocator);
647647 defer testing.allocator.free(cwd);
648 if (builtin.os == .windows) {
648 if (builtin.os.tag == .windows) {
649649 if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) {
650650 cwd[0] = asciiUpper(cwd[0]);
651651 }
......@@ -661,7 +661,7 @@ test "resolveWindows" {
661661 // TODO https://github.com/ziglang/zig/issues/3288
662662 return error.SkipZigTest;
663663 }
664 if (builtin.os == .windows) {
664 if (builtin.os.tag == .windows) {
665665 const cwd = try process.getCwdAlloc(testing.allocator);
666666 defer testing.allocator.free(cwd);
667667 const parsed_cwd = windowsParsePath(cwd);
......@@ -732,7 +732,7 @@ fn testResolvePosix(paths: []const []const u8, expected: []const u8) !void {
732732/// If the path is a file in the current directory (no directory component)
733733/// then returns null
734734pub fn dirname(path: []const u8) ?[]const u8 {
735 if (builtin.os == .windows) {
735 if (builtin.os.tag == .windows) {
736736 return dirnameWindows(path);
737737 } else {
738738 return dirnamePosix(path);
......@@ -864,7 +864,7 @@ fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void {
864864}
865865
866866pub fn basename(path: []const u8) []const u8 {
867 if (builtin.os == .windows) {
867 if (builtin.os.tag == .windows) {
868868 return basenameWindows(path);
869869 } else {
870870 return basenamePosix(path);
......@@ -980,7 +980,7 @@ fn testBasenameWindows(input: []const u8, expected_output: []const u8) void {
980980/// string is returned.
981981/// On Windows this canonicalizes the drive to a capital letter and paths to `\\`.
982982pub fn relative(allocator: *Allocator, from: []const u8, to: []const u8) ![]u8 {
983 if (builtin.os == .windows) {
983 if (builtin.os.tag == .windows) {
984984 return relativeWindows(allocator, from, to);
985985 } else {
986986 return relativePosix(allocator, from, to);
lib/std/fs/watch.zig+4-4
......@@ -42,7 +42,7 @@ pub fn Watch(comptime V: type) type {
4242 os_data: OsData,
4343 allocator: *Allocator,
4444
45 const OsData = switch (builtin.os) {
45 const OsData = switch (builtin.os.tag) {
4646 // TODO https://github.com/ziglang/zig/issues/3778
4747 .macosx, .freebsd, .netbsd, .dragonfly => KqOsData,
4848 .linux => LinuxOsData,
......@@ -121,7 +121,7 @@ pub fn Watch(comptime V: type) type {
121121 const self = try allocator.create(Self);
122122 errdefer allocator.destroy(self);
123123
124 switch (builtin.os) {
124 switch (builtin.os.tag) {
125125 .linux => {
126126 const inotify_fd = try os.inotify_init1(os.linux.IN_NONBLOCK | os.linux.IN_CLOEXEC);
127127 errdefer os.close(inotify_fd);
......@@ -172,7 +172,7 @@ pub fn Watch(comptime V: type) type {
172172
173173 /// All addFile calls and removeFile calls must have completed.
174174 pub fn deinit(self: *Self) void {
175 switch (builtin.os) {
175 switch (builtin.os.tag) {
176176 .macosx, .freebsd, .netbsd, .dragonfly => {
177177 // TODO we need to cancel the frames before destroying the lock
178178 self.os_data.table_lock.deinit();
......@@ -223,7 +223,7 @@ pub fn Watch(comptime V: type) type {
223223 }
224224
225225 pub fn addFile(self: *Self, file_path: []const u8, value: V) !?V {
226 switch (builtin.os) {
226 switch (builtin.os.tag) {
227227 .macosx, .freebsd, .netbsd, .dragonfly => return addFileKEvent(self, file_path, value),
228228 .linux => return addFileLinux(self, file_path, value),
229229 .windows => return addFileWindows(self, file_path, value),
lib/std/heap.zig+7-7
......@@ -36,7 +36,7 @@ fn cShrink(self: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new
3636/// Thread-safe and lock-free.
3737pub const page_allocator = if (std.Target.current.isWasm())
3838 &wasm_page_allocator_state
39else if (std.Target.current.getOs() == .freestanding)
39else if (std.Target.current.os.tag == .freestanding)
4040 root.os.heap.page_allocator
4141else
4242 &page_allocator_state;
......@@ -57,7 +57,7 @@ const PageAllocator = struct {
5757 fn alloc(allocator: *Allocator, n: usize, alignment: u29) error{OutOfMemory}![]u8 {
5858 if (n == 0) return &[0]u8{};
5959
60 if (builtin.os == .windows) {
60 if (builtin.os.tag == .windows) {
6161 const w = os.windows;
6262
6363 // Although officially it's at least aligned to page boundary,
......@@ -143,7 +143,7 @@ const PageAllocator = struct {
143143
144144 fn shrink(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
145145 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
146 if (builtin.os == .windows) {
146 if (builtin.os.tag == .windows) {
147147 const w = os.windows;
148148 if (new_size == 0) {
149149 // From the docs:
......@@ -183,7 +183,7 @@ const PageAllocator = struct {
183183
184184 fn realloc(allocator: *Allocator, old_mem_unaligned: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
185185 const old_mem = @alignCast(mem.page_size, old_mem_unaligned);
186 if (builtin.os == .windows) {
186 if (builtin.os.tag == .windows) {
187187 if (old_mem.len == 0) {
188188 return alloc(allocator, new_size, new_align);
189189 }
......@@ -412,7 +412,7 @@ const WasmPageAllocator = struct {
412412 }
413413};
414414
415pub const HeapAllocator = switch (builtin.os) {
415pub const HeapAllocator = switch (builtin.os.tag) {
416416 .windows => struct {
417417 allocator: Allocator,
418418 heap_handle: ?HeapHandle,
......@@ -855,7 +855,7 @@ test "PageAllocator" {
855855 try testAllocatorAlignedShrink(allocator);
856856 }
857857
858 if (builtin.os == .windows) {
858 if (builtin.os.tag == .windows) {
859859 // Trying really large alignment. As mentionned in the implementation,
860860 // VirtualAlloc returns 64K aligned addresses. We want to make sure
861861 // PageAllocator works beyond that, as it's not tested by
......@@ -868,7 +868,7 @@ test "PageAllocator" {
868868}
869869
870870test "HeapAllocator" {
871 if (builtin.os == .windows) {
871 if (builtin.os.tag == .windows) {
872872 var heap_allocator = HeapAllocator.init();
873873 defer heap_allocator.deinit();
874874
lib/std/io.zig+3-3
......@@ -35,7 +35,7 @@ else
3535pub const is_async = mode != .blocking;
3636
3737fn getStdOutHandle() os.fd_t {
38 if (builtin.os == .windows) {
38 if (builtin.os.tag == .windows) {
3939 return os.windows.peb().ProcessParameters.hStdOutput;
4040 }
4141
......@@ -54,7 +54,7 @@ pub fn getStdOut() File {
5454}
5555
5656fn getStdErrHandle() os.fd_t {
57 if (builtin.os == .windows) {
57 if (builtin.os.tag == .windows) {
5858 return os.windows.peb().ProcessParameters.hStdError;
5959 }
6060
......@@ -74,7 +74,7 @@ pub fn getStdErr() File {
7474}
7575
7676fn getStdInHandle() os.fd_t {
77 if (builtin.os == .windows) {
77 if (builtin.os.tag == .windows) {
7878 return os.windows.peb().ProcessParameters.hStdInput;
7979 }
8080
lib/std/mutex.zig+2-2
......@@ -73,7 +73,7 @@ pub const Mutex = if (builtin.single_threaded)
7373 return self.tryAcquire() orelse @panic("deadlock detected");
7474 }
7575 }
76else if (builtin.os == .windows)
76else if (builtin.os.tag == .windows)
7777// https://locklessinc.com/articles/keyed_events/
7878 extern union {
7979 locked: u8,
......@@ -161,7 +161,7 @@ else if (builtin.os == .windows)
161161 }
162162 };
163163 }
164else if (builtin.link_libc or builtin.os == .linux)
164else if (builtin.link_libc or builtin.os.tag == .linux)
165165// stack-based version of https://github.com/Amanieu/parking_lot/blob/master/core/src/word_lock.rs
166166 struct {
167167 state: usize,
lib/std/net.zig+1-1
......@@ -501,7 +501,7 @@ pub fn getAddressList(allocator: *mem.Allocator, name: []const u8, port: u16) !*
501501
502502 return result;
503503 }
504 if (builtin.os == .linux) {
504 if (builtin.os.tag == .linux) {
505505 const flags = std.c.AI_NUMERICSERV;
506506 const family = os.AF_UNSPEC;
507507 var lookup_addrs = std.ArrayList(LookupAddr).init(allocator);
lib/std/net/test.zig+2-2
......@@ -63,7 +63,7 @@ test "parse and render IPv4 addresses" {
6363}
6464
6565test "resolve DNS" {
66 if (std.builtin.os == .windows) {
66 if (std.builtin.os.tag == .windows) {
6767 // DNS resolution not implemented on Windows yet.
6868 return error.SkipZigTest;
6969 }
......@@ -81,7 +81,7 @@ test "resolve DNS" {
8181test "listen on a port, send bytes, receive bytes" {
8282 if (!std.io.is_async) return error.SkipZigTest;
8383
84 if (std.builtin.os != .linux) {
84 if (std.builtin.os.tag != .linux) {
8585 // TODO build abstractions for other operating systems
8686 return error.SkipZigTest;
8787 }
lib/std/os.zig+68-68
......@@ -56,7 +56,7 @@ pub const system = if (@hasDecl(root, "os") and root.os != @This())
5656 root.os.system
5757else if (builtin.link_libc)
5858 std.c
59else switch (builtin.os) {
59else switch (builtin.os.tag) {
6060 .macosx, .ios, .watchos, .tvos => darwin,
6161 .freebsd => freebsd,
6262 .linux => linux,
......@@ -93,10 +93,10 @@ pub const errno = system.getErrno;
9393/// must call `fsync` before `close`.
9494/// Note: The Zig standard library does not support POSIX thread cancellation.
9595pub fn close(fd: fd_t) void {
96 if (builtin.os == .windows) {
96 if (builtin.os.tag == .windows) {
9797 return windows.CloseHandle(fd);
9898 }
99 if (builtin.os == .wasi) {
99 if (builtin.os.tag == .wasi) {
100100 _ = wasi.fd_close(fd);
101101 }
102102 if (comptime std.Target.current.isDarwin()) {
......@@ -121,12 +121,12 @@ pub const GetRandomError = OpenError;
121121/// appropriate OS-specific library call. Otherwise it uses the zig standard
122122/// library implementation.
123123pub fn getrandom(buffer: []u8) GetRandomError!void {
124 if (builtin.os == .windows) {
124 if (builtin.os.tag == .windows) {
125125 return windows.RtlGenRandom(buffer);
126126 }
127 if (builtin.os == .linux or builtin.os == .freebsd) {
127 if (builtin.os.tag == .linux or builtin.os.tag == .freebsd) {
128128 var buf = buffer;
129 const use_c = builtin.os != .linux or
129 const use_c = builtin.os.tag != .linux or
130130 std.c.versionCheck(builtin.Version{ .major = 2, .minor = 25, .patch = 0 }).ok;
131131
132132 while (buf.len != 0) {
......@@ -153,7 +153,7 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
153153 }
154154 return;
155155 }
156 if (builtin.os == .wasi) {
156 if (builtin.os.tag == .wasi) {
157157 switch (wasi.random_get(buffer.ptr, buffer.len)) {
158158 0 => return,
159159 else => |err| return unexpectedErrno(err),
......@@ -188,13 +188,13 @@ pub fn abort() noreturn {
188188 // MSVCRT abort() sometimes opens a popup window which is undesirable, so
189189 // even when linking libc on Windows we use our own abort implementation.
190190 // See https://github.com/ziglang/zig/issues/2071 for more details.
191 if (builtin.os == .windows) {
191 if (builtin.os.tag == .windows) {
192192 if (builtin.mode == .Debug) {
193193 @breakpoint();
194194 }
195195 windows.kernel32.ExitProcess(3);
196196 }
197 if (!builtin.link_libc and builtin.os == .linux) {
197 if (!builtin.link_libc and builtin.os.tag == .linux) {
198198 raise(SIGABRT) catch {};
199199
200200 // TODO the rest of the implementation of abort() from musl libc here
......@@ -202,10 +202,10 @@ pub fn abort() noreturn {
202202 raise(SIGKILL) catch {};
203203 exit(127);
204204 }
205 if (builtin.os == .uefi) {
205 if (builtin.os.tag == .uefi) {
206206 exit(0); // TODO choose appropriate exit code
207207 }
208 if (builtin.os == .wasi) {
208 if (builtin.os.tag == .wasi) {
209209 @breakpoint();
210210 exit(1);
211211 }
......@@ -223,7 +223,7 @@ pub fn raise(sig: u8) RaiseError!void {
223223 }
224224 }
225225
226 if (builtin.os == .linux) {
226 if (builtin.os.tag == .linux) {
227227 var set: linux.sigset_t = undefined;
228228 // block application signals
229229 _ = linux.sigprocmask(SIG_BLOCK, &linux.app_mask, &set);
......@@ -260,16 +260,16 @@ pub fn exit(status: u8) noreturn {
260260 if (builtin.link_libc) {
261261 system.exit(status);
262262 }
263 if (builtin.os == .windows) {
263 if (builtin.os.tag == .windows) {
264264 windows.kernel32.ExitProcess(status);
265265 }
266 if (builtin.os == .wasi) {
266 if (builtin.os.tag == .wasi) {
267267 wasi.proc_exit(status);
268268 }
269 if (builtin.os == .linux and !builtin.single_threaded) {
269 if (builtin.os.tag == .linux and !builtin.single_threaded) {
270270 linux.exit_group(status);
271271 }
272 if (builtin.os == .uefi) {
272 if (builtin.os.tag == .uefi) {
273273 // exit() is only avaliable if exitBootServices() has not been called yet.
274274 // This call to exit should not fail, so we don't care about its return value.
275275 if (uefi.system_table.boot_services) |bs| {
......@@ -299,11 +299,11 @@ pub const ReadError = error{
299299/// If the application has a global event loop enabled, EAGAIN is handled
300300/// via the event loop. Otherwise EAGAIN results in error.WouldBlock.
301301pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
302 if (builtin.os == .windows) {
302 if (builtin.os.tag == .windows) {
303303 return windows.ReadFile(fd, buf, null);
304304 }
305305
306 if (builtin.os == .wasi and !builtin.link_libc) {
306 if (builtin.os.tag == .wasi and !builtin.link_libc) {
307307 const iovs = [1]iovec{iovec{
308308 .iov_base = buf.ptr,
309309 .iov_len = buf.len,
......@@ -352,7 +352,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
352352/// * Windows
353353/// On these systems, the read races with concurrent writes to the same file descriptor.
354354pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
355 if (builtin.os == .windows) {
355 if (builtin.os.tag == .windows) {
356356 // TODO batch these into parallel requests
357357 var off: usize = 0;
358358 var iov_i: usize = 0;
......@@ -406,7 +406,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
406406/// On Windows, if the application has a global event loop enabled, I/O Completion Ports are
407407/// used to perform the I/O. `error.WouldBlock` is not possible on Windows.
408408pub fn pread(fd: fd_t, buf: []u8, offset: u64) ReadError!usize {
409 if (builtin.os == .windows) {
409 if (builtin.os.tag == .windows) {
410410 return windows.ReadFile(fd, buf, offset);
411411 }
412412
......@@ -493,7 +493,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) ReadError!usize {
493493 }
494494 }
495495
496 if (builtin.os == .windows) {
496 if (builtin.os.tag == .windows) {
497497 // TODO batch these into parallel requests
498498 var off: usize = 0;
499499 var iov_i: usize = 0;
......@@ -557,11 +557,11 @@ pub const WriteError = error{
557557/// If the application has a global event loop enabled, EAGAIN is handled
558558/// via the event loop. Otherwise EAGAIN results in error.WouldBlock.
559559pub fn write(fd: fd_t, bytes: []const u8) WriteError!void {
560 if (builtin.os == .windows) {
560 if (builtin.os.tag == .windows) {
561561 return windows.WriteFile(fd, bytes, null);
562562 }
563563
564 if (builtin.os == .wasi and !builtin.link_libc) {
564 if (builtin.os.tag == .wasi and !builtin.link_libc) {
565565 const ciovs = [1]iovec_const{iovec_const{
566566 .iov_base = bytes.ptr,
567567 .iov_len = bytes.len,
......@@ -1129,7 +1129,7 @@ pub fn getenv(key: []const u8) ?[]const u8 {
11291129 }
11301130 return null;
11311131 }
1132 if (builtin.os == .windows) {
1132 if (builtin.os.tag == .windows) {
11331133 @compileError("std.os.getenv is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API.");
11341134 }
11351135 // TODO see https://github.com/ziglang/zig/issues/4524
......@@ -1158,7 +1158,7 @@ pub fn getenvZ(key: [*:0]const u8) ?[]const u8 {
11581158 const value = system.getenv(key) orelse return null;
11591159 return mem.toSliceConst(u8, value);
11601160 }
1161 if (builtin.os == .windows) {
1161 if (builtin.os.tag == .windows) {
11621162 @compileError("std.os.getenvZ is unavailable for Windows because environment string is in WTF-16 format. See std.process.getEnvVarOwned for cross-platform API or std.os.getenvW for Windows-specific API.");
11631163 }
11641164 return getenv(mem.toSliceConst(u8, key));
......@@ -1167,7 +1167,7 @@ pub fn getenvZ(key: [*:0]const u8) ?[]const u8 {
11671167/// Windows-only. Get an environment variable with a null-terminated, WTF-16 encoded name.
11681168/// See also `getenv`.
11691169pub fn getenvW(key: [*:0]const u16) ?[:0]const u16 {
1170 if (builtin.os != .windows) {
1170 if (builtin.os.tag != .windows) {
11711171 @compileError("std.os.getenvW is a Windows-only API");
11721172 }
11731173 const key_slice = mem.toSliceConst(u16, key);
......@@ -1199,7 +1199,7 @@ pub const GetCwdError = error{
11991199
12001200/// The result is a slice of out_buffer, indexed from 0.
12011201pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
1202 if (builtin.os == .windows) {
1202 if (builtin.os.tag == .windows) {
12031203 return windows.GetCurrentDirectory(out_buffer);
12041204 }
12051205
......@@ -1240,7 +1240,7 @@ pub const SymLinkError = error{
12401240/// If `sym_link_path` exists, it will not be overwritten.
12411241/// See also `symlinkC` and `symlinkW`.
12421242pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
1243 if (builtin.os == .windows) {
1243 if (builtin.os.tag == .windows) {
12441244 const target_path_w = try windows.sliceToPrefixedFileW(target_path);
12451245 const sym_link_path_w = try windows.sliceToPrefixedFileW(sym_link_path);
12461246 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
......@@ -1254,7 +1254,7 @@ pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!
12541254/// This is the same as `symlink` except the parameters are null-terminated pointers.
12551255/// See also `symlink`.
12561256pub fn symlinkC(target_path: [*:0]const u8, sym_link_path: [*:0]const u8) SymLinkError!void {
1257 if (builtin.os == .windows) {
1257 if (builtin.os.tag == .windows) {
12581258 const target_path_w = try windows.cStrToPrefixedFileW(target_path);
12591259 const sym_link_path_w = try windows.cStrToPrefixedFileW(sym_link_path);
12601260 return windows.CreateSymbolicLinkW(&sym_link_path_w, &target_path_w, 0);
......@@ -1329,7 +1329,7 @@ pub const UnlinkError = error{
13291329/// Delete a name and possibly the file it refers to.
13301330/// See also `unlinkC`.
13311331pub fn unlink(file_path: []const u8) UnlinkError!void {
1332 if (builtin.os == .windows) {
1332 if (builtin.os.tag == .windows) {
13331333 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
13341334 return windows.DeleteFileW(&file_path_w);
13351335 } else {
......@@ -1340,7 +1340,7 @@ pub fn unlink(file_path: []const u8) UnlinkError!void {
13401340
13411341/// Same as `unlink` except the parameter is a null terminated UTF8-encoded string.
13421342pub fn unlinkC(file_path: [*:0]const u8) UnlinkError!void {
1343 if (builtin.os == .windows) {
1343 if (builtin.os.tag == .windows) {
13441344 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
13451345 return windows.DeleteFileW(&file_path_w);
13461346 }
......@@ -1372,7 +1372,7 @@ pub const UnlinkatError = UnlinkError || error{
13721372/// Asserts that the path parameter has no null bytes.
13731373pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
13741374 if (std.debug.runtime_safety) for (file_path) |byte| assert(byte != 0);
1375 if (builtin.os == .windows) {
1375 if (builtin.os.tag == .windows) {
13761376 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
13771377 return unlinkatW(dirfd, &file_path_w, flags);
13781378 }
......@@ -1382,7 +1382,7 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
13821382
13831383/// Same as `unlinkat` but `file_path` is a null-terminated string.
13841384pub fn unlinkatC(dirfd: fd_t, file_path_c: [*:0]const u8, flags: u32) UnlinkatError!void {
1385 if (builtin.os == .windows) {
1385 if (builtin.os.tag == .windows) {
13861386 const file_path_w = try windows.cStrToPrefixedFileW(file_path_c);
13871387 return unlinkatW(dirfd, &file_path_w, flags);
13881388 }
......@@ -1493,7 +1493,7 @@ const RenameError = error{
14931493
14941494/// Change the name or location of a file.
14951495pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
1496 if (builtin.os == .windows) {
1496 if (builtin.os.tag == .windows) {
14971497 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
14981498 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
14991499 return renameW(&old_path_w, &new_path_w);
......@@ -1506,7 +1506,7 @@ pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
15061506
15071507/// Same as `rename` except the parameters are null-terminated byte arrays.
15081508pub fn renameC(old_path: [*:0]const u8, new_path: [*:0]const u8) RenameError!void {
1509 if (builtin.os == .windows) {
1509 if (builtin.os.tag == .windows) {
15101510 const old_path_w = try windows.cStrToPrefixedFileW(old_path);
15111511 const new_path_w = try windows.cStrToPrefixedFileW(new_path);
15121512 return renameW(&old_path_w, &new_path_w);
......@@ -1561,7 +1561,7 @@ pub const MakeDirError = error{
15611561/// Create a directory.
15621562/// `mode` is ignored on Windows.
15631563pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
1564 if (builtin.os == .windows) {
1564 if (builtin.os.tag == .windows) {
15651565 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
15661566 return windows.CreateDirectoryW(&dir_path_w, null);
15671567 } else {
......@@ -1572,7 +1572,7 @@ pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
15721572
15731573/// Same as `mkdir` but the parameter is a null-terminated UTF8-encoded string.
15741574pub fn mkdirC(dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
1575 if (builtin.os == .windows) {
1575 if (builtin.os.tag == .windows) {
15761576 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
15771577 return windows.CreateDirectoryW(&dir_path_w, null);
15781578 }
......@@ -1611,7 +1611,7 @@ pub const DeleteDirError = error{
16111611
16121612/// Deletes an empty directory.
16131613pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
1614 if (builtin.os == .windows) {
1614 if (builtin.os.tag == .windows) {
16151615 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
16161616 return windows.RemoveDirectoryW(&dir_path_w);
16171617 } else {
......@@ -1622,7 +1622,7 @@ pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
16221622
16231623/// Same as `rmdir` except the parameter is null-terminated.
16241624pub fn rmdirC(dir_path: [*:0]const u8) DeleteDirError!void {
1625 if (builtin.os == .windows) {
1625 if (builtin.os.tag == .windows) {
16261626 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
16271627 return windows.RemoveDirectoryW(&dir_path_w);
16281628 }
......@@ -1658,7 +1658,7 @@ pub const ChangeCurDirError = error{
16581658/// Changes the current working directory of the calling process.
16591659/// `dir_path` is recommended to be a UTF-8 encoded string.
16601660pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
1661 if (builtin.os == .windows) {
1661 if (builtin.os.tag == .windows) {
16621662 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
16631663 @compileError("TODO implement chdir for Windows");
16641664 } else {
......@@ -1669,7 +1669,7 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
16691669
16701670/// Same as `chdir` except the parameter is null-terminated.
16711671pub fn chdirC(dir_path: [*:0]const u8) ChangeCurDirError!void {
1672 if (builtin.os == .windows) {
1672 if (builtin.os.tag == .windows) {
16731673 const dir_path_w = try windows.cStrToPrefixedFileW(dir_path);
16741674 @compileError("TODO implement chdir for Windows");
16751675 }
......@@ -1700,7 +1700,7 @@ pub const ReadLinkError = error{
17001700/// Read value of a symbolic link.
17011701/// The return value is a slice of `out_buffer` from index 0.
17021702pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
1703 if (builtin.os == .windows) {
1703 if (builtin.os.tag == .windows) {
17041704 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
17051705 @compileError("TODO implement readlink for Windows");
17061706 } else {
......@@ -1711,7 +1711,7 @@ pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
17111711
17121712/// Same as `readlink` except `file_path` is null-terminated.
17131713pub fn readlinkC(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 {
1714 if (builtin.os == .windows) {
1714 if (builtin.os.tag == .windows) {
17151715 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
17161716 @compileError("TODO implement readlink for Windows");
17171717 }
......@@ -1732,7 +1732,7 @@ pub fn readlinkC(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8
17321732}
17331733
17341734pub fn readlinkatC(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8 {
1735 if (builtin.os == .windows) {
1735 if (builtin.os.tag == .windows) {
17361736 const file_path_w = try windows.cStrToPrefixedFileW(file_path);
17371737 @compileError("TODO implement readlink for Windows");
17381738 }
......@@ -1800,7 +1800,7 @@ pub fn setregid(rgid: u32, egid: u32) SetIdError!void {
18001800
18011801/// Test whether a file descriptor refers to a terminal.
18021802pub fn isatty(handle: fd_t) bool {
1803 if (builtin.os == .windows) {
1803 if (builtin.os.tag == .windows) {
18041804 if (isCygwinPty(handle))
18051805 return true;
18061806
......@@ -1810,7 +1810,7 @@ pub fn isatty(handle: fd_t) bool {
18101810 if (builtin.link_libc) {
18111811 return system.isatty(handle) != 0;
18121812 }
1813 if (builtin.os == .wasi) {
1813 if (builtin.os.tag == .wasi) {
18141814 var statbuf: fdstat_t = undefined;
18151815 const err = system.fd_fdstat_get(handle, &statbuf);
18161816 if (err != 0) {
......@@ -1828,7 +1828,7 @@ pub fn isatty(handle: fd_t) bool {
18281828
18291829 return true;
18301830 }
1831 if (builtin.os == .linux) {
1831 if (builtin.os.tag == .linux) {
18321832 var wsz: linux.winsize = undefined;
18331833 return linux.syscall3(linux.SYS_ioctl, @bitCast(usize, @as(isize, handle)), linux.TIOCGWINSZ, @ptrToInt(&wsz)) == 0;
18341834 }
......@@ -1836,7 +1836,7 @@ pub fn isatty(handle: fd_t) bool {
18361836}
18371837
18381838pub fn isCygwinPty(handle: fd_t) bool {
1839 if (builtin.os != .windows) return false;
1839 if (builtin.os.tag != .windows) return false;
18401840
18411841 const size = @sizeOf(windows.FILE_NAME_INFO);
18421842 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (size + windows.MAX_PATH);
......@@ -2589,7 +2589,7 @@ pub const AccessError = error{
25892589/// check user's permissions for a file
25902590/// TODO currently this assumes `mode` is `F_OK` on Windows.
25912591pub fn access(path: []const u8, mode: u32) AccessError!void {
2592 if (builtin.os == .windows) {
2592 if (builtin.os.tag == .windows) {
25932593 const path_w = try windows.sliceToPrefixedFileW(path);
25942594 _ = try windows.GetFileAttributesW(&path_w);
25952595 return;
......@@ -2603,7 +2603,7 @@ pub const accessC = accessZ;
26032603
26042604/// Same as `access` except `path` is null-terminated.
26052605pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {
2606 if (builtin.os == .windows) {
2606 if (builtin.os.tag == .windows) {
26072607 const path_w = try windows.cStrToPrefixedFileW(path);
26082608 _ = try windows.GetFileAttributesW(&path_w);
26092609 return;
......@@ -2644,7 +2644,7 @@ pub fn accessW(path: [*:0]const u16, mode: u32) windows.GetFileAttributesError!v
26442644/// Check user's permissions for a file, based on an open directory handle.
26452645/// TODO currently this ignores `mode` and `flags` on Windows.
26462646pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessError!void {
2647 if (builtin.os == .windows) {
2647 if (builtin.os.tag == .windows) {
26482648 const path_w = try windows.sliceToPrefixedFileW(path);
26492649 return faccessatW(dirfd, &path_w, mode, flags);
26502650 }
......@@ -2654,7 +2654,7 @@ pub fn faccessat(dirfd: fd_t, path: []const u8, mode: u32, flags: u32) AccessErr
26542654
26552655/// Same as `faccessat` except the path parameter is null-terminated.
26562656pub fn faccessatZ(dirfd: fd_t, path: [*:0]const u8, mode: u32, flags: u32) AccessError!void {
2657 if (builtin.os == .windows) {
2657 if (builtin.os.tag == .windows) {
26582658 const path_w = try windows.cStrToPrefixedFileW(path);
26592659 return faccessatW(dirfd, &path_w, mode, flags);
26602660 }
......@@ -2811,7 +2811,7 @@ pub const SeekError = error{Unseekable} || UnexpectedError;
28112811
28122812/// Repositions read/write file offset relative to the beginning.
28132813pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
2814 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2814 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
28152815 var result: u64 = undefined;
28162816 switch (errno(system.llseek(fd, offset, &result, SEEK_SET))) {
28172817 0 => return,
......@@ -2823,7 +2823,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
28232823 else => |err| return unexpectedErrno(err),
28242824 }
28252825 }
2826 if (builtin.os == .windows) {
2826 if (builtin.os.tag == .windows) {
28272827 return windows.SetFilePointerEx_BEGIN(fd, offset);
28282828 }
28292829 const ipos = @bitCast(i64, offset); // the OS treats this as unsigned
......@@ -2840,7 +2840,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
28402840
28412841/// Repositions read/write file offset relative to the current offset.
28422842pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
2843 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2843 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
28442844 var result: u64 = undefined;
28452845 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_CUR))) {
28462846 0 => return,
......@@ -2852,7 +2852,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
28522852 else => |err| return unexpectedErrno(err),
28532853 }
28542854 }
2855 if (builtin.os == .windows) {
2855 if (builtin.os.tag == .windows) {
28562856 return windows.SetFilePointerEx_CURRENT(fd, offset);
28572857 }
28582858 switch (errno(system.lseek(fd, offset, SEEK_CUR))) {
......@@ -2868,7 +2868,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
28682868
28692869/// Repositions read/write file offset relative to the end.
28702870pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
2871 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2871 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
28722872 var result: u64 = undefined;
28732873 switch (errno(system.llseek(fd, @bitCast(u64, offset), &result, SEEK_END))) {
28742874 0 => return,
......@@ -2880,7 +2880,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
28802880 else => |err| return unexpectedErrno(err),
28812881 }
28822882 }
2883 if (builtin.os == .windows) {
2883 if (builtin.os.tag == .windows) {
28842884 return windows.SetFilePointerEx_END(fd, offset);
28852885 }
28862886 switch (errno(system.lseek(fd, offset, SEEK_END))) {
......@@ -2896,7 +2896,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
28962896
28972897/// Returns the read/write file offset relative to the beginning.
28982898pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
2899 if (builtin.os == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
2899 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
29002900 var result: u64 = undefined;
29012901 switch (errno(system.llseek(fd, 0, &result, SEEK_CUR))) {
29022902 0 => return result,
......@@ -2908,7 +2908,7 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
29082908 else => |err| return unexpectedErrno(err),
29092909 }
29102910 }
2911 if (builtin.os == .windows) {
2911 if (builtin.os.tag == .windows) {
29122912 return windows.SetFilePointerEx_CURRENT_get(fd);
29132913 }
29142914 const rc = system.lseek(fd, 0, SEEK_CUR);
......@@ -2957,7 +2957,7 @@ pub const RealPathError = error{
29572957/// The return value is a slice of `out_buffer`, but not necessarily from the beginning.
29582958/// See also `realpathC` and `realpathW`.
29592959pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
2960 if (builtin.os == .windows) {
2960 if (builtin.os.tag == .windows) {
29612961 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
29622962 return realpathW(&pathname_w, out_buffer);
29632963 }
......@@ -2967,11 +2967,11 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
29672967
29682968/// Same as `realpath` except `pathname` is null-terminated.
29692969pub fn realpathC(pathname: [*:0]const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
2970 if (builtin.os == .windows) {
2970 if (builtin.os.tag == .windows) {
29712971 const pathname_w = try windows.cStrToPrefixedFileW(pathname);
29722972 return realpathW(&pathname_w, out_buffer);
29732973 }
2974 if (builtin.os == .linux and !builtin.link_libc) {
2974 if (builtin.os.tag == .linux and !builtin.link_libc) {
29752975 const fd = try openC(pathname, linux.O_PATH | linux.O_NONBLOCK | linux.O_CLOEXEC, 0);
29762976 defer close(fd);
29772977
......@@ -3121,7 +3121,7 @@ pub fn dl_iterate_phdr(
31213121pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
31223122
31233123pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
3124 if (comptime std.Target.current.getOs() == .wasi) {
3124 if (std.Target.current.os.tag == .wasi) {
31253125 var ts: timestamp_t = undefined;
31263126 switch (system.clock_time_get(@bitCast(u32, clk_id), 1, &ts)) {
31273127 0 => {
......@@ -3144,7 +3144,7 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
31443144}
31453145
31463146pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
3147 if (comptime std.Target.current.getOs() == .wasi) {
3147 if (std.Target.current.os.tag == .wasi) {
31483148 var ts: timestamp_t = undefined;
31493149 switch (system.clock_res_get(@bitCast(u32, clk_id), &ts)) {
31503150 0 => res.* = .{
......@@ -3222,7 +3222,7 @@ pub const SigaltstackError = error{
32223222} || UnexpectedError;
32233223
32243224pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) SigaltstackError!void {
3225 if (builtin.os == .windows or builtin.os == .uefi or builtin.os == .wasi)
3225 if (builtin.os.tag == .windows or builtin.os.tag == .uefi or builtin.os.tag == .wasi)
32263226 @compileError("std.os.sigaltstack not available for this target");
32273227
32283228 switch (errno(system.sigaltstack(ss, old_ss))) {
......@@ -3294,7 +3294,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
32943294 else => |err| return unexpectedErrno(err),
32953295 }
32963296 }
3297 if (builtin.os == .linux) {
3297 if (builtin.os.tag == .linux) {
32983298 var uts: utsname = undefined;
32993299 switch (errno(system.uname(&uts))) {
33003300 0 => {
......@@ -3611,7 +3611,7 @@ pub const SchedYieldError = error{
36113611};
36123612
36133613pub fn sched_yield() SchedYieldError!void {
3614 if (builtin.os == .windows) {
3614 if (builtin.os.tag == .windows) {
36153615 // The return value has to do with how many other threads there are; it is not
36163616 // an error condition on Windows.
36173617 _ = windows.kernel32.SwitchToThread();
lib/std/os/bits.zig+2-2
......@@ -3,10 +3,10 @@
33//! Root source files can define `os.bits` and these will additionally be added
44//! to the namespace.
55
6const builtin = @import("builtin");
6const std = @import("std");
77const root = @import("root");
88
9pub usingnamespace switch (builtin.os) {
9pub usingnamespace switch (std.Target.current.os.tag) {
1010 .macosx, .ios, .tvos, .watchos => @import("bits/darwin.zig"),
1111 .dragonfly => @import("bits/dragonfly.zig"),
1212 .freebsd => @import("bits/freebsd.zig"),
lib/std/os/linux.zig+1-1
......@@ -1070,7 +1070,7 @@ pub fn tcsetattr(fd: fd_t, optional_action: TCSA, termios_p: *const termios) usi
10701070}
10711071
10721072test "" {
1073 if (builtin.os == .linux) {
1073 if (builtin.os.tag == .linux) {
10741074 _ = @import("linux/test.zig");
10751075 }
10761076}
lib/std/os/test.zig+8-8
......@@ -53,7 +53,7 @@ test "std.Thread.getCurrentId" {
5353 thread.wait();
5454 if (Thread.use_pthreads) {
5555 expect(thread_current_id == thread_id);
56 } else if (builtin.os == .windows) {
56 } else if (builtin.os.tag == .windows) {
5757 expect(Thread.getCurrentId() != thread_current_id);
5858 } else {
5959 // If the thread completes very quickly, then thread_id can be 0. See the
......@@ -151,7 +151,7 @@ test "realpath" {
151151}
152152
153153test "sigaltstack" {
154 if (builtin.os == .windows or builtin.os == .wasi) return error.SkipZigTest;
154 if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
155155
156156 var st: os.stack_t = undefined;
157157 try os.sigaltstack(null, &st);
......@@ -204,7 +204,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
204204}
205205
206206test "dl_iterate_phdr" {
207 if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
207 if (builtin.os.tag == .windows or builtin.os.tag == .wasi or builtin.os.tag == .macosx)
208208 return error.SkipZigTest;
209209
210210 var counter: usize = 0;
......@@ -213,7 +213,7 @@ test "dl_iterate_phdr" {
213213}
214214
215215test "gethostname" {
216 if (builtin.os == .windows)
216 if (builtin.os.tag == .windows)
217217 return error.SkipZigTest;
218218
219219 var buf: [os.HOST_NAME_MAX]u8 = undefined;
......@@ -222,7 +222,7 @@ test "gethostname" {
222222}
223223
224224test "pipe" {
225 if (builtin.os == .windows)
225 if (builtin.os.tag == .windows)
226226 return error.SkipZigTest;
227227
228228 var fds = try os.pipe();
......@@ -241,7 +241,7 @@ test "argsAlloc" {
241241
242242test "memfd_create" {
243243 // memfd_create is linux specific.
244 if (builtin.os != .linux) return error.SkipZigTest;
244 if (builtin.os.tag != .linux) return error.SkipZigTest;
245245 const fd = std.os.memfd_create("test", 0) catch |err| switch (err) {
246246 // Related: https://github.com/ziglang/zig/issues/4019
247247 error.SystemOutdated => return error.SkipZigTest,
......@@ -258,7 +258,7 @@ test "memfd_create" {
258258}
259259
260260test "mmap" {
261 if (builtin.os == .windows)
261 if (builtin.os.tag == .windows)
262262 return error.SkipZigTest;
263263
264264 // Simple mmap() call with non page-aligned size
......@@ -353,7 +353,7 @@ test "mmap" {
353353}
354354
355355test "getenv" {
356 if (builtin.os == .windows) {
356 if (builtin.os.tag == .windows) {
357357 expect(os.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null);
358358 } else {
359359 expect(os.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null);
lib/std/packed_int_array.zig+2-2
......@@ -593,7 +593,7 @@ test "PackedInt(Array/Slice)Endian" {
593593// after this one is not mapped and will cause a segfault if we
594594// don't account for the bounds.
595595test "PackedIntArray at end of available memory" {
596 switch (builtin.os) {
596 switch (builtin.os.tag) {
597597 .linux, .macosx, .ios, .freebsd, .netbsd, .windows => {},
598598 else => return,
599599 }
......@@ -612,7 +612,7 @@ test "PackedIntArray at end of available memory" {
612612}
613613
614614test "PackedIntSlice at end of available memory" {
615 switch (builtin.os) {
615 switch (builtin.os.tag) {
616616 .linux, .macosx, .ios, .freebsd, .netbsd, .windows => {},
617617 else => return,
618618 }
lib/std/process.zig+11-11
......@@ -36,7 +36,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
3636 var result = BufMap.init(allocator);
3737 errdefer result.deinit();
3838
39 if (builtin.os == .windows) {
39 if (builtin.os.tag == .windows) {
4040 const ptr = os.windows.peb().ProcessParameters.Environment;
4141
4242 var i: usize = 0;
......@@ -61,7 +61,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
6161 try result.setMove(key, value);
6262 }
6363 return result;
64 } else if (builtin.os == .wasi) {
64 } else if (builtin.os.tag == .wasi) {
6565 var environ_count: usize = undefined;
6666 var environ_buf_size: usize = undefined;
6767
......@@ -137,7 +137,7 @@ pub const GetEnvVarOwnedError = error{
137137
138138/// Caller must free returned memory.
139139pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
140 if (builtin.os == .windows) {
140 if (builtin.os.tag == .windows) {
141141 const result_w = blk: {
142142 const key_w = try std.unicode.utf8ToUtf16LeWithNull(allocator, key);
143143 defer allocator.free(key_w);
......@@ -338,12 +338,12 @@ pub const ArgIteratorWindows = struct {
338338};
339339
340340pub const ArgIterator = struct {
341 const InnerType = if (builtin.os == .windows) ArgIteratorWindows else ArgIteratorPosix;
341 const InnerType = if (builtin.os.tag == .windows) ArgIteratorWindows else ArgIteratorPosix;
342342
343343 inner: InnerType,
344344
345345 pub fn init() ArgIterator {
346 if (builtin.os == .wasi) {
346 if (builtin.os.tag == .wasi) {
347347 // TODO: Figure out a compatible interface accomodating WASI
348348 @compileError("ArgIterator is not yet supported in WASI. Use argsAlloc and argsFree instead.");
349349 }
......@@ -355,7 +355,7 @@ pub const ArgIterator = struct {
355355
356356 /// You must free the returned memory when done.
357357 pub fn next(self: *ArgIterator, allocator: *Allocator) ?(NextError![]u8) {
358 if (builtin.os == .windows) {
358 if (builtin.os.tag == .windows) {
359359 return self.inner.next(allocator);
360360 } else {
361361 return mem.dupe(allocator, u8, self.inner.next() orelse return null);
......@@ -380,7 +380,7 @@ pub fn args() ArgIterator {
380380
381381/// Caller must call argsFree on result.
382382pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 {
383 if (builtin.os == .wasi) {
383 if (builtin.os.tag == .wasi) {
384384 var count: usize = undefined;
385385 var buf_size: usize = undefined;
386386
......@@ -445,7 +445,7 @@ pub fn argsAlloc(allocator: *mem.Allocator) ![][]u8 {
445445}
446446
447447pub fn argsFree(allocator: *mem.Allocator, args_alloc: []const []u8) void {
448 if (builtin.os == .wasi) {
448 if (builtin.os.tag == .wasi) {
449449 const last_item = args_alloc[args_alloc.len - 1];
450450 const last_byte_addr = @ptrToInt(last_item.ptr) + last_item.len + 1; // null terminated
451451 const first_item_ptr = args_alloc[0].ptr;
......@@ -498,7 +498,7 @@ pub const UserInfo = struct {
498498
499499/// POSIX function which gets a uid from username.
500500pub fn getUserInfo(name: []const u8) !UserInfo {
501 return switch (builtin.os) {
501 return switch (builtin.os.tag) {
502502 .linux, .macosx, .watchos, .tvos, .ios, .freebsd, .netbsd => posixGetUserInfo(name),
503503 else => @compileError("Unsupported OS"),
504504 };
......@@ -591,7 +591,7 @@ pub fn posixGetUserInfo(name: []const u8) !UserInfo {
591591}
592592
593593pub fn getBaseAddress() usize {
594 switch (builtin.os) {
594 switch (builtin.os.tag) {
595595 .linux => {
596596 const base = os.system.getauxval(std.elf.AT_BASE);
597597 if (base != 0) {
......@@ -615,7 +615,7 @@ pub fn getSelfExeSharedLibPaths(allocator: *Allocator) error{OutOfMemory}![][:0]
615615 .Dynamic => {},
616616 }
617617 const List = std.ArrayList([:0]u8);
618 switch (builtin.os) {
618 switch (builtin.os.tag) {
619619 .linux,
620620 .freebsd,
621621 .netbsd,
lib/std/reset_event.zig+3-3
......@@ -16,7 +16,7 @@ pub const ResetEvent = struct {
1616
1717 pub const OsEvent = if (builtin.single_threaded)
1818 DebugEvent
19 else if (builtin.link_libc and builtin.os != .windows and builtin.os != .linux)
19 else if (builtin.link_libc and builtin.os.tag != .windows and builtin.os.tag != .linux)
2020 PosixEvent
2121 else
2222 AtomicEvent;
......@@ -106,7 +106,7 @@ const PosixEvent = struct {
106106 fn deinit(self: *PosixEvent) void {
107107 // on dragonfly, *destroy() functions can return EINVAL
108108 // for statically initialized pthread structures
109 const err = if (builtin.os == .dragonfly) os.EINVAL else 0;
109 const err = if (builtin.os.tag == .dragonfly) os.EINVAL else 0;
110110
111111 const retm = c.pthread_mutex_destroy(&self.mutex);
112112 assert(retm == 0 or retm == err);
......@@ -215,7 +215,7 @@ const AtomicEvent = struct {
215215 }
216216 }
217217
218 pub const Futex = switch (builtin.os) {
218 pub const Futex = switch (builtin.os.tag) {
219219 .windows => WindowsFutex,
220220 .linux => LinuxFutex,
221221 else => SpinFutex,
lib/std/special/c.zig+4-4
......@@ -17,7 +17,7 @@ const is_msvc = switch (builtin.abi) {
1717 .msvc => true,
1818 else => false,
1919};
20const is_freestanding = switch (builtin.os) {
20const is_freestanding = switch (builtin.os.tag) {
2121 .freestanding => true,
2222 else => false,
2323};
......@@ -81,7 +81,7 @@ pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn
8181 @setCold(true);
8282 std.debug.panic("{}", .{msg});
8383 }
84 if (builtin.os != .freestanding and builtin.os != .other) {
84 if (builtin.os.tag != .freestanding and builtin.os.tag != .other) {
8585 std.os.abort();
8686 }
8787 while (true) {}
......@@ -178,11 +178,11 @@ test "test_bcmp" {
178178comptime {
179179 if (builtin.mode != builtin.Mode.ReleaseFast and
180180 builtin.mode != builtin.Mode.ReleaseSmall and
181 builtin.os != builtin.Os.windows)
181 builtin.os.tag != .windows)
182182 {
183183 @export(__stack_chk_fail, .{ .name = "__stack_chk_fail" });
184184 }
185 if (builtin.os == builtin.Os.linux) {
185 if (builtin.os.tag == .linux) {
186186 @export(clone, .{ .name = "clone" });
187187 }
188188}
lib/std/special/compiler_rt.zig+7-6
......@@ -1,11 +1,12 @@
1const builtin = @import("builtin");
1const std = @import("std");
2const builtin = std.builtin;
23const is_test = builtin.is_test;
34
45const is_gnu = switch (builtin.abi) {
56 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
67 else => false,
78};
8const is_mingw = builtin.os == .windows and is_gnu;
9const is_mingw = builtin.os.tag == .windows and is_gnu;
910
1011comptime {
1112 const linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Weak;
......@@ -180,7 +181,7 @@ comptime {
180181 @export(@import("compiler_rt/arm.zig").__aeabi_memclr, .{ .name = "__aeabi_memclr4", .linkage = linkage });
181182 @export(@import("compiler_rt/arm.zig").__aeabi_memclr, .{ .name = "__aeabi_memclr8", .linkage = linkage });
182183
183 if (builtin.os == .linux) {
184 if (builtin.os.tag == .linux) {
184185 @export(@import("compiler_rt/arm.zig").__aeabi_read_tp, .{ .name = "__aeabi_read_tp", .linkage = linkage });
185186 }
186187
......@@ -250,7 +251,7 @@ comptime {
250251 @export(@import("compiler_rt/aullrem.zig")._aullrem, .{ .name = "\x01__aullrem", .linkage = strong_linkage });
251252 }
252253
253 if (builtin.os == .windows) {
254 if (builtin.os.tag == .windows) {
254255 // Default stack-probe functions emitted by LLVM
255256 if (is_mingw) {
256257 @export(@import("compiler_rt/stack_probe.zig")._chkstk, .{ .name = "_alloca", .linkage = strong_linkage });
......@@ -288,7 +289,7 @@ comptime {
288289 else => {},
289290 }
290291 } else {
291 if (builtin.glibc_version != null) {
292 if (std.Target.current.isGnuLibC()) {
292293 @export(__stack_chk_guard, .{ .name = "__stack_chk_guard", .linkage = linkage });
293294 }
294295 @export(@import("compiler_rt/divti3.zig").__divti3, .{ .name = "__divti3", .linkage = linkage });
......@@ -307,7 +308,7 @@ comptime {
307308pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
308309 @setCold(true);
309310 if (is_test) {
310 @import("std").debug.panic("{}", .{msg});
311 std.debug.panic("{}", .{msg});
311312 } else {
312313 unreachable;
313314 }
lib/std/special/compiler_rt/extendXfYf2_test.zig+1-1
......@@ -90,7 +90,7 @@ test "extendhfsf2" {
9090 test__extendhfsf2(0x7f00, 0x7fe00000); // sNaN
9191 // On x86 the NaN becomes quiet because the return is pushed on the x87
9292 // stack due to ABI requirements
93 if (builtin.arch != .i386 and builtin.os == .windows)
93 if (builtin.arch != .i386 and builtin.os.tag == .windows)
9494 test__extendhfsf2(0x7c01, 0x7f802000); // sNaN
9595
9696 test__extendhfsf2(0, 0); // 0
lib/std/spinlock.zig+1-1
......@@ -46,7 +46,7 @@ pub const SpinLock = struct {
4646 // and yielding for 380-410 iterations was found to be
4747 // a nice sweet spot. Posix systems on the other hand,
4848 // especially linux, perform better by yielding the thread.
49 switch (builtin.os) {
49 switch (builtin.os.tag) {
5050 .windows => loopHint(400),
5151 else => std.os.sched_yield() catch loopHint(1),
5252 }
lib/std/start.zig+8-8
......@@ -12,7 +12,7 @@ const start_sym_name = if (builtin.arch.isMIPS()) "__start" else "_start";
1212
1313comptime {
1414 if (builtin.output_mode == .Lib and builtin.link_mode == .Dynamic) {
15 if (builtin.os == .windows and !@hasDecl(root, "_DllMainCRTStartup")) {
15 if (builtin.os.tag == .windows and !@hasDecl(root, "_DllMainCRTStartup")) {
1616 @export(_DllMainCRTStartup, .{ .name = "_DllMainCRTStartup" });
1717 }
1818 } else if (builtin.output_mode == .Exe or @hasDecl(root, "main")) {
......@@ -20,17 +20,17 @@ comptime {
2020 if (@typeInfo(@TypeOf(root.main)).Fn.calling_convention != .C) {
2121 @export(main, .{ .name = "main", .linkage = .Weak });
2222 }
23 } else if (builtin.os == .windows) {
23 } else if (builtin.os.tag == .windows) {
2424 if (!@hasDecl(root, "WinMain") and !@hasDecl(root, "WinMainCRTStartup") and
2525 !@hasDecl(root, "wWinMain") and !@hasDecl(root, "wWinMainCRTStartup"))
2626 {
2727 @export(WinMainCRTStartup, .{ .name = "WinMainCRTStartup" });
2828 }
29 } else if (builtin.os == .uefi) {
29 } else if (builtin.os.tag == .uefi) {
3030 if (!@hasDecl(root, "EfiMain")) @export(EfiMain, .{ .name = "EfiMain" });
31 } else if (builtin.arch.isWasm() and builtin.os == .freestanding) {
31 } else if (builtin.arch.isWasm() and builtin.os.tag == .freestanding) {
3232 if (!@hasDecl(root, start_sym_name)) @export(wasm_freestanding_start, .{ .name = start_sym_name });
33 } else if (builtin.os != .other and builtin.os != .freestanding) {
33 } else if (builtin.os.tag != .other and builtin.os.tag != .freestanding) {
3434 if (!@hasDecl(root, start_sym_name)) @export(_start, .{ .name = start_sym_name });
3535 }
3636 }
......@@ -78,7 +78,7 @@ fn EfiMain(handle: uefi.Handle, system_table: *uefi.tables.SystemTable) callconv
7878}
7979
8080fn _start() callconv(.Naked) noreturn {
81 if (builtin.os == builtin.Os.wasi) {
81 if (builtin.os.tag == .wasi) {
8282 // This is marked inline because for some reason LLVM in release mode fails to inline it,
8383 // and we want fewer call frames in stack traces.
8484 std.os.wasi.proc_exit(@call(.{ .modifier = .always_inline }, callMain, .{}));
......@@ -133,7 +133,7 @@ fn WinMainCRTStartup() callconv(.Stdcall) noreturn {
133133
134134// TODO https://github.com/ziglang/zig/issues/265
135135fn posixCallMainAndExit() noreturn {
136 if (builtin.os == builtin.Os.freebsd) {
136 if (builtin.os.tag == .freebsd) {
137137 @setAlignStack(16);
138138 }
139139 const argc = starting_stack_ptr[0];
......@@ -144,7 +144,7 @@ fn posixCallMainAndExit() noreturn {
144144 while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
145145 const envp = @ptrCast([*][*:0]u8, envp_optional)[0..envp_count];
146146
147 if (builtin.os == .linux) {
147 if (builtin.os.tag == .linux) {
148148 // Find the beginning of the auxiliary vector
149149 const auxv = @ptrCast([*]std.elf.Auxv, @alignCast(@alignOf(usize), envp.ptr + envp_count + 1));
150150 std.os.linux.elf_aux_maybe = auxv;
lib/std/target.zig+308-291
......@@ -11,143 +11,48 @@ pub const Target = struct {
1111 os: Os,
1212 abi: Abi,
1313
14 /// The version ranges here represent the minimum OS version to be supported
15 /// and the maximum OS version to be supported. The default values represent
16 /// the range that the Zig Standard Library bases its abstractions on.
17 ///
18 /// The minimum version of the range is the main setting to tweak for a target.
19 /// Usually, the maximum target OS version will remain the default, which is
20 /// the latest released version of the OS.
21 ///
22 /// To test at compile time if the target is guaranteed to support a given OS feature,
23 /// one should check that the minimum version of the range is greater than or equal to
24 /// the version the feature was introduced in.
25 ///
26 /// To test at compile time if the target certainly will not support a given OS feature,
27 /// one should check that the maximum version of the range is less than the version the
28 /// feature was introduced in.
29 ///
30 /// If neither of these cases apply, a runtime check should be used to determine if the
31 /// target supports a given OS feature.
32 ///
33 /// Binaries built with a given maximum version will continue to function on newer operating system
34 /// versions. However, such a binary may not take full advantage of the newer operating system APIs.
35 pub const Os = union(enum) {
36 freestanding,
37 ananas,
38 cloudabi,
39 dragonfly,
40 freebsd: Version.Range,
41 fuchsia,
42 ios,
43 kfreebsd,
44 linux: LinuxVersionRange,
45 lv2,
46 macosx: Version.Range,
47 netbsd: Version.Range,
48 openbsd: Version.Range,
49 solaris,
50 windows: WindowsVersion.Range,
51 haiku,
52 minix,
53 rtems,
54 nacl,
55 cnk,
56 aix,
57 cuda,
58 nvcl,
59 amdhsa,
60 ps4,
61 elfiamcu,
62 tvos,
63 watchos,
64 mesa3d,
65 contiki,
66 amdpal,
67 hermit,
68 hurd,
69 wasi,
70 emscripten,
71 uefi,
72 other,
73
74 /// See the documentation for `Os` for an explanation of the default version range.
75 pub fn defaultVersionRange(tag: @TagType(Os)) Os {
76 switch (tag) {
77 .freestanding => return .freestanding,
78 .ananas => return .ananas,
79 .cloudabi => return .cloudabi,
80 .dragonfly => return .dragonfly,
81 .freebsd => return .{
82 .freebsd = Version.Range{
83 .min = .{ .major = 12, .minor = 0 },
84 .max = .{ .major = 12, .minor = 1 },
85 },
86 },
87 .fuchsia => return .fuchsia,
88 .ios => return .ios,
89 .kfreebsd => return .kfreebsd,
90 .linux => return .{
91 .linux = .{
92 .range = .{
93 .min = .{ .major = 3, .minor = 16 },
94 .max = .{ .major = 5, .minor = 5, .patch = 5 },
95 },
96 .glibc = .{ .major = 2, .minor = 17 },
97 },
98 },
99 .lv2 => return .lv2,
100 .macosx => return .{
101 .min = .{ .major = 10, .minor = 13 },
102 .max = .{ .major = 10, .minor = 15, .patch = 3 },
103 },
104 .netbsd => return .{
105 .min = .{ .major = 8, .minor = 0 },
106 .max = .{ .major = 9, .minor = 0 },
107 },
108 .openbsd => return .{
109 .min = .{ .major = 6, .minor = 6 },
110 .max = .{ .major = 6, .minor = 6 },
111 },
112 solaris => return .solaris,
113 windows => return .{
114 .windows = .{
115 .min = .win8_1,
116 .max = .win10_19h1,
117 },
118 },
119 haiku => return .haiku,
120 minix => return .minix,
121 rtems => return .rtems,
122 nacl => return .nacl,
123 cnk => return .cnk,
124 aix => return .aix,
125 cuda => return .cuda,
126 nvcl => return .nvcl,
127 amdhsa => return .amdhsa,
128 ps4 => return .ps4,
129 elfiamcu => return .elfiamcu,
130 tvos => return .tvos,
131 watchos => return .watchos,
132 mesa3d => return .mesa3d,
133 contiki => return .contiki,
134 amdpal => return .amdpal,
135 hermit => return .hermit,
136 hurd => return .hurd,
137 wasi => return .wasi,
138 emscripten => return .emscripten,
139 uefi => return .uefi,
140 other => return .other,
141 }
142 }
143
144 pub const LinuxVersionRange = struct {
145 range: Version.Range,
146 glibc: Version,
147
148 pub fn includesVersion(self: LinuxVersionRange, ver: Version) bool {
149 return self.range.includesVersion(ver);
150 }
14 pub const Os = struct {
15 tag: Tag,
16 version_range: VersionRange,
17
18 pub const Tag = enum {
19 freestanding,
20 ananas,
21 cloudabi,
22 dragonfly,
23 freebsd,
24 fuchsia,
25 ios,
26 kfreebsd,
27 linux,
28 lv2,
29 macosx,
30 netbsd,
31 openbsd,
32 solaris,
33 windows,
34 haiku,
35 minix,
36 rtems,
37 nacl,
38 cnk,
39 aix,
40 cuda,
41 nvcl,
42 amdhsa,
43 ps4,
44 elfiamcu,
45 tvos,
46 watchos,
47 mesa3d,
48 contiki,
49 amdpal,
50 hermit,
51 hurd,
52 wasi,
53 emscripten,
54 uefi,
55 other,
15156 };
15257
15358 /// Based on NTDDI version constants from
......@@ -178,29 +83,137 @@ pub const Target = struct {
17883 return @enumToInt(ver) >= @enumToInt(self.min) and @enumToInt(ver) <= @enumToInt(self.max);
17984 }
18085 };
86 };
18187
182 pub fn nameToTag(name: []const u8) ?WindowsVersion {
183 const info = @typeInfo(WindowsVersion);
184 inline for (info.Enum.fields) |field| {
185 if (mem.eql(u8, name, field.name)) {
186 return @field(WindowsVersion, field.name);
187 }
88 pub const LinuxVersionRange = struct {
89 range: Version.Range,
90 glibc: Version,
91
92 pub fn includesVersion(self: LinuxVersionRange, ver: Version) bool {
93 return self.range.includesVersion(ver);
94 }
95 };
96
97 /// The version ranges here represent the minimum OS version to be supported
98 /// and the maximum OS version to be supported. The default values represent
99 /// the range that the Zig Standard Library bases its abstractions on.
100 ///
101 /// The minimum version of the range is the main setting to tweak for a target.
102 /// Usually, the maximum target OS version will remain the default, which is
103 /// the latest released version of the OS.
104 ///
105 /// To test at compile time if the target is guaranteed to support a given OS feature,
106 /// one should check that the minimum version of the range is greater than or equal to
107 /// the version the feature was introduced in.
108 ///
109 /// To test at compile time if the target certainly will not support a given OS feature,
110 /// one should check that the maximum version of the range is less than the version the
111 /// feature was introduced in.
112 ///
113 /// If neither of these cases apply, a runtime check should be used to determine if the
114 /// target supports a given OS feature.
115 ///
116 /// Binaries built with a given maximum version will continue to function on newer operating system
117 /// versions. However, such a binary may not take full advantage of the newer operating system APIs.
118 pub const VersionRange = union {
119 none: void,
120 semver: Version.Range,
121 linux: LinuxVersionRange,
122 windows: WindowsVersion.Range,
123
124 /// The default `VersionRange` represents the range that the Zig Standard Library
125 /// bases its abstractions on.
126 pub fn default(tag: Tag) VersionRange {
127 switch (tag) {
128 .freestanding,
129 .ananas,
130 .cloudabi,
131 .dragonfly,
132 .fuchsia,
133 .ios,
134 .kfreebsd,
135 .lv2,
136 .solaris,
137 .haiku,
138 .minix,
139 .rtems,
140 .nacl,
141 .cnk,
142 .aix,
143 .cuda,
144 .nvcl,
145 .amdhsa,
146 .ps4,
147 .elfiamcu,
148 .tvos,
149 .watchos,
150 .mesa3d,
151 .contiki,
152 .amdpal,
153 .hermit,
154 .hurd,
155 .wasi,
156 .emscripten,
157 .uefi,
158 .other,
159 => return .{ .none = {} },
160
161 .freebsd => return .{
162 .semver = Version.Range{
163 .min = .{ .major = 12, .minor = 0 },
164 .max = .{ .major = 12, .minor = 1 },
165 },
166 },
167 .macosx => return .{
168 .semver = .{
169 .min = .{ .major = 10, .minor = 13 },
170 .max = .{ .major = 10, .minor = 15, .patch = 3 },
171 },
172 },
173 .netbsd => return .{
174 .semver = .{
175 .min = .{ .major = 8, .minor = 0 },
176 .max = .{ .major = 9, .minor = 0 },
177 },
178 },
179 .openbsd => return .{
180 .semver = .{
181 .min = .{ .major = 6, .minor = 6 },
182 .max = .{ .major = 6, .minor = 6 },
183 },
184 },
185
186 .linux => return .{
187 .linux = .{
188 .range = .{
189 .min = .{ .major = 3, .minor = 16 },
190 .max = .{ .major = 5, .minor = 5, .patch = 5 },
191 },
192 .glibc = .{ .major = 2, .minor = 17 },
193 },
194 },
195
196 .windows => return .{
197 .windows = .{
198 .min = .win8_1,
199 .max = .win10_19h1,
200 },
201 },
188202 }
189 return null;
190203 }
191204 };
192205
193206 pub fn parse(text: []const u8) !Os {
194207 var it = mem.separate(text, ".");
195208 const os_name = it.next().?;
196 const tag = nameToTag(os_name) orelse return error.UnknownOperatingSystem;
209 const tag = std.meta.stringToEnum(Tag, os_name) orelse return error.UnknownOperatingSystem;
197210 const version_text = it.rest();
198211 const S = struct {
199212 fn parseNone(s: []const u8) !void {
200213 if (s.len != 0) return error.InvalidOperatingSystemVersion;
201214 }
202 fn parseSemVer(s: []const u8, default: Version.Range) !Version.Range {
203 if (s.len == 0) return default;
215 fn parseSemVer(s: []const u8, d_range: Version.Range) !Version.Range {
216 if (s.len == 0) return d_range;
204217 var range_it = mem.separate(s, "...");
205218
206219 const min_text = range_it.next().?;
......@@ -212,7 +225,7 @@ pub const Target = struct {
212225
213226 const max_text = range_it.next() orelse return Version.Range{
214227 .min = min_ver,
215 .max = default.max,
228 .max = d_range.max,
216229 };
217230 const max_ver = Version.parse(max_text) catch |err| switch (err) {
218231 error.Overflow => return error.InvalidOperatingSystemVersion,
......@@ -222,79 +235,93 @@ pub const Target = struct {
222235
223236 return Version.Range{ .min = min_ver, .max = max_ver };
224237 }
225 fn parseWindows(s: []const u8, default: WindowsVersion.Range) !WindowsVersion.Range {
226 if (s.len == 0) return default;
238 fn parseWindows(s: []const u8, d_range: WindowsVersion.Range) !WindowsVersion.Range {
239 if (s.len == 0) return d_range;
227240 var range_it = mem.separate(s, "...");
228241
229242 const min_text = range_it.next().?;
230 const min_ver = WindowsVersion.nameToTag(min_text) orelse
243 const min_ver = std.meta.stringToEnum(WindowsVersion, min_text) orelse
231244 return error.InvalidOperatingSystemVersion;
232245
233246 const max_text = range_it.next() orelse return WindowsVersion.Range{
234247 .min = min_ver,
235 .max = default.max,
248 .max = d_range.max,
236249 };
237 const max_ver = WindowsVersion.nameToTag(max_text) orelse
250 const max_ver = std.meta.stringToEnum(WindowsVersion, max_text) orelse
238251 return error.InvalidOperatingSystemVersion;
239252
240253 return WindowsVersion.Range{ .min = min_ver, .max = max_ver };
241254 }
242255 };
243 const default = defaultVersionRange(tag);
256 const d_range = VersionRange.default(tag);
244257 switch (tag) {
245 .freestanding => return Os{ .freestanding = try S.parseNone(version_text) },
246 .ananas => return Os{ .ananas = try S.parseNone(version_text) },
247 .cloudabi => return Os{ .cloudabi = try S.parseNone(version_text) },
248 .dragonfly => return Os{ .dragonfly = try S.parseNone(version_text) },
249 .freebsd => return Os{ .freebsd = try S.parseSemVer(version_text, default.freebsd) },
250 .fuchsia => return Os{ .fuchsia = try S.parseNone(version_text) },
251 .ios => return Os{ .ios = try S.parseNone(version_text) },
252 .kfreebsd => return Os{ .kfreebsd = try S.parseNone(version_text) },
258 .freestanding,
259 .ananas,
260 .cloudabi,
261 .dragonfly,
262 .fuchsia,
263 .ios,
264 .kfreebsd,
265 .lv2,
266 .solaris,
267 .haiku,
268 .minix,
269 .rtems,
270 .nacl,
271 .cnk,
272 .aix,
273 .cuda,
274 .nvcl,
275 .amdhsa,
276 .ps4,
277 .elfiamcu,
278 .tvos,
279 .watchos,
280 .mesa3d,
281 .contiki,
282 .amdpal,
283 .hermit,
284 .hurd,
285 .wasi,
286 .emscripten,
287 .uefi,
288 .other,
289 => return Os{
290 .tag = tag,
291 .version_range = .{ .none = try S.parseNone(version_text) },
292 },
293
294 .freebsd,
295 .macosx,
296 .netbsd,
297 .openbsd,
298 => return Os{
299 .tag = tag,
300 .version_range = .{ .semver = try S.parseSemVer(version_text, d_range.semver) },
301 },
302
253303 .linux => return Os{
254 .linux = .{
255 .range = try S.parseSemVer(version_text, default.linux.range),
256 .glibc = default.linux.glibc,
304 .tag = tag,
305 .version_range = .{
306 .linux = .{
307 .range = try S.parseSemVer(version_text, d_range.linux.range),
308 .glibc = d_range.linux.glibc,
309 },
257310 },
258311 },
259 .lv2 => return Os{ .lv2 = try S.parseNone(version_text) },
260 .macosx => return Os{ .macosx = try S.parseSemVer(version_text, default.macosx) },
261 .netbsd => return Os{ .netbsd = try S.parseSemVer(version_text, default.netbsd) },
262 .openbsd => return Os{ .openbsd = try S.parseSemVer(version_text, default.openbsd) },
263 .solaris => return Os{ .solaris = try S.parseNone(version_text) },
264 .windows => return Os{ .windows = try S.parseWindows(version_text, default.windows) },
265 .haiku => return Os{ .haiku = try S.parseNone(version_text) },
266 .minix => return Os{ .minix = try S.parseNone(version_text) },
267 .rtems => return Os{ .rtems = try S.parseNone(version_text) },
268 .nacl => return Os{ .nacl = try S.parseNone(version_text) },
269 .cnk => return Os{ .cnk = try S.parseNone(version_text) },
270 .aix => return Os{ .aix = try S.parseNone(version_text) },
271 .cuda => return Os{ .cuda = try S.parseNone(version_text) },
272 .nvcl => return Os{ .nvcl = try S.parseNone(version_text) },
273 .amdhsa => return Os{ .amdhsa = try S.parseNone(version_text) },
274 .ps4 => return Os{ .ps4 = try S.parseNone(version_text) },
275 .elfiamcu => return Os{ .elfiamcu = try S.parseNone(version_text) },
276 .tvos => return Os{ .tvos = try S.parseNone(version_text) },
277 .watchos => return Os{ .watchos = try S.parseNone(version_text) },
278 .mesa3d => return Os{ .mesa3d = try S.parseNone(version_text) },
279 .contiki => return Os{ .contiki = try S.parseNone(version_text) },
280 .amdpal => return Os{ .amdpal = try S.parseNone(version_text) },
281 .hermit => return Os{ .hermit = try S.parseNone(version_text) },
282 .hurd => return Os{ .hurd = try S.parseNone(version_text) },
283 .wasi => return Os{ .wasi = try S.parseNone(version_text) },
284 .emscripten => return Os{ .emscripten = try S.parseNone(version_text) },
285 .uefi => return Os{ .uefi = try S.parseNone(version_text) },
286 .other => return Os{ .other = try S.parseNone(version_text) },
312
313 .windows => return Os{
314 .tag = tag,
315 .version_range = .{ .windows = try S.parseWindows(version_text, d_range.windows) },
316 },
287317 }
288318 }
289319
290 pub fn nameToTag(name: []const u8) ?@TagType(Os) {
291 const info = @typeInfo(Os);
292 inline for (info.Union.fields) |field| {
293 if (mem.eql(u8, name, field.name)) {
294 return @field(Os, field.name);
295 }
296 }
297 return null;
320 pub fn defaultVersionRange(tag: Tag) Os {
321 return .{
322 .tag = tag,
323 .version_range = VersionRange.default(tag),
324 };
298325 }
299326 };
300327
......@@ -339,11 +366,10 @@ pub const Target = struct {
339366 macabi,
340367
341368 pub fn default(arch: Cpu.Arch, target_os: Os) Abi {
342 switch (arch) {
343 .wasm32, .wasm64 => return .musl,
344 else => {},
369 if (arch.isWasm()) {
370 return .musl;
345371 }
346 switch (target_os) {
372 switch (target_os.tag) {
347373 .freestanding,
348374 .ananas,
349375 .cloudabi,
......@@ -388,40 +414,19 @@ pub const Target = struct {
388414 }
389415 }
390416
391 pub fn nameToTag(text: []const u8) ?Abi {
392 const info = @typeInfo(Abi);
393 inline for (info.Enum.fields) |field| {
394 if (mem.eql(u8, text, field.name)) {
395 return @field(Abi, field.name);
396 }
397 }
398 return null;
399 }
400
401 pub fn parse(text: []const u8, os: *Os) !Abi {
402 var it = mem.separate(text, ".");
403 const tag = nameToTag(it.next().?) orelse return error.UnknownApplicationBinaryInterface;
404 const version_text = it.rest();
405 if (version_text.len != 0) {
406 if (@as(@TagType(Os), os.*) == .linux and tag.isGnu()) {
407 os.linux.glibc = Version.parse(version_text) catch |err| switch (err) {
408 error.Overflow => return error.InvalidGlibcVersion,
409 error.InvalidCharacter => return error.InvalidGlibcVersion,
410 error.InvalidVersion => return error.InvalidGlibcVersion,
411 };
412 } else {
413 return error.InvalidAbiVersion;
414 }
415 }
416 return tag;
417 }
418
419417 pub fn isGnu(abi: Abi) bool {
420418 return switch (abi) {
421419 .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => true,
422420 else => false,
423421 };
424422 }
423
424 pub fn isMusl(abi: Abi) bool {
425 return switch (abi) {
426 .musl, .musleabi, .musleabihf => true,
427 else => false,
428 };
429 }
425430 };
426431
427432 pub const ObjectFormat = enum {
......@@ -909,15 +914,15 @@ pub const Target = struct {
909914 /// TODO add OS version ranges and glibc version
910915 pub fn zigTriple(self: Target, allocator: *mem.Allocator) ![]u8 {
911916 return std.fmt.allocPrint(allocator, "{}-{}-{}", .{
912 @tagName(self.getArch()),
913 @tagName(self.os),
917 @tagName(self.cpu.arch),
918 @tagName(self.os.tag),
914919 @tagName(self.abi),
915920 });
916921 }
917922
918923 /// Returned slice must be freed by the caller.
919924 pub fn vcpkgTriplet(allocator: *mem.Allocator, target: Target, linkage: std.build.VcpkgLinkage) ![]const u8 {
920 const arch = switch (target.getArch()) {
925 const arch = switch (target.cpu.arch) {
921926 .i386 => "x86",
922927 .x86_64 => "x64",
923928
......@@ -957,16 +962,16 @@ pub const Target = struct {
957962
958963 pub fn zigTripleNoSubArch(self: Target, allocator: *mem.Allocator) ![]u8 {
959964 return std.fmt.allocPrint(allocator, "{}-{}-{}", .{
960 @tagName(self.getArch()),
961 @tagName(self.os),
965 @tagName(self.cpu.arch),
966 @tagName(self.os.tag),
962967 @tagName(self.abi),
963968 });
964969 }
965970
966971 pub fn linuxTriple(self: Target, allocator: *mem.Allocator) ![]u8 {
967972 return std.fmt.allocPrint(allocator, "{}-{}-{}", .{
968 @tagName(self.getArch()),
969 @tagName(self.os),
973 @tagName(self.cpu.arch),
974 @tagName(self.os.tag),
970975 @tagName(self.abi),
971976 });
972977 }
......@@ -1017,11 +1022,28 @@ pub const Target = struct {
10171022 diags.arch = arch;
10181023
10191024 const os_name = it.next() orelse return error.MissingOperatingSystem;
1020 var os = try Os.parse(os_name); // var because Abi.parse can update linux.glibc version
1025 var os = try Os.parse(os_name);
10211026 diags.os = os;
10221027
1023 const abi_name = it.next();
1024 const abi = if (abi_name) |n| try Abi.parse(n, &os) else Abi.default(arch, os);
1028 const opt_abi_text = it.next();
1029 const abi = if (opt_abi_text) |abi_text| blk: {
1030 var abi_it = mem.separate(abi_text, ".");
1031 const abi = std.meta.stringToEnum(Abi, abi_it.next().?) orelse
1032 return error.UnknownApplicationBinaryInterface;
1033 const abi_ver_text = abi_it.rest();
1034 if (abi_ver_text.len != 0) {
1035 if (os.tag == .linux and abi.isGnu()) {
1036 os.version_range.linux.glibc = Version.parse(abi_ver_text) catch |err| switch (err) {
1037 error.Overflow => return error.InvalidAbiVersion,
1038 error.InvalidCharacter => return error.InvalidAbiVersion,
1039 error.InvalidVersion => return error.InvalidAbiVersion,
1040 };
1041 } else {
1042 return error.InvalidAbiVersion;
1043 }
1044 }
1045 break :blk abi;
1046 } else Abi.default(arch, os);
10251047 diags.abi = abi;
10261048
10271049 if (it.next() != null) return error.UnexpectedExtraField;
......@@ -1130,25 +1152,6 @@ pub const Target = struct {
11301152 }
11311153 }
11321154
1133 /// Deprecated; access the `os` field directly.
1134 pub fn getOs(self: Target) @TagType(Os) {
1135 return self.os;
1136 }
1137
1138 /// Deprecated; access the `cpu` field directly.
1139 pub fn getCpu(self: Target) Cpu {
1140 return self.cpu;
1141 }
1142
1143 /// Deprecated; access the `abi` field directly.
1144 pub fn getAbi(self: Target) Abi {
1145 return self.abi;
1146 }
1147
1148 pub fn getArch(self: Target) Cpu.Arch {
1149 return self.cpu.arch;
1150 }
1151
11521155 pub fn getObjectFormat(self: Target) ObjectFormat {
11531156 if (self.isWindows() or self.isUefi()) {
11541157 return .coff;
......@@ -1170,28 +1173,25 @@ pub const Target = struct {
11701173 }
11711174
11721175 pub fn isMusl(self: Target) bool {
1173 return switch (self.abi) {
1174 .musl, .musleabi, .musleabihf => true,
1175 else => false,
1176 };
1176 return self.abi.isMusl();
11771177 }
11781178
11791179 pub fn isDarwin(self: Target) bool {
1180 return switch (self.os) {
1180 return switch (self.os.tag) {
11811181 .ios, .macosx, .watchos, .tvos => true,
11821182 else => false,
11831183 };
11841184 }
11851185
11861186 pub fn isWindows(self: Target) bool {
1187 return switch (self.os) {
1187 return switch (self.os.tag) {
11881188 .windows => true,
11891189 else => false,
11901190 };
11911191 }
11921192
11931193 pub fn isLinux(self: Target) bool {
1194 return switch (self.os) {
1194 return switch (self.os.tag) {
11951195 .linux => true,
11961196 else => false,
11971197 };
......@@ -1205,40 +1205,41 @@ pub const Target = struct {
12051205 }
12061206
12071207 pub fn isDragonFlyBSD(self: Target) bool {
1208 return switch (self.os) {
1208 return switch (self.os.tag) {
12091209 .dragonfly => true,
12101210 else => false,
12111211 };
12121212 }
12131213
12141214 pub fn isUefi(self: Target) bool {
1215 return switch (self.os) {
1215 return switch (self.os.tag) {
12161216 .uefi => true,
12171217 else => false,
12181218 };
12191219 }
12201220
12211221 pub fn isWasm(self: Target) bool {
1222 return switch (self.getArch()) {
1223 .wasm32, .wasm64 => true,
1224 else => false,
1225 };
1222 return self.cpu.arch.isWasm();
12261223 }
12271224
12281225 pub fn isFreeBSD(self: Target) bool {
1229 return switch (self.os) {
1226 return switch (self.os.tag) {
12301227 .freebsd => true,
12311228 else => false,
12321229 };
12331230 }
12341231
12351232 pub fn isNetBSD(self: Target) bool {
1236 return switch (self.os) {
1233 return switch (self.os.tag) {
12371234 .netbsd => true,
12381235 else => false,
12391236 };
12401237 }
12411238
1239 pub fn isGnuLibC(self: Target) bool {
1240 return self.os.tag == .linux and self.abi.isGnu();
1241 }
1242
12421243 pub fn wantSharedLibSymLinks(self: Target) bool {
12431244 return !self.isWindows();
12441245 }
......@@ -1248,7 +1249,7 @@ pub const Target = struct {
12481249 }
12491250
12501251 pub fn getArchPtrBitWidth(self: Target) u32 {
1251 switch (self.getArch()) {
1252 switch (self.cpu.arch) {
12521253 .avr,
12531254 .msp430,
12541255 => return 16,
......@@ -1323,8 +1324,8 @@ pub const Target = struct {
13231324 if (@as(@TagType(Target), self) == .Native) return .native;
13241325
13251326 // If the target OS matches the host OS, we can use QEMU to emulate a foreign architecture.
1326 if (self.os == builtin.os) {
1327 return switch (self.getArch()) {
1327 if (self.os.tag == builtin.os.tag) {
1328 return switch (self.cpu.arch) {
13281329 .aarch64 => Executor{ .qemu = "qemu-aarch64" },
13291330 .aarch64_be => Executor{ .qemu = "qemu-aarch64_be" },
13301331 .arm => Executor{ .qemu = "qemu-arm" },
......@@ -1381,13 +1382,10 @@ pub const Target = struct {
13811382 }
13821383
13831384 pub fn hasDynamicLinker(self: Target) bool {
1384 switch (self.getArch()) {
1385 .wasm32,
1386 .wasm64,
1387 => return false,
1388 else => {},
1385 if (self.cpu.arch.isWasm()) {
1386 return false;
13891387 }
1390 switch (self.os) {
1388 switch (self.os.tag) {
13911389 .freestanding,
13921390 .ios,
13931391 .tvos,
......@@ -1424,7 +1422,7 @@ pub const Target = struct {
14241422 defer result.deinit();
14251423
14261424 var is_arm = false;
1427 switch (self.getArch()) {
1425 switch (self.cpu.arch) {
14281426 .arm, .thumb => {
14291427 try result.append("arm");
14301428 is_arm = true;
......@@ -1442,11 +1440,11 @@ pub const Target = struct {
14421440 return result.toOwnedSlice();
14431441 }
14441442
1445 switch (self.os) {
1443 switch (self.os.tag) {
14461444 .freebsd => return mem.dupeZ(a, u8, "/libexec/ld-elf.so.1"),
14471445 .netbsd => return mem.dupeZ(a, u8, "/libexec/ld.elf_so"),
14481446 .dragonfly => return mem.dupeZ(a, u8, "/libexec/ld-elf.so.2"),
1449 .linux => switch (self.getArch()) {
1447 .linux => switch (self.cpu.arch) {
14501448 .i386,
14511449 .sparc,
14521450 .sparcel,
......@@ -1539,7 +1537,7 @@ test "Target.parse" {
15391537 .cpu_features = "x86_64-sse-sse2-avx-cx8",
15401538 });
15411539
1542 std.testing.expect(target.os == .linux);
1540 std.testing.expect(target.os.tag == .linux);
15431541 std.testing.expect(target.abi == .gnu);
15441542 std.testing.expect(target.cpu.arch == .x86_64);
15451543 std.testing.expect(!Target.x86.featureSetHas(target.cpu.features, .sse));
......@@ -1554,10 +1552,29 @@ test "Target.parse" {
15541552 .cpu_features = "generic+v8a",
15551553 });
15561554
1557 std.testing.expect(target.os == .linux);
1555 std.testing.expect(target.os.tag == .linux);
15581556 std.testing.expect(target.abi == .musleabihf);
15591557 std.testing.expect(target.cpu.arch == .arm);
15601558 std.testing.expect(target.cpu.model == &Target.arm.cpu.generic);
15611559 std.testing.expect(Target.arm.featureSetHas(target.cpu.features, .v8a));
15621560 }
1561 {
1562 const target = try Target.parse(.{
1563 .arch_os_abi = "aarch64-linux.3.10...4.4.1-gnu.2.27",
1564 .cpu_features = "generic+v8a",
1565 });
1566
1567 std.testing.expect(target.cpu.arch == .aarch64);
1568 std.testing.expect(target.os.tag == .linux);
1569 std.testing.expect(target.os.version_range.linux.min.major == 3);
1570 std.testing.expect(target.os.version_range.linux.min.minor == 10);
1571 std.testing.expect(target.os.version_range.linux.min.patch == 0);
1572 std.testing.expect(target.os.version_range.linux.max.major == 4);
1573 std.testing.expect(target.os.version_range.linux.max.minor == 4);
1574 std.testing.expect(target.os.version_range.linux.max.patch == 1);
1575 std.testing.expect(target.os.version_range.linux.glibc.major == 2);
1576 std.testing.expect(target.os.version_range.linux.glibc.minor == 27);
1577 std.testing.expect(target.os.version_range.linux.glibc.patch == 0);
1578 std.testing.expect(target.abi == .gnu);
1579 }
15631580}
lib/std/thread.zig+10-10
......@@ -9,14 +9,14 @@ const assert = std.debug.assert;
99pub const Thread = struct {
1010 data: Data,
1111
12 pub const use_pthreads = builtin.os != .windows and builtin.link_libc;
12 pub const use_pthreads = builtin.os.tag != .windows and builtin.link_libc;
1313
1414 /// Represents a kernel thread handle.
1515 /// May be an integer or a pointer depending on the platform.
1616 /// On Linux and POSIX, this is the same as Id.
1717 pub const Handle = if (use_pthreads)
1818 c.pthread_t
19 else switch (builtin.os) {
19 else switch (builtin.os.tag) {
2020 .linux => i32,
2121 .windows => windows.HANDLE,
2222 else => void,
......@@ -25,7 +25,7 @@ pub const Thread = struct {
2525 /// Represents a unique ID per thread.
2626 /// May be an integer or pointer depending on the platform.
2727 /// On Linux and POSIX, this is the same as Handle.
28 pub const Id = switch (builtin.os) {
28 pub const Id = switch (builtin.os.tag) {
2929 .windows => windows.DWORD,
3030 else => Handle,
3131 };
......@@ -35,7 +35,7 @@ pub const Thread = struct {
3535 handle: Thread.Handle,
3636 memory: []align(mem.page_size) u8,
3737 }
38 else switch (builtin.os) {
38 else switch (builtin.os.tag) {
3939 .linux => struct {
4040 handle: Thread.Handle,
4141 memory: []align(mem.page_size) u8,
......@@ -55,7 +55,7 @@ pub const Thread = struct {
5555 if (use_pthreads) {
5656 return c.pthread_self();
5757 } else
58 return switch (builtin.os) {
58 return switch (builtin.os.tag) {
5959 .linux => os.linux.gettid(),
6060 .windows => windows.kernel32.GetCurrentThreadId(),
6161 else => @compileError("Unsupported OS"),
......@@ -83,7 +83,7 @@ pub const Thread = struct {
8383 else => unreachable,
8484 }
8585 os.munmap(self.data.memory);
86 } else switch (builtin.os) {
86 } else switch (builtin.os.tag) {
8787 .linux => {
8888 while (true) {
8989 const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
......@@ -150,7 +150,7 @@ pub const Thread = struct {
150150 const Context = @TypeOf(context);
151151 comptime assert(@typeInfo(@TypeOf(startFn)).Fn.args[0].arg_type.? == Context);
152152
153 if (builtin.os == builtin.Os.windows) {
153 if (builtin.os.tag == .windows) {
154154 const WinThread = struct {
155155 const OuterContext = struct {
156156 thread: Thread,
......@@ -309,7 +309,7 @@ pub const Thread = struct {
309309 os.EINVAL => unreachable,
310310 else => return os.unexpectedErrno(@intCast(usize, err)),
311311 }
312 } else if (builtin.os == .linux) {
312 } else if (builtin.os.tag == .linux) {
313313 var flags: u32 = os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES | os.CLONE_SIGHAND |
314314 os.CLONE_THREAD | os.CLONE_SYSVSEM | os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
315315 os.CLONE_DETACHED;
......@@ -369,11 +369,11 @@ pub const Thread = struct {
369369 };
370370
371371 pub fn cpuCount() CpuCountError!usize {
372 if (builtin.os == .linux) {
372 if (builtin.os.tag == .linux) {
373373 const cpu_set = try os.sched_getaffinity(0);
374374 return @as(usize, os.CPU_COUNT(cpu_set)); // TODO should not need this usize cast
375375 }
376 if (builtin.os == .windows) {
376 if (builtin.os.tag == .windows) {
377377 var system_info: windows.SYSTEM_INFO = undefined;
378378 windows.kernel32.GetSystemInfo(&system_info);
379379 return @intCast(usize, system_info.dwNumberOfProcessors);
lib/std/time.zig+10-8
......@@ -1,5 +1,5 @@
1const builtin = @import("builtin");
21const std = @import("std.zig");
2const builtin = std.builtin;
33const assert = std.debug.assert;
44const testing = std.testing;
55const os = std.os;
......@@ -7,10 +7,12 @@ const math = std.math;
77
88pub const epoch = @import("time/epoch.zig");
99
10const is_windows = std.Target.current.os.tag == .windows;
11
1012/// Spurious wakeups are possible and no precision of timing is guaranteed.
1113/// TODO integrate with evented I/O
1214pub fn sleep(nanoseconds: u64) void {
13 if (builtin.os == .windows) {
15 if (is_windows) {
1416 const ns_per_ms = ns_per_s / ms_per_s;
1517 const big_ms_from_ns = nanoseconds / ns_per_ms;
1618 const ms = math.cast(os.windows.DWORD, big_ms_from_ns) catch math.maxInt(os.windows.DWORD);
......@@ -31,7 +33,7 @@ pub fn timestamp() u64 {
3133/// Get the posix timestamp, UTC, in milliseconds
3234/// TODO audit this function. is it possible to return an error?
3335pub fn milliTimestamp() u64 {
34 if (builtin.os == .windows) {
36 if (is_windows) {
3537 //FileTime has a granularity of 100 nanoseconds
3638 // and uses the NTFS/Windows epoch
3739 var ft: os.windows.FILETIME = undefined;
......@@ -42,7 +44,7 @@ pub fn milliTimestamp() u64 {
4244 const ft64 = (@as(u64, ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
4345 return @divFloor(ft64, hns_per_ms) - -epoch_adj;
4446 }
45 if (builtin.os == .wasi and !builtin.link_libc) {
47 if (builtin.os.tag == .wasi and !builtin.link_libc) {
4648 var ns: os.wasi.timestamp_t = undefined;
4749
4850 // TODO: Verify that precision is ignored
......@@ -102,7 +104,7 @@ pub const Timer = struct {
102104 ///if we used resolution's value when performing the
103105 /// performance counter calc on windows/darwin, it would
104106 /// be less precise
105 frequency: switch (builtin.os) {
107 frequency: switch (builtin.os.tag) {
106108 .windows => u64,
107109 .macosx, .ios, .tvos, .watchos => os.darwin.mach_timebase_info_data,
108110 else => void,
......@@ -127,7 +129,7 @@ pub const Timer = struct {
127129 pub fn start() Error!Timer {
128130 var self: Timer = undefined;
129131
130 if (builtin.os == .windows) {
132 if (is_windows) {
131133 self.frequency = os.windows.QueryPerformanceFrequency();
132134 self.resolution = @divFloor(ns_per_s, self.frequency);
133135 self.start_time = os.windows.QueryPerformanceCounter();
......@@ -172,7 +174,7 @@ pub const Timer = struct {
172174 }
173175
174176 fn clockNative() u64 {
175 if (builtin.os == .windows) {
177 if (is_windows) {
176178 return os.windows.QueryPerformanceCounter();
177179 }
178180 if (comptime std.Target.current.isDarwin()) {
......@@ -184,7 +186,7 @@ pub const Timer = struct {
184186 }
185187
186188 fn nativeDurationToNanos(self: Timer, duration: u64) u64 {
187 if (builtin.os == .windows) {
189 if (is_windows) {
188190 return @divFloor(duration * ns_per_s, self.frequency);
189191 }
190192 if (comptime std.Target.current.isDarwin()) {
lib/std/zig/system.zig+328-2
......@@ -1,11 +1,14 @@
11const std = @import("../std.zig");
2const elf = std.elf;
23const mem = std.mem;
4const fs = std.fs;
35const Allocator = std.mem.Allocator;
46const ArrayList = std.ArrayList;
57const assert = std.debug.assert;
68const process = std.process;
9const Target = std.Target;
710
8const is_windows = std.Target.current.isWindows();
11const is_windows = Target.current.os.tag == .windows;
912
1013pub const NativePaths = struct {
1114 include_dirs: ArrayList([:0]u8),
......@@ -77,7 +80,7 @@ pub const NativePaths = struct {
7780 }
7881
7982 if (!is_windows) {
80 const triple = try std.Target.current.linuxTriple(allocator);
83 const triple = try Target.current.linuxTriple(allocator);
8184
8285 // TODO: $ ld --verbose | grep SEARCH_DIR
8386 // the output contains some paths that end with lib64, maybe include them too?
......@@ -161,3 +164,326 @@ pub const NativePaths = struct {
161164 try array.append(item);
162165 }
163166};
167
168pub const NativeTargetInfo = struct {
169 target: Target,
170 dynamic_linker: ?[:0]u8,
171
172 pub const DetectError = error{
173 OutOfMemory,
174 FileSystem,
175 SystemResources,
176 SymLinkLoop,
177 ProcessFdQuotaExceeded,
178 SystemFdQuotaExceeded,
179 DeviceBusy,
180 };
181
182 /// Detects the native CPU model & features, operating system & version, and C ABI & dynamic linker.
183 /// On Linux, this is additionally responsible for detecting the native glibc version when applicable.
184 pub fn detect(allocator: *Allocator) DetectError!NativeTargetInfo {
185 const arch = Target.current.cpu.arch;
186 const os_tag = Target.current.os.tag;
187
188 // TODO Detect native CPU model & features. Until that is implemented we hard code baseline.
189 const cpu = Target.Cpu.baseline(arch);
190
191 // TODO Detect native operating system version. Until that is implemented we use the minimum version
192 // of the default range.
193 const os = Target.Os.defaultVersionRange(os_tag);
194
195 return detectAbiAndDynamicLinker(allocator, cpu, os);
196 }
197
198 /// Must be the same `Allocator` passed to `detect`.
199 pub fn deinit(self: *NativeTargetInfo, allocator: *Allocator) void {
200 if (self.dynamic_linker) |dl| allocator.free(dl);
201 self.* = undefined;
202 }
203
204 /// First we attempt to use the executable's own binary. If it is dynamically
205 /// linked, then it should answer both the C ABI question and the dynamic linker question.
206 /// If it is statically linked, then we try /usr/bin/env. If that does not provide the answer, then
207 /// we fall back to the defaults.
208 fn detectAbiAndDynamicLinker(
209 allocator: *Allocator,
210 cpu: Target.Cpu,
211 os: Target.Os,
212 ) DetectError!NativeTargetInfo {
213 if (!comptime Target.current.hasDynamicLinker()) {
214 return defaultAbiAndDynamicLinker(allocator, cpu, os);
215 }
216 // The current target's ABI cannot be relied on for this. For example, we may build the zig
217 // compiler for target riscv64-linux-musl and provide a tarball for users to download.
218 // A user could then run that zig compiler on riscv64-linux-gnu. This use case is well-defined
219 // and supported by Zig. But that means that we must detect the system ABI here rather than
220 // relying on `Target.current`.
221 const LdInfo = struct {
222 ld_path: []u8,
223 abi: Target.Abi,
224 };
225 var ld_info_list = std.ArrayList(LdInfo).init(allocator);
226 defer {
227 for (ld_info_list.toSlice()) |ld_info| allocator.free(ld_info.ld_path);
228 ld_info_list.deinit();
229 }
230
231 const all_abis = comptime blk: {
232 assert(@enumToInt(Target.Abi.none) == 0);
233 const fields = std.meta.fields(Target.Abi)[1..];
234 var array: [fields.len]Target.Abi = undefined;
235 inline for (fields) |field, i| {
236 array[i] = @field(Target.Abi, field.name);
237 }
238 break :blk array;
239 };
240 for (all_abis) |abi| {
241 // This may be a nonsensical parameter. We detect this with error.UnknownDynamicLinkerPath and
242 // skip adding it to `ld_info_list`.
243 const target: Target = .{
244 .cpu = cpu,
245 .os = os,
246 .abi = abi,
247 };
248 const standard_ld_path = target.getStandardDynamicLinkerPath(allocator) catch |err| switch (err) {
249 error.OutOfMemory => return error.OutOfMemory,
250 error.UnknownDynamicLinkerPath, error.TargetHasNoDynamicLinker => continue,
251 };
252 errdefer allocator.free(standard_ld_path);
253 try ld_info_list.append(.{
254 .ld_path = standard_ld_path,
255 .abi = abi,
256 });
257 }
258
259 // Best case scenario: the executable is dynamically linked, and we can iterate
260 // over our own shared objects and find a dynamic linker.
261 self_exe: {
262 const lib_paths = try std.process.getSelfExeSharedLibPaths(allocator);
263 defer allocator.free(lib_paths);
264
265 var found_ld_info: LdInfo = undefined;
266 var found_ld_path: [:0]const u8 = undefined;
267
268 // Look for dynamic linker.
269 // This is O(N^M) but typical case here is N=2 and M=10.
270 find_ld: for (lib_paths) |lib_path| {
271 for (ld_info_list.toSlice()) |ld_info| {
272 const standard_ld_basename = fs.path.basename(ld_info.ld_path);
273 if (std.mem.endsWith(u8, lib_path, standard_ld_basename)) {
274 found_ld_info = ld_info;
275 found_ld_path = lib_path;
276 break :find_ld;
277 }
278 }
279 } else break :self_exe;
280
281 // Look for glibc version.
282 var os_adjusted = os;
283 if (Target.current.os.tag == .linux and found_ld_info.abi.isGnu()) {
284 for (lib_paths) |lib_path| {
285 if (std.mem.endsWith(u8, lib_path, glibc_so_basename)) {
286 os_adjusted.version_range.linux.glibc = glibcVerFromSO(lib_path) catch |err| switch (err) {
287 error.UnrecognizedGnuLibCFileName => continue,
288 error.InvalidGnuLibCVersion => continue,
289 error.GnuLibCVersionUnavailable => continue,
290 else => |e| return e,
291 };
292 break;
293 }
294 }
295 }
296
297 return NativeTargetInfo{
298 .target = .{
299 .cpu = cpu,
300 .os = os_adjusted,
301 .abi = found_ld_info.abi,
302 },
303 .dynamic_linker = try mem.dupeZ(allocator, u8, found_ld_path),
304 };
305 }
306
307 // If Zig is statically linked, such as via distributed binary static builds, the above
308 // trick won't work. The next thing we fall back to is the same thing, but for /usr/bin/env.
309 // Since that path is hard-coded into the shebang line of many portable scripts, it's a
310 // reasonably reliable path to check for.
311 return abiAndDynamicLinkerFromUsrBinEnv(allocator, cpu, os) catch |err| switch (err) {
312 error.OutOfMemory => return error.OutOfMemory,
313 error.FileSystem => return error.FileSystem,
314 error.SystemResources => return error.SystemResources,
315 error.SymLinkLoop => return error.SymLinkLoop,
316 error.ProcessFdQuotaExceeded => return error.ProcessFdQuotaExceeded,
317 error.SystemFdQuotaExceeded => return error.SystemFdQuotaExceeded,
318 error.DeviceBusy => return error.DeviceBusy,
319
320 error.UnableToReadElfFile,
321 error.ElfNotADynamicExecutable,
322 error.InvalidElfProgramHeaders,
323 error.InvalidElfClass,
324 error.InvalidElfVersion,
325 error.InvalidElfEndian,
326 error.InvalidElfFile,
327 error.InvalidElfMagic,
328 error.UsrBinEnvNotAvailable,
329 error.Unexpected,
330 // Finally, we fall back on the standard path.
331 => defaultAbiAndDynamicLinker(allocator, cpu, os),
332 };
333 }
334
335 const glibc_so_basename = "libc.so.6";
336
337 fn glibcVerFromSO(so_path: [:0]const u8) !std.builtin.Version {
338 var link_buf: [std.os.PATH_MAX]u8 = undefined;
339 const link_name = std.os.readlinkC(so_path.ptr, &link_buf) catch |err| switch (err) {
340 error.AccessDenied => return error.GnuLibCVersionUnavailable,
341 error.FileSystem => return error.FileSystem,
342 error.SymLinkLoop => return error.SymLinkLoop,
343 error.NameTooLong => unreachable,
344 error.FileNotFound => return error.GnuLibCVersionUnavailable,
345 error.SystemResources => return error.SystemResources,
346 error.NotDir => return error.GnuLibCVersionUnavailable,
347 error.Unexpected => return error.GnuLibCVersionUnavailable,
348 };
349 // example: "libc-2.3.4.so"
350 // example: "libc-2.27.so"
351 const prefix = "libc-";
352 const suffix = ".so";
353 if (!mem.startsWith(u8, link_name, prefix) or !mem.endsWith(u8, link_name, suffix)) {
354 return error.UnrecognizedGnuLibCFileName;
355 }
356 // chop off "libc-" and ".so"
357 const link_name_chopped = link_name[prefix.len .. link_name.len - suffix.len];
358 return std.builtin.Version.parse(link_name_chopped) catch |err| switch (err) {
359 error.Overflow => return error.InvalidGnuLibCVersion,
360 error.InvalidCharacter => return error.InvalidGnuLibCVersion,
361 error.InvalidVersion => return error.InvalidGnuLibCVersion,
362 };
363 }
364
365 fn abiAndDynamicLinkerFromUsrBinEnv(
366 allocator: *Allocator,
367 cpu: Target.Cpu,
368 os: Target.Os,
369 ) !NativeTargetInfo {
370 const env_file = std.fs.openFileAbsoluteC("/usr/bin/env", .{}) catch |err| switch (err) {
371 error.NoSpaceLeft => unreachable,
372 error.NameTooLong => unreachable,
373 error.PathAlreadyExists => unreachable,
374 error.SharingViolation => unreachable,
375 error.InvalidUtf8 => unreachable,
376 error.BadPathName => unreachable,
377 error.PipeBusy => unreachable,
378
379 error.IsDir => return error.UsrBinEnvNotAvailable,
380 error.NotDir => return error.UsrBinEnvNotAvailable,
381 error.AccessDenied => return error.UsrBinEnvNotAvailable,
382 error.NoDevice => return error.UsrBinEnvNotAvailable,
383 error.FileNotFound => return error.UsrBinEnvNotAvailable,
384 error.FileTooBig => return error.UsrBinEnvNotAvailable,
385
386 else => |e| return e,
387 };
388 var hdr_buf: [@sizeOf(elf.Elf64_Ehdr)]u8 align(@alignOf(elf.Elf64_Ehdr)) = undefined;
389 const hdr_bytes_len = try wrapRead(env_file.pread(&hdr_buf, 0));
390 if (hdr_bytes_len < @sizeOf(elf.Elf32_Ehdr)) return error.InvalidElfFile;
391 const hdr32 = @ptrCast(*elf.Elf32_Ehdr, &hdr_buf);
392 const hdr64 = @ptrCast(*elf.Elf64_Ehdr, &hdr_buf);
393 if (!mem.eql(u8, hdr32.e_ident[0..4], "\x7fELF")) return error.InvalidElfMagic;
394 const elf_endian: std.builtin.Endian = switch (hdr32.e_ident[elf.EI_DATA]) {
395 elf.ELFDATA2LSB => .Little,
396 elf.ELFDATA2MSB => .Big,
397 else => return error.InvalidElfEndian,
398 };
399 const need_bswap = elf_endian != std.builtin.endian;
400 if (hdr32.e_ident[elf.EI_VERSION] != 1) return error.InvalidElfVersion;
401
402 const is_64 = switch (hdr32.e_ident[elf.EI_CLASS]) {
403 elf.ELFCLASS32 => false,
404 elf.ELFCLASS64 => true,
405 else => return error.InvalidElfClass,
406 };
407 var phoff = elfInt(is_64, need_bswap, hdr32.e_phoff, hdr64.e_phoff);
408 const phentsize = elfInt(is_64, need_bswap, hdr32.e_phentsize, hdr64.e_phentsize);
409 const phnum = elfInt(is_64, need_bswap, hdr32.e_phnum, hdr64.e_phnum);
410 const shstrndx = elfInt(is_64, need_bswap, hdr32.e_shstrndx, hdr64.e_shstrndx);
411
412 const ph_total_size = std.math.mul(u32, phentsize, phnum) catch |err| switch (err) {
413 error.Overflow => return error.InvalidElfProgramHeaders,
414 };
415 var ph_buf: [16 * @sizeOf(elf.Elf64_Phdr)]u8 align(@alignOf(elf.Elf64_Phdr)) = undefined;
416 var ph_i: u16 = 0;
417 while (ph_i < phnum) {
418 // Reserve some bytes so that we can deref the 64-bit struct fields even when the ELF file is 32-bits.
419 const reserve = @sizeOf(elf.Elf64_Phdr) - @sizeOf(elf.Elf32_Phdr);
420 const read_byte_len = try wrapRead(env_file.pread(ph_buf[0 .. ph_buf.len - reserve], phoff));
421 if (read_byte_len < phentsize) return error.ElfNotADynamicExecutable;
422 var buf_i: usize = 0;
423 while (buf_i < read_byte_len and ph_i < phnum) : ({
424 ph_i += 1;
425 phoff += phentsize;
426 buf_i += phentsize;
427 }) {
428 const ph32 = @ptrCast(*elf.Elf32_Phdr, @alignCast(@alignOf(elf.Elf32_Phdr), &ph_buf[buf_i]));
429 const ph64 = @ptrCast(*elf.Elf64_Phdr, @alignCast(@alignOf(elf.Elf64_Phdr), &ph_buf[buf_i]));
430 const p_type = elfInt(is_64, need_bswap, ph32.p_type, ph64.p_type);
431 switch (p_type) {
432 elf.PT_INTERP => {
433 std.debug.warn("found PT_INTERP\n", .{});
434 },
435 elf.PT_DYNAMIC => {
436 std.debug.warn("found PT_DYNAMIC\n", .{});
437 },
438 else => continue,
439 }
440 }
441 }
442
443 return error.OutOfMemory; // TODO
444 }
445
446 fn wrapRead(res: std.os.ReadError!usize) !usize {
447 return res catch |err| switch (err) {
448 error.OperationAborted => unreachable, // Windows-only
449 error.WouldBlock => unreachable, // Did not request blocking mode
450 error.SystemResources => return error.SystemResources,
451 error.IsDir => return error.UnableToReadElfFile,
452 error.BrokenPipe => return error.UnableToReadElfFile,
453 error.ConnectionResetByPeer => return error.UnableToReadElfFile,
454 error.Unexpected => return error.Unexpected,
455 error.InputOutput => return error.FileSystem,
456 };
457 }
458
459 fn defaultAbiAndDynamicLinker(allocator: *Allocator, cpu: Target.Cpu, os: Target.Os) !NativeTargetInfo {
460 const target: Target = .{
461 .cpu = cpu,
462 .os = os,
463 .abi = Target.Abi.default(cpu.arch, os),
464 };
465 return @as(NativeTargetInfo, .{
466 .target = target,
467 .dynamic_linker = target.getStandardDynamicLinkerPath(allocator) catch |err| switch (err) {
468 error.OutOfMemory => return error.OutOfMemory,
469 error.UnknownDynamicLinkerPath, error.TargetHasNoDynamicLinker => null,
470 },
471 });
472 }
473};
474
475fn elfInt(is_64: bool, need_bswap: bool, int_32: var, int_64: var) @TypeOf(int_64) {
476 if (is_64) {
477 if (need_bswap) {
478 return @byteSwap(@TypeOf(int_64), int_64);
479 } else {
480 return int_64;
481 }
482 } else {
483 if (need_bswap) {
484 return @byteSwap(@TypeOf(int_32), int_32);
485 } else {
486 return int_32;
487 }
488 }
489}
src-self-hosted/c_int.zig+1-1
......@@ -70,7 +70,7 @@ pub const CInt = struct {
7070
7171 pub fn sizeInBits(cint: CInt, self: Target) u32 {
7272 const arch = self.getArch();
73 switch (self.getOs()) {
73 switch (self.os.tag) {
7474 .freestanding, .other => switch (self.getArch()) {
7575 .msp430 => switch (cint.id) {
7676 .Short,
src-self-hosted/clang.zig+1-1
......@@ -1050,7 +1050,7 @@ pub const struct_ZigClangExprEvalResult = extern struct {
10501050
10511051pub const struct_ZigClangAPValue = extern struct {
10521052 Kind: ZigClangAPValueKind,
1053 Data: if (builtin.os == .windows and builtin.abi == .msvc) [52]u8 else [68]u8,
1053 Data: if (builtin.os.tag == .windows and builtin.abi == .msvc) [52]u8 else [68]u8,
10541054};
10551055pub extern fn ZigClangVarDecl_getTypeSourceInfo_getType(self: *const struct_ZigClangVarDecl) struct_ZigClangQualType;
10561056
src-self-hosted/introspect.zig+1-9
......@@ -1,4 +1,4 @@
1// Introspection and determination of system libraries needed by zig.
1//! Introspection and determination of system libraries needed by zig.
22
33const std = @import("std");
44const mem = std.mem;
......@@ -6,14 +6,6 @@ const fs = std.fs;
66
77const warn = std.debug.warn;
88
9pub fn detectDynamicLinker(allocator: *mem.Allocator, target: std.Target) ![:0]u8 {
10 if (target == .Native) {
11 return @import("libc_installation.zig").detectNativeDynamicLinker(allocator);
12 } else {
13 return target.getStandardDynamicLinkerPath(allocator);
14 }
15}
16
179/// Caller must free result
1810pub fn testZigInstallPrefix(allocator: *mem.Allocator, test_path: []const u8) ![]u8 {
1911 const test_zig_dir = try fs.path.join(allocator, &[_][]const u8{ test_path, "lib", "zig" });
src-self-hosted/libc_installation.zig+7-105
......@@ -99,27 +99,27 @@ pub const LibCInstallation = struct {
9999 return error.ParseError;
100100 }
101101 if (self.crt_dir == null and !is_darwin) {
102 try stderr.print("crt_dir may not be empty for {}\n", .{@tagName(Target.current.getOs())});
102 try stderr.print("crt_dir may not be empty for {}\n", .{@tagName(Target.current.os.tag)});
103103 return error.ParseError;
104104 }
105105 if (self.static_crt_dir == null and is_windows and is_gnu) {
106106 try stderr.print("static_crt_dir may not be empty for {}-{}\n", .{
107 @tagName(Target.current.getOs()),
108 @tagName(Target.current.getAbi()),
107 @tagName(Target.current.os.tag),
108 @tagName(Target.current.abi),
109109 });
110110 return error.ParseError;
111111 }
112112 if (self.msvc_lib_dir == null and is_windows and !is_gnu) {
113113 try stderr.print("msvc_lib_dir may not be empty for {}-{}\n", .{
114 @tagName(Target.current.getOs()),
115 @tagName(Target.current.getAbi()),
114 @tagName(Target.current.os.tag),
115 @tagName(Target.current.abi),
116116 });
117117 return error.ParseError;
118118 }
119119 if (self.kernel32_lib_dir == null and is_windows and !is_gnu) {
120120 try stderr.print("kernel32_lib_dir may not be empty for {}-{}\n", .{
121 @tagName(Target.current.getOs()),
122 @tagName(Target.current.getAbi()),
121 @tagName(Target.current.os.tag),
122 @tagName(Target.current.abi),
123123 });
124124 return error.ParseError;
125125 }
......@@ -616,104 +616,6 @@ fn printVerboseInvocation(
616616 }
617617}
618618
619/// Caller owns returned memory.
620pub fn detectNativeDynamicLinker(allocator: *Allocator) error{
621 OutOfMemory,
622 TargetHasNoDynamicLinker,
623 UnknownDynamicLinkerPath,
624}![:0]u8 {
625 if (!comptime Target.current.hasDynamicLinker()) {
626 return error.TargetHasNoDynamicLinker;
627 }
628
629 // The current target's ABI cannot be relied on for this. For example, we may build the zig
630 // compiler for target riscv64-linux-musl and provide a tarball for users to download.
631 // A user could then run that zig compiler on riscv64-linux-gnu. This use case is well-defined
632 // and supported by Zig. But that means that we must detect the system ABI here rather than
633 // relying on `std.Target.current`.
634
635 const LdInfo = struct {
636 ld_path: []u8,
637 abi: Target.Abi,
638 };
639 var ld_info_list = std.ArrayList(LdInfo).init(allocator);
640 defer {
641 for (ld_info_list.toSlice()) |ld_info| allocator.free(ld_info.ld_path);
642 ld_info_list.deinit();
643 }
644
645 const all_abis = comptime blk: {
646 const fields = std.meta.fields(Target.Abi);
647 var array: [fields.len]Target.Abi = undefined;
648 inline for (fields) |field, i| {
649 array[i] = @field(Target.Abi, field.name);
650 }
651 break :blk array;
652 };
653 for (all_abis) |abi| {
654 // This may be a nonsensical parameter. We detect this with error.UnknownDynamicLinkerPath and
655 // skip adding it to `ld_info_list`.
656 const target: Target = .{
657 .Cross = .{
658 .cpu = Target.Cpu.baseline(Target.current.getArch()),
659 .os = Target.current.getOs(),
660 .abi = abi,
661 },
662 };
663 const standard_ld_path = target.getStandardDynamicLinkerPath(allocator) catch |err| switch (err) {
664 error.OutOfMemory => return error.OutOfMemory,
665 error.UnknownDynamicLinkerPath, error.TargetHasNoDynamicLinker => continue,
666 };
667 errdefer allocator.free(standard_ld_path);
668 try ld_info_list.append(.{
669 .ld_path = standard_ld_path,
670 .abi = abi,
671 });
672 }
673
674 // Best case scenario: the zig compiler is dynamically linked, and we can iterate
675 // over our own shared objects and find a dynamic linker.
676 {
677 const lib_paths = try std.process.getSelfExeSharedLibPaths(allocator);
678 defer allocator.free(lib_paths);
679
680 // This is O(N^M) but typical case here is N=2 and M=10.
681 for (lib_paths) |lib_path| {
682 for (ld_info_list.toSlice()) |ld_info| {
683 const standard_ld_basename = fs.path.basename(ld_info.ld_path);
684 if (std.mem.endsWith(u8, lib_path, standard_ld_basename)) {
685 return std.mem.dupeZ(allocator, u8, lib_path);
686 }
687 }
688 }
689 }
690
691 // If Zig is statically linked, such as via distributed binary static builds, the above
692 // trick won't work. What are we left with? Try to run the system C compiler and get
693 // it to tell us the dynamic linker path.
694 // TODO: instead of this, look at the shared libs of /usr/bin/env.
695 for (ld_info_list.toSlice()) |ld_info| {
696 const standard_ld_basename = fs.path.basename(ld_info.ld_path);
697
698 const full_ld_path = ccPrintFileName(.{
699 .allocator = allocator,
700 .search_basename = standard_ld_basename,
701 .want_dirname = .full_path,
702 }) catch |err| switch (err) {
703 error.OutOfMemory => return error.OutOfMemory,
704 error.LibCRuntimeNotFound,
705 error.CCompilerExitCode,
706 error.CCompilerCrashed,
707 error.UnableToSpawnCCompiler,
708 => continue,
709 };
710 return full_ld_path;
711 }
712
713 // Finally, we fall back on the standard path.
714 return Target.current.getStandardDynamicLinkerPath(allocator);
715}
716
717619const Search = struct {
718620 path: []const u8,
719621 version: []const u8,
src-self-hosted/link.zig+2-2
......@@ -515,7 +515,7 @@ const DarwinPlatform = struct {
515515 break :blk ver;
516516 },
517517 .None => blk: {
518 assert(comp.target.getOs() == .macosx);
518 assert(comp.target.os.tag == .macosx);
519519 result.kind = .MacOS;
520520 break :blk "10.14";
521521 },
......@@ -534,7 +534,7 @@ const DarwinPlatform = struct {
534534 }
535535
536536 if (result.kind == .IPhoneOS) {
537 switch (comp.target.getArch()) {
537 switch (comp.target.cpu.arch) {
538538 .i386,
539539 .x86_64,
540540 => result.kind = .IPhoneOSSimulator,
src-self-hosted/main.zig+3-3
......@@ -79,9 +79,9 @@ pub fn main() !void {
7979 } else if (mem.eql(u8, cmd, "libc")) {
8080 return cmdLibC(allocator, cmd_args);
8181 } else if (mem.eql(u8, cmd, "targets")) {
82 // TODO figure out the current target rather than using the target that was specified when
83 // compiling the compiler
84 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout, Target.current);
82 const info = try std.zig.system.NativeTargetInfo.detect(allocator);
83 defer info.deinit(allocator);
84 return @import("print_targets.zig").cmdTargets(allocator, cmd_args, stdout, info.target);
8585 } else if (mem.eql(u8, cmd, "version")) {
8686 return cmdVersion(allocator, cmd_args);
8787 } else if (mem.eql(u8, cmd, "zen")) {
src-self-hosted/print_targets.zig+6-6
......@@ -124,7 +124,7 @@ pub fn cmdTargets(
124124
125125 try jws.objectField("os");
126126 try jws.beginArray();
127 inline for (@typeInfo(Target.Os).Enum.fields) |field| {
127 inline for (@typeInfo(Target.Os.Tag).Enum.fields) |field| {
128128 try jws.arrayElem();
129129 try jws.emitString(field.name);
130130 }
......@@ -201,16 +201,16 @@ pub fn cmdTargets(
201201 try jws.objectField("cpu");
202202 try jws.beginObject();
203203 try jws.objectField("arch");
204 try jws.emitString(@tagName(native_target.getArch()));
204 try jws.emitString(@tagName(native_target.cpu.arch));
205205
206206 try jws.objectField("name");
207 const cpu = native_target.getCpu();
207 const cpu = native_target.cpu;
208208 try jws.emitString(cpu.model.name);
209209
210210 {
211211 try jws.objectField("features");
212212 try jws.beginArray();
213 for (native_target.getArch().allFeaturesList()) |feature, i_usize| {
213 for (native_target.cpu.arch.allFeaturesList()) |feature, i_usize| {
214214 const index = @intCast(Target.Cpu.Feature.Set.Index, i_usize);
215215 if (cpu.features.isEnabled(index)) {
216216 try jws.arrayElem();
......@@ -222,9 +222,9 @@ pub fn cmdTargets(
222222 try jws.endObject();
223223 }
224224 try jws.objectField("os");
225 try jws.emitString(@tagName(native_target.getOs()));
225 try jws.emitString(@tagName(native_target.os.tag));
226226 try jws.objectField("abi");
227 try jws.emitString(@tagName(native_target.getAbi()));
227 try jws.emitString(@tagName(native_target.abi));
228228 // TODO implement native glibc version detection in self-hosted
229229 try jws.endObject();
230230
src-self-hosted/stage2.zig+271-133
......@@ -110,6 +110,8 @@ const Error = extern enum {
110110 WindowsSdkNotFound,
111111 UnknownDynamicLinkerPath,
112112 TargetHasNoDynamicLinker,
113 InvalidAbiVersion,
114 InvalidOperatingSystemVersion,
113115};
114116
115117const FILE = std.c.FILE;
......@@ -633,11 +635,11 @@ export fn stage2_cmd_targets(zig_triple: [*:0]const u8) c_int {
633635
634636fn cmdTargets(zig_triple: [*:0]const u8) !void {
635637 var target = try Target.parse(.{ .arch_os_abi = mem.toSliceConst(u8, zig_triple) });
636 target.Cross.cpu = blk: {
638 target.cpu = blk: {
637639 const llvm = @import("llvm.zig");
638640 const llvm_cpu_name = llvm.GetHostCPUName();
639641 const llvm_cpu_features = llvm.GetNativeFeatures();
640 break :blk try detectNativeCpuWithLLVM(target.getArch(), llvm_cpu_name, llvm_cpu_features);
642 break :blk try detectNativeCpuWithLLVM(target.cpu.arch, llvm_cpu_name, llvm_cpu_features);
641643 };
642644 return @import("print_targets.zig").cmdTargets(
643645 std.heap.c_allocator,
......@@ -662,6 +664,14 @@ export fn stage2_target_parse(
662664 error.MissingArchitecture => return .MissingArchitecture,
663665 error.InvalidLlvmCpuFeaturesFormat => return .InvalidLlvmCpuFeaturesFormat,
664666 error.UnexpectedExtraField => return .SemanticAnalyzeFail,
667 error.InvalidAbiVersion => return .InvalidAbiVersion,
668 error.InvalidOperatingSystemVersion => return .InvalidOperatingSystemVersion,
669 error.FileSystem => return .FileSystem,
670 error.SymLinkLoop => return .SymLinkLoop,
671 error.SystemResources => return .SystemResources,
672 error.ProcessFdQuotaExceeded => return .ProcessFdQuotaExceeded,
673 error.SystemFdQuotaExceeded => return .SystemFdQuotaExceeded,
674 error.DeviceBusy => return .DeviceBusy,
665675 };
666676 return .None;
667677}
......@@ -671,108 +681,48 @@ fn stage2TargetParse(
671681 zig_triple_oz: ?[*:0]const u8,
672682 mcpu_oz: ?[*:0]const u8,
673683) !void {
674 const target: Target = if (zig_triple_oz) |zig_triple_z| blk: {
684 const target: std.build.Target = if (zig_triple_oz) |zig_triple_z| blk: {
675685 const zig_triple = mem.toSliceConst(u8, zig_triple_z);
676686 const mcpu = if (mcpu_oz) |mcpu_z| mem.toSliceConst(u8, mcpu_z) else "baseline";
677687 var diags: std.Target.ParseOptions.Diagnostics = .{};
678 break :blk Target.parse(.{
679 .arch_os_abi = zig_triple,
680 .cpu_features = mcpu,
681 .diagnostics = &diags,
682 }) catch |err| switch (err) {
683 error.UnknownCpu => {
684 std.debug.warn("Unknown CPU: '{}'\nAvailable CPUs for architecture '{}':\n", .{
685 diags.cpu_name.?,
686 @tagName(diags.arch.?),
687 });
688 for (diags.arch.?.allCpuModels()) |cpu| {
689 std.debug.warn(" {}\n", .{cpu.name});
690 }
691 process.exit(1);
692 },
693 error.UnknownCpuFeature => {
694 std.debug.warn(
695 \\Unknown CPU feature: '{}'
696 \\Available CPU features for architecture '{}':
697 \\
698 , .{
699 diags.unknown_feature_name,
700 @tagName(diags.arch.?),
701 });
702 for (diags.arch.?.allFeaturesList()) |feature| {
703 std.debug.warn(" {}: {}\n", .{ feature.name, feature.description });
704 }
705 process.exit(1);
688 break :blk std.build.Target{
689 .Cross = Target.parse(.{
690 .arch_os_abi = zig_triple,
691 .cpu_features = mcpu,
692 .diagnostics = &diags,
693 }) catch |err| switch (err) {
694 error.UnknownCpu => {
695 std.debug.warn("Unknown CPU: '{}'\nAvailable CPUs for architecture '{}':\n", .{
696 diags.cpu_name.?,
697 @tagName(diags.arch.?),
698 });
699 for (diags.arch.?.allCpuModels()) |cpu| {
700 std.debug.warn(" {}\n", .{cpu.name});
701 }
702 process.exit(1);
703 },
704 error.UnknownCpuFeature => {
705 std.debug.warn(
706 \\Unknown CPU feature: '{}'
707 \\Available CPU features for architecture '{}':
708 \\
709 , .{
710 diags.unknown_feature_name,
711 @tagName(diags.arch.?),
712 });
713 for (diags.arch.?.allFeaturesList()) |feature| {
714 std.debug.warn(" {}: {}\n", .{ feature.name, feature.description });
715 }
716 process.exit(1);
717 },
718 else => |e| return e,
706719 },
707 else => |e| return e,
708720 };
709 } else Target.Native;
721 } else std.build.Target.Native;
710722
711723 try stage1_target.fromTarget(target);
712724}
713725
714fn initStage1TargetCpuFeatures(stage1_target: *Stage2Target, cpu: Target.Cpu) !void {
715 const allocator = std.heap.c_allocator;
716 const cache_hash = try std.fmt.allocPrint0(allocator, "{}\n{}", .{
717 cpu.model.name,
718 cpu.features.asBytes(),
719 });
720 errdefer allocator.free(cache_hash);
721
722 const generic_arch_name = cpu.arch.genericName();
723 var builtin_str_buffer = try std.Buffer.allocPrint(allocator,
724 \\Cpu{{
725 \\ .arch = .{},
726 \\ .model = &Target.{}.cpu.{},
727 \\ .features = Target.{}.featureSet(&[_]Target.{}.Feature{{
728 \\
729 , .{
730 @tagName(cpu.arch),
731 generic_arch_name,
732 cpu.model.name,
733 generic_arch_name,
734 generic_arch_name,
735 });
736 defer builtin_str_buffer.deinit();
737
738 var llvm_features_buffer = try std.Buffer.initSize(allocator, 0);
739 defer llvm_features_buffer.deinit();
740
741 for (cpu.arch.allFeaturesList()) |feature, index_usize| {
742 const index = @intCast(Target.Cpu.Feature.Set.Index, index_usize);
743 const is_enabled = cpu.features.isEnabled(index);
744
745 if (feature.llvm_name) |llvm_name| {
746 const plus_or_minus = "-+"[@boolToInt(is_enabled)];
747 try llvm_features_buffer.appendByte(plus_or_minus);
748 try llvm_features_buffer.append(llvm_name);
749 try llvm_features_buffer.append(",");
750 }
751
752 if (is_enabled) {
753 // TODO some kind of "zig identifier escape" function rather than
754 // unconditionally using @"" syntax
755 try builtin_str_buffer.append(" .@\"");
756 try builtin_str_buffer.append(feature.name);
757 try builtin_str_buffer.append("\",\n");
758 }
759 }
760
761 try builtin_str_buffer.append(
762 \\ }),
763 \\};
764 \\
765 );
766
767 assert(mem.endsWith(u8, llvm_features_buffer.toSliceConst(), ","));
768 llvm_features_buffer.shrink(llvm_features_buffer.len() - 1);
769
770 stage1_target.llvm_cpu_name = if (cpu.model.llvm_name) |s| s.ptr else null;
771 stage1_target.llvm_cpu_features = llvm_features_buffer.toOwnedSlice().ptr;
772 stage1_target.builtin_str = builtin_str_buffer.toOwnedSlice().ptr;
773 stage1_target.cache_hash = cache_hash.ptr;
774}
775
776726// ABI warning
777727const Stage2LibCInstallation = extern struct {
778728 include_dir: [*:0]const u8,
......@@ -952,10 +902,13 @@ const Stage2Target = extern struct {
952902
953903 llvm_cpu_name: ?[*:0]const u8,
954904 llvm_cpu_features: ?[*:0]const u8,
955 builtin_str: ?[*:0]const u8,
905 cpu_builtin_str: ?[*:0]const u8,
956906 cache_hash: ?[*:0]const u8,
907 os_builtin_str: ?[*:0]const u8,
908
909 dynamic_linker: ?[*:0]const u8,
957910
958 fn toTarget(in_target: Stage2Target) Target {
911 fn toTarget(in_target: Stage2Target) std.build.Target {
959912 if (in_target.is_native) return .Native;
960913
961914 const in_arch = in_target.arch - 1; // skip over ZigLLVM_UnknownArch
......@@ -965,39 +918,244 @@ const Stage2Target = extern struct {
965918 return .{
966919 .Cross = .{
967920 .cpu = Target.Cpu.baseline(enumInt(Target.Cpu.Arch, in_arch)),
968 .os = enumInt(Target.Os, in_os),
921 .os = Target.Os.defaultVersionRange(enumInt(Target.Os.Tag, in_os)),
969922 .abi = enumInt(Target.Abi, in_abi),
970923 },
971924 };
972925 }
973926
974 fn fromTarget(self: *Stage2Target, target: Target) !void {
975 const cpu = switch (target) {
927 fn fromTarget(self: *Stage2Target, build_target: std.build.Target) !void {
928 const allocator = std.heap.c_allocator;
929 var dynamic_linker: ?[*:0]u8 = null;
930 const target = switch (build_target) {
976931 .Native => blk: {
977 // TODO self-host CPU model and feature detection instead of relying on LLVM
932 const info = try std.zig.system.NativeTargetInfo.detect(std.heap.c_allocator);
933 if (info.dynamic_linker) |dl| {
934 dynamic_linker = dl.ptr;
935 }
936
937 // TODO we want to just use info.target but implementing CPU model & feature detection is todo
938 // so here we rely on LLVM
978939 const llvm = @import("llvm.zig");
979940 const llvm_cpu_name = llvm.GetHostCPUName();
980941 const llvm_cpu_features = llvm.GetNativeFeatures();
981 break :blk try detectNativeCpuWithLLVM(target.getArch(), llvm_cpu_name, llvm_cpu_features);
942 const arch = std.Target.current.cpu.arch;
943 var t = info.target;
944 t.cpu = try detectNativeCpuWithLLVM(arch, llvm_cpu_name, llvm_cpu_features);
945 break :blk t;
982946 },
983 .Cross => target.getCpu(),
947 .Cross => |t| t,
984948 };
949
950 var cache_hash = try std.Buffer.allocPrint(allocator, "{}\n{}\n", .{
951 target.cpu.model.name,
952 target.cpu.features.asBytes(),
953 });
954 defer cache_hash.deinit();
955
956 const generic_arch_name = target.cpu.arch.genericName();
957 var cpu_builtin_str_buffer = try std.Buffer.allocPrint(allocator,
958 \\Cpu{{
959 \\ .arch = .{},
960 \\ .model = &Target.{}.cpu.{},
961 \\ .features = Target.{}.featureSet(&[_]Target.{}.Feature{{
962 \\
963 , .{
964 @tagName(target.cpu.arch),
965 generic_arch_name,
966 target.cpu.model.name,
967 generic_arch_name,
968 generic_arch_name,
969 });
970 defer cpu_builtin_str_buffer.deinit();
971
972 var llvm_features_buffer = try std.Buffer.initSize(allocator, 0);
973 defer llvm_features_buffer.deinit();
974
975 for (target.cpu.arch.allFeaturesList()) |feature, index_usize| {
976 const index = @intCast(Target.Cpu.Feature.Set.Index, index_usize);
977 const is_enabled = target.cpu.features.isEnabled(index);
978
979 if (feature.llvm_name) |llvm_name| {
980 const plus_or_minus = "-+"[@boolToInt(is_enabled)];
981 try llvm_features_buffer.appendByte(plus_or_minus);
982 try llvm_features_buffer.append(llvm_name);
983 try llvm_features_buffer.append(",");
984 }
985
986 if (is_enabled) {
987 // TODO some kind of "zig identifier escape" function rather than
988 // unconditionally using @"" syntax
989 try cpu_builtin_str_buffer.append(" .@\"");
990 try cpu_builtin_str_buffer.append(feature.name);
991 try cpu_builtin_str_buffer.append("\",\n");
992 }
993 }
994
995 try cpu_builtin_str_buffer.append(
996 \\ }),
997 \\};
998 \\
999 );
1000
1001 assert(mem.endsWith(u8, llvm_features_buffer.toSliceConst(), ","));
1002 llvm_features_buffer.shrink(llvm_features_buffer.len() - 1);
1003
1004 var os_builtin_str_buffer = try std.Buffer.allocPrint(allocator,
1005 \\Os{{
1006 \\ .tag = .{},
1007 \\ .version_range = .{{
1008 , .{@tagName(target.os.tag)});
1009 defer os_builtin_str_buffer.deinit();
1010
1011 // We'll re-use the OS version range builtin string for the cache hash.
1012 const os_builtin_str_ver_start_index = os_builtin_str_buffer.len();
1013
1014 @setEvalBranchQuota(2000);
1015 switch (target.os.tag) {
1016 .freestanding,
1017 .ananas,
1018 .cloudabi,
1019 .dragonfly,
1020 .fuchsia,
1021 .ios,
1022 .kfreebsd,
1023 .lv2,
1024 .solaris,
1025 .haiku,
1026 .minix,
1027 .rtems,
1028 .nacl,
1029 .cnk,
1030 .aix,
1031 .cuda,
1032 .nvcl,
1033 .amdhsa,
1034 .ps4,
1035 .elfiamcu,
1036 .tvos,
1037 .watchos,
1038 .mesa3d,
1039 .contiki,
1040 .amdpal,
1041 .hermit,
1042 .hurd,
1043 .wasi,
1044 .emscripten,
1045 .uefi,
1046 .other,
1047 => try os_builtin_str_buffer.append(" .none = {} }\n"),
1048
1049 .freebsd,
1050 .macosx,
1051 .netbsd,
1052 .openbsd,
1053 => try os_builtin_str_buffer.print(
1054 \\.semver = .{{
1055 \\ .min = .{{
1056 \\ .major = {},
1057 \\ .minor = {},
1058 \\ .patch = {},
1059 \\ }},
1060 \\ .max = .{{
1061 \\ .major = {},
1062 \\ .minor = {},
1063 \\ .patch = {},
1064 \\ }},
1065 \\ }}}},
1066 , .{
1067 target.os.version_range.semver.min.major,
1068 target.os.version_range.semver.min.minor,
1069 target.os.version_range.semver.min.patch,
1070
1071 target.os.version_range.semver.max.major,
1072 target.os.version_range.semver.max.minor,
1073 target.os.version_range.semver.max.patch,
1074 }),
1075
1076 .linux => try os_builtin_str_buffer.print(
1077 \\.linux = .{{
1078 \\ .range = .{{
1079 \\ .min = .{{
1080 \\ .major = {},
1081 \\ .minor = {},
1082 \\ .patch = {},
1083 \\ }},
1084 \\ .max = .{{
1085 \\ .major = {},
1086 \\ .minor = {},
1087 \\ .patch = {},
1088 \\ }},
1089 \\ }},
1090 \\ .glibc = .{{
1091 \\ .major = {},
1092 \\ .minor = {},
1093 \\ .patch = {},
1094 \\ }},
1095 \\ }}}},
1096 \\
1097 , .{
1098 target.os.version_range.linux.range.min.major,
1099 target.os.version_range.linux.range.min.minor,
1100 target.os.version_range.linux.range.min.patch,
1101
1102 target.os.version_range.linux.range.max.major,
1103 target.os.version_range.linux.range.max.minor,
1104 target.os.version_range.linux.range.max.patch,
1105
1106 target.os.version_range.linux.glibc.major,
1107 target.os.version_range.linux.glibc.minor,
1108 target.os.version_range.linux.glibc.patch,
1109 }),
1110
1111 .windows => try os_builtin_str_buffer.print(
1112 \\.semver = .{{
1113 \\ .min = .{},
1114 \\ .max = .{},
1115 \\ }}}},
1116 , .{
1117 @tagName(target.os.version_range.windows.min),
1118 @tagName(target.os.version_range.windows.max),
1119 }),
1120 }
1121 try os_builtin_str_buffer.append("};\n");
1122
1123 try cache_hash.append(
1124 os_builtin_str_buffer.toSlice()[os_builtin_str_ver_start_index..os_builtin_str_buffer.len()],
1125 );
1126
1127 const glibc_version = if (target.isGnuLibC()) blk: {
1128 const stage1_glibc = try std.heap.c_allocator.create(Stage2GLibCVersion);
1129 const stage2_glibc = target.os.version_range.linux.glibc;
1130 stage1_glibc.* = .{
1131 .major = stage2_glibc.major,
1132 .minor = stage2_glibc.minor,
1133 .patch = stage2_glibc.patch,
1134 };
1135 break :blk stage1_glibc;
1136 } else null;
1137
9851138 self.* = .{
986 .arch = @enumToInt(target.getArch()) + 1, // skip over ZigLLVM_UnknownArch
1139 .arch = @enumToInt(target.cpu.arch) + 1, // skip over ZigLLVM_UnknownArch
9871140 .vendor = 0,
988 .os = @enumToInt(target.getOs()),
989 .abi = @enumToInt(target.getAbi()),
990 .llvm_cpu_name = null,
991 .llvm_cpu_features = null,
992 .builtin_str = null,
993 .cache_hash = null,
994 .is_native = target == .Native,
995 .glibc_version = null,
1141 .os = @enumToInt(target.os.tag),
1142 .abi = @enumToInt(target.abi),
1143 .llvm_cpu_name = if (target.cpu.model.llvm_name) |s| s.ptr else null,
1144 .llvm_cpu_features = llvm_features_buffer.toOwnedSlice().ptr,
1145 .cpu_builtin_str = cpu_builtin_str_buffer.toOwnedSlice().ptr,
1146 .os_builtin_str = os_builtin_str_buffer.toOwnedSlice().ptr,
1147 .cache_hash = cache_hash.toOwnedSlice().ptr,
1148 .is_native = build_target == .Native,
1149 .glibc_version = glibc_version,
1150 .dynamic_linker = dynamic_linker,
9961151 };
997 try initStage1TargetCpuFeatures(self, cpu);
9981152 }
9991153};
10001154
1155fn enumInt(comptime Enum: type, int: c_int) Enum {
1156 return @intToEnum(Enum, @intCast(@TagType(Enum), int));
1157}
1158
10011159// ABI warning
10021160const Stage2GLibCVersion = extern struct {
10031161 major: u32,
......@@ -1005,26 +1163,6 @@ const Stage2GLibCVersion = extern struct {
10051163 patch: u32,
10061164};
10071165
1008// ABI warning
1009export fn stage2_detect_dynamic_linker(in_target: *const Stage2Target, out_ptr: *[*:0]u8, out_len: *usize) Error {
1010 const target = in_target.toTarget();
1011 const result = @import("introspect.zig").detectDynamicLinker(
1012 std.heap.c_allocator,
1013 target,
1014 ) catch |err| switch (err) {
1015 error.OutOfMemory => return .OutOfMemory,
1016 error.UnknownDynamicLinkerPath => return .UnknownDynamicLinkerPath,
1017 error.TargetHasNoDynamicLinker => return .TargetHasNoDynamicLinker,
1018 };
1019 out_ptr.* = result.ptr;
1020 out_len.* = result.len;
1021 return .None;
1022}
1023
1024fn enumInt(comptime Enum: type, int: c_int) Enum {
1025 return @intToEnum(Enum, @intCast(@TagType(Enum), int));
1026}
1027
10281166// ABI warning
10291167const Stage2NativePaths = extern struct {
10301168 include_dirs_ptr: [*][*:0]u8,
src-self-hosted/util.zig-22
......@@ -34,25 +34,3 @@ pub fn initializeAllTargets() void {
3434 llvm.InitializeAllAsmPrinters();
3535 llvm.InitializeAllAsmParsers();
3636}
37
38pub fn getTriple(allocator: *std.mem.Allocator, self: std.Target) !std.Buffer {
39 var result = try std.Buffer.initSize(allocator, 0);
40 errdefer result.deinit();
41
42 // LLVM WebAssembly output support requires the target to be activated at
43 // build type with -DCMAKE_LLVM_EXPIERMENTAL_TARGETS_TO_BUILD=WebAssembly.
44 //
45 // LLVM determines the output format based on the abi suffix,
46 // defaulting to an object based on the architecture. The default format in
47 // LLVM 6 sets the wasm arch output incorrectly to ELF. We need to
48 // explicitly set this ourself in order for it to work.
49 //
50 // This is fixed in LLVM 7 and you will be able to get wasm output by
51 // using the target triple `wasm32-unknown-unknown-unknown`.
52 const env_name = if (self.isWasm()) "wasm" else @tagName(self.getAbi());
53
54 var out = &std.io.BufferOutStream.init(&result).stream;
55 try out.print("{}-unknown-{}-{}", .{ @tagName(self.getArch()), @tagName(self.getOs()), env_name });
56
57 return result;
58}
src/codegen.cpp+14-24
......@@ -4483,7 +4483,7 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutableGen *execu
44834483
44844484 if (!type_has_bits(field->type_entry)) {
44854485 ZigType *tag_type = union_type->data.unionation.tag_type;
4486 if (!instruction->initializing || !type_has_bits(tag_type))
4486 if (!instruction->initializing || tag_type == nullptr || !type_has_bits(tag_type))
44874487 return nullptr;
44884488
44894489 // The field has no bits but we still have to change the discriminant
......@@ -8543,25 +8543,24 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
85438543 buf_appendf(contents, "pub const link_mode = LinkMode.%s;\n", link_type);
85448544 buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
85458545 buf_appendf(contents, "pub const single_threaded = %s;\n", bool_to_str(g->is_single_threaded));
8546 buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);
8546 buf_append_str(contents, "/// Deprecated: use `std.Target.cpu.arch`\n");
85478547 buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch);
85488548 buf_appendf(contents, "pub const abi = Abi.%s;\n", cur_abi);
85498549 {
85508550 buf_append_str(contents, "pub const cpu: Cpu = ");
8551 if (g->zig_target->builtin_str != nullptr) {
8552 buf_append_str(contents, g->zig_target->builtin_str);
8551 if (g->zig_target->cpu_builtin_str != nullptr) {
8552 buf_append_str(contents, g->zig_target->cpu_builtin_str);
85538553 } else {
8554 buf_append_str(contents, "Target.Cpu.baseline(arch);\n");
8554 buf_appendf(contents, "Target.Cpu.baseline(.%s);\n", cur_arch);
85558555 }
85568556 }
8557 if (g->libc_link_lib != nullptr && g->zig_target->glibc_version != nullptr) {
8558 buf_appendf(contents,
8559 "pub const glibc_version: ?Version = Version{.major = %d, .minor = %d, .patch = %d};\n",
8560 g->zig_target->glibc_version->major,
8561 g->zig_target->glibc_version->minor,
8562 g->zig_target->glibc_version->patch);
8563 } else {
8564 buf_appendf(contents, "pub const glibc_version: ?Version = null;\n");
8557 {
8558 buf_append_str(contents, "pub const os = ");
8559 if (g->zig_target->os_builtin_str != nullptr) {
8560 buf_append_str(contents, g->zig_target->os_builtin_str);
8561 } else {
8562 buf_appendf(contents, "Target.Os.defaultVersionRange(.%s);\n", cur_os);
8563 }
85658564 }
85668565 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);
85678566 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));
......@@ -8867,8 +8866,6 @@ static void init(CodeGen *g) {
88678866}
88688867
88698868static void detect_dynamic_linker(CodeGen *g) {
8870 Error err;
8871
88728869 if (g->dynamic_linker_path != nullptr)
88738870 return;
88748871 if (!g->have_dynamic_link)
......@@ -8876,16 +8873,9 @@ static void detect_dynamic_linker(CodeGen *g) {
88768873 if (g->out_type == OutTypeObj || (g->out_type == OutTypeLib && !g->is_dynamic))
88778874 return;
88788875
8879 char *dynamic_linker_ptr;
8880 size_t dynamic_linker_len;
8881 if ((err = stage2_detect_dynamic_linker(g->zig_target, &dynamic_linker_ptr, &dynamic_linker_len))) {
8882 if (err == ErrorTargetHasNoDynamicLinker) return;
8883 fprintf(stderr, "Unable to detect dynamic linker: %s\n", err_str(err));
8884 exit(1);
8876 if (g->zig_target->dynamic_linker != nullptr) {
8877 g->dynamic_linker_path = buf_create_from_str(g->zig_target->dynamic_linker);
88858878 }
8886 g->dynamic_linker_path = buf_create_from_mem(dynamic_linker_ptr, dynamic_linker_len);
8887 // Skips heap::c_allocator because the memory is allocated by stage2 library.
8888 free(dynamic_linker_ptr);
88898879}
88908880
88918881static void detect_libc(CodeGen *g) {
src/error.cpp+2
......@@ -81,6 +81,8 @@ const char *err_str(Error err) {
8181 case ErrorWindowsSdkNotFound: return "Windows SDK not found";
8282 case ErrorUnknownDynamicLinkerPath: return "unknown dynamic linker path";
8383 case ErrorTargetHasNoDynamicLinker: return "target has no dynamic linker";
84 case ErrorInvalidAbiVersion: return "invalid C ABI version";
85 case ErrorInvalidOperatingSystemVersion: return "invalid operating system version";
8486 }
8587 return "(invalid error)";
8688}
src/main.cpp+1-28
......@@ -89,8 +89,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
8989 " --single-threaded source may assume it is only used single-threaded\n"
9090 " -dynamic create a shared library (.so; .dll; .dylib)\n"
9191 " --strip exclude debug symbols\n"
92 " -target [name] <arch><sub>-<os>-<abi> see the targets command\n"
93 " -target-glibc [version] target a specific glibc version (default: 2.17)\n"
92 " -target [name] <arch>-<os>-<abi> see the targets command\n"
9493 " --verbose-tokenize enable compiler debug output for tokenization\n"
9594 " --verbose-ast enable compiler debug output for AST parsing\n"
9695 " --verbose-link enable compiler debug output for linking\n"
......@@ -419,7 +418,6 @@ static int main0(int argc, char **argv) {
419418 const char *mios_version_min = nullptr;
420419 const char *linker_script = nullptr;
421420 Buf *version_script = nullptr;
422 const char *target_glibc = nullptr;
423421 ZigList<const char *> rpath_list = {0};
424422 bool each_lib_rpath = false;
425423 ZigList<const char *> objects = {0};
......@@ -853,8 +851,6 @@ static int main0(int argc, char **argv) {
853851 linker_script = argv[i];
854852 } else if (strcmp(arg, "--version-script") == 0) {
855853 version_script = buf_create_from_str(argv[i]);
856 } else if (strcmp(arg, "-target-glibc") == 0) {
857 target_glibc = argv[i];
858854 } else if (strcmp(arg, "-rpath") == 0) {
859855 rpath_list.append(argv[i]);
860856 } else if (strcmp(arg, "--test-filter") == 0) {
......@@ -982,29 +978,6 @@ static int main0(int argc, char **argv) {
982978 "See `%s targets` to display valid targets.\n", err_str(err), arg0);
983979 return print_error_usage(arg0);
984980 }
985 if (target_is_glibc(&target)) {
986 target.glibc_version = heap::c_allocator.create<ZigGLibCVersion>();
987
988 if (target_glibc != nullptr) {
989 if ((err = target_parse_glibc_version(target.glibc_version, target_glibc))) {
990 fprintf(stderr, "invalid glibc version '%s': %s\n", target_glibc, err_str(err));
991 return print_error_usage(arg0);
992 }
993 } else {
994 target_init_default_glibc_version(&target);
995#if defined(ZIG_OS_LINUX)
996 if (target.is_native) {
997 // TODO self-host glibc version detection, and then this logic can go away
998 if ((err = glibc_detect_native_version(target.glibc_version))) {
999 // Fall back to the default version.
1000 }
1001 }
1002#endif
1003 }
1004 } else if (target_glibc != nullptr) {
1005 fprintf(stderr, "'%s' is not a glibc-compatible target", target_string);
1006 return print_error_usage(arg0);
1007 }
1008981
1009982 Buf zig_triple_buf = BUF_INIT;
1010983 target_triple_zig(&zig_triple_buf, &target);
src/stage2.cpp-8
......@@ -100,13 +100,11 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
100100 if (mcpu == nullptr) {
101101 target->llvm_cpu_name = ZigLLVMGetHostCPUName();
102102 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();
103 target->builtin_str = "Target.Cpu.baseline(arch);\n";
104103 target->cache_hash = "native\n\n";
105104 } else if (strcmp(mcpu, "baseline") == 0) {
106105 target->is_native = false;
107106 target->llvm_cpu_name = "";
108107 target->llvm_cpu_features = "";
109 target->builtin_str = "Target.Cpu.baseline(arch);\n";
110108 target->cache_hash = "baseline\n\n";
111109 } else {
112110 const char *msg = "stage0 can't handle CPU/features in the target";
......@@ -148,7 +146,6 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
148146 const char *msg = "stage0 can't handle CPU/features in the target";
149147 stage2_panic(msg, strlen(msg));
150148 }
151 target->builtin_str = "Target.Cpu.baseline(arch);\n";
152149 target->cache_hash = "\n\n";
153150 }
154151
......@@ -186,11 +183,6 @@ enum Error stage2_libc_find_native(struct Stage2LibCInstallation *libc) {
186183 stage2_panic(msg, strlen(msg));
187184}
188185
189enum Error stage2_detect_dynamic_linker(const struct ZigTarget *target, char **out_ptr, size_t *out_len) {
190 const char *msg = "stage0 called stage2_detect_dynamic_linker";
191 stage2_panic(msg, strlen(msg));
192}
193
194186enum Error stage2_detect_native_paths(struct Stage2NativePaths *native_paths) {
195187 native_paths->include_dirs_ptr = nullptr;
196188 native_paths->include_dirs_len = 0;
src/stage2.h+5-5
......@@ -103,6 +103,8 @@ enum Error {
103103 ErrorWindowsSdkNotFound,
104104 ErrorUnknownDynamicLinkerPath,
105105 ErrorTargetHasNoDynamicLinker,
106 ErrorInvalidAbiVersion,
107 ErrorInvalidOperatingSystemVersion,
106108};
107109
108110// ABI warning
......@@ -290,14 +292,12 @@ struct ZigTarget {
290292
291293 const char *llvm_cpu_name;
292294 const char *llvm_cpu_features;
293 const char *builtin_str;
295 const char *cpu_builtin_str;
294296 const char *cache_hash;
297 const char *os_builtin_str;
298 const char *dynamic_linker;
295299};
296300
297// ABI warning
298ZIG_EXTERN_C enum Error stage2_detect_dynamic_linker(const struct ZigTarget *target,
299 char **out_ptr, size_t *out_len);
300
301301// ABI warning
302302ZIG_EXTERN_C enum Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, const char *mcpu);
303303
test/stage1/behavior/asm.zig+4-3
......@@ -1,9 +1,10 @@
11const std = @import("std");
2const config = @import("builtin");
32const expect = std.testing.expect;
43
4const is_x86_64_linux = std.Target.current.cpu.arch == .x86_64 and std.Target.current.os.tag == .linux;
5
56comptime {
6 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
7 if (is_x86_64_linux) {
78 asm (
89 \\.globl this_is_my_alias;
910 \\.type this_is_my_alias, @function;
......@@ -13,7 +14,7 @@ comptime {
1314}
1415
1516test "module level assembly" {
16 if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) {
17 if (is_x86_64_linux) {
1718 expect(this_is_my_alias() == 1234);
1819 }
1920}
test/stage1/behavior/byteswap.zig+2-3
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const builtin = @import("builtin");
43
54test "@byteSwap integers" {
65 const ByteSwapIntTest = struct {
......@@ -41,10 +40,10 @@ test "@byteSwap integers" {
4140
4241test "@byteSwap vectors" {
4342 // https://github.com/ziglang/zig/issues/3563
44 if (builtin.os == .dragonfly) return error.SkipZigTest;
43 if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;
4544
4645 // https://github.com/ziglang/zig/issues/3317
47 if (builtin.arch == .mipsel) return error.SkipZigTest;
46 if (std.Target.current.cpu.arch == .mipsel) return error.SkipZigTest;
4847
4948 const ByteSwapVectorTest = struct {
5049 fn run() void {
test/stage1/behavior/namespace_depends_on_compile_var.zig+4-4
......@@ -1,5 +1,5 @@
1const builtin = @import("builtin");
2const expect = @import("std").testing.expect;
1const std = @import("std");
2const expect = std.testing.expect;
33
44test "namespace depends on compile var" {
55 if (some_namespace.a_bool) {
......@@ -8,7 +8,7 @@ test "namespace depends on compile var" {
88 expect(!some_namespace.a_bool);
99 }
1010}
11const some_namespace = switch (builtin.os) {
12 builtin.Os.linux => @import("namespace_depends_on_compile_var/a.zig"),
11const some_namespace = switch (std.builtin.os.tag) {
12 .linux => @import("namespace_depends_on_compile_var/a.zig"),
1313 else => @import("namespace_depends_on_compile_var/b.zig"),
1414};
test/stage1/behavior/vector.zig+1-2
......@@ -2,7 +2,6 @@ const std = @import("std");
22const mem = std.mem;
33const expect = std.testing.expect;
44const expectEqual = std.testing.expectEqual;
5const builtin = @import("builtin");
65
76test "implicit cast vector to array - bool" {
87 const S = struct {
......@@ -114,7 +113,7 @@ test "array to vector" {
114113
115114test "vector casts of sizes not divisable by 8" {
116115 // https://github.com/ziglang/zig/issues/3563
117 if (builtin.os == .dragonfly) return error.SkipZigTest;
116 if (std.Target.current.os.tag == .dragonfly) return error.SkipZigTest;
118117
119118 const S = struct {
120119 fn doTheTest() void {
test/tests.zig+1-1
......@@ -31,7 +31,7 @@ pub const RunTranslatedCContext = @import("src/run_translated_c.zig").RunTransla
3131pub const CompareOutputContext = @import("src/compare_output.zig").CompareOutputContext;
3232
3333const TestTarget = struct {
34 target: Target = .Native,
34 target: build.Target = .Native,
3535 mode: builtin.Mode = .Debug,
3636 link_libc: bool = false,
3737 single_threaded: bool = false,