Skip to content

Commit

Permalink
Make the compiler build for UEFI
Browse files Browse the repository at this point in the history
  • Loading branch information
RossComputerGuy committed Apr 5, 2024
1 parent fad96df commit 4ec3b02
Show file tree
Hide file tree
Showing 18 changed files with 578 additions and 59 deletions.
12 changes: 6 additions & 6 deletions lib/std/Build/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ pub const Manifest = struct {
// WASI does not currently support flock, so we bypass it here.
// TODO: If/when flock is supported on WASI, this check should be removed.
// See https://github.com/WebAssembly/wasi-filesystem/issues/2
if (builtin.os.tag != .wasi or std.process.can_spawn or !builtin.single_threaded) {
if (builtin.os.tag != .wasi or builtin.os.tag != .uefi or std.process.can_spawn or !builtin.single_threaded) {
const manifest_file = self.manifest_file.?;
try manifest_file.downgradeLock();
}
Expand All @@ -945,7 +945,7 @@ pub const Manifest = struct {
// WASI does not currently support flock, so we bypass it here.
// TODO: If/when flock is supported on WASI, this check should be removed.
// See https://github.com/WebAssembly/wasi-filesystem/issues/2
if (builtin.os.tag != .wasi or std.process.can_spawn or !builtin.single_threaded) {
if (builtin.os.tag != .wasi or builtin.os.tag != .uefi or std.process.can_spawn or !builtin.single_threaded) {
const manifest_file = self.manifest_file.?;
// Here we intentionally have a period where the lock is released, in case there are
// other processes holding a shared lock.
Expand Down Expand Up @@ -1041,7 +1041,7 @@ fn testGetCurrentFileTimestamp(dir: fs.Dir) !i128 {
}

test "cache file and then recall it" {
if (builtin.os.tag == .wasi) {
if (builtin.os.tag == .wasi or builtin.os.tag != .uefi) {
// https://github.com/ziglang/zig/issues/5437
return error.SkipZigTest;
}
Expand Down Expand Up @@ -1107,7 +1107,7 @@ test "cache file and then recall it" {
}

test "check that changing a file makes cache fail" {
if (builtin.os.tag == .wasi) {
if (builtin.os.tag == .wasi or builtin.os.tag != .uefi) {
// https://github.com/ziglang/zig/issues/5437
return error.SkipZigTest;
}
Expand Down Expand Up @@ -1181,7 +1181,7 @@ test "check that changing a file makes cache fail" {
}

test "no file inputs" {
if (builtin.os.tag == .wasi) {
if (builtin.os.tag == .wasi or builtin.os.tag != .uefi) {
// https://github.com/ziglang/zig/issues/5437
return error.SkipZigTest;
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ test "no file inputs" {
}

test "Manifest with files added after initial hash work" {
if (builtin.os.tag == .wasi) {
if (builtin.os.tag == .wasi or builtin.os.tag != .uefi) {
// https://github.com/ziglang/zig/issues/5437
return error.SkipZigTest;
}
Expand Down
22 changes: 17 additions & 5 deletions lib/std/child_process.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const native_os = builtin.os.tag;
pub const ChildProcess = struct {
pub const Id = switch (native_os) {
.windows => windows.HANDLE,
.wasi => void,
.wasi, .uefi => void,
else => posix.pid_t,
};

Expand Down Expand Up @@ -47,10 +47,16 @@ pub const ChildProcess = struct {
stderr_behavior: StdIo,

/// Set to change the user id when spawning the child process.
uid: if (native_os == .windows or native_os == .wasi) void else ?posix.uid_t,
uid: switch (native_os) {
.windows, .wasi, .uefi => void,
else => ?posix.uid_t,
},

/// Set to change the group id when spawning the child process.
gid: if (native_os == .windows or native_os == .wasi) void else ?posix.gid_t,
gid: switch (native_os) {
.windows, .wasi, .uefi => void,
else => ?posix.gid_t,
},

/// Set to change the current working directory when spawning the child process.
cwd: ?[]const u8,
Expand Down Expand Up @@ -169,8 +175,14 @@ pub const ChildProcess = struct {
.term = null,
.env_map = null,
.cwd = null,
.uid = if (native_os == .windows or native_os == .wasi) {} else null,
.gid = if (native_os == .windows or native_os == .wasi) {} else null,
.uid = switch (native_os) {
.windows, .wasi, .uefi => {},
else => null,
},
.gid = switch (native_os) {
.windows, .wasi, .uefi => {},
else => null,
},
.stdin = null,
.stdout = null,
.stderr = null,
Expand Down
58 changes: 56 additions & 2 deletions lib/std/fs/Dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,22 @@ pub const Iterator = switch (native_os) {
self.cookie = std.os.wasi.DIRCOOKIE_START;
}
},
.uefi => struct {
dir: Dir,

const Self = @This();

pub const Error = IteratorError;

pub fn next(self: *Self) Error!?Entry {
_ = self;
return null;
}

pub fn reset(self: *Self) void {
_ = self;
}
},
else => @compileError("unimplemented"),
};

Expand Down Expand Up @@ -621,6 +637,9 @@ fn iterateImpl(self: Dir, first_iter_start_value: bool) Iterator {
.end_index = 0,
.buf = undefined,
},
.uefi => return Iterator{
.dir = self,
},
else => @compileError("unimplemented"),
}
}
Expand Down Expand Up @@ -962,7 +981,7 @@ pub fn createFileZ(self: Dir, sub_path_c: [*:0]const u8, flags: File.CreateFlags
const path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c);
return self.createFileW(path_w.span(), flags);
},
.wasi => {
.wasi, .uefi => {
return createFile(self, mem.sliceTo(sub_path_c, 0), flags);
},
else => {},
Expand Down Expand Up @@ -1412,6 +1431,17 @@ pub fn openDir(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!
};
return .{ .fd = fd };
},
.uefi => {
var temp_path: std.os.windows.PathSpace = undefined;
temp_path.len = try std.unicode.utf8ToUtf16Le(&temp_path.data, sub_path);
temp_path.data[temp_path.len] = 0;
const uefi = std.os.uefi;

const handle = try uefi.openat(self.fd, temp_path.span(), .{
.read = args.iterate,
});
return .{ .fd = handle };
},
else => {
const sub_path_c = try posix.toPosixPath(sub_path);
return self.openDirZ(&sub_path_c, args);
Expand All @@ -1426,7 +1456,7 @@ pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) Open
const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path_c);
return self.openDirW(sub_path_w.span().ptr, args);
},
.wasi => {
.wasi, .uefi => {
return openDir(self, mem.sliceTo(sub_path_c, 0), args);
},
else => {
Expand Down Expand Up @@ -1625,6 +1655,10 @@ pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {

/// Same as `deleteDir` except the parameter is null-terminated.
pub fn deleteDirZ(self: Dir, sub_path_c: [*:0]const u8) DeleteDirError!void {
if (native_os == .uefi) {
self.fd.file.delete();
return;
}
posix.unlinkatZ(self.fd, sub_path_c, posix.AT.REMOVEDIR) catch |err| switch (err) {
error.IsDir => unreachable, // not possible since we pass AT.REMOVEDIR
else => |e| return e,
Expand Down Expand Up @@ -2320,6 +2354,23 @@ pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessErro
};
return self.accessW(sub_path_w.span().ptr, flags);
}
if (native_os == .uefi) {
var temp_path: std.os.windows.PathSpace = undefined;
temp_path.len = try std.unicode.utf8ToUtf16Le(&temp_path.data, sub_path);
temp_path.data[temp_path.len] = 0;
const uefi = std.os.uefi;

const handle = uefi.openat(self.fd, temp_path.span(), .{
.read = flags.isRead(),
.write = flags.isWrite(),
}) catch |err| return switch (err) {
error.AccessDenied => error.PermissionDenied,
error.NotDir, error.NoDevice => error.InputOutput,
else => |e| e,
};
errdefer uefi.close(handle);
return;
}
const path_c = try posix.toPosixPath(sub_path);
return self.accessZ(&path_c, flags);
}
Expand All @@ -2333,6 +2384,9 @@ pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) Access
};
return self.accessW(sub_path_w.span().ptr, flags);
}
if (native_os == .uefi) {
return access(self, mem.sliceTo(sub_path, 0), flags);
}
const os_mode = switch (flags.mode) {
.read_only => @as(u32, posix.F_OK),
.write_only => @as(u32, posix.W_OK),
Expand Down
19 changes: 17 additions & 2 deletions lib/std/fs/File.zig
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ pub const Stat = struct {
const atime = st.atime();
const mtime = st.mtime();
const ctime = st.ctime();
if (builtin.os.tag == .uefi) {
return .{
.inode = st.ino,
.size = st.size,
.mode = st.mode,
.kind = st.kind,
.atime = atime,
.mtime = mtime,
.ctime = ctime,
};
}
return .{
.inode = st.ino,
.size = @bitCast(st.size),
Expand Down Expand Up @@ -1589,7 +1600,7 @@ pub fn lock(file: File, l: Lock) LockError!void {
error.WouldBlock => unreachable, // non-blocking=false
else => |e| return e,
};
} else {
} else if (builtin.os.tag == .uefi) {} else {
return posix.flock(file.handle, switch (l) {
.none => posix.LOCK.UN,
.shared => posix.LOCK.SH,
Expand All @@ -1615,7 +1626,7 @@ pub fn unlock(file: File) void {
error.RangeNotLocked => unreachable, // Function assumes unlocked.
error.Unexpected => unreachable, // Resource deallocation must succeed.
};
} else {
} else if (builtin.os.tag == .uefi) {} else {
return posix.flock(file.handle, posix.LOCK.UN) catch |err| switch (err) {
error.WouldBlock => unreachable, // unlocking can't block
error.SystemResources => unreachable, // We are deallocating resources.
Expand Down Expand Up @@ -1656,6 +1667,8 @@ pub fn tryLock(file: File, l: Lock) LockError!bool {
error.WouldBlock => return false,
else => |e| return e,
};
} else if (builtin.os.tag == .uefi) {
return error.Unexpected;
} else {
posix.flock(file.handle, switch (l) {
.none => posix.LOCK.UN,
Expand Down Expand Up @@ -1706,6 +1719,8 @@ pub fn downgradeLock(file: File) LockError!void {
error.RangeNotLocked => unreachable, // File was not locked.
error.Unexpected => unreachable, // Resource deallocation must succeed.
};
} else if (builtin.os.tag == .uefi) {
return error.Unexpected;
} else {
return posix.flock(file.handle, posix.LOCK.SH | posix.LOCK.NB) catch |err| switch (err) {
error.WouldBlock => unreachable, // File was not locked in exclusive mode.
Expand Down
1 change: 1 addition & 0 deletions lib/std/fs/get_app_data_dir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
else => return error.AppDataDirUnavailable,
}
},
.uefi => return std.process.getCwdAlloc(allocator) catch error.AppDataDirUnavailable,
else => @compileError("Unsupported OS"),
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/fs/path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ test resolveUefi {
// Keep relative paths relative.
try testResolveUefi(&[_][]const u8{"a\\b"}, "a\\b");
try testResolveUefi(&[_][]const u8{"."}, ".");
try testResolveUefi(&[_][]const u8{".", "src\\test.zig", "..", "..\\test\\cases.zig"}, "test\\cases.zig");
try testResolveUefi(&[_][]const u8{ ".", "src\\test.zig", "..", "..\\test\\cases.zig" }, "test\\cases.zig");
}

fn testResolveWindows(paths: []const []const u8, expected: []const u8) !void {
Expand Down Expand Up @@ -1487,7 +1487,7 @@ test relative {
try testRelativePosix("/baz", "/baz-quux", "../baz-quux");

try testRelativeUefi("\\var\\lib", "\\var", "..");
try testRelativeUefi("\\var\\lib", "\\bin", "..\\..\\bin");
try testRelativeUefi("\\var\\lib", "\\bin", "..\\..\\bin");
try testRelativeUefi("\\var\\lib", "\\var\\lib", "");
try testRelativeUefi("\\var\\lib", "\\var\\apache", "..\\apache");
try testRelativeUefi("\\var\\", "\\var\\lib", "lib");
Expand Down
1 change: 1 addition & 0 deletions lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const windows = std.os.windows;
// first release to support them.
pub const has_unix_sockets = switch (native_os) {
.windows => builtin.os.version_range.windows.isAtLeast(.win10_rs4) orelse false,
.uefi => false,
else => true,
};

Expand Down
Loading

0 comments on commit 4ec3b02

Please sign in to comment.