1
1
const std = @import ("std" );
2
+ const builtin = @import ("builtin" );
2
3
const build_options = @import ("build_options" );
3
- const introspect = @import ("introspect .zig" );
4
+ const Compilation = @import ("Compilation .zig" );
4
5
const Allocator = std .mem .Allocator ;
6
+ const EnvVar = std .zig .EnvVar ;
5
7
const fatal = std .process .fatal ;
6
8
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
+ },
13
26
};
14
- defer zig_lib_directory .handle .close ();
15
27
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 ();
17
37
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 "" ;
19
41
20
42
const host = try std .zig .system .resolveTargetQuery (.{});
21
43
const triple = try host .zigTriple (arena );
@@ -24,7 +46,7 @@ pub fn cmdEnv(arena: Allocator, out: *std.Io.Writer) !void {
24
46
var root = try serializer .beginStruct (.{});
25
47
26
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
50
try root .field ("std_dir" , zig_std_dir , .{});
29
51
try root .field ("global_cache_dir" , global_cache_dir , .{});
30
52
try root .field ("version" , build_options .version , .{});
0 commit comments