Commit bb9cd6db1c

Cody Tapscott <topolarity@tapscott.me>
2022-04-19 08:06:49
stage2: Move WASI/Zig-specific selfExePath to introspect.zig
1 parent edb4a07
Changed files (4)
lib/std/fs.zig
@@ -2547,15 +2547,6 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError || os.RealPathEr
 /// `selfExePath` except allocates the result on the heap.
 /// Caller owns returned memory.
 pub fn selfExePathAlloc(allocator: 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 exe_name = args.next() orelse return error.FileNotFound;
-
-        var buf: [MAX_PATH_BYTES]u8 = undefined;
-        return allocator.dupe(u8, try selfExePathWasi(&buf, exe_name));
-    }
     // Use of MAX_PATH_BYTES here is justified as, at least on one tested Linux
     // system, readlink will completely fail to return a result larger than
     // PATH_MAX even if given a sufficiently large buffer. This makes it
@@ -2657,43 +2648,6 @@ pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
     }
 }
 
-/// WASI-specific implementation of selfExePath
-///
-/// On WASI argv0 is always just the executable basename, so this function relies
-/// using a fixed executable directory path: "/zig"
-///
-/// This path can be configured in wasmtime using `--mapdir=/zig::/path/to/zig/dir/`
-fn selfExePathWasi(out_buffer: []u8, argv0: []const u8) SelfExePathError![]const u8 {
-    var allocator = std.heap.FixedBufferAllocator.init(out_buffer);
-    var alloc = allocator.allocator();
-
-    // 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{ path.basename(argv0), "zig.wasm" };
-
-    for (base_paths_to_check) |base_path| {
-        for (exe_names_to_check) |exe_name| {
-            const test_path = path.join(alloc, &.{ base_path, exe_name }) catch continue;
-
-            // 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: [MAX_PATH_BYTES]u8 = undefined;
-            if (os.realpath(test_path, &real_path_buf)) |real_path| {
-                if (real_path.len > out_buffer.len)
-                    return error.NameTooLong;
-                mem.copy(u8, out_buffer, real_path);
-                return out_buffer[0..real_path.len];
-            } else |_| continue;
-        }
-    }
-    return error.FileNotFound;
-}
-
 /// The result is UTF16LE-encoded.
 pub fn selfExePathW() [:0]const u16 {
     const image_path_name = &os.windows.peb().ProcessParameters.ImagePathName;
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);
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;
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| {