From 12ce4a2d092f67188c735c223985af0c8a42b42f Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Wed, 20 May 2020 07:04:22 +0300 Subject: [PATCH 01/17] Added help for command "info" --- src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.cpp b/src/main.cpp index 41aade9431a5..19d87abd293e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,6 +47,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " translate-c [source] convert c code to zig code\n" " targets list available compilation targets\n" " test [source] create and run a test build\n" + " info print std path, version and other useful info\n" " version print version number and exit\n" " zen print zen of zig and exit\n" "\n" From 5e370213ba8bef790845f54e030d2ef07e195f3f Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Wed, 20 May 2020 10:10:14 +0300 Subject: [PATCH 02/17] Added basic std path command --- src-self-hosted/main.zig | 7 +++++++ src/main.cpp | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 4ef4acc24bc4..8cc9440951f6 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -10,6 +10,7 @@ const Module = @import("Module.zig"); const link = @import("link.zig"); const Package = @import("Package.zig"); const zir = @import("zir.zig"); +const introspect = @import("introspect.zig"); // TODO Improve async I/O enough that we feel comfortable doing this. //pub const io_mode = .evented; @@ -33,6 +34,7 @@ const usage = \\ fmt [source] Parse file and render in canonical zig format \\ targets List available compilation targets \\ version Print version number and exit + \\ info Print lib path, std path, compiler id and version \\ zen Print zen of zig and exit \\ \\ @@ -70,6 +72,11 @@ pub fn main() !void { // Need to set up the build script to give the version as a comptime value. std.debug.warn("TODO version command not implemented yet\n", .{}); return error.Unimplemented; + } else if (mem.eql(u8, cmd, "info")) { + const zig_lib_dir = try introspect.resolveZigLibDir(arena); + const result = try std.fmt.allocPrint(arena, "zig_lib_dir = {}\n", .{zig_lib_dir}); + + try std.io.getStdOut().writeAll(result); } else if (mem.eql(u8, cmd, "zen")) { try io.getStdOut().writeAll(info_zen); } else if (mem.eql(u8, cmd, "help")) { diff --git a/src/main.cpp b/src/main.cpp index 19d87abd293e..41aade9431a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,7 +47,6 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " translate-c [source] convert c code to zig code\n" " targets list available compilation targets\n" " test [source] create and run a test build\n" - " info print std path, version and other useful info\n" " version print version number and exit\n" " zen print zen of zig and exit\n" "\n" From 8b7b289eaa6a954092075691e76b1a984df5e5ef Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 06:51:36 +0300 Subject: [PATCH 03/17] Added mock info printing in the required format --- src-self-hosted/main.zig | 5 +--- src-self-hosted/print_info.zig | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src-self-hosted/print_info.zig diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 8cc9440951f6..4676f533bf61 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -73,10 +73,7 @@ pub fn main() !void { std.debug.warn("TODO version command not implemented yet\n", .{}); return error.Unimplemented; } else if (mem.eql(u8, cmd, "info")) { - const zig_lib_dir = try introspect.resolveZigLibDir(arena); - const result = try std.fmt.allocPrint(arena, "zig_lib_dir = {}\n", .{zig_lib_dir}); - - try std.io.getStdOut().writeAll(result); + try @import("print_info.zig").cmdInfo(arena, io.getStdOut().outStream()); } else if (mem.eql(u8, cmd, "zen")) { try io.getStdOut().writeAll(info_zen); } else if (mem.eql(u8, cmd, "help")) { diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig new file mode 100644 index 000000000000..61366e9f09c7 --- /dev/null +++ b/src-self-hosted/print_info.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const io = std.io; +const json = std.json; +const StringifyOptions = json.StringifyOptions; +const Allocator = std.mem.Allocator; + +pub const CompilerInfo = struct { + /// Compiler id hash + id: []const u8, + + /// Compiler version + version: []const u8, + + /// Path to lib/ + lib_dir: []const u8, + + /// Path to lib/zig/std + std_dir: []const u8, + + /// Path to the global cache dir + global_cache_dir: []const u8, +}; + +fn resolveCompilerInfo(allocator: *Allocator) CompilerInfo { + return CompilerInfo{ + .id = "test", + .version = "0.7.0", + .lib_dir = "/some/path", + .std_dir = "/some/path/std", + .global_cache_dir = "/global/" + }; +} + +pub fn cmdInfo(allocator: *Allocator, stdout: var) !void { + const info = resolveCompilerInfo(allocator); + + var bos = io.bufferedOutStream(stdout); + const bos_stream = bos.outStream(); + + const stringifyOptions = StringifyOptions{ + .whitespace = StringifyOptions.Whitespace{ + // Match indentation of zig targets + .indent = .{ .Space = 2 } + }, + }; + try json.stringify(info, stringifyOptions, bos_stream); + + try bos_stream.writeByte('\n'); + try bos.flush(); +} From e9ca6955ef1ee5622b6eecdee8185cb8b0367fff Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 07:13:05 +0300 Subject: [PATCH 04/17] Added resolution of lib path and zig std path --- src-self-hosted/print_info.zig | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 61366e9f09c7..95e6a00574c7 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -1,8 +1,10 @@ const std = @import("std"); const io = std.io; +const fs = std.fs; const json = std.json; const StringifyOptions = json.StringifyOptions; const Allocator = std.mem.Allocator; +const introspect = @import("introspect.zig"); pub const CompilerInfo = struct { /// Compiler id hash @@ -19,20 +21,28 @@ pub const CompilerInfo = struct { /// Path to the global cache dir global_cache_dir: []const u8, -}; -fn resolveCompilerInfo(allocator: *Allocator) CompilerInfo { - return CompilerInfo{ - .id = "test", - .version = "0.7.0", - .lib_dir = "/some/path", - .std_dir = "/some/path/std", - .global_cache_dir = "/global/" - }; -} + pub fn init(allocator: *Allocator) !CompilerInfo { + const zig_lib_dir = try introspect.resolveZigLibDir(allocator); + const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); + return CompilerInfo{ + .id = "test", + .version = "0.7.0", + .lib_dir = zig_lib_dir, + .std_dir = zig_std_dir, + .global_cache_dir = "/global/" + }; + } + + pub fn deinit(self: *CompilerInfo, allocator: *Allocator) void { + allocator.free(self.lib_dir); + allocator.free(self.std_dir); + } +}; pub fn cmdInfo(allocator: *Allocator, stdout: var) !void { - const info = resolveCompilerInfo(allocator); + var info = try CompilerInfo.init(allocator); + defer info.deinit(allocator); var bos = io.bufferedOutStream(stdout); const bos_stream = bos.outStream(); From 4771c29f3edeaa1a78aa78fa6abdfe3a4c5bb247 Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 07:55:01 +0300 Subject: [PATCH 05/17] Added global cache dir resolution --- src-self-hosted/print_info.zig | 59 +++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 95e6a00574c7..cb6c4cfd5555 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -1,6 +1,8 @@ +const builtin = @import("builtin"); const std = @import("std"); const io = std.io; const fs = std.fs; +const os = std.os; const json = std.json; const StringifyOptions = json.StringifyOptions; const Allocator = std.mem.Allocator; @@ -25,18 +27,20 @@ pub const CompilerInfo = struct { pub fn init(allocator: *Allocator) !CompilerInfo { const zig_lib_dir = try introspect.resolveZigLibDir(allocator); const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); + const global_cache_dir = try getAppCacheDir(allocator, "zig"); return CompilerInfo{ .id = "test", .version = "0.7.0", .lib_dir = zig_lib_dir, .std_dir = zig_std_dir, - .global_cache_dir = "/global/" + .global_cache_dir = global_cache_dir, }; } pub fn deinit(self: *CompilerInfo, allocator: *Allocator) void { allocator.free(self.lib_dir); allocator.free(self.std_dir); + allocator.free(self.global_cache_dir); } }; @@ -58,3 +62,56 @@ pub fn cmdInfo(allocator: *Allocator, stdout: var) !void { try bos_stream.writeByte('\n'); try bos.flush(); } + +pub const GetAppCacheDirError = error{ + OutOfMemory, + AppCacheDirUnavailable, +}; + +// Copied from fs.getAppDataDir, but changed it to return .cache/ dir on linux. +// This is the same behavior as the current zig compiler global cache resolution. +fn getAppCacheDir(allocator: *Allocator, appname: []const u8) GetAppCacheDirError![]u8 { + switch (builtin.os.tag) { + .windows => { + var dir_path_ptr: [*:0]u16 = undefined; + switch (os.windows.shell32.SHGetKnownFolderPath( + &os.windows.FOLDERID_LocalAppData, + os.windows.KF_FLAG_CREATE, + null, + &dir_path_ptr, + )) { + os.windows.S_OK => { + defer os.windows.ole32.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr)); + const global_dir = unicode.utf16leToUtf8Alloc(allocator, mem.spanZ(dir_path_ptr)) catch |err| switch (err) { + error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, + error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, + error.DanglingSurrogateHalf => return error.AppDataDirUnavailable, + error.OutOfMemory => return error.OutOfMemory, + }; + defer allocator.free(global_dir); + return fs.path.join(allocator, &[_][]const u8{ global_dir, appname }); + }, + os.windows.E_OUTOFMEMORY => return error.OutOfMemory, + else => return error.AppCacheDirUnavailable, + } + }, + .macosx => { + const home_dir = os.getenv("HOME") orelse { + // TODO look in /etc/passwd + return error.AppCacheDirUnavailable; + }; + return fs.path.join(allocator, &[_][]const u8{ home_dir, "Library", "Application Support", appname }); + }, + .linux, .freebsd, .netbsd, .dragonfly => { + if (os.getenv("XDG_CACHE_HOME")) |cache_home| { + return fs.path.join(allocator, &[_][]const u8{ cache_home, appname }); + } + + const home_dir = os.getenv("HOME") orelse { + return error.AppCacheDirUnavailable; + }; + return fs.path.join(allocator, &[_][]const u8{ home_dir, ".cache", appname }); + }, + else => @compileError("Unsupported OS"), + } +} From e74f09ed029a5c88e65992c31021e7ef81b43e2e Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 08:03:39 +0300 Subject: [PATCH 06/17] Added self_hosted subdir to the global cache dir, to not conflict with stage1 --- src-self-hosted/print_info.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index cb6c4cfd5555..1e300aafc9d3 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -28,12 +28,14 @@ pub const CompilerInfo = struct { const zig_lib_dir = try introspect.resolveZigLibDir(allocator); const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); const global_cache_dir = try getAppCacheDir(allocator, "zig"); + defer allocator.free(global_cache_dir); + const self_hosted_cache_dir = try fs.path.join(allocator, &[_][]const u8{global_cache_dir, "self_hosted"}); // stage1 compiler uses $cache_dir/zig/stage1 return CompilerInfo{ .id = "test", .version = "0.7.0", .lib_dir = zig_lib_dir, .std_dir = zig_std_dir, - .global_cache_dir = global_cache_dir, + .global_cache_dir = self_hosted_cache_dir, }; } From 40720b3ddecd7bfcfb143dfd33a0388091cc394c Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 08:20:52 +0300 Subject: [PATCH 07/17] Commented out the stuff that doesn't work yet --- src-self-hosted/print_info.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 1e300aafc9d3..413a8c97df00 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -9,11 +9,13 @@ const Allocator = std.mem.Allocator; const introspect = @import("introspect.zig"); pub const CompilerInfo = struct { - /// Compiler id hash - id: []const u8, + // TODO: port compiler id hash from stage1 + // /// Compiler id hash + // id: []const u8, - /// Compiler version - version: []const u8, + // TODO: figure out how to pass a constant from build.zig + // /// Compiler version + // version: []const u8, /// Path to lib/ lib_dir: []const u8, @@ -31,8 +33,6 @@ pub const CompilerInfo = struct { defer allocator.free(global_cache_dir); const self_hosted_cache_dir = try fs.path.join(allocator, &[_][]const u8{global_cache_dir, "self_hosted"}); // stage1 compiler uses $cache_dir/zig/stage1 return CompilerInfo{ - .id = "test", - .version = "0.7.0", .lib_dir = zig_lib_dir, .std_dir = zig_std_dir, .global_cache_dir = self_hosted_cache_dir, From 6686f1ee5a673b0e9d309a295857ce3fb6edcc71 Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Thu, 21 May 2020 08:22:53 +0300 Subject: [PATCH 08/17] Removed unused import --- src-self-hosted/main.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 4676f533bf61..8238b1a27494 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -10,7 +10,6 @@ const Module = @import("Module.zig"); const link = @import("link.zig"); const Package = @import("Package.zig"); const zir = @import("zir.zig"); -const introspect = @import("introspect.zig"); // TODO Improve async I/O enough that we feel comfortable doing this. //pub const io_mode = .evented; From 41dccb167850bb5315e0d29685091e63edd7d5aa Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Fri, 22 May 2020 06:27:02 +0300 Subject: [PATCH 09/17] Fixed self-hosted windows build --- src-self-hosted/print_info.zig | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 413a8c97df00..da61f52824dc 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -1,5 +1,7 @@ const builtin = @import("builtin"); const std = @import("std"); +const mem = std.mem; +const unicode = std.unicode; const io = std.io; const fs = std.fs; const os = std.os; @@ -85,9 +87,9 @@ fn getAppCacheDir(allocator: *Allocator, appname: []const u8) GetAppCacheDirErro os.windows.S_OK => { defer os.windows.ole32.CoTaskMemFree(@ptrCast(*c_void, dir_path_ptr)); const global_dir = unicode.utf16leToUtf8Alloc(allocator, mem.spanZ(dir_path_ptr)) catch |err| switch (err) { - error.UnexpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, - error.ExpectedSecondSurrogateHalf => return error.AppDataDirUnavailable, - error.DanglingSurrogateHalf => return error.AppDataDirUnavailable, + error.UnexpectedSecondSurrogateHalf => return error.AppCacheDirUnavailable, + error.ExpectedSecondSurrogateHalf => return error.AppCacheDirUnavailable, + error.DanglingSurrogateHalf => return error.AppCacheDirUnavailable, error.OutOfMemory => return error.OutOfMemory, }; defer allocator.free(global_dir); From a170e99fba776893c814360aa140a3b724a32d7c Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Fri, 22 May 2020 07:01:40 +0300 Subject: [PATCH 10/17] Call the info command from stage0 --- src-self-hosted/main.zig | 2 +- src-self-hosted/stage2.zig | 9 +++++++++ src/main.cpp | 8 ++++++++ src/stage2.cpp | 5 +++++ src/stage2.h | 3 +++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 8238b1a27494..b58c5ea9bc67 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -32,8 +32,8 @@ const usage = \\ build-obj [source] Create object from source or assembly \\ fmt [source] Parse file and render in canonical zig format \\ targets List available compilation targets - \\ version Print version number and exit \\ info Print lib path, std path, compiler id and version + \\ version Print version number and exit \\ zen Print zen of zig and exit \\ \\ diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index bd24ffb399f1..d6a6b0df0d41 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -387,6 +387,15 @@ fn detectNativeCpuWithLLVM( return result; } +export fn stage2_cmd_info() c_int { + @import("print_info.zig").cmdInfo(std.heap.c_allocator, std.io.getStdOut().outStream()) catch |err| { + std.debug.warn("unable to print info: {}\n", .{@errorName(err)}); + return -1; + }; + + return 0; +} + // ABI warning export fn stage2_cmd_targets( zig_triple: ?[*:0]const u8, diff --git a/src/main.cpp b/src/main.cpp index 41aade9431a5..58c238de4202 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "dump_analysis.hpp" #include "mem_profile.hpp" +#include #include static int print_error_usage(const char *arg0) { @@ -47,6 +48,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " translate-c [source] convert c code to zig code\n" " targets list available compilation targets\n" " test [source] create and run a test build\n" + " info print lib path, std path, compiler id and version\n" " version print version number and exit\n" " zen print zen of zig and exit\n" "\n" @@ -172,6 +174,7 @@ enum Cmd { CmdTargets, CmdTest, CmdTranslateC, + CmdInfo, CmdVersion, CmdZen, CmdLibC, @@ -1282,6 +1285,8 @@ static int main0(int argc, char **argv) { } else if (strcmp(arg, "run") == 0) { cmd = CmdRun; out_type = OutTypeExe; + } else if (strcmp(arg, "info") == 0) { + cmd = CmdInfo; } else if (strcmp(arg, "version") == 0) { cmd = CmdVersion; } else if (strcmp(arg, "zen") == 0) { @@ -1316,6 +1321,7 @@ static int main0(int argc, char **argv) { } break; case CmdBuiltin: + case CmdInfo: case CmdVersion: case CmdZen: case CmdTargets: @@ -1832,6 +1838,8 @@ static int main0(int argc, char **argv) { zig_unreachable(); } } + case CmdInfo: + return stage2_cmd_info(); case CmdVersion: printf("%s\n", ZIG_VERSION_STRING); return main_exit(root_progress_node, EXIT_SUCCESS); diff --git a/src/stage2.cpp b/src/stage2.cpp index 44e788b01c0f..b018d74f7438 100644 --- a/src/stage2.cpp +++ b/src/stage2.cpp @@ -27,6 +27,11 @@ void stage2_zen(const char **ptr, size_t *len) { stage2_panic(msg, strlen(msg)); } +int stage2_cmd_info() { + const char *msg = "stage0 called stage2_info"; + stage2_panic(msg, strlen(msg)); +} + void stage2_attach_segfault_handler(void) { } void stage2_panic(const char *ptr, size_t len) { diff --git a/src/stage2.h b/src/stage2.h index 0acef166d709..c6c1df92f178 100644 --- a/src/stage2.h +++ b/src/stage2.h @@ -141,6 +141,9 @@ ZIG_EXTERN_C void stage2_render_ast(struct Stage2Ast *ast, FILE *output_file); // ABI warning ZIG_EXTERN_C void stage2_zen(const char **ptr, size_t *len); +// ABI warning +ZIG_EXTERN_C int stage2_cmd_info(); + // ABI warning ZIG_EXTERN_C void stage2_attach_segfault_handler(void); From f591d7add6fead926a6c8462f0bf24f5a85d0bbf Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Fri, 22 May 2020 08:47:10 +0300 Subject: [PATCH 11/17] Refactored print_info a bit, added const version and CacheType param --- src-self-hosted/print_info.zig | 44 +++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index da61f52824dc..3539dea9ee25 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -11,13 +11,12 @@ const Allocator = std.mem.Allocator; const introspect = @import("introspect.zig"); pub const CompilerInfo = struct { - // TODO: port compiler id hash from stage1 + // TODO: port compiler id hash from cpp // /// Compiler id hash // id: []const u8, - // TODO: figure out how to pass a constant from build.zig - // /// Compiler version - // version: []const u8, + /// Compiler version + version: []const u8, /// Path to lib/ lib_dir: []const u8, @@ -28,16 +27,45 @@ pub const CompilerInfo = struct { /// Path to the global cache dir global_cache_dir: []const u8, + const CacheType = enum { + SelfHosted, + Stage1, + }; + + pub fn getVersionString() []const u8 { + // TODO: get this from build.zig somehow + return "0.6.0"; + } + + pub fn getCacheDir(allocator: *Allocator, cache_type: CacheType) ![]u8 { + const global_cache_dir = try getAppCacheDir(allocator, "zig"); + defer allocator.free(global_cache_dir); + + const postfix = switch(cache_type) { + .SelfHosted => "self_hosted", + .Stage1 => "stage1", + }; + return try fs.path.join(allocator, &[_][]const u8{global_cache_dir, postfix}); // stage1 compiler uses $cache_dir/zig/stage1 + } + + // TODO: add CacheType argument here to make it return correct cache dir for stage1 pub fn init(allocator: *Allocator) !CompilerInfo { + const version_str = CompilerInfo.getVersionString(); + const zig_lib_dir = try introspect.resolveZigLibDir(allocator); + errdefer allocator.free(zig_lib_dir); + const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); - const global_cache_dir = try getAppCacheDir(allocator, "zig"); - defer allocator.free(global_cache_dir); - const self_hosted_cache_dir = try fs.path.join(allocator, &[_][]const u8{global_cache_dir, "self_hosted"}); // stage1 compiler uses $cache_dir/zig/stage1 + errdefer allocator.free(zig_std_dir); + + const cache_dir = try CompilerInfo.getCacheDir(allocator, .SelfHosted); + errdefer allocator.free(cache_dir); + return CompilerInfo{ + .version = version_str, .lib_dir = zig_lib_dir, .std_dir = zig_std_dir, - .global_cache_dir = self_hosted_cache_dir, + .global_cache_dir = cache_dir, }; } From 1bcc11b5c043f2b10775363a5fb736c4a60629be Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Fri, 22 May 2020 08:53:59 +0300 Subject: [PATCH 12/17] Made cmdInfo return different cache dir based on compiler type --- src-self-hosted/main.zig | 2 +- src-self-hosted/print_info.zig | 16 ++++++++-------- src-self-hosted/stage2.zig | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index b58c5ea9bc67..117c0f5ea802 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -72,7 +72,7 @@ pub fn main() !void { std.debug.warn("TODO version command not implemented yet\n", .{}); return error.Unimplemented; } else if (mem.eql(u8, cmd, "info")) { - try @import("print_info.zig").cmdInfo(arena, io.getStdOut().outStream()); + try @import("print_info.zig").cmdInfo(arena, .SelfHosted, io.getStdOut().outStream()); } else if (mem.eql(u8, cmd, "zen")) { try io.getStdOut().writeAll(info_zen); } else if (mem.eql(u8, cmd, "help")) { diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 3539dea9ee25..352c35ab8d44 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -27,9 +27,9 @@ pub const CompilerInfo = struct { /// Path to the global cache dir global_cache_dir: []const u8, - const CacheType = enum { - SelfHosted, + const CompilerType = enum { Stage1, + SelfHosted, }; pub fn getVersionString() []const u8 { @@ -37,11 +37,11 @@ pub const CompilerInfo = struct { return "0.6.0"; } - pub fn getCacheDir(allocator: *Allocator, cache_type: CacheType) ![]u8 { + pub fn getCacheDir(allocator: *Allocator, compiler_type: CompilerType) ![]u8 { const global_cache_dir = try getAppCacheDir(allocator, "zig"); defer allocator.free(global_cache_dir); - const postfix = switch(cache_type) { + const postfix = switch(compiler_type) { .SelfHosted => "self_hosted", .Stage1 => "stage1", }; @@ -49,7 +49,7 @@ pub const CompilerInfo = struct { } // TODO: add CacheType argument here to make it return correct cache dir for stage1 - pub fn init(allocator: *Allocator) !CompilerInfo { + pub fn init(allocator: *Allocator, compiler_type: CompilerType) !CompilerInfo { const version_str = CompilerInfo.getVersionString(); const zig_lib_dir = try introspect.resolveZigLibDir(allocator); @@ -58,7 +58,7 @@ pub const CompilerInfo = struct { const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); errdefer allocator.free(zig_std_dir); - const cache_dir = try CompilerInfo.getCacheDir(allocator, .SelfHosted); + const cache_dir = try CompilerInfo.getCacheDir(allocator, compiler_type); errdefer allocator.free(cache_dir); return CompilerInfo{ @@ -76,8 +76,8 @@ pub const CompilerInfo = struct { } }; -pub fn cmdInfo(allocator: *Allocator, stdout: var) !void { - var info = try CompilerInfo.init(allocator); +pub fn cmdInfo(allocator: *Allocator, compiler_type: CompilerInfo.CompilerType, stdout: var) !void { + var info = try CompilerInfo.init(allocator, compiler_type); defer info.deinit(allocator); var bos = io.bufferedOutStream(stdout); diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index d6a6b0df0d41..287bd5d6b293 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -388,7 +388,7 @@ fn detectNativeCpuWithLLVM( } export fn stage2_cmd_info() c_int { - @import("print_info.zig").cmdInfo(std.heap.c_allocator, std.io.getStdOut().outStream()) catch |err| { + @import("print_info.zig").cmdInfo(std.heap.c_allocator, .Stage1, std.io.getStdOut().outStream()) catch |err| { std.debug.warn("unable to print info: {}\n", .{@errorName(err)}); return -1; }; From c772be0e0bd448da58c425ae5a1cbbefc63a58a9 Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Mon, 25 May 2020 15:24:48 +0300 Subject: [PATCH 13/17] Removed mock version from zig info --- src-self-hosted/print_info.zig | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 352c35ab8d44..8a6d0dda7a34 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -15,8 +15,8 @@ pub const CompilerInfo = struct { // /// Compiler id hash // id: []const u8, - /// Compiler version - version: []const u8, + // /// Compiler version + // version: []const u8, /// Path to lib/ lib_dir: []const u8, @@ -50,8 +50,6 @@ pub const CompilerInfo = struct { // TODO: add CacheType argument here to make it return correct cache dir for stage1 pub fn init(allocator: *Allocator, compiler_type: CompilerType) !CompilerInfo { - const version_str = CompilerInfo.getVersionString(); - const zig_lib_dir = try introspect.resolveZigLibDir(allocator); errdefer allocator.free(zig_lib_dir); @@ -62,7 +60,6 @@ pub const CompilerInfo = struct { errdefer allocator.free(cache_dir); return CompilerInfo{ - .version = version_str, .lib_dir = zig_lib_dir, .std_dir = zig_std_dir, .global_cache_dir = cache_dir, From 4a4a1030bc81981c66266179b1c147d843243eaa Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Tue, 26 May 2020 07:39:59 +0300 Subject: [PATCH 14/17] Added --json and --help, moved to table formatting by default --- deps/SoftFloat-3e/COPYING.txt | 74 +++++++++++++++++----------------- deps/SoftFloat-3e/README.txt | 42 +++++++++---------- src-self-hosted/main.zig | 2 +- src-self-hosted/print_info.zig | 64 +++++++++++++++++++++++------ src-self-hosted/stage2.zig | 27 ++++++++++--- src/main.cpp | 8 +--- src/stage2.cpp | 2 +- src/stage2.h | 2 +- 8 files changed, 136 insertions(+), 85 deletions(-) diff --git a/deps/SoftFloat-3e/COPYING.txt b/deps/SoftFloat-3e/COPYING.txt index 9c05d49ca452..b5690face082 100644 --- a/deps/SoftFloat-3e/COPYING.txt +++ b/deps/SoftFloat-3e/COPYING.txt @@ -1,37 +1,37 @@ - -License for Berkeley SoftFloat Release 3e - -John R. Hauser -2018 January 20 - -The following applies to the whole of SoftFloat Release 3e as well as to -each source file individually. - -Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the -University of California. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions, and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions, and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. Neither the name of the University nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE -DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY -DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF -THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - + +License for Berkeley SoftFloat Release 3e + +John R. Hauser +2018 January 20 + +The following applies to the whole of SoftFloat Release 3e as well as to +each source file individually. + +Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 The Regents of the +University of California. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions, and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions, and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the University nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS", AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/deps/SoftFloat-3e/README.txt b/deps/SoftFloat-3e/README.txt index f819baa5c5cc..1613c7671800 100644 --- a/deps/SoftFloat-3e/README.txt +++ b/deps/SoftFloat-3e/README.txt @@ -1,21 +1,21 @@ - -Package Overview for Berkeley SoftFloat Release 3e - -John R. Hauser -2018 January 20 - -Berkeley SoftFloat is a software implementation of binary floating-point -that conforms to the IEEE Standard for Floating-Point Arithmetic. SoftFloat -is distributed in the form of C source code. Building the SoftFloat sources -generates a library file (typically "softfloat.a" or "libsoftfloat.a") -containing the floating-point subroutines. - -The SoftFloat package is documented in the following files in the "doc" -subdirectory: - - SoftFloat.html Documentation for using the SoftFloat functions. - SoftFloat-source.html Documentation for building SoftFloat. - SoftFloat-history.html History of the major changes to SoftFloat. - -Other files in the package comprise the source code for SoftFloat. - + +Package Overview for Berkeley SoftFloat Release 3e + +John R. Hauser +2018 January 20 + +Berkeley SoftFloat is a software implementation of binary floating-point +that conforms to the IEEE Standard for Floating-Point Arithmetic. SoftFloat +is distributed in the form of C source code. Building the SoftFloat sources +generates a library file (typically "softfloat.a" or "libsoftfloat.a") +containing the floating-point subroutines. + +The SoftFloat package is documented in the following files in the "doc" +subdirectory: + + SoftFloat.html Documentation for using the SoftFloat functions. + SoftFloat-source.html Documentation for building SoftFloat. + SoftFloat-history.html History of the major changes to SoftFloat. + +Other files in the package comprise the source code for SoftFloat. + diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 117c0f5ea802..ad1e89f9169c 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -72,7 +72,7 @@ pub fn main() !void { std.debug.warn("TODO version command not implemented yet\n", .{}); return error.Unimplemented; } else if (mem.eql(u8, cmd, "info")) { - try @import("print_info.zig").cmdInfo(arena, .SelfHosted, io.getStdOut().outStream()); + try @import("print_info.zig").cmdInfo(arena, cmd_args, .SelfHosted, io.getStdOut().outStream()); } else if (mem.eql(u8, cmd, "zen")) { try io.getStdOut().writeAll(info_zen); } else if (mem.eql(u8, cmd, "help")) { diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 8a6d0dda7a34..9d51039da582 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -1,5 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); +const process = std.process; const mem = std.mem; const unicode = std.unicode; const io = std.io; @@ -10,6 +11,17 @@ const StringifyOptions = json.StringifyOptions; const Allocator = std.mem.Allocator; const introspect = @import("introspect.zig"); +const usage_info = + \\Usage: zig info [options] + \\ + \\ Outputs path to zig lib dir, std dir and the global cache dir. + \\ + \\Options: + \\ --help Print this help and exit + \\ --json Output as json instead of a table format + \\ +; + pub const CompilerInfo = struct { // TODO: port compiler id hash from cpp // /// Compiler id hash @@ -17,7 +29,6 @@ pub const CompilerInfo = struct { // /// Compiler version // version: []const u8, - /// Path to lib/ lib_dir: []const u8, @@ -41,11 +52,11 @@ pub const CompilerInfo = struct { const global_cache_dir = try getAppCacheDir(allocator, "zig"); defer allocator.free(global_cache_dir); - const postfix = switch(compiler_type) { + const postfix = switch (compiler_type) { .SelfHosted => "self_hosted", .Stage1 => "stage1", }; - return try fs.path.join(allocator, &[_][]const u8{global_cache_dir, postfix}); // stage1 compiler uses $cache_dir/zig/stage1 + return try fs.path.join(allocator, &[_][]const u8{ global_cache_dir, postfix }); // stage1 compiler uses $cache_dir/zig/stage1 } // TODO: add CacheType argument here to make it return correct cache dir for stage1 @@ -53,7 +64,7 @@ pub const CompilerInfo = struct { const zig_lib_dir = try introspect.resolveZigLibDir(allocator); errdefer allocator.free(zig_lib_dir); - const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{zig_lib_dir, "std"}); + const zig_std_dir = try fs.path.join(allocator, &[_][]const u8{ zig_lib_dir, "std" }); errdefer allocator.free(zig_std_dir); const cache_dir = try CompilerInfo.getCacheDir(allocator, compiler_type); @@ -66,6 +77,22 @@ pub const CompilerInfo = struct { }; } + pub fn toString(self: *CompilerInfo, out_stream: var) !void { + inline for (@typeInfo(CompilerInfo).Struct.fields) |field| { + try std.fmt.format(out_stream, "{: <16}\t{: <}\n", .{ field.name, @field(self, field.name) }); + } + } + + pub fn toJSON(self: *CompilerInfo, out_stream: var) !void { + const stringifyOptions = StringifyOptions{ + .whitespace = StringifyOptions.Whitespace{ + // Match indentation of zig targets + .indent = .{ .Space = 2 }, + }, + }; + try json.stringify(self, stringifyOptions, out_stream); + } + pub fn deinit(self: *CompilerInfo, allocator: *Allocator) void { allocator.free(self.lib_dir); allocator.free(self.std_dir); @@ -73,22 +100,33 @@ pub const CompilerInfo = struct { } }; -pub fn cmdInfo(allocator: *Allocator, compiler_type: CompilerInfo.CompilerType, stdout: var) !void { +pub fn cmdInfo(allocator: *Allocator, cmd_args: []const []const u8, compiler_type: CompilerInfo.CompilerType, stdout: var) !void { var info = try CompilerInfo.init(allocator, compiler_type); defer info.deinit(allocator); var bos = io.bufferedOutStream(stdout); const bos_stream = bos.outStream(); - const stringifyOptions = StringifyOptions{ - .whitespace = StringifyOptions.Whitespace{ - // Match indentation of zig targets - .indent = .{ .Space = 2 } - }, - }; - try json.stringify(info, stringifyOptions, bos_stream); + var json_format = false; + for (cmd_args) |arg| { + if (mem.eql(u8, arg, "--json")) { + json_format = true; + } else if (mem.eql(u8, arg, "--help")) { + try stdout.writeAll(usage_info); + return; + } else { + std.debug.warn("Unknown argument passed to info command: {}\n", .{arg}); + process.exit(1); + } + } + + if (json_format) { + try info.toJSON(bos_stream); + try bos_stream.writeByte('\n'); + } else { + try info.toString(bos_stream); + } - try bos_stream.writeByte('\n'); try bos.flush(); } diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index 287bd5d6b293..36cddf07e025 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -179,8 +179,7 @@ export fn stage2_fmt(argc: c_int, argv: [*]const [*:0]const u8) c_int { return 0; } -fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void { - const allocator = std.heap.c_allocator; +fn argvToArrayList(allocator: *Allocator, argc: c_int, argv: [*]const [*:0]const u8) !ArrayList([]const u8) { var args_list = std.ArrayList([]const u8).init(allocator); const argc_usize = @intCast(usize, argc); var arg_i: usize = 0; @@ -188,8 +187,16 @@ fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void { try args_list.append(mem.spanZ(argv[arg_i])); } - const args = args_list.span()[2..]; + return args_list; +} + +fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void { + const allocator = std.heap.c_allocator; + + var args_list = try argvToArrayList(allocator, argc, argv); + defer args_list.deinit(); + const args = args_list.span()[2..]; return self_hosted_main.cmdFmt(allocator, args); } @@ -387,8 +394,18 @@ fn detectNativeCpuWithLLVM( return result; } -export fn stage2_cmd_info() c_int { - @import("print_info.zig").cmdInfo(std.heap.c_allocator, .Stage1, std.io.getStdOut().outStream()) catch |err| { +export fn stage2_info(argc: c_int, argv: [*]const [*:0]const u8) c_int { + const allocator = std.heap.c_allocator; + + var args_list = argvToArrayList(allocator, argc, argv) catch |err| { + std.debug.warn("unable to parse arguments: {}\n", .{@errorName(err)}); + return -1; + }; + defer args_list.deinit(); + + const args = args_list.span()[2..]; + + @import("print_info.zig").cmdInfo(allocator, args, .Stage1, std.io.getStdOut().outStream()) catch |err| { std.debug.warn("unable to print info: {}\n", .{@errorName(err)}); return -1; }; diff --git a/src/main.cpp b/src/main.cpp index 58c238de4202..05e2b2fca06e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -174,7 +174,6 @@ enum Cmd { CmdTargets, CmdTest, CmdTranslateC, - CmdInfo, CmdVersion, CmdZen, CmdLibC, @@ -584,6 +583,8 @@ static int main0(int argc, char **argv) { return (term.how == TerminationIdClean) ? term.code : -1; } else if (argc >= 2 && strcmp(argv[1], "fmt") == 0) { return stage2_fmt(argc, argv); + } else if (argc >= 2 && strcmp(argv[1], "info") == 0) { + return stage2_info(argc, argv); } else if (argc >= 2 && (strcmp(argv[1], "cc") == 0 || strcmp(argv[1], "c++") == 0)) { emit_h = false; strip = true; @@ -1285,8 +1286,6 @@ static int main0(int argc, char **argv) { } else if (strcmp(arg, "run") == 0) { cmd = CmdRun; out_type = OutTypeExe; - } else if (strcmp(arg, "info") == 0) { - cmd = CmdInfo; } else if (strcmp(arg, "version") == 0) { cmd = CmdVersion; } else if (strcmp(arg, "zen") == 0) { @@ -1321,7 +1320,6 @@ static int main0(int argc, char **argv) { } break; case CmdBuiltin: - case CmdInfo: case CmdVersion: case CmdZen: case CmdTargets: @@ -1838,8 +1836,6 @@ static int main0(int argc, char **argv) { zig_unreachable(); } } - case CmdInfo: - return stage2_cmd_info(); case CmdVersion: printf("%s\n", ZIG_VERSION_STRING); return main_exit(root_progress_node, EXIT_SUCCESS); diff --git a/src/stage2.cpp b/src/stage2.cpp index b018d74f7438..313b109fb94f 100644 --- a/src/stage2.cpp +++ b/src/stage2.cpp @@ -27,7 +27,7 @@ void stage2_zen(const char **ptr, size_t *len) { stage2_panic(msg, strlen(msg)); } -int stage2_cmd_info() { +int stage2_info(int argc, char** argv) { const char *msg = "stage0 called stage2_info"; stage2_panic(msg, strlen(msg)); } diff --git a/src/stage2.h b/src/stage2.h index c6c1df92f178..dbb3f6f61cdd 100644 --- a/src/stage2.h +++ b/src/stage2.h @@ -142,7 +142,7 @@ ZIG_EXTERN_C void stage2_render_ast(struct Stage2Ast *ast, FILE *output_file); ZIG_EXTERN_C void stage2_zen(const char **ptr, size_t *len); // ABI warning -ZIG_EXTERN_C int stage2_cmd_info(); +ZIG_EXTERN_C int stage2_info(int argc, char **argv); // ABI warning ZIG_EXTERN_C void stage2_attach_segfault_handler(void); From acf0312283af00e313eeaf4a2568f937420cf1f8 Mon Sep 17 00:00:00 2001 From: Sergey Poznyak Date: Wed, 27 May 2020 06:23:19 +0300 Subject: [PATCH 15/17] Removed unused #include from main.cpp --- src/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 05e2b2fca06e..867bf99ff5b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,7 +19,6 @@ #include "dump_analysis.hpp" #include "mem_profile.hpp" -#include #include static int print_error_usage(const char *arg0) { From 7fe5963c12e86757796588b0c0d2d7e3fba50bd4 Mon Sep 17 00:00:00 2001 From: Sergeeeek Date: Wed, 3 Jun 2020 06:48:45 +0300 Subject: [PATCH 16/17] Removed toJSON function from CompilerInfo --- src-self-hosted/print_info.zig | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 9d51039da582..96495634fefd 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -83,16 +83,6 @@ pub const CompilerInfo = struct { } } - pub fn toJSON(self: *CompilerInfo, out_stream: var) !void { - const stringifyOptions = StringifyOptions{ - .whitespace = StringifyOptions.Whitespace{ - // Match indentation of zig targets - .indent = .{ .Space = 2 }, - }, - }; - try json.stringify(self, stringifyOptions, out_stream); - } - pub fn deinit(self: *CompilerInfo, allocator: *Allocator) void { allocator.free(self.lib_dir); allocator.free(self.std_dir); @@ -121,7 +111,9 @@ pub fn cmdInfo(allocator: *Allocator, cmd_args: []const []const u8, compiler_typ } if (json_format) { - try info.toJSON(bos_stream); + try json.stringify(info, StringifyOptions{ + .whitespace = StringifyOptions.Whitespace{ .indent = .{ .Space = 2 } }, + }, bos_stream); try bos_stream.writeByte('\n'); } else { try info.toString(bos_stream); From fd0380c8e0fa612f32bbd6018431023963331794 Mon Sep 17 00:00:00 2001 From: Sergeeeek Date: Thu, 4 Jun 2020 07:11:09 +0300 Subject: [PATCH 17/17] Changed --json to --format, with ability to override Also some help text and error changes to better match the rest of zig --- src-self-hosted/print_info.zig | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src-self-hosted/print_info.zig b/src-self-hosted/print_info.zig index 96495634fefd..dadf7efbfe2a 100644 --- a/src-self-hosted/print_info.zig +++ b/src-self-hosted/print_info.zig @@ -18,7 +18,7 @@ const usage_info = \\ \\Options: \\ --help Print this help and exit - \\ --json Output as json instead of a table format + \\ --format [text|json] Choose output format (defaults to text) \\ ; @@ -98,14 +98,30 @@ pub fn cmdInfo(allocator: *Allocator, cmd_args: []const []const u8, compiler_typ const bos_stream = bos.outStream(); var json_format = false; - for (cmd_args) |arg| { - if (mem.eql(u8, arg, "--json")) { - json_format = true; + + var i: usize = 0; + while (i < cmd_args.len) : (i += 1) { + const arg = cmd_args[i]; + if (mem.eql(u8, arg, "--format")) { + if (cmd_args.len <= i + 1) { + std.debug.warn("expected [text|json] after --format\n", .{}); + process.exit(1); + } + const format = cmd_args[i + 1]; + i += 1; + if (mem.eql(u8, format, "text")) { + json_format = false; + } else if (mem.eql(u8, format, "json")) { + json_format = true; + } else { + std.debug.warn("expected [text|json] after --format, found '{}'\n", .{format}); + process.exit(1); + } } else if (mem.eql(u8, arg, "--help")) { try stdout.writeAll(usage_info); return; } else { - std.debug.warn("Unknown argument passed to info command: {}\n", .{arg}); + std.debug.warn("unrecognized parameter: '{}'\n", .{arg}); process.exit(1); } }