authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-18 23:06:49-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-04-18 23:06:49-07:00
logbb9cd6db1cab40d5d5fba7c2ea4fc979efdefa44
treecd567d917137ffb0a1138f95e71f673b608e2c7b
parentedb4a07d4ddf01c7f4d589b0f427ba6b9e03134b

stage2: Move WASI/Zig-specific selfExePath to introspect.zig


4 files changed, 42 insertions(+), 51 deletions(-)

lib/std/fs.zig-46
......@@ -2547,15 +2547,6 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathEr
25472547/// `selfExePath` except allocates the result on the heap.
25482548/// Caller owns returned memory.
25492549pub fn selfExePathAlloc(allocator: Allocator) ![]u8 {
2550 if (builtin.os.tag == .wasi) {
2551 var args = try std.process.argsWithAllocator(allocator);
2552 defer args.deinit();
2553 // On WASI, argv[0] is always just the basename of the current executable
2554 const exe_name = args.next() orelse return error.FileNotFound;
2555
2556 var buf: [MAX_PATH_BYTES]u8 = undefined;
2557 return allocator.dupe(u8, try selfExePathWasi(&buf, exe_name));
2558 }
25592550 // Use of MAX_PATH_BYTES here is justified as, at least on one tested Linux
25602551 // system, readlink will completely fail to return a result larger than
25612552 // PATH_MAX even if given a sufficiently large buffer. This makes it
......@@ -2657,43 +2648,6 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
26572648 }
26582649}
26592650
2660/// WASI-specific implementation of selfExePath
2661///
2662/// On WASI argv0 is always just the executable basename, so this function relies
2663/// using a fixed executable directory path: "/zig"
2664///
2665/// This path can be configured in wasmtime using `--mapdir=/zig::/path/to/zig/dir/`
2666fn selfExePathWasi(out_buffer: []u8, argv0: []const u8) SelfExePathError![]const u8 {
2667 var allocator = std.heap.FixedBufferAllocator.init(out_buffer);
2668 var alloc = allocator.allocator();
2669
2670 // Check these paths:
2671 // 1. "/zig/{exe_name}"
2672 // 2. "/zig/bin/{exe_name}"
2673 const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" };
2674 const exe_names_to_check = &[_][]const u8{ path.basename(argv0), "zig.wasm" };
2675
2676 for (base_paths_to_check) |base_path| {
2677 for (exe_names_to_check) |exe_name| {
2678 const test_path = path.join(alloc, &.{ base_path, exe_name }) catch continue;
2679
2680 // Make sure it's a file we're pointing to
2681 const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
2682 if (file.filetype != .REGULAR_FILE) continue;
2683
2684 // Path seems to be valid, let's try to turn it into an absolute path
2685 var real_path_buf: [MAX_PATH_BYTES]u8 = undefined;
2686 if (os.realpath(test_path, &real_path_buf)) |real_path| {
2687 if (real_path.len > out_buffer.len)
2688 return error.NameTooLong;
2689 mem.copy(u8, out_buffer, real_path);
2690 return out_buffer[0..real_path.len];
2691 } else |_| continue;
2692 }
2693 }
2694 return error.FileNotFound;
2695}
2696
26972651/// The result is UTF16LE-encoded.
26982652pub fn selfExePathW() [:0]const u16 {
26992653 const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName;
src/introspect.zig+38-1
......@@ -33,9 +33,46 @@ fn testZigInstallPrefix(base_dir: fs.Dir) ?Compilation.Directory {
3333 return Compilation.Directory{ .handle = test_zig_dir, .path = "lib" };
3434}
3535
36/// This is a small wrapper around selfExePathAlloc that adds support for WASI
37/// based on a hard-coded Preopen directory ("/zig")
38pub fn findZigExePath(allocator: mem.Allocator) ![]u8 {
39 if (builtin.os.tag == .wasi) {
40 var args = try std.process.argsWithAllocator(allocator);
41 defer args.deinit();
42 // On WASI, argv[0] is always just the basename of the current executable
43 const argv0 = args.next() orelse return error.FileNotFound;
44
45 // Check these paths:
46 // 1. "/zig/{exe_name}"
47 // 2. "/zig/bin/{exe_name}"
48 const base_paths_to_check = &[_][]const u8{ "/zig", "/zig/bin" };
49 const exe_names_to_check = &[_][]const u8{ fs.path.basename(argv0), "zig.wasm" };
50
51 for (base_paths_to_check) |base_path| {
52 for (exe_names_to_check) |exe_name| {
53 const test_path = fs.path.join(allocator, &.{ base_path, exe_name }) catch continue;
54 defer allocator.free(test_path);
55
56 // Make sure it's a file we're pointing to
57 const file = os.fstatat(os.wasi.AT.FDCWD, test_path, 0) catch continue;
58 if (file.filetype != .REGULAR_FILE) continue;
59
60 // Path seems to be valid, let's try to turn it into an absolute path
61 var real_path_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
62 if (os.realpath(test_path, &real_path_buf)) |real_path| {
63 return allocator.dupe(u8, real_path); // Success: return absolute path
64 } else |_| continue;
65 }
66 }
67 return error.FileNotFound;
68 }
69
70 return fs.selfExePathAlloc(allocator);
71}
72
3673/// Both the directory handle and the path are newly allocated resources which the caller now owns.
3774pub fn findZigLibDir(gpa: mem.Allocator) !Compilation.Directory {
38 const self_exe_path = try fs.selfExePathAlloc(gpa);
75 const self_exe_path = try findZigExePath(gpa);
3976 defer gpa.free(self_exe_path);
4077
4178 return findZigLibDirFromSelfExe(gpa, self_exe_path);
src/main.zig+3-3
......@@ -2526,7 +2526,7 @@ fn buildOutputType(
25262526 pkg_tree_root.table = .{};
25272527 }
25282528
2529 const self_exe_path = try fs.selfExePathAlloc(arena);
2529 const self_exe_path = try introspect.findZigExePath(arena);
25302530 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
25312531 .path = lib_dir,
25322532 .handle = fs.cwd().openDir(lib_dir, .{}) catch |err| {
......@@ -3395,7 +3395,7 @@ pub fn cmdInit(
33953395 }
33963396 }
33973397 }
3398 const self_exe_path = try fs.selfExePathAlloc(arena);
3398 const self_exe_path = try introspect.findZigExePath(arena);
33993399 var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {
34003400 fatal("unable to find zig installation directory: {s}\n", .{@errorName(err)});
34013401 };
......@@ -3475,7 +3475,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
34753475 // We want to release all the locks before executing the child process, so we make a nice
34763476 // big block here to ensure the cleanup gets run when we extract out our argv.
34773477 const child_argv = argv: {
3478 const self_exe_path = try fs.selfExePathAlloc(arena);
3478 const self_exe_path = try introspect.findZigExePath(arena);
34793479
34803480 var build_file: ?[]const u8 = null;
34813481 var override_lib_dir: ?[]const u8 = null;
src/print_env.zig+1-1
......@@ -6,7 +6,7 @@ const fatal = @import("main.zig").fatal;
66
77pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void {
88 _ = args;
9 const self_exe_path = try std.fs.selfExePathAlloc(gpa);
9 const self_exe_path = try introspect.findZigExePath(gpa);
1010 defer gpa.free(self_exe_path);
1111
1212 var zig_lib_directory = introspect.findZigLibDirFromSelfExe(gpa, self_exe_path) catch |err| {