diff options
| author | Cody Tapscott <topolarity@tapscott.me> | 2022-04-18 23:06:49 -0700 |
|---|---|---|
| committer | Cody Tapscott <topolarity@tapscott.me> | 2022-04-18 23:06:49 -0700 |
| commit | bb9cd6db1cab40d5d5fba7c2ea4fc979efdefa44 (patch) | |
| tree | cd567d917137ffb0a1138f95e71f673b608e2c7b /src/introspect.zig | |
| parent | edb4a07d4ddf01c7f4d589b0f427ba6b9e03134b (diff) | |
| download | zig-bb9cd6db1cab40d5d5fba7c2ea4fc979efdefa44.tar.gz zig-bb9cd6db1cab40d5d5fba7c2ea4fc979efdefa44.zip | |
stage2: Move WASI/Zig-specific selfExePath to introspect.zig
Diffstat (limited to 'src/introspect.zig')
| -rw-r--r-- | src/introspect.zig | 39 |
1 files changed, 38 insertions, 1 deletions
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); |
