Skip to content

Commit 4a1594f

Browse files
Techatrixmlugg
authored andcommitted
update zig env to respect ZIG_LIB_DIR and support wasi
1 parent 1fcaf90 commit 4a1594f

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

src/main.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,12 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
361361
dev.check(.env_command);
362362
verifyLibcxxCorrectlyLinked();
363363
var stdout_writer = fs.File.stdout().writer(&stdout_buffer);
364-
try @import("print_env.zig").cmdEnv(arena, &stdout_writer.interface);
364+
try @import("print_env.zig").cmdEnv(
365+
arena,
366+
&stdout_writer.interface,
367+
args,
368+
if (native_os == .wasi) wasi_preopens,
369+
);
365370
return stdout_writer.interface.flush();
366371
} else if (mem.eql(u8, cmd, "reduce")) {
367372
return jitCmd(gpa, arena, cmd_args, .{

src/print_env.zig

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
const std = @import("std");
2+
const builtin = @import("builtin");
23
const build_options = @import("build_options");
3-
const introspect = @import("introspect.zig");
4+
const Compilation = @import("Compilation.zig");
45
const Allocator = std.mem.Allocator;
6+
const EnvVar = std.zig.EnvVar;
57
const fatal = std.process.fatal;
68

7-
pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {
8-
const cwd_path = try introspect.getResolvedCwd(arena);
9-
const self_exe_path = try std.fs.selfExePathAlloc(arena);
10-
11-
var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, cwd_path, self_exe_path) catch |err| {
12-
fatal("unable to find zig installation directory: {s}\n", .{@errorName(err)});
9+
pub fn cmdEnv(
10+
arena: Allocator,
11+
out: *std.Io.Writer,
12+
args: []const []const u8,
13+
wasi_preopens: switch (builtin.target.os.tag) {
14+
.wasi => std.fs.wasi.Preopens,
15+
else => void,
16+
},
17+
) !void {
18+
const override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
19+
const override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
20+
21+
const self_exe_path = switch (builtin.target.os.tag) {
22+
.wasi => args[0],
23+
else => std.fs.selfExePathAlloc(arena) catch |err| {
24+
fatal("unable to find zig self exe path: {s}", .{@errorName(err)});
25+
},
1326
};
14-
defer zig_lib_directory.handle.close();
1527

16-
const zig_std_dir = try std.fs.path.join(arena, &[_][]const u8{ zig_lib_directory.path.?, "std" });
28+
var dirs: Compilation.Directories = .init(
29+
arena,
30+
override_lib_dir,
31+
override_global_cache_dir,
32+
.global,
33+
if (builtin.target.os.tag == .wasi) wasi_preopens,
34+
if (builtin.target.os.tag != .wasi) self_exe_path,
35+
);
36+
defer dirs.deinit();
1737

18-
const global_cache_dir = try introspect.resolveGlobalCacheDir(arena);
38+
const zig_lib_dir = dirs.zig_lib.path orelse "";
39+
const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});
40+
const global_cache_dir = dirs.global_cache.path orelse "";
1941

2042
const host = try std.zig.system.resolveTargetQuery(.{});
2143
const triple = try host.zigTriple(arena);
@@ -24,7 +46,7 @@ pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {
2446
var root = try serializer.beginStruct(.{});
2547

2648
try root.field("zig_exe", self_exe_path, .{});
27-
try root.field("lib_dir", zig_lib_directory.path.?, .{});
49+
try root.field("lib_dir", zig_lib_dir, .{});
2850
try root.field("std_dir", zig_std_dir, .{});
2951
try root.field("global_cache_dir", global_cache_dir, .{});
3052
try root.field("version", build_options.version, .{});

0 commit comments

Comments
 (0)