mirror of
https://github.com/ziglang/zig.git
synced 2026-02-09 11:03:30 +00:00
Implement some more environment functions for WASI.
This commit is contained in:
parent
7a2d7ff628
commit
24b4e643f4
@ -110,27 +110,28 @@ pub fn getSelfDebugInfo() !*DebugInfo {
|
||||
}
|
||||
|
||||
pub fn detectTTYConfig(file: std.fs.File) TTY.Config {
|
||||
if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
|
||||
if (builtin.os.tag == .wasi) {
|
||||
// Per https://github.com/WebAssembly/WASI/issues/162 ANSI codes
|
||||
// aren't currently supported.
|
||||
return .no_color;
|
||||
} else if (process.hasEnvVarConstant("ZIG_DEBUG_COLOR")) {
|
||||
return .escape_codes;
|
||||
} else if (process.hasEnvVarConstant("NO_COLOR")) {
|
||||
return .no_color;
|
||||
} else {
|
||||
if (file.supportsAnsiEscapeCodes()) {
|
||||
return .escape_codes;
|
||||
} else if (native_os == .windows and file.isTty()) {
|
||||
var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
|
||||
if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
|
||||
// TODO: Should this return an error instead?
|
||||
return .no_color;
|
||||
}
|
||||
return .{ .windows_api = .{
|
||||
.handle = file.handle,
|
||||
.reset_attributes = info.wAttributes,
|
||||
} };
|
||||
} else {
|
||||
} else if (file.supportsAnsiEscapeCodes()) {
|
||||
return .escape_codes;
|
||||
} else if (native_os == .windows and file.isTty()) {
|
||||
var info: windows.CONSOLE_SCREEN_BUFFER_INFO = undefined;
|
||||
if (windows.kernel32.GetConsoleScreenBufferInfo(file.handle, &info) != windows.TRUE) {
|
||||
// TODO: Should this return an error instead?
|
||||
return .no_color;
|
||||
}
|
||||
return .{ .windows_api = .{
|
||||
.handle = file.handle,
|
||||
.reset_attributes = info.wAttributes,
|
||||
} };
|
||||
}
|
||||
return .no_color;
|
||||
}
|
||||
|
||||
/// Tries to print the current stack trace to stderr, unbuffered, and ignores any error returned.
|
||||
|
||||
@ -369,6 +369,11 @@ pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError
|
||||
error.UnexpectedSecondSurrogateHalf => return error.InvalidUtf8,
|
||||
else => |e| return e,
|
||||
};
|
||||
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
||||
var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
|
||||
defer envmap.deinit();
|
||||
const val = envmap.get(key) orelse return error.EnvironmentVariableNotFound;
|
||||
return allocator.dupe(u8, val);
|
||||
} else {
|
||||
const result = os.getenv(key) orelse return error.EnvironmentVariableNotFound;
|
||||
return allocator.dupe(u8, result);
|
||||
@ -379,6 +384,8 @@ pub fn hasEnvVarConstant(comptime key: []const u8) bool {
|
||||
if (builtin.os.tag == .windows) {
|
||||
const key_w = comptime std.unicode.utf8ToUtf16LeStringLiteral(key);
|
||||
return std.os.getenvW(key_w) != null;
|
||||
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
||||
@compileError("hasEnvVarConstant is not supported for WASI without libc");
|
||||
} else {
|
||||
return os.getenv(key) != null;
|
||||
}
|
||||
@ -390,6 +397,10 @@ pub fn hasEnvVar(allocator: Allocator, key: []const u8) error{OutOfMemory}!bool
|
||||
const key_w = try std.unicode.utf8ToUtf16LeWithNull(stack_alloc.get(), key);
|
||||
defer stack_alloc.allocator.free(key_w);
|
||||
return std.os.getenvW(key_w) != null;
|
||||
} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
|
||||
var envmap = getEnvMap(allocator) catch return error.OutOfMemory;
|
||||
defer envmap.deinit();
|
||||
return envmap.getPtr(key) != null;
|
||||
} else {
|
||||
return os.getenv(key) != null;
|
||||
}
|
||||
|
||||
11
src/main.zig
11
src/main.zig
@ -637,6 +637,10 @@ const Emit = union(enum) {
|
||||
};
|
||||
|
||||
fn optionalStringEnvVar(arena: Allocator, name: []const u8) !?[]const u8 {
|
||||
// Env vars aren't used in the bootstrap stage.
|
||||
if (build_options.only_c) {
|
||||
return null;
|
||||
}
|
||||
if (std.process.getEnvVarOwned(arena, name)) |value| {
|
||||
return value;
|
||||
} else |err| switch (err) {
|
||||
@ -676,8 +680,8 @@ fn buildOutputType(
|
||||
var no_builtin = false;
|
||||
var watch = false;
|
||||
var debug_compile_errors = false;
|
||||
var verbose_link = std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
|
||||
var verbose_cc = std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
|
||||
var verbose_link = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
|
||||
var verbose_cc = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
|
||||
var verbose_air = false;
|
||||
var verbose_llvm_ir = false;
|
||||
var verbose_cimport = false;
|
||||
@ -859,7 +863,8 @@ fn buildOutputType(
|
||||
// before arg parsing, check for the NO_COLOR environment variable
|
||||
// if it exists, default the color setting to .off
|
||||
// explicit --color arguments will still override this setting.
|
||||
color = if (std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
|
||||
// Disable color on WASI per https://github.com/WebAssembly/WASI/issues/162
|
||||
color = if (builtin.os.tag == .wasi or std.process.hasEnvVarConstant("NO_COLOR")) .off else .auto;
|
||||
|
||||
switch (arg_mode) {
|
||||
.build, .translate_c, .zig_test, .run => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user