Skip to content

Commit 17eb24a

Browse files
committed
move types from builtin to std
* All the data types from `@import("builtin")` are moved to `@import("std").builtin`. The target-related types are moved to `std.Target`. This allows the data types to have methods, such as `std.Target.current.isDarwin()`. * `std.os.windows.subsystem` is moved to `std.Target.current.subsystem`. * Remove the concept of the panic package from the compiler implementation. Instead, `std.builtin.panic` is always the panic function. It checks for `@hasDecl(@import("root"), "panic")`, or else provides a default implementation. This is an important step for multibuilds (#3028). Without this change, the types inside the builtin namespace look like different types, when trying to merge builds with different target settings. With this change, Zig can figure out that, e.g., `std.builtin.Os` (the enum type) from one compilation and `std.builtin.Os` from another compilation are the same type, even if the target OS value differs.
1 parent ef62452 commit 17eb24a

16 files changed

+1212
-949
lines changed

lib/std/build.zig

+8-427
Large diffs are not rendered by default.

lib/std/builtin.zig

+413
Large diffs are not rendered by default.

lib/std/os.zig

+16-18
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ const elf = std.elf;
2323
const dl = @import("dynamic_library.zig");
2424
const MAX_PATH_BYTES = std.fs.MAX_PATH_BYTES;
2525

26-
comptime {
27-
assert(@import("std") == std); // std lib tests require --override-lib-dir
28-
}
29-
3026
pub const darwin = @import("os/darwin.zig");
3127
pub const freebsd = @import("os/freebsd.zig");
3228
pub const linux = @import("os/linux.zig");
@@ -36,6 +32,22 @@ pub const wasi = @import("os/wasi.zig");
3632
pub const windows = @import("os/windows.zig");
3733
pub const zen = @import("os/zen.zig");
3834

35+
comptime {
36+
assert(@import("std") == std); // std lib tests require --override-lib-dir
37+
if (builtin.is_test) {
38+
_ = darwin;
39+
_ = freebsd;
40+
_ = linux;
41+
_ = netbsd;
42+
_ = uefi;
43+
_ = wasi;
44+
_ = windows;
45+
_ = zen;
46+
47+
_ = @import("os/test.zig");
48+
}
49+
}
50+
3951
/// When linking libc, this is the C API. Otherwise, it is the OS-specific system interface.
4052
pub const system = if (builtin.link_libc) std.c else switch (builtin.os) {
4153
.macosx, .ios, .watchos, .tvos => darwin,
@@ -1063,7 +1075,6 @@ pub fn unlinkatW(dirfd: fd_t, sub_path_w: [*]const u16, flags: u32) UnlinkatErro
10631075
return error.FileBusy;
10641076
}
10651077

1066-
10671078
var attr = w.OBJECT_ATTRIBUTES{
10681079
.Length = @sizeOf(w.OBJECT_ATTRIBUTES),
10691080
.RootDirectory = dirfd,
@@ -2813,16 +2824,3 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
28132824

28142825
@compileError("TODO implement gethostname for this OS");
28152826
}
2816-
2817-
test "" {
2818-
_ = @import("os/darwin.zig");
2819-
_ = @import("os/freebsd.zig");
2820-
_ = @import("os/linux.zig");
2821-
_ = @import("os/netbsd.zig");
2822-
_ = @import("os/uefi.zig");
2823-
_ = @import("os/wasi.zig");
2824-
_ = @import("os/windows.zig");
2825-
_ = @import("os/zen.zig");
2826-
2827-
_ = @import("os/test.zig");
2828-
}

lib/std/os/windows.zig

-26
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,6 @@ pub usingnamespace @import("windows/bits.zig");
2222

2323
pub const self_process_handle = @intToPtr(HANDLE, maxInt(usize));
2424

25-
/// `builtin` is missing `subsystem` when the subsystem is automatically detected,
26-
/// so Zig standard library has the subsystem detection logic here. This should generally be
27-
/// used rather than `builtin.subsystem`.
28-
/// On non-windows targets, this is `null`.
29-
pub const subsystem: ?builtin.SubSystem = blk: {
30-
if (@hasDecl(builtin, "subsystem")) break :blk builtin.subsystem;
31-
switch (builtin.os) {
32-
.windows => {
33-
if (builtin.is_test) {
34-
break :blk builtin.SubSystem.Console;
35-
}
36-
const root = @import("root");
37-
if (@hasDecl(root, "WinMain") or
38-
@hasDecl(root, "wWinMain") or
39-
@hasDecl(root, "WinMainCRTStartup") or
40-
@hasDecl(root, "wWinMainCRTStartup"))
41-
{
42-
break :blk builtin.SubSystem.Windows;
43-
} else {
44-
break :blk builtin.SubSystem.Console;
45-
}
46-
},
47-
else => break :blk null,
48-
}
49-
};
50-
5125
pub const CreateFileError = error{
5226
SharingViolation,
5327
PathAlreadyExists,

lib/std/special/panic.zig

-31
This file was deleted.

lib/std/std.zig

+2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ pub const SpinLock = @import("spinlock.zig").SpinLock;
2222
pub const StaticallyInitializedMutex = @import("statically_initialized_mutex.zig").StaticallyInitializedMutex;
2323
pub const StringHashMap = @import("hash_map.zig").StringHashMap;
2424
pub const TailQueue = @import("linked_list.zig").TailQueue;
25+
pub const Target = @import("target.zig").Target;
2526
pub const Thread = @import("thread.zig").Thread;
2627

2728
pub const atomic = @import("atomic.zig");
2829
pub const base64 = @import("base64.zig");
2930
pub const build = @import("build.zig");
31+
pub const builtin = @import("builtin.zig");
3032
pub const c = @import("c.zig");
3133
pub const coff = @import("coff.zig");
3234
pub const crypto = @import("crypto.zig");

0 commit comments

Comments
 (0)