Skip to content

Commit 24b4e64

Browse files
Implement some more environment functions for WASI.
1 parent 7a2d7ff commit 24b4e64

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

lib/std/debug.zig

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,27 +110,28 @@ pub fn getSelfDebugInfo() !*DebugInfo {
110110
}
111111

112112
pub 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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,10 @@ const Emit = union(enum) {
637637
};
638638

639639
fn 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 => {

0 commit comments

Comments
 (0)