aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2022-04-18 23:53:41 +0200
committerGitHub <noreply@github.com>2022-04-18 23:53:41 +0200
commitb03345f32a5ba2849ddbeeae0e31e0e77ca01b01 (patch)
tree4100326d0b400a38525d980739214feaf48508b8 /src
parent5195b87639a1dc56b90751d0aecc4fcf4f2a1bb0 (diff)
parent3a63fa6b7f56a2f384ebd460e80c00e6bbd2efee (diff)
downloadzig-b03345f32a5ba2849ddbeeae0e31e0e77ca01b01.tar.gz
zig-b03345f32a5ba2849ddbeeae0e31e0e77ca01b01.zip
Merge pull request #11024 from topolarity/wasi-stage2
stage2: Add limited WASI support for selfExePath and globalCacheDir
Diffstat (limited to 'src')
-rw-r--r--src/Cache.zig18
-rw-r--r--src/introspect.zig13
-rw-r--r--src/main.zig10
3 files changed, 33 insertions, 8 deletions
diff --git a/src/Cache.zig b/src/Cache.zig
index 993114905e..37cd7a7529 100644
--- a/src/Cache.zig
+++ b/src/Cache.zig
@@ -762,18 +762,22 @@ pub const Manifest = struct {
fn downgradeToSharedLock(self: *Manifest) !void {
if (!self.have_exclusive_lock) return;
- const manifest_file = self.manifest_file.?;
- try manifest_file.downgradeLock();
+ if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock
+ const manifest_file = self.manifest_file.?;
+ try manifest_file.downgradeLock();
+ }
self.have_exclusive_lock = false;
}
fn upgradeToExclusiveLock(self: *Manifest) !void {
if (self.have_exclusive_lock) return;
- 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.
- manifest_file.unlock();
- try manifest_file.lock(.Exclusive);
+ if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock
+ 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.
+ manifest_file.unlock();
+ try manifest_file.lock(.Exclusive);
+ }
self.have_exclusive_lock = true;
}
diff --git a/src/introspect.zig b/src/introspect.zig
index 562d6b04f4..c0de4dc7f5 100644
--- a/src/introspect.zig
+++ b/src/introspect.zig
@@ -1,6 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
+const os = std.os;
const fs = std.fs;
const Compilation = @import("Compilation.zig");
@@ -80,5 +81,15 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 {
}
}
- return fs.getAppDataDir(allocator, appname);
+ if (builtin.os.tag == .wasi) {
+ // On WASI, we have no way to get an App data dir, so we try to use a fixed
+ // Preopen path "/cache" as a last resort
+ const path = "/cache";
+
+ const file = os.fstatat(os.wasi.AT.FDCWD, path, 0) catch return error.CacheDirUnavailable;
+ if (file.filetype != .DIRECTORY) return error.CacheDirUnavailable;
+ return allocator.dupe(u8, path);
+ } else {
+ return fs.getAppDataDir(allocator, appname);
+ }
}
diff --git a/src/main.zig b/src/main.zig
index e341a10f99..823eb91f33 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -162,6 +162,16 @@ pub fn main() anyerror!void {
return mainArgs(gpa_tracy.allocator(), arena, args);
}
+ // WASI: `--dir` instructs the WASM runtime to "preopen" a directory, making
+ // it available to the us, the guest program. This is the only way for us to
+ // access files/dirs on the host filesystem
+ if (builtin.os.tag == .wasi) {
+ // This sets our CWD to "/preopens/cwd"
+ // Dot-prefixed preopens like `--dir=.` are "mounted" at "/preopens/cwd"
+ // Other preopens like `--dir=lib` are "mounted" at "/"
+ try std.os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd");
+ }
+
return mainArgs(gpa, arena, args);
}