aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-04-19 13:30:15 -0400
committerGitHub <noreply@github.com>2022-04-19 13:30:15 -0400
commit78f26b970e1c5c288e45d0edd2ab2f229e6fe924 (patch)
tree8233f52a95ffba1b92661ea5d3bdf989c00950ff /src
parent535d5624e48e31752729d00c469aca504cf50091 (diff)
parentf8dc6fc416d27bc6d93d79689823a4278aacd5b2 (diff)
downloadzig-78f26b970e1c5c288e45d0edd2ab2f229e6fe924.tar.gz
zig-78f26b970e1c5c288e45d0edd2ab2f229e6fe924.zip
Merge pull request #11469 from topolarity/wasm-fixup
stage2: Move `selfExePathWasi` to `introspect.zig`
Diffstat (limited to 'src')
-rw-r--r--src/Cache.zig12
-rw-r--r--src/introspect.zig39
-rw-r--r--src/main.zig6
-rw-r--r--src/print_env.zig2
4 files changed, 52 insertions, 7 deletions
diff --git a/src/Cache.zig b/src/Cache.zig
index 37cd7a7529..0d4b51492d 100644
--- a/src/Cache.zig
+++ b/src/Cache.zig
@@ -762,7 +762,11 @@ pub const Manifest = struct {
fn downgradeToSharedLock(self: *Manifest) !void {
if (!self.have_exclusive_lock) return;
- if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock
+
+ // 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) {
const manifest_file = self.manifest_file.?;
try manifest_file.downgradeLock();
}
@@ -771,7 +775,11 @@ pub const Manifest = struct {
fn upgradeToExclusiveLock(self: *Manifest) !void {
if (self.have_exclusive_lock) return;
- if (std.process.can_spawn or !builtin.single_threaded) { // Some targets (WASI) do not support flock
+
+ // 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) {
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.
diff --git a/src/introspect.zig b/src/introspect.zig
index c0de4dc7f5..74f0d45c80 100644
--- a/src/introspect.zig
+++ b/src/introspect.zig
@@ -33,9 +33,46 @@ fn testZigInstallPrefix(base_dir: fs.Dir) ?Compilation.Directory {
return Compilation.Directory{ .handle = test_zig_dir, .path = "lib" };
}
+/// This is a small wrapper around selfExePathAlloc that adds support for WASI
+/// based on a hard-coded Preopen directory ("/zig")
+pub fn findZigExePath(allocator: mem.Allocator) ![]u8 {
+ if (builtin.os.tag == .wasi) {
+ var args = try std.process.argsWithAllocator(allocator);
+ defer args.deinit();
+ // On WASI, argv[0] is always just the basename of the current executable
+ const argv0 = args.next() orelse return error.FileNotFound;
+
+ // Check these paths:
+ // 1. "/zig/{exe_name}"
+ // 2. "/zig/bin/{exe_name}"
+ const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" };
+ const exe_names_to_check = &[_][]const u8{ fs.path.basename(argv0), "zig.wasm" };
+
+ for (base_paths_to_check) |base_path| {
+ for (exe_names_to_check) |exe_name| {
+ const test_path = fs.path.join(allocator, &.{ base_path, exe_name }) catch continue;
+ defer allocator.free(test_path);
+
+ // Make sure it's a file we're pointing to
+ const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
+ if (file.filetype != .REGULAR_FILE) continue;
+
+ // Path seems to be valid, let's try to turn it into an absolute path
+ var real_path_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
+ if (os.realpath(test_path, &real_path_buf)) |real_path| {
+ return allocator.dupe(u8, real_path); // Success: return absolute path
+ } else |_| continue;
+ }
+ }
+ return error.FileNotFound;
+ }
+
+ return fs.selfExePathAlloc(allocator);
+}
+
/// Both the directory handle and the path are newly allocated resources which the caller now owns.
pub fn findZigLibDir(gpa: mem.Allocator) !Compilation.Directory {
- const self_exe_path = try fs.selfExePathAlloc(gpa);
+ const self_exe_path = try findZigExePath(gpa);
defer gpa.free(self_exe_path);
return findZigLibDirFromSelfExe(gpa, self_exe_path);
diff --git a/src/main.zig b/src/main.zig
index 0d61c3271e..51765edb51 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -2526,7 +2526,7 @@ fn buildOutputType(
pkg_tree_root.table = .{};
}
- const self_exe_path = try fs.selfExePathAlloc(arena);
+ const self_exe_path = try introspect.findZigExePath(arena);
var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
.path = lib_dir,
.handle = fs.cwd().openDir(lib_dir, .{}) catch |err| {
@@ -3395,7 +3395,7 @@ pub fn cmdInit(
}
}
}
- const self_exe_path = try fs.selfExePathAlloc(arena);
+ const self_exe_path = try introspect.findZigExePath(arena);
var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {
fatal("unable to find zig installation directory: {s}\n", .{@errorName(err)});
};
@@ -3475,7 +3475,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
// We want to release all the locks before executing the child process, so we make a nice
// big block here to ensure the cleanup gets run when we extract out our argv.
const child_argv = argv: {
- const self_exe_path = try fs.selfExePathAlloc(arena);
+ const self_exe_path = try introspect.findZigExePath(arena);
var build_file: ?[]const u8 = null;
var override_lib_dir: ?[]const u8 = null;
diff --git a/src/print_env.zig b/src/print_env.zig
index 15f038c50e..ad772d416b 100644
--- a/src/print_env.zig
+++ b/src/print_env.zig
@@ -6,7 +6,7 @@ const fatal = @import("main.zig").fatal;
pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void {
_ = args;
- const self_exe_path = try std.fs.selfExePathAlloc(gpa);
+ const self_exe_path = try introspect.findZigExePath(gpa);
defer gpa.free(self_exe_path);
var zig_lib_directory = introspect.findZigLibDirFromSelfExe(gpa, self_exe_path) catch |err| {