| ... | @@ -1,11 +1,16 @@ | ... | @@ -1,11 +1,16 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| | 2 | const mem = std.mem; |
| 2 | const build_options = @import("build_options"); | 3 | const build_options = @import("build_options"); |
| 3 | const introspect = @import("introspect.zig"); | 4 | const introspect = @import("introspect.zig"); |
| 4 | const Allocator = std.mem.Allocator; | 5 | const Allocator = std.mem.Allocator; |
| 5 | const fatal = @import("main.zig").fatal; | 6 | const fatal = @import("main.zig").fatal; |
| 6 | | 7 | |
| | 8 | const Env = struct { |
| | 9 | name: []const u8, |
| | 10 | value: []const u8, |
| | 11 | }; |
| | 12 | |
| 7 | pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void { | 13 | pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writer) !void { |
| 8 | _ = args; | | |
| 9 | const self_exe_path = try introspect.findZigExePath(gpa); | 14 | const self_exe_path = try introspect.findZigExePath(gpa); |
| 10 | defer gpa.free(self_exe_path); | 15 | defer gpa.free(self_exe_path); |
| 11 | | 16 | |
| ... | @@ -25,32 +30,33 @@ pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writ | ... | @@ -25,32 +30,33 @@ pub fn cmdEnv(gpa: Allocator, args: []const []const u8, stdout: std.fs.File.Writ |
| 25 | const triple = try info.target.zigTriple(gpa); | 30 | const triple = try info.target.zigTriple(gpa); |
| 26 | defer gpa.free(triple); | 31 | defer gpa.free(triple); |
| 27 | | 32 | |
| | 33 | const envars: []Env = &[_]Env{ |
| | 34 | .{ .name = "zig_exe", .value = self_exe_path }, |
| | 35 | .{ .name = "lib_dir", .value = zig_lib_directory.path.? }, |
| | 36 | .{ .name = "std_dir", .value = zig_std_dir }, |
| | 37 | .{ .name = "global_cache_dir", .value = global_cache_dir }, |
| | 38 | .{ .name = "version", .value = build_options.version }, |
| | 39 | .{ .name = "target", .value = triple }, |
| | 40 | }; |
| | 41 | |
| 28 | var bw = std.io.bufferedWriter(stdout); | 42 | var bw = std.io.bufferedWriter(stdout); |
| 29 | const w = bw.writer(); | 43 | const w = bw.writer(); |
| 30 | | 44 | |
| 31 | var jws = std.json.writeStream(w, .{ .whitespace = .indent_1 }); | 45 | if (args.len > 0) { |
| 32 | | 46 | for (args) |name| { |
| 33 | try jws.beginObject(); | 47 | for (envars) |env| { |
| 34 | | 48 | if (mem.eql(u8, name, env.name)) { |
| 35 | try jws.objectField("zig_exe"); | 49 | try w.print("{s}\n", .{env.value}); |
| 36 | try jws.write(self_exe_path); | 50 | } |
| 37 | | 51 | } |
| 38 | try jws.objectField("lib_dir"); | 52 | } |
| 39 | try jws.write(zig_lib_directory.path.?); | 53 | try bw.flush(); |
| 40 | | 54 | |
| 41 | try jws.objectField("std_dir"); | 55 | return; |
| 42 | try jws.write(zig_std_dir); | 56 | } |
| 43 | | 57 | |
| 44 | try jws.objectField("global_cache_dir"); | 58 | for (envars) |env| { |
| 45 | try jws.write(global_cache_dir); | 59 | try w.print("{[name]s}=\"{[value]s}\"\n", env); |
| 46 | | 60 | } |
| 47 | try jws.objectField("version"); | | |
| 48 | try jws.write(build_options.version); | | |
| 49 | | | |
| 50 | try jws.objectField("target"); | | |
| 51 | try jws.write(triple); | | |
| 52 | | | |
| 53 | try jws.endObject(); | | |
| 54 | try w.writeByte('\n'); | | |
| 55 | try bw.flush(); | 61 | try bw.flush(); |
| 56 | } | 62 | } |