master
1const std = @import("std");
2const builtin = @import("builtin");
3const build_options = @import("build_options");
4const Compilation = @import("Compilation.zig");
5const Allocator = std.mem.Allocator;
6const EnvVar = std.zig.EnvVar;
7const fatal = std.process.fatal;
8
9pub 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 host: *const std.Target,
18) !void {
19 const override_lib_dir: ?[]const u8 = try EnvVar.ZIG_LIB_DIR.get(arena);
20 const override_global_cache_dir: ?[]const u8 = try EnvVar.ZIG_GLOBAL_CACHE_DIR.get(arena);
21
22 const self_exe_path = switch (builtin.target.os.tag) {
23 .wasi => args[0],
24 else => std.fs.selfExePathAlloc(arena) catch |err| {
25 fatal("unable to find zig self exe path: {s}", .{@errorName(err)});
26 },
27 };
28
29 var dirs: Compilation.Directories = .init(
30 arena,
31 override_lib_dir,
32 override_global_cache_dir,
33 .global,
34 if (builtin.target.os.tag == .wasi) wasi_preopens,
35 if (builtin.target.os.tag != .wasi) self_exe_path,
36 );
37 defer dirs.deinit();
38
39 const zig_lib_dir = dirs.zig_lib.path orelse "";
40 const zig_std_dir = try dirs.zig_lib.join(arena, &.{"std"});
41 const global_cache_dir = dirs.global_cache.path orelse "";
42 const triple = try host.zigTriple(arena);
43
44 var serializer: std.zon.Serializer = .{ .writer = out };
45 var root = try serializer.beginStruct(.{});
46
47 try root.field("zig_exe", self_exe_path, .{});
48 try root.field("lib_dir", zig_lib_dir, .{});
49 try root.field("std_dir", zig_std_dir, .{});
50 try root.field("global_cache_dir", global_cache_dir, .{});
51 try root.field("version", build_options.version, .{});
52 try root.field("target", triple, .{});
53 var env = try root.beginStructField("env", .{});
54 inline for (@typeInfo(std.zig.EnvVar).@"enum".fields) |field| {
55 try env.field(field.name, try @field(std.zig.EnvVar, field.name).get(arena), .{});
56 }
57 try env.end();
58 try root.end();
59
60 try out.writeByte('\n');
61}