| ... | ... | @@ -0,0 +1,192 @@ |
| 1 | const builtin = @import("builtin"); |
| 2 | const std = @import("std"); |
| 3 | const process = std.process; |
| 4 | const mem = std.mem; |
| 5 | const unicode = std.unicode; |
| 6 | const io = std.io; |
| 7 | const fs = std.fs; |
| 8 | const os = std.os; |
| 9 | const json = std.json; |
| 10 | const StringifyOptions = json.StringifyOptions; |
| 11 | const Allocator = std.mem.Allocator; |
| 12 | const introspect = @import("introspect.zig"); |
| 13 | |
| 14 | const usage_info = |
| 15 | \\Usage: zig info [options] |
| 16 | \\ |
| 17 | \\ Outputs path to zig lib dir, std dir and the global cache dir. |
| 18 | \\ |
| 19 | \\Options: |
| 20 | \\ --help Print this help and exit |
| 21 | \\ --format [text|json] Choose output format (defaults to text) |
| 22 | \\ |
| 23 | ; |
| 24 | |
| 25 | pub const CompilerInfo = struct { |
| 26 | // TODO: port compiler id hash from cpp |
| 27 | // /// Compiler id hash |
| 28 | // id: []const u8, |
| 29 | |
| 30 | // /// Compiler version |
| 31 | // version: []const u8, |
| 32 | /// Path to lib/ |
| 33 | lib_dir: []const u8, |
| 34 | |
| 35 | /// Path to lib/zig/std |
| 36 | std_dir: []const u8, |
| 37 | |
| 38 | /// Path to the global cache dir |
| 39 | global_cache_dir: []const u8, |
| 40 | |
| 41 | const CompilerType = enum { |
| 42 | Stage1, |
| 43 | SelfHosted, |
| 44 | }; |
| 45 | |
| 46 | pub fn getVersionString() []const u8 { |
| 47 | // TODO: get this from build.zig somehow |
| 48 | return "0.6.0"; |
| 49 | } |
| 50 | |
| 51 | pub fn getCacheDir(allocator: *Allocator, compiler_type: CompilerType) ![]u8 { |
| 52 | const global_cache_dir = try getAppCacheDir(allocator, "zig"); |
| 53 | defer allocator.free(global_cache_dir); |
| 54 | |
| 55 | const postfix = switch (compiler_type) { |
| 56 | .SelfHosted => "self_hosted", |
| 57 | .Stage1 => "stage1", |
| 58 | }; |
| 59 | return try fs.path.join(allocator, &[_][]const u8{ global_cache_dir, postfix }); // stage1 compiler uses $cache_dir/zig/stage1 |
| 60 | } |
| 61 | |
| 62 | // TODO: add CacheType argument here to make it return correct cache dir for stage1 |
| 63 | pub fn init(allocator: *Allocator, compiler_type: CompilerType) !CompilerInfo { |
| 64 | const zig_lib_dir = try introspect.resolveZigLibDir(allocator); |
| 65 | errdefer allocator.free(zig_lib_dir); |
| 66 | |
| 67 | const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{ zig_lib_dir, "std" }); |
| 68 | errdefer allocator.free(zig_std_dir); |
| 69 | |
| 70 | const cache_dir = try CompilerInfo.getCacheDir(allocator, compiler_type); |
| 71 | errdefer allocator.free(cache_dir); |
| 72 | |
| 73 | return CompilerInfo{ |
| 74 | .lib_dir = zig_lib_dir, |
| 75 | .std_dir = zig_std_dir, |
| 76 | .global_cache_dir = cache_dir, |
| 77 | }; |
| 78 | } |
| 79 | |
| 80 | pub fn toString(self: *CompilerInfo, out_stream: var) !void { |
| 81 | inline for (@typeInfo(CompilerInfo).Struct.fields) |field| { |
| 82 | try std.fmt.format(out_stream, "{: <16}\t{: <}\n", .{ field.name, @field(self, field.name) }); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | pub fn deinit(self: *CompilerInfo, allocator: *Allocator) void { |
| 87 | allocator.free(self.lib_dir); |
| 88 | allocator.free(self.std_dir); |
| 89 | allocator.free(self.global_cache_dir); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | pub fn cmdInfo(allocator: *Allocator, cmd_args: []const []const u8, compiler_type: CompilerInfo.CompilerType, stdout: var) !void { |
| 94 | var info = try CompilerInfo.init(allocator, compiler_type); |
| 95 | defer info.deinit(allocator); |
| 96 | |
| 97 | var bos = io.bufferedOutStream(stdout); |
| 98 | const bos_stream = bos.outStream(); |
| 99 | |
| 100 | var json_format = false; |
| 101 | |
| 102 | var i: usize = 0; |
| 103 | while (i < cmd_args.len) : (i += 1) { |
| 104 | const arg = cmd_args[i]; |
| 105 | if (mem.eql(u8, arg, "--format")) { |
| 106 | if (cmd_args.len <= i + 1) { |
| 107 | std.debug.warn("expected [text|json] after --format\n", .{}); |
| 108 | process.exit(1); |
| 109 | } |
| 110 | const format = cmd_args[i + 1]; |
| 111 | i += 1; |
| 112 | if (mem.eql(u8, format, "text")) { |
| 113 | json_format = false; |
| 114 | } else if (mem.eql(u8, format, "json")) { |
| 115 | json_format = true; |
| 116 | } else { |
| 117 | std.debug.warn("expected [text|json] after --format, found '{}'\n", .{format}); |
| 118 | process.exit(1); |
| 119 | } |
| 120 | } else if (mem.eql(u8, arg, "--help")) { |
| 121 | try stdout.writeAll(usage_info); |
| 122 | return; |
| 123 | } else { |
| 124 | std.debug.warn("unrecognized parameter: '{}'\n", .{arg}); |
| 125 | process.exit(1); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (json_format) { |
| 130 | try json.stringify(info, StringifyOptions{ |
| 131 | .whitespace = StringifyOptions.Whitespace{ .indent = .{ .Space = 2 } }, |
| 132 | }, bos_stream); |
| 133 | try bos_stream.writeByte('\n'); |
| 134 | } else { |
| 135 | try info.toString(bos_stream); |
| 136 | } |
| 137 | |
| 138 | try bos.flush(); |
| 139 | } |
| 140 | |
| 141 | pub const GetAppCacheDirError = error{ |
| 142 | OutOfMemory, |
| 143 | AppCacheDirUnavailable, |
| 144 | }; |
| 145 | |
| 146 | // Copied from fs.getAppDataDir, but changed it to return .cache/ dir on linux. |
| 147 | // This is the same behavior as the current zig compiler global cache resolution. |
| 148 | fn getAppCacheDir(allocator: *Allocator, appname: []const u8) GetAppCacheDirError![]u8 { |
| 149 | switch (builtin.os.tag) { |
| 150 | .windows => { |
| 151 | var dir_path_ptr: [*:0]u16 = undefined; |
| 152 | switch (os.windows.shell32.SHGetKnownFolderPath( |
| 153 | &os.windows.FOLDERID_LocalAppData, |
| 154 | os.windows.KF_FLAG_CREATE, |
| 155 | null, |
| 156 | &dir_path_ptr, |
| 157 | )) { |
| 158 | os.windows.S_OK => { |
| 159 | defer os.windows.ole32.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr)); |
| 160 | const global_dir = unicode.utf16leToUtf8Alloc(allocator, mem.spanZ(dir_path_ptr)) catch |err| switch (err) { |
| 161 | error.UnexpectedSecondSurrogateHalf => return error.AppCacheDirUnavailable, |
| 162 | error.ExpectedSecondSurrogateHalf => return error.AppCacheDirUnavailable, |
| 163 | error.DanglingSurrogateHalf => return error.AppCacheDirUnavailable, |
| 164 | error.OutOfMemory => return error.OutOfMemory, |
| 165 | }; |
| 166 | defer allocator.free(global_dir); |
| 167 | return fs.path.join(allocator, &[_][]const u8{ global_dir, appname }); |
| 168 | }, |
| 169 | os.windows.E_OUTOFMEMORY => return error.OutOfMemory, |
| 170 | else => return error.AppCacheDirUnavailable, |
| 171 | } |
| 172 | }, |
| 173 | .macosx => { |
| 174 | const home_dir = os.getenv("HOME") orelse { |
| 175 | // TODO look in /etc/passwd |
| 176 | return error.AppCacheDirUnavailable; |
| 177 | }; |
| 178 | return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname }); |
| 179 | }, |
| 180 | .linux, .freebsd, .netbsd, .dragonfly => { |
| 181 | if (os.getenv("XDG_CACHE_HOME")) |cache_home| { |
| 182 | return fs.path.join(allocator, &[_][]const u8{ cache_home, appname }); |
| 183 | } |
| 184 | |
| 185 | const home_dir = os.getenv("HOME") orelse { |
| 186 | return error.AppCacheDirUnavailable; |
| 187 | }; |
| 188 | return fs.path.join(allocator, &[_][]const u8{ home_dir, ".cache", appname }); |
| 189 | }, |
| 190 | else => @compileError("Unsupported OS"), |
| 191 | } |
| 192 | } |