authorgravatar for techatrix@mailbox.orgTechatrix <techatrix@mailbox.org> 2025-05-29 13:27:51+02:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-07-30 09:45:03+01:00
log4a1594fbdebfeae989ff7c74be737b4879e1916e
treeb7b2af6b7b6ff8d523fe8285dd2f6ded8ed1c516
parent1fcaf90dd3c99d452fcab13698a63faf17e8f3c1

update `zig env` to respect `ZIG_LIB_DIR` and support wasi


2 files changed, 39 insertions(+), 12 deletions(-)

src/main.zig+6-1
...@@ -361,7 +361,12 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {...@@ -361,7 +361,12 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
361 dev.check(.env_command);361 dev.check(.env_command);
362 verifyLibcxxCorrectlyLinked();362 verifyLibcxxCorrectlyLinked();
363 var stdout_writer = fs.File.stdout().writer(&stdout_buffer);363 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 );
365 return stdout_writer.interface.flush();370 return stdout_writer.interface.flush();
366 } else if (mem.eql(u8, cmd, "reduce")) {371 } else if (mem.eql(u8, cmd, "reduce")) {
367 return jitCmd(gpa, arena, cmd_args, .{372 return jitCmd(gpa, arena, cmd_args, .{
src/print_env.zig+33-11
...@@ -1,21 +1,43 @@...@@ -1,21 +1,43 @@
1const std = @import("std");1const std = @import("std");
2const builtin = @import("builtin");
2const build_options = @import("build_options");3const build_options = @import("build_options");
3const introspect = @import("introspect.zig");4const Compilation = @import("Compilation.zig");
4const Allocator = std.mem.Allocator;5const Allocator = std.mem.Allocator;
6const EnvVar = std.zig.EnvVar;
5const fatal = std.process.fatal;7const fatal = std.process.fatal;
68
7pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {9pub fn cmdEnv(
8 const cwd_path = try introspect.getResolvedCwd(arena);10 arena: Allocator,
9 const self_exe_path = try std.fs.selfExePathAlloc(arena);11 out: *std.Io.Writer,
1012 args: []const []const u8,
11 var zig_lib_directory = introspect.findZigLibDirFromSelfExe(arena, cwd_path, self_exe_path) catch |err| {13 wasi_preopens: switch (builtin.target.os.tag) {
12 fatal("unable to find zig installation directory: {s}\n", .{@errorName(err)});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 },
13 };26 };
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
20 const host = try std.zig.system.resolveTargetQuery(.{});42 const host = try std.zig.system.resolveTargetQuery(.{});
21 const triple = try host.zigTriple(arena);43 const triple = try host.zigTriple(arena);
...@@ -24,7 +46,7 @@ pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {...@@ -24,7 +46,7 @@ pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {
24 var root = try serializer.beginStruct(.{});46 var root = try serializer.beginStruct(.{});
2547
26 try root.field("zig_exe", self_exe_path, .{});48 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, .{});
28 try root.field("std_dir", zig_std_dir, .{});50 try root.field("std_dir", zig_std_dir, .{});
29 try root.field("global_cache_dir", global_cache_dir, .{});51 try root.field("global_cache_dir", global_cache_dir, .{});
30 try root.field("version", build_options.version, .{});52 try root.field("version", build_options.version, .{});