authorgravatar for bburns@microsoft.comBrendan Burns <bburns@microsoft.com> 2023-01-06 08:40:16-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-01-06 18:40:16+02:00
log24b4e643f486ce803f758a6eab0c587c44b9cfa4
treea462fbd4199453da809cb7239ed52bb81e26c8cc
parent7a2d7ff6280c4e75618b5d4de24c3ff4aedd8a18
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Implement some more environment functions for WASI.


3 files changed, 35 insertions(+), 18 deletions(-)

lib/std/debug.zig+16-15
......@@ -110,27 +110,28 @@ pub fn getSelfDebugInfo() !*DebugInfo {
110110}
111111
112112pub fn detectTTYConfig(file: std.fs.File) TTY.Config {
113 if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
113 if (builtin.os.tag == .wasi) {
114 // Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes
115 // aren't currently supported.
116 return .no_color;
117 } else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
114118 return .escape_codes;
115119 } else if (process.hasEnvVarConstant("NO_COLOR")) {
116120 return .no_color;
117 } else {
118 if (file.supportsAnsiEscapeCodes()) {
119 return .escape_codes;
120 } else if (native_os == .windows and file.isTty()) {
121 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
122 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
123 // TODO: Should this return an error instead?
124 return .no_color;
125 }
126 return .{ .windows_api = .{
127 .handle = file.handle,
128 .reset_attributes = info.wAttributes,
129 } };
130 } else {
121 } else if (file.supportsAnsiEscapeCodes()) {
122 return .escape_codes;
123 } else if (native_os == .windows and file.isTty()) {
124 var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
125 if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
126 // TODO: Should this return an error instead?
131127 return .no_color;
132128 }
129 return .{ .windows_api = .{
130 .handle = file.handle,
131 .reset_attributes = info.wAttributes,
132 } };
133133 }
134 return .no_color;
134135}
135136
136137/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
lib/std/process.zig+11
......@@ -369,6 +369,11 @@ pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError
369369 error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8,
370370 else => |e| return e,
371371 };
372 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
373 var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
374 defer envmap.deinit();
375 const val = envmap.get(key) orelse return error.EnvironmentVariableNotFound;
376 return allocator.dupe(u8, val);
372377 } else {
373378 const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
374379 return allocator.dupe(u8, result);
......@@ -379,6 +384,8 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
379384 if (builtin.os.tag == .windows) {
380385 const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key);
381386 return std.os.getenvW(key_w) != null;
387 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
388 @compileError("hasEnvVarConstant is not supported for WASI without libc");
382389 } else {
383390 return os.getenv(key) != null;
384391 }
......@@ -390,6 +397,10 @@ pub fn hasEnvVar(allocator: Allocator, key: []const u8) error{OutOfMemory}!bool
390397 const key_w = try std.unicode.utf8ToUtf16LeWithNull(stack_alloc.get(), key);
391398 defer stack_alloc.allocator.free(key_w);
392399 return std.os.getenvW(key_w) != null;
400 } else if (builtin.os.tag == .wasi and !builtin.link_libc) {
401 var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
402 defer envmap.deinit();
403 return envmap.getPtr(key) != null;
393404 } else {
394405 return os.getenv(key) != null;
395406 }
src/main.zig+8-3
......@@ -637,6 +637,10 @@ const Emit = union(enum) {
637637};
638638
639639fn optionalStringEnvVar(arena: Allocator, name: []const u8) !?[]const u8 {
640 // Env vars aren't used in the bootstrap stage.
641 if (build_options.only_c) {
642 return null;
643 }
640644 if (std.process.getEnvVarOwned(arena, name)) |value| {
641645 return value;
642646 } else |err| switch (err) {
......@@ -676,8 +680,8 @@ fn buildOutputType(
676680 var no_builtin = false;
677681 var watch = false;
678682 var debug_compile_errors = false;
679 var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
680 var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
683 var verbose_link = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
684 var verbose_cc = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
681685 var verbose_air = false;
682686 var verbose_llvm_ir = false;
683687 var verbose_cimport = false;
......@@ -859,7 +863,8 @@ fn buildOutputType(
859863 // before arg parsing, check for the NO_COLOR environment variable
860864 // if it exists, default the color setting to .off
861865 // explicit --color arguments will still override this setting.
862 color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
866 // Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
867 color = if (builtin.os.tag == .wasi or std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
863868
864869 switch (arg_mode) {
865870 .build, .translate_c, .zig_test, .run => {