authorgravatar for ziga.zeljko@gmail.comŽiga Željko <ziga.zeljko@gmail.com> 2022-01-31 16:12:50+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-01-31 22:54:30+01:00
log5210b9074ca68c23da474b0afcaa4ce11f7cc57c
treeade7a233e0e07c129901ae00115a632c1c2c4296
parent66cf011aa91a66fd8b1489d186152522ccd27550

os,wasi: use wasi-libc if available


2 files changed, 52 insertions(+), 19 deletions(-)

lib/std/c/wasi.zig+41-8
......@@ -8,6 +8,15 @@ pub fn _errno() *c_int {
88 return &errno;
99}
1010
11pub const AT = wasi.AT;
12pub const CLOCK = wasi.CLOCK;
13pub const E = wasi.E;
14pub const IOV_MAX = wasi.IOV_MAX;
15pub const LOCK = wasi.LOCK;
16pub const S = wasi.S;
17pub const STDERR_FILENO = wasi.STDERR_FILENO;
18pub const STDIN_FILENO = wasi.STDIN_FILENO;
19pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
1120pub const fd_t = wasi.fd_t;
1221pub const pid_t = c_int;
1322pub const uid_t = u32;
......@@ -17,14 +26,6 @@ pub const ino_t = wasi.ino_t;
1726pub const mode_t = wasi.mode_t;
1827pub const time_t = wasi.time_t;
1928pub const timespec = wasi.timespec;
20pub const STDERR_FILENO = wasi.STDERR_FILENO;
21pub const STDIN_FILENO = wasi.STDIN_FILENO;
22pub const STDOUT_FILENO = wasi.STDOUT_FILENO;
23pub const E = wasi.E;
24pub const CLOCK = wasi.CLOCK;
25pub const S = wasi.S;
26pub const IOV_MAX = wasi.IOV_MAX;
27pub const AT = wasi.AT;
2829
2930pub const Stat = extern struct {
3031 dev: i32,
......@@ -93,8 +94,40 @@ pub const O = struct {
9394 pub const WRONLY = (0x10000000);
9495};
9596
97pub const F = struct {
98 pub const GETFD = 1;
99 pub const SETFD = 2;
100 pub const GETFL = 3;
101 pub const SETFL = 4;
102};
103
104pub const FD_CLOEXEC = 1;
105
106pub const F_OK = 0;
107pub const X_OK = 1;
108pub const W_OK = 2;
109pub const R_OK = 4;
110
96111pub const SEEK = struct {
97112 pub const SET: wasi.whence_t = .SET;
98113 pub const CUR: wasi.whence_t = .CUR;
99114 pub const END: wasi.whence_t = .END;
100115};
116
117pub const nfds_t = usize;
118
119pub const pollfd = extern struct {
120 fd: fd_t,
121 events: i16,
122 revents: i16,
123};
124
125pub const POLL = struct {
126 pub const RDNORM = 0x1;
127 pub const WRNORM = 0x2;
128 pub const IN = RDNORM;
129 pub const OUT = WRNORM;
130 pub const ERR = 0x1000;
131 pub const HUP = 0x2000;
132 pub const NVAL = 0x4000;
133};
lib/std/os.zig+11-11
......@@ -238,7 +238,7 @@ pub fn close(fd: fd_t) void {
238238 if (builtin.os.tag == .windows) {
239239 return windows.CloseHandle(fd);
240240 }
241 if (builtin.os.tag == .wasi) {
241 if (builtin.os.tag == .wasi and !builtin.link_libc) {
242242 _ = wasi.fd_close(fd);
243243 return;
244244 }
......@@ -1395,7 +1395,7 @@ pub fn openW(file_path_w: []const u16, flags: u32, perm: mode_t) OpenError!fd_t
13951395/// `file_path` is relative to the open directory handle `dir_fd`.
13961396/// See also `openatZ`.
13971397pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) OpenError!fd_t {
1398 if (builtin.os.tag == .wasi) {
1398 if (builtin.os.tag == .wasi and !builtin.link_libc) {
13991399 @compileError("use openatWasi instead");
14001400 }
14011401 if (builtin.os.tag == .windows) {
......@@ -1760,7 +1760,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
17601760 if (builtin.os.tag == .windows) {
17611761 return windows.GetCurrentDirectory(out_buffer);
17621762 }
1763 if (builtin.os.tag == .wasi) {
1763 if (builtin.os.tag == .wasi and !builtin.link_libc) {
17641764 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
17651765 }
17661766
......@@ -1804,7 +1804,7 @@ pub const SymLinkError = error{
18041804/// If `sym_link_path` exists, it will not be overwritten.
18051805/// See also `symlinkZ.
18061806pub fn symlink(target_path: []const u8, sym_link_path: []const u8) SymLinkError!void {
1807 if (builtin.os.tag == .wasi) {
1807 if (builtin.os.tag == .wasi and !builtin.link_libc) {
18081808 @compileError("symlink is not supported in WASI; use symlinkat instead");
18091809 }
18101810 if (builtin.os.tag == .windows) {
......@@ -2021,7 +2021,7 @@ pub const UnlinkError = error{
20212021/// Delete a name and possibly the file it refers to.
20222022/// See also `unlinkZ`.
20232023pub fn unlink(file_path: []const u8) UnlinkError!void {
2024 if (builtin.os.tag == .wasi) {
2024 if (builtin.os.tag == .wasi and !builtin.link_libc) {
20252025 @compileError("unlink is not supported in WASI; use unlinkat instead");
20262026 } else if (builtin.os.tag == .windows) {
20272027 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
......@@ -2175,7 +2175,7 @@ pub const RenameError = error{
21752175
21762176/// Change the name or location of a file.
21772177pub fn rename(old_path: []const u8, new_path: []const u8) RenameError!void {
2178 if (builtin.os.tag == .wasi) {
2178 if (builtin.os.tag == .wasi and !builtin.link_libc) {
21792179 @compileError("rename is not supported in WASI; use renameat instead");
21802180 } else if (builtin.os.tag == .windows) {
21812181 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
......@@ -2469,7 +2469,7 @@ pub const MakeDirError = error{
24692469/// Create a directory.
24702470/// `mode` is ignored on Windows.
24712471pub fn mkdir(dir_path: []const u8, mode: u32) MakeDirError!void {
2472 if (builtin.os.tag == .wasi) {
2472 if (builtin.os.tag == .wasi and !builtin.link_libc) {
24732473 @compileError("mkdir is not supported in WASI; use mkdirat instead");
24742474 } else if (builtin.os.tag == .windows) {
24752475 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
......@@ -2539,7 +2539,7 @@ pub const DeleteDirError = error{
25392539
25402540/// Deletes an empty directory.
25412541pub fn rmdir(dir_path: []const u8) DeleteDirError!void {
2542 if (builtin.os.tag == .wasi) {
2542 if (builtin.os.tag == .wasi and !builtin.link_libc) {
25432543 @compileError("rmdir is not supported in WASI; use unlinkat instead");
25442544 } else if (builtin.os.tag == .windows) {
25452545 const dir_path_w = try windows.sliceToPrefixedFileW(dir_path);
......@@ -2600,7 +2600,7 @@ pub const ChangeCurDirError = error{
26002600/// Changes the current working directory of the calling process.
26012601/// `dir_path` is recommended to be a UTF-8 encoded string.
26022602pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
2603 if (builtin.os.tag == .wasi) {
2603 if (builtin.os.tag == .wasi and !builtin.link_libc) {
26042604 @compileError("chdir is not supported in WASI");
26052605 } else if (builtin.os.tag == .windows) {
26062606 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
......@@ -2684,7 +2684,7 @@ pub const ReadLinkError = error{
26842684/// Read value of a symbolic link.
26852685/// The return value is a slice of `out_buffer` from index 0.
26862686pub fn readlink(file_path: []const u8, out_buffer: []u8) ReadLinkError![]u8 {
2687 if (builtin.os.tag == .wasi) {
2687 if (builtin.os.tag == .wasi and !builtin.link_libc) {
26882688 @compileError("readlink is not supported in WASI; use readlinkat instead");
26892689 } else if (builtin.os.tag == .windows) {
26902690 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
......@@ -4631,7 +4631,7 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
46314631 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
46324632 return realpathW(pathname_w.span(), out_buffer);
46334633 }
4634 if (builtin.os.tag == .wasi) {
4634 if (builtin.os.tag == .wasi and !builtin.link_libc) {
46354635 @compileError("Use std.fs.wasi.PreopenList to obtain valid Dir handles instead of using absolute paths");
46364636 }
46374637 const pathname_c = try toPosixPath(pathname);